UNPKG

@saberhq/sail

Version:

Account caching and batched loading for React-based Solana applications.

51 lines 1.48 kB
import { EventEmitter as Emitter } from "eventemitter3"; import { startTransition } from "react"; import { getCacheKeyOfPublicKey } from "."; /** * Emitted when the cache is updated. */ export class CacheBatchUpdateEvent { constructor(ids) { this.ids = ids; } /** * Construct an event from keys. * @param keys * @returns */ static fromKeys(keys) { return new CacheBatchUpdateEvent(new Set([...keys.map((k) => getCacheKeyOfPublicKey(k))])); } /** * Returns true if the key was present in the batch. * @param key * @returns */ hasKey(key) { return this.ids.has(key.toString()); } } CacheBatchUpdateEvent.type = "CacheBatchUpdate"; export class CacheClearEvent { } CacheClearEvent.type = "CacheDelete"; export class AccountsEmitter { constructor() { this._emitter = new Emitter(); this.onBatchCache = (callback) => { this._emitter.on(CacheBatchUpdateEvent.type, callback); return () => this._emitter.removeListener(CacheBatchUpdateEvent.type, callback); }; } raiseBatchCacheUpdated(ids) { startTransition(() => { this._emitter.emit(CacheBatchUpdateEvent.type, new CacheBatchUpdateEvent(ids)); }); } raiseCacheCleared() { startTransition(() => { this._emitter.emit(CacheClearEvent.type, new CacheClearEvent()); }); } } //# sourceMappingURL=emitter.js.map