Skip to content

Instantly share code, notes, and snippets.

@itslukej
Last active March 30, 2020 22:40
Show Gist options
  • Select an option

  • Save itslukej/2003bc00a27fff7bf82b739108fc984d to your computer and use it in GitHub Desktop.

Select an option

Save itslukej/2003bc00a27fff7bf82b739108fc984d to your computer and use it in GitHub Desktop.
Simple map-like interface for encoded storage w/ bigint keys
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