Skip to content

Commit

Permalink
fix(replicache): If Replicache is closed, don't fire subscriptions (#…
Browse files Browse the repository at this point in the history
…2591)

This fixes the error at zbugs startup
  • Loading branch information
arv authored Oct 8, 2024
1 parent 467cea5 commit 3fa82e8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/replicache/src/replicache-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ export class ReplicacheImpl<MD extends MutatorDefs = {}> {
this.subscriptions = new SubscriptionsManagerImpl(
this.#queryInternal,
this.#lc,
this.#closeAbortController.signal,
);

const kvStoreProvider = getKVStoreProvider(this.#lc, options.kvStore);
Expand Down
12 changes: 11 additions & 1 deletion packages/replicache/src/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,16 @@ export class SubscriptionsManagerImpl implements SubscriptionsManager {
readonly #queryInternal: QueryInternal;
readonly #lc: LogContext;
hasPendingSubscriptionRuns = false;
readonly #signal: AbortSignal;

constructor(queryInternal: QueryInternal, lc: LogContext) {
constructor(
queryInternal: QueryInternal,
lc: LogContext,
signal: AbortSignal,
) {
this.#queryInternal = queryInternal;
this.#lc = lc;
this.#signal = signal;
}

add<R>(subscription: Subscription<R>): () => void {
Expand Down Expand Up @@ -406,6 +412,10 @@ export class SubscriptionsManagerImpl implements SubscriptionsManager {
kind: InvokeKind.Type,
diffs: DiffsMap | undefined,
) {
if (this.#signal.aborted) {
return;
}

const subs = [...subscriptions] as readonly Subscription<unknown>[];
if (subs.length === 0) {
return;
Expand Down

0 comments on commit 3fa82e8

Please sign in to comment.