Skip to content

Commit 61d082b

Browse files
committed
refactor: extract flushUnused
1 parent 7936799 commit 61d082b

File tree

3 files changed

+48
-35
lines changed

3 files changed

+48
-35
lines changed

src/index.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
} from './index';
3333
import { rawStoreSymbol } from './internal/exposeRawStores';
3434
import { RawStoreFlags } from './internal/store';
35-
import { flushUnused } from './internal/storeTrackingUsage';
35+
import { flushUnused } from './internal/asyncFlush';
3636
import type { RawStoreWritable } from './internal/storeWritable';
3737

3838
const expectCorrectlyCleanedUp = <T>(store: StoreInput<T>) => {

src/internal/asyncFlush.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { RawStoreFlags } from './store';
2+
3+
export interface Flushable {
4+
flags: RawStoreFlags;
5+
checkUnused(): void;
6+
}
7+
8+
let flushUnusedQueue: Flushable[] | null = null;
9+
export let inFlushUnused = false;
10+
11+
export const planFlush = (object: Flushable): void => {
12+
if (!(object.flags & RawStoreFlags.FLUSH_PLANNED)) {
13+
object.flags |= RawStoreFlags.FLUSH_PLANNED;
14+
if (!flushUnusedQueue) {
15+
flushUnusedQueue = [];
16+
queueMicrotask(flushUnused);
17+
}
18+
flushUnusedQueue.push(object);
19+
}
20+
};
21+
22+
export const flushUnused = (): void => {
23+
// Ignoring coverage for the following lines because, unless there is a bug in tansu (which would have to be fixed!)
24+
// there should be no way to trigger this error.
25+
/* v8 ignore next 3 */
26+
if (inFlushUnused) {
27+
throw new Error('assert failed: recursive flushUnused call');
28+
}
29+
inFlushUnused = true;
30+
try {
31+
const queue = flushUnusedQueue;
32+
if (queue) {
33+
flushUnusedQueue = null;
34+
for (let i = 0, l = queue.length; i < l; i++) {
35+
const producer = queue[i];
36+
producer.flags &= ~RawStoreFlags.FLUSH_PLANNED;
37+
producer.checkUnused();
38+
}
39+
}
40+
} finally {
41+
inFlushUnused = false;
42+
}
43+
};

src/internal/storeTrackingUsage.ts

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,9 @@
1+
import { type Flushable, inFlushUnused, planFlush } from './asyncFlush';
12
import { RawStoreFlags } from './store';
23
import { checkNotInNotificationPhase, RawStoreWritable } from './storeWritable';
34
import { activeConsumer, untrack } from './untrack';
45

5-
let flushUnusedQueue: RawStoreTrackingUsage<any>[] | null = null;
6-
let inFlushUnused = false;
7-
8-
export const flushUnused = (): void => {
9-
// Ignoring coverage for the following lines because, unless there is a bug in tansu (which would have to be fixed!)
10-
// there should be no way to trigger this error.
11-
/* v8 ignore next 3 */
12-
if (inFlushUnused) {
13-
throw new Error('assert failed: recursive flushUnused call');
14-
}
15-
inFlushUnused = true;
16-
try {
17-
const queue = flushUnusedQueue;
18-
if (queue) {
19-
flushUnusedQueue = null;
20-
for (let i = 0, l = queue.length; i < l; i++) {
21-
const producer = queue[i];
22-
producer.flags &= ~RawStoreFlags.FLUSH_PLANNED;
23-
producer.checkUnused();
24-
}
25-
}
26-
} finally {
27-
inFlushUnused = false;
28-
}
29-
};
30-
31-
export abstract class RawStoreTrackingUsage<T> extends RawStoreWritable<T> {
6+
export abstract class RawStoreTrackingUsage<T> extends RawStoreWritable<T> implements Flushable {
327
private extraUsages = 0;
338
abstract startUse(): void;
349
abstract endUse(): void;
@@ -53,13 +28,8 @@ export abstract class RawStoreTrackingUsage<T> extends RawStoreWritable<T> {
5328
if (inFlushUnused || flags & RawStoreFlags.HAS_VISIBLE_ONUSE) {
5429
this.flags &= ~RawStoreFlags.START_USE_CALLED;
5530
untrack(() => this.endUse());
56-
} else if (!(flags & RawStoreFlags.FLUSH_PLANNED)) {
57-
this.flags |= RawStoreFlags.FLUSH_PLANNED;
58-
if (!flushUnusedQueue) {
59-
flushUnusedQueue = [];
60-
queueMicrotask(flushUnused);
61-
}
62-
flushUnusedQueue.push(this);
31+
} else {
32+
planFlush(this);
6333
}
6434
}
6535
}

0 commit comments

Comments
 (0)