UNPKG

@saberhq/sail

Version:

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

57 lines 1.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AccountsEmitter = exports.CacheClearEvent = exports.CacheBatchUpdateEvent = void 0; const eventemitter3_1 = require("eventemitter3"); const react_1 = require("react"); const _1 = require("."); /** * Emitted when the cache is updated. */ 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) => (0, _1.getCacheKeyOfPublicKey)(k))])); } /** * Returns true if the key was present in the batch. * @param key * @returns */ hasKey(key) { return this.ids.has(key.toString()); } } exports.CacheBatchUpdateEvent = CacheBatchUpdateEvent; CacheBatchUpdateEvent.type = "CacheBatchUpdate"; class CacheClearEvent { } exports.CacheClearEvent = CacheClearEvent; CacheClearEvent.type = "CacheDelete"; class AccountsEmitter { constructor() { this._emitter = new eventemitter3_1.EventEmitter(); this.onBatchCache = (callback) => { this._emitter.on(CacheBatchUpdateEvent.type, callback); return () => this._emitter.removeListener(CacheBatchUpdateEvent.type, callback); }; } raiseBatchCacheUpdated(ids) { (0, react_1.startTransition)(() => { this._emitter.emit(CacheBatchUpdateEvent.type, new CacheBatchUpdateEvent(ids)); }); } raiseCacheCleared() { (0, react_1.startTransition)(() => { this._emitter.emit(CacheClearEvent.type, new CacheClearEvent()); }); } } exports.AccountsEmitter = AccountsEmitter; //# sourceMappingURL=emitter.js.map