Skip to content

Commit ace1532

Browse files
authored
Clear state on teardown, make grace optional (#377)
1 parent 966c685 commit ace1532

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

lib/collection.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export const getCollection = <State>(
2727
subscribeUpdates?: (
2828
conn: Connection,
2929
store: Store<State>
30-
) => Promise<UnsubscribeFunc>
30+
) => Promise<UnsubscribeFunc>,
31+
options: { unsubGrace: boolean } = { unsubGrace: true }
3132
): Collection<State> => {
3233
if (conn[key]) {
3334
return conn[key];
@@ -93,6 +94,7 @@ export const getCollection = <State>(
9394
unsubProm.then((unsub) => {
9495
unsub();
9596
});
97+
store.clearState();
9698
conn.removeEventListener("ready", refresh);
9799
conn.removeEventListener("disconnected", handleDisconnect);
98100
};
@@ -149,7 +151,9 @@ export const getCollection = <State>(
149151
}
150152

151153
if (!active) {
152-
scheduleTeardownUpdateSubscription();
154+
options.unsubGrace
155+
? scheduleTeardownUpdateSubscription()
156+
: teardownUpdateSubscription();
153157
}
154158
};
155159
},

lib/store.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type Store<State> = {
1515
state: State | undefined;
1616
action(action: Action<State>): BoundAction<State>;
1717
setState(update: Partial<State>, overwrite?: boolean): void;
18+
clearState(): void;
1819
subscribe(listener: Listener<State>): UnsubscribeFunc;
1920
};
2021

@@ -82,12 +83,16 @@ export const createStore = <State>(state?: State): Store<State> => {
8283
*/
8384
setState,
8485

86+
clearState() {
87+
state = undefined;
88+
},
89+
8590
/**
8691
* Register a listener function to be called whenever state is changed. Returns an `unsubscribe()` function.
8792
* @param {Function} listener A function to call when state changes. Gets passed the new state.
8893
* @returns {Function} unsubscribe()
8994
*/
90-
subscribe(listener: Listener<State>) {
95+
subscribe(listener: Listener<State>): UnsubscribeFunc {
9196
listeners.push(listener);
9297
return () => {
9398
unsubscribe(listener);

0 commit comments

Comments
 (0)