Last active
March 30, 2020 22:40
-
-
Save itslukej/2003bc00a27fff7bf82b739108fc984d to your computer and use it in GitHub Desktop.
Simple map-like interface for encoded storage w/ bigint keys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| declare class ProtoMessage { | |
| serializeBinary(): Uint8Array; | |
| static deserializeBinary(bytes: Uint8Array): any; | |
| } | |
| export default class ProtoMap<K, V extends ProtoMessage> { | |
| private map: Map<K, ReturnType<ProtoMessage["serializeBinary"]>> = new Map; | |
| constructor(private message: typeof ProtoMessage) {} | |
| set(id: K, data: V) { | |
| return this.map.set(id, data.serializeBinary()); | |
| } | |
| get(id: K): V | undefined { | |
| const data = this.map.get(id); | |
| if (data) return this.message.deserializeBinary(data); | |
| } | |
| has(id: K) { | |
| return this.map.has(id); | |
| } | |
| delete(id: K) { | |
| return this.map.delete(id); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment