Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tyleroooo committed Jan 9, 2025
1 parent f2b669e commit 07039f4
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/abacus-ts/websocket/lib/indexerValueManagerHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { ResourceCacheManager } from '@/abacus-ts/lib/resourceCacheManager';
import stableStringify from 'fast-json-stable-stringify';

import { IndexerWebsocket } from './indexerWebsocket';
import { IndexerWebsocketManager } from './indexerWebsocketManager';
import { WebsocketDerivedValue } from './websocketDerivedValue';

type WebsocketValueCreator<Args, ReturnType> = (
websocket: IndexerWebsocket,
args: Args
) => WebsocketDerivedValue<ReturnType>;

export function makeWsValueManager<Args, ReturnType>(
creator: WebsocketValueCreator<Args, ReturnType>
): ResourceCacheManager<WebsocketDerivedValue<ReturnType>, Args & { wsUrl: string }> {
return new ResourceCacheManager({
constructor: (allArgs: Args & { wsUrl: string }) =>
creator(IndexerWebsocketManager.use(allArgs.wsUrl), allArgs),

destroyer: (instance, { wsUrl }) => {
instance.teardown();
IndexerWebsocketManager.markDone(wsUrl);
},

// take care - extra properties on the key will cause divergent behavior
// (cache misses, unexpected new object creation, marking incorrect objects as done, etc)
// only ever pass the exact key type for correct behavior
keySerializer: (allArgs) => stableStringify(allArgs),

// this is set to just above the websocket subscribe timeout because of race conditions in the indexer backend
destroyDelayMs: 21000,
});
}

export function subscribeToWsValue<Args, ReturnType>(
manager: ResourceCacheManager<WebsocketDerivedValue<ReturnType>, Args & { wsUrl: string }>,
args: NoInfer<Args> & { wsUrl: string },
handleChange: (val: NoInfer<ReturnType>) => void
): () => void {
const value = manager.use(args);
const unsub = value.subscribe(handleChange);
return () => {
unsub();
manager.markDone(args);
};
}

0 comments on commit 07039f4

Please sign in to comment.