Skip to content

Commit 0dfb729

Browse files
committed
compute the meta when we PUSH / POP
Otherwise we have to build it on every scroll event and that would be very expensive.
1 parent a3b898e commit 0dfb729

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

packages/@headlessui-react/src/hooks/document-overflow/overflow-store.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ interface DocEntry {
99
count: number
1010
d: Disposables
1111
meta: Set<MetaFn>
12+
computedMeta: Record<string, any>
1213
}
1314

1415
function buildMeta(fns: Iterable<MetaFn>) {
@@ -39,10 +40,12 @@ export let overflows = createStore(() => new Map<Document, DocEntry>(), {
3940
count: 0,
4041
d: disposables(),
4142
meta: new Set(),
43+
computedMeta: {},
4244
}
4345

4446
entry.count++
4547
entry.meta.add(meta)
48+
entry.computedMeta = buildMeta(entry.meta)
4649
this.set(doc, entry)
4750

4851
return this
@@ -53,15 +56,16 @@ export let overflows = createStore(() => new Map<Document, DocEntry>(), {
5356
if (entry) {
5457
entry.count--
5558
entry.meta.delete(meta)
59+
entry.computedMeta = buildMeta(entry.meta)
5660
}
5761

5862
return this
5963
},
6064

61-
SCROLL_PREVENT({ doc, d, meta }: DocEntry) {
65+
SCROLL_PREVENT(entry: DocEntry) {
6266
let ctx = {
63-
doc,
64-
d,
67+
doc: entry.doc,
68+
d: entry.d,
6569

6670
// The moment we `PUSH`, we also `SCROLL_PREVENT`. But a later `PUSH` will
6771
// not re-trigger a `SCROLL_PREVENT` because we are already in a locked
@@ -71,7 +75,7 @@ export let overflows = createStore(() => new Map<Document, DocEntry>(), {
7175
// that happens later can update the meta information. Otherwise we would
7276
// use stale meta information.
7377
meta() {
74-
return buildMeta(meta)
78+
return entry.computedMeta
7579
},
7680
}
7781

0 commit comments

Comments
 (0)