@askrjs/askr 0.0.36 → 0.0.38
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -9
- package/dist/bench/components/benchmark-table.js +2 -2
- package/dist/bench/components/benchmark-table.js.map +1 -1
- package/dist/benchmark.js +3 -3
- package/dist/boot/index.d.ts +1 -7
- package/dist/boot/index.d.ts.map +1 -1
- package/dist/boot/index.js +22 -13
- package/dist/boot/index.js.map +1 -1
- package/dist/components/index.d.ts +2 -0
- package/dist/components/index.js +2 -0
- package/dist/components/link.d.ts +4 -5
- package/dist/components/link.d.ts.map +1 -1
- package/dist/components/link.js +2 -1
- package/dist/components/link.js.map +1 -1
- package/dist/foundations/icon/icon.d.ts.map +1 -1
- package/dist/foundations/icon/icon.js.map +1 -1
- package/dist/foundations/icon/icon.types.d.ts +2 -1
- package/dist/foundations/icon/icon.types.d.ts.map +1 -1
- package/dist/foundations/icon/index.d.ts +3 -0
- package/dist/foundations/icon/index.js +2 -0
- package/dist/foundations/index.d.ts +1 -18
- package/dist/foundations/index.js +2 -18
- package/dist/foundations/interactions/index.d.ts +7 -0
- package/dist/foundations/interactions/index.js +7 -0
- package/dist/foundations/interactions/interaction-policy.js +2 -2
- package/dist/foundations/state/index.d.ts +2 -0
- package/dist/foundations/state/index.js +2 -0
- package/dist/foundations/structures/index.d.ts +8 -0
- package/dist/foundations/structures/index.js +7 -0
- package/dist/foundations/utilities/index.d.ts +7 -0
- package/dist/foundations/utilities/index.js +6 -0
- package/dist/index.d.ts +3 -14
- package/dist/index.js +2 -14
- package/dist/index.js.map +1 -1
- package/dist/jsx-dev-runtime.d.ts +1 -1
- package/dist/jsx-dev-runtime.js.map +1 -1
- package/dist/jsx-runtime.d.ts +1 -1
- package/dist/jsx-runtime.js.map +1 -1
- package/dist/renderer/dom.js +26 -366
- package/dist/renderer/dom.js.map +1 -1
- package/dist/renderer/evaluate.js.map +1 -1
- package/dist/renderer/fastpath.js +20 -18
- package/dist/renderer/fastpath.js.map +1 -1
- package/dist/renderer/for-commit.js +486 -0
- package/dist/renderer/for-commit.js.map +1 -0
- package/dist/renderer/index.js.map +1 -1
- package/dist/renderer/keyed.js +84 -23
- package/dist/renderer/keyed.js.map +1 -1
- package/dist/renderer/reconcile.js +11 -18
- package/dist/renderer/reconcile.js.map +1 -1
- package/dist/renderer/utils.js +5 -1
- package/dist/renderer/utils.js.map +1 -1
- package/dist/resources/index.d.ts +1 -2
- package/dist/resources/index.js +1 -2
- package/dist/router/route.js +1 -1
- package/dist/runtime/for.d.ts.map +1 -1
- package/dist/runtime/for.js +33 -21
- package/dist/runtime/for.js.map +1 -1
- package/dist/runtime/operations.js +1 -1
- package/dist/runtime/selector.d.ts.map +1 -1
- package/dist/runtime/selector.js +180 -103
- package/dist/runtime/selector.js.map +1 -1
- package/dist/runtime/state.d.ts +12 -4
- package/dist/runtime/state.d.ts.map +1 -1
- package/dist/runtime/state.js +1 -1
- package/dist/runtime/state.js.map +1 -1
- package/dist/ssg/index.d.ts +1 -6
- package/dist/ssg/index.js +1 -6
- package/dist/ssr/context.d.ts +2 -1
- package/dist/ssr/context.d.ts.map +1 -1
- package/dist/ssr/context.js +28 -11
- package/dist/ssr/context.js.map +1 -1
- package/dist/ssr/index.d.ts +2 -31
- package/dist/ssr/index.d.ts.map +1 -1
- package/dist/ssr/index.js +4 -46
- package/dist/ssr/index.js.map +1 -1
- package/dist/ssr/render-keys.js +1 -12
- package/dist/ssr/render-keys.js.map +1 -1
- package/dist/ssr/verify-hydration.js +25 -0
- package/dist/ssr/verify-hydration.js.map +1 -0
- package/package.json +39 -16
- package/dist/_virtual/_rolldown/runtime.js +0 -7
- package/dist/foundations/core.d.ts +0 -15
- package/dist/foundations/core.js +0 -13
- package/dist/foundations/structures.d.ts +0 -7
- package/dist/foundations/structures.js +0 -6
- package/dist/ssg/batch-render.d.ts +0 -16
- package/dist/ssg/batch-render.d.ts.map +0 -1
- package/dist/ssg/generate-metadata.d.ts +0 -24
- package/dist/ssg/generate-metadata.d.ts.map +0 -1
- package/dist/ssg/resolve-ssg-data.d.ts +0 -23
- package/dist/ssg/resolve-ssg-data.d.ts.map +0 -1
- package/dist/ssg/route-utils.d.ts +0 -16
- package/dist/ssg/route-utils.d.ts.map +0 -1
- package/dist/ssg/write-static-files.d.ts +0 -15
- package/dist/ssg/write-static-files.d.ts.map +0 -1
- package/dist/ssr/render-keys.d.ts +0 -37
- package/dist/ssr/render-keys.d.ts.map +0 -1
- package/dist/ssr/stream-render.js +0 -88
- package/dist/ssr/stream-render.js.map +0 -1
package/dist/runtime/selector.js
CHANGED
|
@@ -4,7 +4,8 @@ import { clearDerivedDependencySubscriptions, markReactivePropsDirtySource, mark
|
|
|
4
4
|
import { claimHookIndex, getCurrentInstance } from "./component.js";
|
|
5
5
|
//#region src/runtime/selector.ts
|
|
6
6
|
const selectorCells = /* @__PURE__ */ new WeakMap();
|
|
7
|
-
const
|
|
7
|
+
const selectorRecords = /* @__PURE__ */ new WeakMap();
|
|
8
|
+
const dirtySelectorRecords = /* @__PURE__ */ new Set();
|
|
8
9
|
let hasPendingSelectorFlush = false;
|
|
9
10
|
function getSelectorStore(instance) {
|
|
10
11
|
let store = selectorCells.get(instance);
|
|
@@ -17,24 +18,24 @@ function getSelectorStore(instance) {
|
|
|
17
18
|
function scheduleSelectorFlush() {
|
|
18
19
|
if (hasPendingSelectorFlush) return;
|
|
19
20
|
hasPendingSelectorFlush = true;
|
|
20
|
-
globalScheduler.enqueueInLane("derived",
|
|
21
|
+
globalScheduler.enqueueInLane("derived", flushDirtySelectorRecords);
|
|
21
22
|
}
|
|
22
|
-
function
|
|
23
|
-
|
|
24
|
-
if (
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
function markSelectorRecordDirty(record) {
|
|
24
|
+
record._dirty = true;
|
|
25
|
+
if (record._scheduled) return;
|
|
26
|
+
record._scheduled = true;
|
|
27
|
+
dirtySelectorRecords.add(record);
|
|
27
28
|
scheduleSelectorFlush();
|
|
28
29
|
}
|
|
29
|
-
function
|
|
30
|
+
function flushDirtySelectorRecords() {
|
|
30
31
|
hasPendingSelectorFlush = false;
|
|
31
|
-
if (
|
|
32
|
-
const pending = Array.from(
|
|
33
|
-
|
|
34
|
-
for (const
|
|
35
|
-
|
|
36
|
-
if (!
|
|
37
|
-
|
|
32
|
+
if (dirtySelectorRecords.size === 0) return;
|
|
33
|
+
const pending = Array.from(dirtySelectorRecords);
|
|
34
|
+
dirtySelectorRecords.clear();
|
|
35
|
+
for (const record of pending) {
|
|
36
|
+
record._scheduled = false;
|
|
37
|
+
if (!record._dirty) continue;
|
|
38
|
+
recomputeSelectorSourceRecord(record, true);
|
|
38
39
|
}
|
|
39
40
|
}
|
|
40
41
|
function isObjectCandidate(value) {
|
|
@@ -48,131 +49,206 @@ function createCandidateSource(candidate) {
|
|
|
48
49
|
source._candidate = candidate;
|
|
49
50
|
return source;
|
|
50
51
|
}
|
|
51
|
-
function getCandidateSource(
|
|
52
|
+
function getCandidateSource(lane, candidate) {
|
|
52
53
|
if (isObjectCandidate(candidate)) {
|
|
53
|
-
const cached =
|
|
54
|
+
const cached = lane._objectCandidates.get(candidate);
|
|
54
55
|
if (cached) return cached;
|
|
55
56
|
const created = createCandidateSource(candidate);
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
cell._objectCandidateSources.add(created);
|
|
57
|
+
lane._objectCandidates.set(candidate, created);
|
|
58
|
+
lane._objectCandidateSources.add(created);
|
|
59
59
|
return created;
|
|
60
60
|
}
|
|
61
61
|
const key = candidate;
|
|
62
|
-
const cached =
|
|
62
|
+
const cached = lane._primitiveCandidates.get(key);
|
|
63
63
|
if (cached) return cached;
|
|
64
64
|
const created = createCandidateSource(candidate);
|
|
65
|
-
|
|
66
|
-
cell._primitiveCandidates.set(key, created);
|
|
65
|
+
lane._primitiveCandidates.set(key, created);
|
|
67
66
|
return created;
|
|
68
67
|
}
|
|
68
|
+
function getSelectorSourceRecord(source) {
|
|
69
|
+
const cached = selectorRecords.get(source);
|
|
70
|
+
if (cached) return cached;
|
|
71
|
+
const record = createSelectorSourceRecord(source);
|
|
72
|
+
selectorRecords.set(source, record);
|
|
73
|
+
return record;
|
|
74
|
+
}
|
|
75
|
+
function createSelectorSourceRecord(source) {
|
|
76
|
+
let record;
|
|
77
|
+
record = {
|
|
78
|
+
_source: source,
|
|
79
|
+
_value: void 0,
|
|
80
|
+
_hasValue: false,
|
|
81
|
+
_dirty: true,
|
|
82
|
+
_scheduled: false,
|
|
83
|
+
_evaluating: false,
|
|
84
|
+
_sources: /* @__PURE__ */ new Set(),
|
|
85
|
+
_lanes: /* @__PURE__ */ new Map(),
|
|
86
|
+
_markDirty: () => {
|
|
87
|
+
markSelectorRecordDirty(record);
|
|
88
|
+
},
|
|
89
|
+
_cleanup: () => {
|
|
90
|
+
record._scheduled = false;
|
|
91
|
+
record._dirty = false;
|
|
92
|
+
record._hasValue = false;
|
|
93
|
+
record._evaluating = false;
|
|
94
|
+
record._pendingDependencySources = void 0;
|
|
95
|
+
clearDerivedDependencySubscriptions(record, record._sources);
|
|
96
|
+
record._lanes.clear();
|
|
97
|
+
selectorRecords.delete(source);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
return record;
|
|
101
|
+
}
|
|
102
|
+
function getSelectorLane(record, equals) {
|
|
103
|
+
const cached = record._lanes.get(equals);
|
|
104
|
+
if (cached) return cached;
|
|
105
|
+
const lane = createSelectorLane(record, equals);
|
|
106
|
+
record._lanes.set(equals, lane);
|
|
107
|
+
return lane;
|
|
108
|
+
}
|
|
109
|
+
function createSelectorLane(record, equals) {
|
|
110
|
+
const lane = {
|
|
111
|
+
_record: record,
|
|
112
|
+
_equals: equals,
|
|
113
|
+
_bindingCount: 0,
|
|
114
|
+
_primitiveCandidates: /* @__PURE__ */ new Map(),
|
|
115
|
+
_objectCandidates: /* @__PURE__ */ new WeakMap(),
|
|
116
|
+
_objectCandidateSources: /* @__PURE__ */ new Set(),
|
|
117
|
+
_cleanup: () => {
|
|
118
|
+
for (const sourceRef of lane._primitiveCandidates.values()) {
|
|
119
|
+
sourceRef._readers?.clear();
|
|
120
|
+
sourceRef._derivedSubscribers?.clear();
|
|
121
|
+
}
|
|
122
|
+
for (const sourceRef of lane._objectCandidateSources) {
|
|
123
|
+
sourceRef._readers?.clear();
|
|
124
|
+
sourceRef._derivedSubscribers?.clear();
|
|
125
|
+
}
|
|
126
|
+
lane._primitiveCandidates.clear();
|
|
127
|
+
lane._objectCandidates = /* @__PURE__ */ new WeakMap();
|
|
128
|
+
lane._objectCandidateSources.clear();
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
return lane;
|
|
132
|
+
}
|
|
69
133
|
function notifySelectorSource(source) {
|
|
70
134
|
incrementPerfMetric("selectorInvalidations");
|
|
71
135
|
markReadableDerivedSubscribersDirty(source);
|
|
72
136
|
markReactivePropsDirtySource(source);
|
|
73
137
|
notifyReadableReaders(source);
|
|
74
138
|
}
|
|
75
|
-
function notifyAllSelectorSources(
|
|
76
|
-
for (const source of
|
|
77
|
-
for (const source of
|
|
139
|
+
function notifyAllSelectorSources(lane) {
|
|
140
|
+
for (const source of lane._primitiveCandidates.values()) notifySelectorSource(source);
|
|
141
|
+
for (const source of lane._objectCandidateSources) notifySelectorSource(source);
|
|
78
142
|
}
|
|
79
|
-
function
|
|
80
|
-
if (!
|
|
81
|
-
|
|
143
|
+
function notifySelectorLaneValueChange(lane, prevValue, nextValue) {
|
|
144
|
+
if (!lane._bindingCount) return;
|
|
145
|
+
if (!isDefaultSelectorEquals(lane._equals)) {
|
|
146
|
+
if (lane._equals(prevValue, nextValue)) return;
|
|
147
|
+
notifyAllSelectorSources(lane);
|
|
82
148
|
return;
|
|
83
149
|
}
|
|
84
150
|
if (!Object.is(prevValue, nextValue)) {
|
|
85
|
-
notifySelectorSource(getCandidateSource(
|
|
86
|
-
notifySelectorSource(getCandidateSource(
|
|
151
|
+
notifySelectorSource(getCandidateSource(lane, prevValue));
|
|
152
|
+
notifySelectorSource(getCandidateSource(lane, nextValue));
|
|
87
153
|
}
|
|
88
154
|
}
|
|
89
|
-
function
|
|
90
|
-
if (!
|
|
91
|
-
if (
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const prevSources =
|
|
96
|
-
const hadValue =
|
|
97
|
-
const prevValue =
|
|
155
|
+
function recomputeSelectorSourceRecord(record, notifyDownstream) {
|
|
156
|
+
if (!record._dirty && record._hasValue) return record._value;
|
|
157
|
+
if (record._evaluating) throw new Error("selector() cannot read itself recursively");
|
|
158
|
+
record._evaluating = true;
|
|
159
|
+
record._dirty = false;
|
|
160
|
+
record._pendingDependencySources = /* @__PURE__ */ new Set();
|
|
161
|
+
const prevSources = record._sources;
|
|
162
|
+
const hadValue = record._hasValue;
|
|
163
|
+
const prevValue = record._value;
|
|
98
164
|
let nextValue;
|
|
99
165
|
try {
|
|
100
|
-
nextValue = withDerivedReadTracking(
|
|
166
|
+
nextValue = withDerivedReadTracking(record, record._source);
|
|
101
167
|
} catch (error) {
|
|
102
|
-
|
|
103
|
-
|
|
168
|
+
record._dirty = true;
|
|
169
|
+
record._pendingDependencySources = void 0;
|
|
104
170
|
throw error;
|
|
105
171
|
} finally {
|
|
106
|
-
|
|
172
|
+
record._evaluating = false;
|
|
107
173
|
}
|
|
108
|
-
const nextSources =
|
|
109
|
-
|
|
110
|
-
syncDerivedDependencySubscriptions(
|
|
111
|
-
|
|
112
|
-
const valueChanged = !
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
if (valueChanged && notifyDownstream && hadValue)
|
|
116
|
-
return
|
|
117
|
-
}
|
|
118
|
-
function
|
|
119
|
-
const
|
|
120
|
-
const
|
|
121
|
-
|
|
174
|
+
const nextSources = record._pendingDependencySources ?? /* @__PURE__ */ new Set();
|
|
175
|
+
record._pendingDependencySources = void 0;
|
|
176
|
+
syncDerivedDependencySubscriptions(record, prevSources, nextSources);
|
|
177
|
+
record._sources = nextSources;
|
|
178
|
+
const valueChanged = !record._hasValue || !Object.is(prevValue, nextValue);
|
|
179
|
+
record._hasValue = true;
|
|
180
|
+
record._value = nextValue;
|
|
181
|
+
if (valueChanged && notifyDownstream && hadValue) for (const lane of Array.from(record._lanes.values())) notifySelectorLaneValueChange(lane, prevValue, nextValue);
|
|
182
|
+
return record._value;
|
|
183
|
+
}
|
|
184
|
+
function attachSelectorHookBinding(hook, source, equals) {
|
|
185
|
+
const record = getSelectorSourceRecord(source);
|
|
186
|
+
const lane = getSelectorLane(record, equals);
|
|
187
|
+
hook._source = source;
|
|
188
|
+
hook._equals = equals;
|
|
189
|
+
hook._record = record;
|
|
190
|
+
hook._lane = lane;
|
|
191
|
+
lane._bindingCount += 1;
|
|
192
|
+
}
|
|
193
|
+
function detachSelectorHookBinding(hook) {
|
|
194
|
+
const record = hook._record;
|
|
195
|
+
const lane = hook._lane;
|
|
196
|
+
hook._record = null;
|
|
197
|
+
hook._lane = null;
|
|
198
|
+
if (!record || !lane) return;
|
|
199
|
+
if (lane._bindingCount > 0) lane._bindingCount -= 1;
|
|
200
|
+
if (lane._bindingCount > 0) return;
|
|
201
|
+
lane._cleanup();
|
|
202
|
+
record._lanes.delete(lane._equals);
|
|
203
|
+
if (record._lanes.size === 0) record._cleanup();
|
|
204
|
+
}
|
|
205
|
+
function ensureSelectorHookBinding(hook) {
|
|
206
|
+
const record = hook._record;
|
|
207
|
+
const lane = hook._lane;
|
|
208
|
+
if (record && lane && lane._bindingCount > 0 && record._lanes.get(hook._equals) === lane && record._source === hook._source) return record;
|
|
209
|
+
if (hook._record || hook._lane) detachSelectorHookBinding(hook);
|
|
210
|
+
attachSelectorHookBinding(hook, hook._source, hook._equals);
|
|
211
|
+
return hook._record;
|
|
212
|
+
}
|
|
213
|
+
function createSelectorHook(instance, hookIndex, source, equals) {
|
|
214
|
+
const hook = function selectorPredicate(candidate) {
|
|
215
|
+
const selectorHook = hook;
|
|
216
|
+
const record = ensureSelectorHookBinding(selectorHook);
|
|
217
|
+
const lane = selectorHook._lane;
|
|
218
|
+
if (!lane) throw new Error("selector() binding could not be established.");
|
|
219
|
+
recordReadableRead(getCandidateSource(lane, candidate));
|
|
220
|
+
const perfMetricsStore = getPerfMetricsStore();
|
|
122
221
|
if (perfMetricsStore) perfMetricsStore.selectorCandidateReads += 1;
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
return selectorCell._equals(current, candidate);
|
|
126
|
-
};
|
|
127
|
-
cell._owner = instance;
|
|
128
|
-
cell._hookIndex = hookIndex;
|
|
129
|
-
cell._source = source;
|
|
130
|
-
cell._equals = equals;
|
|
131
|
-
cell._value = void 0;
|
|
132
|
-
cell._hasValue = false;
|
|
133
|
-
cell._dirty = true;
|
|
134
|
-
cell._scheduled = false;
|
|
135
|
-
cell._evaluating = false;
|
|
136
|
-
cell._sources = /* @__PURE__ */ new Set();
|
|
137
|
-
cell._primitiveCandidates = /* @__PURE__ */ new Map();
|
|
138
|
-
cell._objectCandidates = /* @__PURE__ */ new WeakMap();
|
|
139
|
-
cell._objectCandidateSources = /* @__PURE__ */ new Set();
|
|
140
|
-
cell._markDirty = () => {
|
|
141
|
-
markSelectorCellDirty(cell);
|
|
222
|
+
const current = record._dirty || !record._hasValue ? recomputeSelectorSourceRecord(record, record._scheduled) : record._value;
|
|
223
|
+
return lane._equals(current, candidate);
|
|
142
224
|
};
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
sourceRef._derivedSubscribers?.clear();
|
|
152
|
-
}
|
|
153
|
-
for (const sourceRef of cell._objectCandidateSources) {
|
|
154
|
-
sourceRef._readers?.clear();
|
|
155
|
-
sourceRef._derivedSubscribers?.clear();
|
|
156
|
-
}
|
|
157
|
-
cell._primitiveCandidates.clear();
|
|
158
|
-
cell._objectCandidateSources.clear();
|
|
225
|
+
hook._owner = instance;
|
|
226
|
+
hook._hookIndex = hookIndex;
|
|
227
|
+
hook._source = source;
|
|
228
|
+
hook._equals = equals;
|
|
229
|
+
hook._record = null;
|
|
230
|
+
hook._lane = null;
|
|
231
|
+
hook._cleanup = () => {
|
|
232
|
+
detachSelectorHookBinding(hook);
|
|
159
233
|
};
|
|
234
|
+
attachSelectorHookBinding(hook, source, equals);
|
|
160
235
|
(instance.cleanupFns ??= []).push(() => {
|
|
161
|
-
|
|
236
|
+
hook._cleanup();
|
|
162
237
|
selectorCells.get(instance)?.delete(hookIndex);
|
|
163
238
|
});
|
|
164
|
-
return
|
|
239
|
+
return hook;
|
|
165
240
|
}
|
|
166
|
-
function
|
|
241
|
+
function getOrCreateSelectorHook(instance, hookIndex, source, equals) {
|
|
167
242
|
const store = getSelectorStore(instance);
|
|
168
243
|
const existing = store.get(hookIndex);
|
|
169
244
|
if (existing) {
|
|
170
|
-
existing._source
|
|
171
|
-
|
|
172
|
-
|
|
245
|
+
if (existing._source !== source || existing._equals !== equals) {
|
|
246
|
+
detachSelectorHookBinding(existing);
|
|
247
|
+
attachSelectorHookBinding(existing, source, equals);
|
|
248
|
+
} else ensureSelectorHookBinding(existing);
|
|
173
249
|
return existing;
|
|
174
250
|
}
|
|
175
|
-
const created =
|
|
251
|
+
const created = createSelectorHook(instance, hookIndex, source, equals);
|
|
176
252
|
store.set(hookIndex, created);
|
|
177
253
|
return created;
|
|
178
254
|
}
|
|
@@ -180,9 +256,10 @@ function selector(source, equals = Object.is) {
|
|
|
180
256
|
markReadableUsage(source);
|
|
181
257
|
const instance = getCurrentInstance();
|
|
182
258
|
if (!instance) throw new Error("selector() can only be called during component render execution. Move selector() calls to the top level of your component function.");
|
|
183
|
-
const
|
|
184
|
-
|
|
185
|
-
|
|
259
|
+
const hook = getOrCreateSelectorHook(instance, claimHookIndex(instance, "selector"), source, equals);
|
|
260
|
+
if (!hook._record) throw new Error("selector() record binding was not established.");
|
|
261
|
+
if (!hook._record._hasValue) recomputeSelectorSourceRecord(hook._record, false);
|
|
262
|
+
return hook;
|
|
186
263
|
}
|
|
187
264
|
//#endregion
|
|
188
265
|
export { selector };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"selector.js","names":[],"sources":["../../src/runtime/selector.ts"],"sourcesContent":["import {\n claimHookIndex,\n getCurrentInstance,\n type ComponentInstance,\n} from './component';\nimport { globalScheduler } from './scheduler';\nimport {\n clearDerivedDependencySubscriptions,\n markReadableDerivedSubscribersDirty,\n markReadableUsage,\n markReactivePropsDirtySource,\n notifyReadableReaders,\n recordReadableRead,\n syncDerivedDependencySubscriptions,\n withDerivedReadTracking,\n type DerivedSubscriber,\n type ReadableSource,\n} from './readable';\nimport { getPerfMetricsStore, incrementPerfMetric } from './perf-metrics';\n\ntype PrimitiveKey =\n | string\n | number\n | boolean\n | symbol\n | bigint\n | null\n | undefined;\n\nexport interface Selector<T> {\n (candidate: T): boolean;\n}\n\ninterface SelectorCandidateSource<T> extends ReadableSource<boolean> {\n _candidate: T;\n}\n\ntype SelectorEquals<T> = {\n bivarianceHack(a: T, b: T): boolean;\n}['bivarianceHack'];\n\ninterface SelectorCell<T> extends Selector<T>, DerivedSubscriber {\n _owner: ComponentInstance;\n _hookIndex: number;\n _source: () => T;\n _equals: SelectorEquals<T>;\n _value: T;\n _hasValue: boolean;\n _dirty: boolean;\n _scheduled: boolean;\n _evaluating: boolean;\n _sources: Set<ReadableSource<unknown>>;\n _pendingDependencySources?: Set<ReadableSource<unknown>>;\n _primitiveCandidates: Map<PrimitiveKey, SelectorCandidateSource<T>>;\n _objectCandidates: WeakMap<object, SelectorCandidateSource<T>>;\n _objectCandidateSources: Set<SelectorCandidateSource<T>>;\n _cleanup(): void;\n}\n\nconst selectorCells = new WeakMap<\n ComponentInstance,\n Map<number, SelectorCell<unknown>>\n>();\nconst dirtySelectorCells = new Set<SelectorCell<unknown>>();\nlet hasPendingSelectorFlush = false;\n\nfunction getSelectorStore(\n instance: ComponentInstance\n): Map<number, SelectorCell<unknown>> {\n let store = selectorCells.get(instance);\n if (!store) {\n store = new Map();\n selectorCells.set(instance, store);\n }\n return store;\n}\n\nfunction scheduleSelectorFlush(): void {\n if (hasPendingSelectorFlush) {\n return;\n }\n hasPendingSelectorFlush = true;\n globalScheduler.enqueueInLane('derived', flushDirtySelectorCells);\n}\n\nfunction markSelectorCellDirty(cell: SelectorCell<unknown>): void {\n cell._dirty = true;\n if (cell._scheduled) {\n return;\n }\n\n cell._scheduled = true;\n dirtySelectorCells.add(cell);\n scheduleSelectorFlush();\n}\n\nfunction flushDirtySelectorCells(): void {\n hasPendingSelectorFlush = false;\n\n if (dirtySelectorCells.size === 0) {\n return;\n }\n\n const pending = Array.from(dirtySelectorCells);\n dirtySelectorCells.clear();\n\n for (const cell of pending) {\n cell._scheduled = false;\n if (!cell._dirty) {\n continue;\n }\n recomputeSelectorCell(cell, true);\n }\n}\n\nfunction isObjectCandidate(value: unknown): value is object {\n return (\n (typeof value === 'object' && value !== null) || typeof value === 'function'\n );\n}\n\nfunction isDefaultSelectorEquals<T>(equals: SelectorEquals<T>): boolean {\n return equals === Object.is;\n}\n\nfunction createCandidateSource<T>(candidate: T): SelectorCandidateSource<T> {\n const source = (() => false) as SelectorCandidateSource<T>;\n source._candidate = candidate;\n return source;\n}\n\nfunction getCandidateSource<T>(\n cell: SelectorCell<T>,\n candidate: T\n): SelectorCandidateSource<T> {\n if (isObjectCandidate(candidate)) {\n const cached = cell._objectCandidates.get(candidate);\n if (cached) {\n return cached;\n }\n\n const created = createCandidateSource(candidate);\n created._candidate = candidate;\n cell._objectCandidates.set(candidate, created);\n cell._objectCandidateSources.add(created);\n return created;\n }\n\n const key = candidate as PrimitiveKey;\n const cached = cell._primitiveCandidates.get(key);\n if (cached) {\n return cached;\n }\n\n const created = createCandidateSource(candidate);\n created._candidate = candidate;\n cell._primitiveCandidates.set(key, created);\n return created;\n}\n\nfunction notifySelectorSource(source: SelectorCandidateSource<unknown>): void {\n incrementPerfMetric('selectorInvalidations');\n markReadableDerivedSubscribersDirty(source);\n markReactivePropsDirtySource(source);\n notifyReadableReaders(source);\n}\n\nfunction notifyAllSelectorSources<T>(cell: SelectorCell<T>): void {\n for (const source of cell._primitiveCandidates.values()) {\n notifySelectorSource(source);\n }\n for (const source of cell._objectCandidateSources) {\n notifySelectorSource(source);\n }\n}\n\nfunction notifySelectorValueChange<T>(\n cell: SelectorCell<T>,\n prevValue: T,\n nextValue: T\n): void {\n if (!isDefaultSelectorEquals(cell._equals)) {\n notifyAllSelectorSources(cell);\n return;\n }\n\n if (!Object.is(prevValue, nextValue)) {\n notifySelectorSource(getCandidateSource(cell, prevValue));\n notifySelectorSource(getCandidateSource(cell, nextValue));\n }\n}\n\nfunction recomputeSelectorCell<T>(\n cell: SelectorCell<T>,\n notifyDownstream: boolean\n): T {\n if (!cell._dirty && cell._hasValue) {\n return cell._value;\n }\n\n if (cell._evaluating) {\n throw new Error('selector() cannot read itself recursively');\n }\n\n cell._evaluating = true;\n cell._dirty = false;\n cell._pendingDependencySources = new Set();\n\n const prevSources = cell._sources;\n const hadValue = cell._hasValue;\n const prevValue = cell._value;\n let nextValue: T;\n\n try {\n nextValue = withDerivedReadTracking(cell, cell._source);\n } catch (error) {\n cell._dirty = true;\n cell._pendingDependencySources = undefined;\n throw error;\n } finally {\n cell._evaluating = false;\n }\n\n const nextSources = cell._pendingDependencySources ?? new Set();\n cell._pendingDependencySources = undefined;\n syncDerivedDependencySubscriptions(cell, prevSources, nextSources);\n cell._sources = nextSources;\n\n const valueChanged = !cell._hasValue || !cell._equals(prevValue, nextValue);\n cell._hasValue = true;\n cell._value = nextValue;\n\n if (valueChanged && notifyDownstream && hadValue) {\n notifySelectorValueChange(cell, prevValue, nextValue);\n }\n\n return cell._value;\n}\n\nfunction createSelectorCell<T>(\n instance: ComponentInstance,\n hookIndex: number,\n source: () => T,\n equals: SelectorEquals<T>\n): SelectorCell<T> {\n const perfMetricsStore = getPerfMetricsStore();\n const cell = function selectorPredicate(candidate: T): boolean {\n const sourceRef = getCandidateSource(cell as SelectorCell<T>, candidate);\n recordReadableRead(sourceRef);\n if (perfMetricsStore) {\n perfMetricsStore.selectorCandidateReads += 1;\n }\n const selectorCell = cell as SelectorCell<T>;\n const current =\n selectorCell._dirty || !selectorCell._hasValue\n ? recomputeSelectorCell(selectorCell, selectorCell._scheduled)\n : selectorCell._value;\n return selectorCell._equals(current, candidate);\n } as SelectorCell<T>;\n\n cell._owner = instance;\n cell._hookIndex = hookIndex;\n cell._source = source;\n cell._equals = equals;\n cell._value = undefined as T;\n cell._hasValue = false;\n cell._dirty = true;\n cell._scheduled = false;\n cell._evaluating = false;\n cell._sources = new Set();\n cell._primitiveCandidates = new Map();\n cell._objectCandidates = new WeakMap();\n cell._objectCandidateSources = new Set();\n cell._markDirty = () => {\n markSelectorCellDirty(cell as unknown as SelectorCell<unknown>);\n };\n cell._cleanup = () => {\n cell._scheduled = false;\n cell._dirty = false;\n cell._hasValue = false;\n dirtySelectorCells.delete(cell as unknown as SelectorCell<unknown>);\n clearDerivedDependencySubscriptions(cell, cell._sources);\n for (const sourceRef of cell._primitiveCandidates.values()) {\n sourceRef._readers?.clear();\n sourceRef._derivedSubscribers?.clear();\n }\n for (const sourceRef of cell._objectCandidateSources) {\n sourceRef._readers?.clear();\n sourceRef._derivedSubscribers?.clear();\n }\n cell._primitiveCandidates.clear();\n cell._objectCandidateSources.clear();\n };\n\n (instance.cleanupFns ??= []).push(() => {\n cell._cleanup();\n selectorCells.get(instance)?.delete(hookIndex);\n });\n\n return cell;\n}\n\nfunction getOrCreateSelectorCell<T>(\n instance: ComponentInstance,\n hookIndex: number,\n source: () => T,\n equals: SelectorEquals<T>\n): SelectorCell<T> {\n const store = getSelectorStore(instance);\n const existing = store.get(hookIndex) as SelectorCell<T> | undefined;\n if (existing) {\n existing._source = source;\n existing._equals = equals;\n existing._dirty = true;\n return existing;\n }\n\n const created = createSelectorCell(instance, hookIndex, source, equals);\n store.set(hookIndex, created as unknown as SelectorCell<unknown>);\n return created;\n}\n\nexport function selector<T>(\n source: () => T,\n equals: SelectorEquals<T> = Object.is\n): Selector<T> {\n markReadableUsage(source);\n\n const instance = getCurrentInstance();\n if (!instance) {\n throw new Error(\n 'selector() can only be called during component render execution. ' +\n 'Move selector() calls to the top level of your component function.'\n );\n }\n\n const hookIndex = claimHookIndex(instance, 'selector');\n const cell = getOrCreateSelectorCell(instance, hookIndex, source, equals);\n recomputeSelectorCell(cell, false);\n return cell;\n}\n"],"mappings":";;;;;AA2DA,MAAM,gCAAgB,IAAI,SAGvB;AACH,MAAM,qCAAqB,IAAI,KAA4B;AAC3D,IAAI,0BAA0B;AAE9B,SAAS,iBACP,UACoC;CACpC,IAAI,QAAQ,cAAc,IAAI,SAAS;CACvC,IAAI,CAAC,OAAO;EACV,wBAAQ,IAAI,KAAK;EACjB,cAAc,IAAI,UAAU,MAAM;;CAEpC,OAAO;;AAGT,SAAS,wBAA8B;CACrC,IAAI,yBACF;CAEF,0BAA0B;CAC1B,gBAAgB,cAAc,WAAW,wBAAwB;;AAGnE,SAAS,sBAAsB,MAAmC;CAChE,KAAK,SAAS;CACd,IAAI,KAAK,YACP;CAGF,KAAK,aAAa;CAClB,mBAAmB,IAAI,KAAK;CAC5B,uBAAuB;;AAGzB,SAAS,0BAAgC;CACvC,0BAA0B;CAE1B,IAAI,mBAAmB,SAAS,GAC9B;CAGF,MAAM,UAAU,MAAM,KAAK,mBAAmB;CAC9C,mBAAmB,OAAO;CAE1B,KAAK,MAAM,QAAQ,SAAS;EAC1B,KAAK,aAAa;EAClB,IAAI,CAAC,KAAK,QACR;EAEF,sBAAsB,MAAM,KAAK;;;AAIrC,SAAS,kBAAkB,OAAiC;CAC1D,OACG,OAAO,UAAU,YAAY,UAAU,QAAS,OAAO,UAAU;;AAItE,SAAS,wBAA2B,QAAoC;CACtE,OAAO,WAAW,OAAO;;AAG3B,SAAS,sBAAyB,WAA0C;CAC1E,MAAM,gBAAgB;CACtB,OAAO,aAAa;CACpB,OAAO;;AAGT,SAAS,mBACP,MACA,WAC4B;CAC5B,IAAI,kBAAkB,UAAU,EAAE;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qB,QAAQ,EAAE;GAC1D,UAAU,UAAU,OAAO;GAC3B,UAAU,qBAAqB,OAAO;;EAExC,KAAK,MAAM,aAAa,KAAK,yBAAyB;GACpD,UAAU,UAAU,OAAO;GAC3B,UAAU,qBAAqB,OAAO;;EAExC,KAAK,qBAAqB,OAAO;EACjC,KAAK,wBAAwB,OAAO;;CAGtC,CAAC,SAAS,eAAe,EAAE,EAAE,WAAW;EACtC,KAAK,UAAU;EACf,cAAc,IAAI,SAAS,EAAE,OAAO,UAAU;GAC9C;CAEF,OAAO;;AAGT,SAAS,wBACP,UACA,WACA,QACA,QACiB;CACjB,MAAM,QAAQ,iBAAiB,SAAS;CACxC,MAAM,WAAW,MAAM,IAAI,UAAU;CACrC,IAAI,UAAU;EACZ,SAAS,UAAU;EACnB,SAAS,UAAU;EACnB,SAAS,SAAS;EAClB,OAAO;;CAGT,MAAM,UAAU,mBAAmB,UAAU,WAAW,QAAQ,OAAO;CACvE,MAAM,IAAI,WAAW,QAA4C;CACjE,OAAO;;AAGT,SAAgB,SACd,QACA,SAA4B,OAAO,IACtB;CACb,kBAAkB,OAAO;CAEzB,MAAM,WAAW,oBAAoB;CACrC,IAAI,CAAC,UACH,MAAM,IAAI,MACR,sIAED;CAIH,MAAM,OAAO,wBAAwB,UADnB,eAAe,UAAU,WACI,EAAW,QAAQ,OAAO;CACzE,sBAAsB,MAAM,MAAM;CAClC,OAAO"}
|
|
1
|
+
{"version":3,"file":"selector.js","names":[],"sources":["../../src/runtime/selector.ts"],"sourcesContent":["import {\n claimHookIndex,\n getCurrentInstance,\n type ComponentInstance,\n} from './component';\nimport { globalScheduler } from './scheduler';\nimport {\n clearDerivedDependencySubscriptions,\n markReadableDerivedSubscribersDirty,\n markReadableUsage,\n markReactivePropsDirtySource,\n notifyReadableReaders,\n recordReadableRead,\n syncDerivedDependencySubscriptions,\n withDerivedReadTracking,\n type DerivedSubscriber,\n type ReadableSource,\n} from './readable';\nimport { getPerfMetricsStore, incrementPerfMetric } from './perf-metrics';\n\ntype PrimitiveKey =\n | string\n | number\n | boolean\n | symbol\n | bigint\n | null\n | undefined;\n\nexport interface Selector<T> {\n (candidate: T): boolean;\n}\n\ninterface SelectorCandidateSource<T> extends ReadableSource<boolean> {\n _candidate: T;\n}\n\ntype SelectorEquals<T> = {\n bivarianceHack(a: T, b: T): boolean;\n}['bivarianceHack'];\n\ninterface SelectorLane<T> {\n _record: SelectorSourceRecord<T>;\n _equals: SelectorEquals<T>;\n _bindingCount: number;\n _primitiveCandidates: Map<PrimitiveKey, SelectorCandidateSource<T>>;\n _objectCandidates: WeakMap<object, SelectorCandidateSource<T>>;\n _objectCandidateSources: Set<SelectorCandidateSource<T>>;\n _cleanup(): void;\n}\n\ninterface SelectorSourceRecord<T> extends DerivedSubscriber {\n _source: () => T;\n _value: T;\n _hasValue: boolean;\n _dirty: boolean;\n _scheduled: boolean;\n _evaluating: boolean;\n _sources: Set<ReadableSource<unknown>>;\n _pendingDependencySources?: Set<ReadableSource<unknown>>;\n _lanes: Map<SelectorEquals<T>, SelectorLane<T>>;\n _cleanup(): void;\n}\n\ninterface SelectorHook<T> extends Selector<T> {\n _owner: ComponentInstance;\n _hookIndex: number;\n _source: () => T;\n _equals: SelectorEquals<T>;\n _record: SelectorSourceRecord<T> | null;\n _lane: SelectorLane<T> | null;\n _cleanup(): void;\n}\n\nconst selectorCells = new WeakMap<\n ComponentInstance,\n Map<number, SelectorHook<unknown>>\n>();\nconst selectorRecords = new WeakMap<\n ReadableSource<unknown>,\n SelectorSourceRecord<unknown>\n>();\nconst dirtySelectorRecords = new Set<SelectorSourceRecord<unknown>>();\nlet hasPendingSelectorFlush = false;\n\nfunction getSelectorStore(\n instance: ComponentInstance\n): Map<number, SelectorHook<unknown>> {\n let store = selectorCells.get(instance);\n if (!store) {\n store = new Map();\n selectorCells.set(instance, store);\n }\n return store;\n}\n\nfunction scheduleSelectorFlush(): void {\n if (hasPendingSelectorFlush) {\n return;\n }\n\n hasPendingSelectorFlush = true;\n globalScheduler.enqueueInLane('derived', flushDirtySelectorRecords);\n}\n\nfunction markSelectorRecordDirty(record: SelectorSourceRecord<unknown>): void {\n record._dirty = true;\n if (record._scheduled) {\n return;\n }\n\n record._scheduled = true;\n dirtySelectorRecords.add(record);\n scheduleSelectorFlush();\n}\n\nfunction flushDirtySelectorRecords(): void {\n hasPendingSelectorFlush = false;\n\n if (dirtySelectorRecords.size === 0) {\n return;\n }\n\n const pending = Array.from(dirtySelectorRecords);\n dirtySelectorRecords.clear();\n\n for (const record of pending) {\n record._scheduled = false;\n if (!record._dirty) {\n continue;\n }\n recomputeSelectorSourceRecord(record, true);\n }\n}\n\nfunction isObjectCandidate(value: unknown): value is object {\n return (\n (typeof value === 'object' && value !== null) || typeof value === 'function'\n );\n}\n\nfunction isDefaultSelectorEquals<T>(equals: SelectorEquals<T>): boolean {\n return equals === Object.is;\n}\n\nfunction createCandidateSource<T>(candidate: T): SelectorCandidateSource<T> {\n const source = (() => false) as SelectorCandidateSource<T>;\n source._candidate = candidate;\n return source;\n}\n\nfunction getCandidateSource<T>(\n lane: SelectorLane<T>,\n candidate: T\n): SelectorCandidateSource<T> {\n if (isObjectCandidate(candidate)) {\n const cached = lane._objectCandidates.get(candidate);\n if (cached) {\n return cached;\n }\n\n const created = createCandidateSource(candidate);\n lane._objectCandidates.set(candidate, created);\n lane._objectCandidateSources.add(created);\n return created;\n }\n\n const key = candidate as PrimitiveKey;\n const cached = lane._primitiveCandidates.get(key);\n if (cached) {\n return cached;\n }\n\n const created = createCandidateSource(candidate);\n lane._primitiveCandidates.set(key, created);\n return created;\n}\n\nfunction getSelectorSourceRecord<T>(source: () => T): SelectorSourceRecord<T> {\n const cached = selectorRecords.get(source);\n if (cached) {\n return cached as SelectorSourceRecord<T>;\n }\n\n const record = createSelectorSourceRecord(source);\n selectorRecords.set(source, record as SelectorSourceRecord<unknown>);\n return record;\n}\n\nfunction createSelectorSourceRecord<T>(\n source: () => T\n): SelectorSourceRecord<T> {\n let record!: SelectorSourceRecord<T>;\n\n record = {\n _source: source,\n _value: undefined as T,\n _hasValue: false,\n _dirty: true,\n _scheduled: false,\n _evaluating: false,\n _sources: new Set(),\n _lanes: new Map(),\n _markDirty: () => {\n markSelectorRecordDirty(record as SelectorSourceRecord<unknown>);\n },\n _cleanup: () => {\n record._scheduled = false;\n record._dirty = false;\n record._hasValue = false;\n record._evaluating = false;\n record._pendingDependencySources = undefined;\n clearDerivedDependencySubscriptions(record, record._sources);\n record._lanes.clear();\n selectorRecords.delete(source as ReadableSource<unknown>);\n },\n };\n\n return record;\n}\n\nfunction getSelectorLane<T>(\n record: SelectorSourceRecord<T>,\n equals: SelectorEquals<T>\n): SelectorLane<T> {\n const cached = record._lanes.get(equals);\n if (cached) {\n return cached;\n }\n\n const lane = createSelectorLane(record, equals);\n record._lanes.set(equals, lane);\n return lane;\n}\n\nfunction createSelectorLane<T>(\n record: SelectorSourceRecord<T>,\n equals: SelectorEquals<T>\n): SelectorLane<T> {\n const lane: SelectorLane<T> = {\n _record: record,\n _equals: equals,\n _bindingCount: 0,\n _primitiveCandidates: new Map(),\n _objectCandidates: new WeakMap(),\n _objectCandidateSources: new Set(),\n _cleanup: () => {\n for (const sourceRef of lane._primitiveCandidates.values()) {\n sourceRef._readers?.clear();\n sourceRef._derivedSubscribers?.clear();\n }\n for (const sourceRef of lane._objectCandidateSources) {\n sourceRef._readers?.clear();\n sourceRef._derivedSubscribers?.clear();\n }\n lane._primitiveCandidates.clear();\n lane._objectCandidates = new WeakMap();\n lane._objectCandidateSources.clear();\n },\n };\n\n return lane;\n}\n\nfunction notifySelectorSource(source: SelectorCandidateSource<unknown>): void {\n incrementPerfMetric('selectorInvalidations');\n markReadableDerivedSubscribersDirty(source);\n markReactivePropsDirtySource(source);\n notifyReadableReaders(source);\n}\n\nfunction notifyAllSelectorSources<T>(lane: SelectorLane<T>): void {\n for (const source of lane._primitiveCandidates.values()) {\n notifySelectorSource(source);\n }\n for (const source of lane._objectCandidateSources) {\n notifySelectorSource(source);\n }\n}\n\nfunction notifySelectorLaneValueChange<T>(\n lane: SelectorLane<T>,\n prevValue: T,\n nextValue: T\n): void {\n if (!lane._bindingCount) {\n return;\n }\n\n if (!isDefaultSelectorEquals(lane._equals)) {\n if (lane._equals(prevValue, nextValue)) {\n return;\n }\n notifyAllSelectorSources(lane);\n return;\n }\n\n if (!Object.is(prevValue, nextValue)) {\n notifySelectorSource(getCandidateSource(lane, prevValue));\n notifySelectorSource(getCandidateSource(lane, nextValue));\n }\n}\n\nfunction recomputeSelectorSourceRecord<T>(\n record: SelectorSourceRecord<T>,\n notifyDownstream: boolean\n): T {\n if (!record._dirty && record._hasValue) {\n return record._value;\n }\n\n if (record._evaluating) {\n throw new Error('selector() cannot read itself recursively');\n }\n\n record._evaluating = true;\n record._dirty = false;\n record._pendingDependencySources = new Set();\n\n const prevSources = record._sources;\n const hadValue = record._hasValue;\n const prevValue = record._value;\n let nextValue: T;\n\n try {\n nextValue = withDerivedReadTracking(record, record._source);\n } catch (error) {\n record._dirty = true;\n record._pendingDependencySources = undefined;\n throw error;\n } finally {\n record._evaluating = false;\n }\n\n const nextSources = record._pendingDependencySources ?? new Set();\n record._pendingDependencySources = undefined;\n syncDerivedDependencySubscriptions(record, prevSources, nextSources);\n record._sources = nextSources;\n\n const valueChanged = !record._hasValue || !Object.is(prevValue, nextValue);\n record._hasValue = true;\n record._value = nextValue;\n\n if (valueChanged && notifyDownstream && hadValue) {\n for (const lane of Array.from(record._lanes.values())) {\n notifySelectorLaneValueChange(lane, prevValue, nextValue);\n }\n }\n\n return record._value;\n}\n\nfunction attachSelectorHookBinding<T>(\n hook: SelectorHook<T>,\n source: () => T,\n equals: SelectorEquals<T>\n): void {\n const record = getSelectorSourceRecord(source);\n const lane = getSelectorLane(record, equals);\n\n hook._source = source;\n hook._equals = equals;\n hook._record = record;\n hook._lane = lane;\n lane._bindingCount += 1;\n}\n\nfunction detachSelectorHookBinding<T>(hook: SelectorHook<T>): void {\n const record = hook._record;\n const lane = hook._lane;\n\n hook._record = null;\n hook._lane = null;\n\n if (!record || !lane) {\n return;\n }\n\n if (lane._bindingCount > 0) {\n lane._bindingCount -= 1;\n }\n\n if (lane._bindingCount > 0) {\n return;\n }\n\n lane._cleanup();\n record._lanes.delete(lane._equals);\n\n if (record._lanes.size === 0) {\n record._cleanup();\n }\n}\n\nfunction ensureSelectorHookBinding<T>(\n hook: SelectorHook<T>\n): SelectorSourceRecord<T> {\n const record = hook._record;\n const lane = hook._lane;\n\n if (\n record &&\n lane &&\n lane._bindingCount > 0 &&\n record._lanes.get(hook._equals) === lane &&\n record._source === hook._source\n ) {\n return record;\n }\n\n if (hook._record || hook._lane) {\n detachSelectorHookBinding(hook);\n }\n\n attachSelectorHookBinding(hook, hook._source, hook._equals);\n return hook._record!;\n}\n\nfunction createSelectorHook<T>(\n instance: ComponentInstance,\n hookIndex: number,\n source: () => T,\n equals: SelectorEquals<T>\n): SelectorHook<T> {\n const hook = function selectorPredicate(candidate: T): boolean {\n const selectorHook = hook as SelectorHook<T>;\n const record = ensureSelectorHookBinding(selectorHook);\n const lane = selectorHook._lane;\n if (!lane) {\n throw new Error('selector() binding could not be established.');\n }\n\n const sourceRef = getCandidateSource(lane, candidate);\n recordReadableRead(sourceRef);\n\n const perfMetricsStore = getPerfMetricsStore();\n if (perfMetricsStore) {\n perfMetricsStore.selectorCandidateReads += 1;\n }\n\n const current =\n record._dirty || !record._hasValue\n ? recomputeSelectorSourceRecord(record, record._scheduled)\n : record._value;\n\n return lane._equals(current, candidate);\n } as SelectorHook<T>;\n\n hook._owner = instance;\n hook._hookIndex = hookIndex;\n hook._source = source;\n hook._equals = equals;\n hook._record = null;\n hook._lane = null;\n hook._cleanup = () => {\n detachSelectorHookBinding(hook);\n };\n\n attachSelectorHookBinding(hook, source, equals);\n\n (instance.cleanupFns ??= []).push(() => {\n hook._cleanup();\n selectorCells.get(instance)?.delete(hookIndex);\n });\n\n return hook;\n}\n\nfunction getOrCreateSelectorHook<T>(\n instance: ComponentInstance,\n hookIndex: number,\n source: () => T,\n equals: SelectorEquals<T>\n): SelectorHook<T> {\n const store = getSelectorStore(instance);\n const existing = store.get(hookIndex) as SelectorHook<T> | undefined;\n if (existing) {\n if (existing._source !== source || existing._equals !== equals) {\n detachSelectorHookBinding(existing);\n attachSelectorHookBinding(existing, source, equals);\n } else {\n ensureSelectorHookBinding(existing);\n }\n return existing;\n }\n\n const created = createSelectorHook(instance, hookIndex, source, equals);\n store.set(hookIndex, created as unknown as SelectorHook<unknown>);\n return created;\n}\n\nexport function selector<T>(\n source: () => T,\n equals: SelectorEquals<T> = Object.is\n): Selector<T> {\n markReadableUsage(source);\n\n const instance = getCurrentInstance();\n if (!instance) {\n throw new Error(\n 'selector() can only be called during component render execution. ' +\n 'Move selector() calls to the top level of your component function.'\n );\n }\n\n const hookIndex = claimHookIndex(instance, 'selector');\n const hook = getOrCreateSelectorHook(instance, hookIndex, source, equals);\n if (!hook._record) {\n throw new Error('selector() record binding was not established.');\n }\n\n if (!hook._record._hasValue) {\n // Initialize the shared source once so downstream selectors can subscribe,\n // but do not consume a pending dirty state on later renders.\n recomputeSelectorSourceRecord(hook._record, false);\n }\n return hook;\n}\n"],"mappings":";;;;;AA0EA,MAAM,gCAAgB,IAAI,SAGvB;AACH,MAAM,kCAAkB,IAAI,SAGzB;AACH,MAAM,uCAAuB,IAAI,KAAoC;AACrE,IAAI,0BAA0B;AAE9B,SAAS,iBACP,UACoC;CACpC,IAAI,QAAQ,cAAc,IAAI,SAAS;CACvC,IAAI,CAAC,OAAO;EACV,wBAAQ,IAAI,KAAK;EACjB,cAAc,IAAI,UAAU,MAAM;;CAEpC,OAAO;;AAGT,SAAS,wBAA8B;CACrC,IAAI,yBACF;CAGF,0BAA0B;CAC1B,gBAAgB,cAAc,WAAW,0BAA0B;;AAGrE,SAAS,wBAAwB,QAA6C;CAC5E,OAAO,SAAS;CAChB,IAAI,OAAO,YACT;CAGF,OAAO,aAAa;CACpB,qBAAqB,IAAI,OAAO;CA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,0BAA0B,KAAK;;CAGjC,0BAA0B,MAAM,QAAQ,OAAO;CAE/C,CAAC,SAAS,eAAe,EAAE,EAAE,WAAW;EACtC,KAAK,UAAU;EACf,cAAc,IAAI,SAAS,EAAE,OAAO,UAAU;GAC9C;CAEF,OAAO;;AAGT,SAAS,wBACP,UACA,WACA,QACA,QACiB;CACjB,MAAM,QAAQ,iBAAiB,SAAS;CACxC,MAAM,WAAW,MAAM,IAAI,UAAU;CACrC,IAAI,UAAU;EACZ,IAAI,SAAS,YAAY,UAAU,SAAS,YAAY,QAAQ;GAC9D,0BAA0B,SAAS;GACnC,0BAA0B,UAAU,QAAQ,OAAO;SAEnD,0BAA0B,SAAS;EAErC,OAAO;;CAGT,MAAM,UAAU,mBAAmB,UAAU,WAAW,QAAQ,OAAO;CACvE,MAAM,IAAI,WAAW,QAA4C;CACjE,OAAO;;AAGT,SAAgB,SACd,QACA,SAA4B,OAAO,IACtB;CACb,kBAAkB,OAAO;CAEzB,MAAM,WAAW,oBAAoB;CACrC,IAAI,CAAC,UACH,MAAM,IAAI,MACR,sIAED;CAIH,MAAM,OAAO,wBAAwB,UADnB,eAAe,UAAU,WACI,EAAW,QAAQ,OAAO;CACzE,IAAI,CAAC,KAAK,SACR,MAAM,IAAI,MAAM,iDAAiD;CAGnE,IAAI,CAAC,KAAK,QAAQ,WAGhB,8BAA8B,KAAK,SAAS,MAAM;CAEpD,OAAO"}
|
package/dist/runtime/state.d.ts
CHANGED
|
@@ -12,8 +12,16 @@ interface State<T> extends ReadableSource<T> {
|
|
|
12
12
|
(): T;
|
|
13
13
|
set(value: T): void;
|
|
14
14
|
set(updater: (prev: T) => T): void;
|
|
15
|
-
[Symbol.iterator]():
|
|
15
|
+
[Symbol.iterator](): IterableIterator<StateTuple<T>[number]>;
|
|
16
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Public setter type for state cells.
|
|
19
|
+
*/
|
|
20
|
+
type StateSetter<T> = State<T>['set'];
|
|
21
|
+
/**
|
|
22
|
+
* Tuple-first state handle returned by `state()`.
|
|
23
|
+
*/
|
|
24
|
+
type StateTuple<T> = [get: State<T>, set: StateSetter<T>] & State<T>;
|
|
17
25
|
/**
|
|
18
26
|
* Creates a local state value for a component
|
|
19
27
|
* Optimized for:
|
|
@@ -29,7 +37,7 @@ interface State<T> extends ReadableSource<T> {
|
|
|
29
37
|
* ```ts
|
|
30
38
|
* // ✅ Correct: called during render
|
|
31
39
|
* export function Counter() {
|
|
32
|
-
* const count = state(0);
|
|
40
|
+
* const [count, setCount] = state(0);
|
|
33
41
|
* return { type: 'button', children: [count()] };
|
|
34
42
|
* }
|
|
35
43
|
*
|
|
@@ -40,7 +48,7 @@ interface State<T> extends ReadableSource<T> {
|
|
|
40
48
|
* }
|
|
41
49
|
* ```
|
|
42
50
|
*/
|
|
43
|
-
declare function state<T>(initialValue: T):
|
|
51
|
+
declare function state<T>(initialValue: T): StateTuple<T>;
|
|
44
52
|
//#endregion
|
|
45
|
-
export { State, state };
|
|
53
|
+
export { State, StateSetter, StateTuple, state };
|
|
46
54
|
//# sourceMappingURL=state.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"state.d.ts","names":[],"sources":["../../src/runtime/state.ts"],"mappings":";;;;;;;;;;UAkCiB,KAAA,YAAiB,cAAA,CAAe,CAAA;EAAA,IAC3C,CAAA;EACJ,GAAA,CAAI,KAAA,EAAO,CAAA;EACX,GAAA,CAAI,OAAA,GAAU,IAAA,EAAM,CAAA,KAAM,CAAA;EAAA,CACzB,MAAA,CAAO,QAAP,KAAoB,
|
|
1
|
+
{"version":3,"file":"state.d.ts","names":[],"sources":["../../src/runtime/state.ts"],"mappings":";;;;;;;;;;UAkCiB,KAAA,YAAiB,cAAA,CAAe,CAAA;EAAA,IAC3C,CAAA;EACJ,GAAA,CAAI,KAAA,EAAO,CAAA;EACX,GAAA,CAAI,OAAA,GAAU,IAAA,EAAM,CAAA,KAAM,CAAA;EAAA,CACzB,MAAA,CAAO,QAAP,KAAoB,gBAAA,CAAiB,UAAA,CAAW,CAAA;AAAA;;;;KAMvC,WAAA,MAAiB,KAAA,CAAM,CAAA;;;;KAKvB,UAAA,OAAiB,GAAA,EAAK,KAAA,CAAM,CAAA,GAAI,GAAA,EAAK,WAAA,CAAY,CAAA,KAAM,KAAA,CAAM,CAAA;;;;AALzE;;;;;;;;;AAKA;;;;;;;;;;;;;;iBA4BgB,KAAA,GAAA,CAAS,YAAA,EAAc,CAAA,GAAI,UAAA,CAAW,CAAA"}
|
package/dist/runtime/state.js
CHANGED
|
@@ -29,7 +29,7 @@ import { claimHookIndex, getCurrentInstance } from "./component.js";
|
|
|
29
29
|
* ```ts
|
|
30
30
|
* // ✅ Correct: called during render
|
|
31
31
|
* export function Counter() {
|
|
32
|
-
* const count = state(0);
|
|
32
|
+
* const [count, setCount] = state(0);
|
|
33
33
|
* return { type: 'button', children: [count()] };
|
|
34
34
|
* }
|
|
35
35
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"state.js","names":[],"sources":["../../src/runtime/state.ts"],"sourcesContent":["/**\n * State primitive for Askr components\n * Optimized for minimal overhead and fast updates\n *\n * INVARIANTS ENFORCED:\n * - state() only callable during component render (currentInstance exists)\n * - state() called at top-level only (indices must be monotonically increasing)\n * - state values persist across re-renders (stored in stateValues array)\n * - state.set() cannot be called during render (causes infinite loops)\n * - state.set() always enqueues through scheduler (never direct mutation)\n * - state.set() callback (notifyUpdate) always available\n */\n\nimport {\n claimHookIndex,\n getCurrentInstance,\n type ComponentInstance,\n} from './component';\nimport { isBulkCommitActive } from './fastlane';\nimport {\n recordReadableRead,\n type ReadableSource,\n markReadableDerivedSubscribersDirty,\n markReactivePropsDirtySource,\n notifyReadableReaders,\n} from './readable';\n\n/**\n * State value holder - callable to read, has set method to update\n * @example\n * const count = state(0);\n * count(); // read: 0\n * count.set(1); // write: triggers re-render\n */\nexport interface State<T> extends ReadableSource<T> {\n (): T;\n set(value: T): void;\n set(updater: (prev: T) => T): void;\n [Symbol.iterator]():
|
|
1
|
+
{"version":3,"file":"state.js","names":[],"sources":["../../src/runtime/state.ts"],"sourcesContent":["/**\n * State primitive for Askr components\n * Optimized for minimal overhead and fast updates\n *\n * INVARIANTS ENFORCED:\n * - state() only callable during component render (currentInstance exists)\n * - state() called at top-level only (indices must be monotonically increasing)\n * - state values persist across re-renders (stored in stateValues array)\n * - state.set() cannot be called during render (causes infinite loops)\n * - state.set() always enqueues through scheduler (never direct mutation)\n * - state.set() callback (notifyUpdate) always available\n */\n\nimport {\n claimHookIndex,\n getCurrentInstance,\n type ComponentInstance,\n} from './component';\nimport { isBulkCommitActive } from './fastlane';\nimport {\n recordReadableRead,\n type ReadableSource,\n markReadableDerivedSubscribersDirty,\n markReactivePropsDirtySource,\n notifyReadableReaders,\n} from './readable';\n\n/**\n * State value holder - callable to read, has set method to update\n * @example\n * const count = state(0);\n * count(); // read: 0\n * count.set(1); // write: triggers re-render\n */\nexport interface State<T> extends ReadableSource<T> {\n (): T;\n set(value: T): void;\n set(updater: (prev: T) => T): void;\n [Symbol.iterator](): IterableIterator<StateTuple<T>[number]>; // Allows destructuring: const [get, set] = state(0)\n}\n\n/**\n * Public setter type for state cells.\n */\nexport type StateSetter<T> = State<T>['set'];\n\n/**\n * Tuple-first state handle returned by `state()`.\n */\nexport type StateTuple<T> = [get: State<T>, set: StateSetter<T>] & State<T>;\n\n/**\n * Creates a local state value for a component\n * Optimized for:\n * - O(1) read performance\n * - Minimal allocation per state\n * - Fast scheduler integration\n *\n * IMPORTANT: state() must be called during component render execution.\n * It captures the current component instance from context.\n * Calling outside a component function will throw an error.\n *\n * @example\n * ```ts\n * // ✅ Correct: called during render\n * export function Counter() {\n * const [count, setCount] = state(0);\n * return { type: 'button', children: [count()] };\n * }\n *\n * // ❌ Wrong: called outside component\n * const count = state(0);\n * export function BadComponent() {\n * return { type: 'div' };\n * }\n * ```\n */\nexport function state<T>(initialValue: T): StateTuple<T> {\n // INVARIANT: state() must be called during component render\n const instance = getCurrentInstance();\n if (!instance) {\n throw new Error(\n 'state() can only be called during component render execution. ' +\n 'Move state() calls to the top level of your component function.'\n );\n }\n\n const index = claimHookIndex(instance, 'state');\n const stateValues = instance.stateValues;\n\n // INVARIANT: Reuse existing state if it exists (fast path on re-renders)\n // This ensures state identity and persistence and enforces ownership stability\n if (stateValues[index]) {\n const existing = stateValues[index] as State<T> & {\n _owner?: ComponentInstance;\n };\n // Ownership must be stable: the state cell belongs to the instance that\n // created it and must never change. This checks for accidental reuse.\n if (existing._owner !== instance) {\n throw new Error(\n `State ownership violation: state() called at index ${index} is owned by a different component instance. ` +\n `State ownership is positional and immutable.`\n );\n }\n return existing as StateTuple<T>;\n }\n\n // Create new state (slow path, only on first render) — delegated to helper\n const cell = createStateCell(initialValue, instance);\n\n // INVARIANT: Store state in instance for persistence across renders\n stateValues[index] = cell;\n\n return cell as StateTuple<T>;\n}\n\n/**\n * Internal helper: create the backing state cell (value + readers + set semantics)\n * This extraction makes it easier to later split hook wiring from storage.\n */\nfunction createStateCell<T>(\n initialValue: T,\n instance: ComponentInstance\n): State<T> {\n let value = initialValue;\n\n // Per-state reader map: component -> last-committed render token\n const readers = new Map<ComponentInstance, number>();\n\n // Use a function as the state object (callable directly)\n function read(): T {\n (read as State<T>)._hasBeenRead = true;\n recordReadableRead(read as State<T>);\n\n return value;\n }\n\n // Attach the readers map to the callable so other runtime parts can access it\n (read as State<T>)._readers = readers;\n\n // Record explicit ownership of this state cell. Ownership is the component\n // instance that created the state cell and must never change for the life\n // of the cell. We expose this for runtime invariant checks/tests.\n (read as State<T> & { _owner?: ComponentInstance })._owner = instance;\n\n // Attach set method directly to function\n read.set = (newValueOrUpdater: T | ((prev: T) => T)): void => {\n // INVARIANT: State cannot be mutated during component render\n // (when currentInstance is non-null). It must be scheduled for consistency.\n const currentInst = getCurrentInstance();\n if (currentInst !== null) {\n throw new Error(\n `[Askr] state.set() cannot be called during component render. ` +\n `State mutations during render break the actor model and cause infinite loops. ` +\n `Move state updates to event handlers or use conditional rendering instead.`\n );\n }\n\n // Compute new value if an updater was provided\n let newValue: T;\n if (typeof newValueOrUpdater === 'function') {\n // Note: function-valued state cannot be set directly via a function argument;\n // such an argument is treated as a functional updater (this follows the common\n // convention from other libraries). If you need to store a function as state,\n // wrap it in an object.\n const updater = newValueOrUpdater as (prev: T) => T;\n newValue = updater(value);\n } else {\n newValue = newValueOrUpdater as T;\n }\n\n // Skip work if value didn't change\n if (Object.is(value, newValue)) return;\n\n // If a bulk commit is active, update backing value only and DO NOT notify or enqueue.\n // Bulk commits must be side-effect silent with respect to runtime notifications.\n if (isBulkCommitActive()) {\n // In bulk commit mode we must be side-effect free: update backing\n // value only and do not notify, enqueue, or log.\n value = newValue;\n return;\n }\n\n // INVARIANT: Update the value\n value = newValue;\n\n markReadableDerivedSubscribersDirty(read as State<T>);\n markReactivePropsDirtySource(read as State<T>);\n\n // notifyUpdate may be temporarily unavailable (e.g. during hydration).\n // We intentionally avoid logging here to keep the state mutation path\n // side-effect free. The scheduler will process updates when the system\n // is stable.\n // After value change, notify only components that *read* this state in their last committed render.\n void notifyReadableReaders(read as State<T>);\n };\n\n // Allow destructuring assignment: const [get, set] = state(0);\n // The state function is iterable and yields the getter then the setter\n (\n read as unknown as {\n [Symbol.iterator]?: () => Iterator<State<T> | State<T>['set']>;\n }\n )[Symbol.iterator] = function* (): Generator<\n State<T> | State<T>['set'],\n void,\n unknown\n > {\n yield read;\n yield read.set;\n };\n\n return read as State<T>;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6EA,SAAgB,MAAS,cAAgC;CAEvD,MAAM,WAAW,oBAAoB;CACrC,IAAI,CAAC,UACH,MAAM,IAAI,MACR,gIAED;CAGH,MAAM,QAAQ,eAAe,UAAU,QAAQ;CAC/C,MAAM,cAAc,SAAS;CAI7B,IAAI,YAAY,QAAQ;EACtB,MAAM,WAAW,YAAY;EAK7B,IAAI,SAAS,WAAW,UACtB,MAAM,IAAI,MACR,sDAAsD,MAAM,2FAE7D;EAEH,OAAO;;CAIT,MAAM,OAAO,gBAAgB,cAAc,SAAS;CAGpD,YAAY,SAAS;CAErB,OAAO;;;;;;AAOT,SAAS,gBACP,cACA,UACU;CACV,IAAI,QAAQ;CAGZ,MAAM,0BAAU,IAAI,KAAgC;CAGpD,SAAS,OAAU;EACjB,KAAmB,eAAe;EAClC,mBAAmB,KAAiB;EAEpC,OAAO;;CAIT,KAAmB,WAAW;CAK9B,KAAoD,SAAS;CAG7D,KAAK,OAAO,sBAAkD;EAI5D,IADoB,oBAChB,KAAgB,MAClB,MAAM,IAAI,MACR,wNAGD;EAIH,IAAI;EACJ,IAAI,OAAO,sBAAsB,YAM/B,WAAW,kBAAQ,MAAM;OAEzB,WAAW;EAIb,IAAI,OAAO,GAAG,OAAO,SAAS,EAAE;EAIhC,IAAI,oBAAoB,EAAE;GAGxB,QAAQ;GACR;;EAIF,QAAQ;EAER,oCAAoC,KAAiB;EACrD,6BAA6B,KAAiB;EAO9C,AAAK,sBAAsB,KAAiB;;CAK9C,KAIE,OAAO,YAAY,aAInB;EACA,MAAM;EACN,MAAM,KAAK;;CAGb,OAAO"}
|
package/dist/ssg/index.d.ts
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
1
|
import { DiscoveredResources, RouteConfig, RouteRenderReason, RouteRenderResult, RouteRenderStatus, SSGGenerateOptions, SSGMetadata, SSGMode, SSGOptions, SSGResult } from "./types.js";
|
|
2
2
|
import { createStaticGen } from "./create-static-gen.js";
|
|
3
|
-
|
|
4
|
-
import { getOutputFilePath } from "./route-utils.js";
|
|
5
|
-
import { writeStaticFiles } from "./write-static-files.js";
|
|
6
|
-
import { generateSSGResult, resultToMetadata, writeMetadata } from "./generate-metadata.js";
|
|
7
|
-
import { resolveSsgData, validateRoutes } from "./resolve-ssg-data.js";
|
|
8
|
-
export { type DiscoveredResources, type RouteConfig, type RouteRenderReason, type RouteRenderResult, type RouteRenderStatus, type SSGGenerateOptions, type SSGMetadata, type SSGMode, type SSGOptions, type SSGResult, batchRenderRoutes, createStaticGen, generateSSGResult, getOutputFilePath, resolveSsgData, resultToMetadata, validateRoutes, writeMetadata, writeStaticFiles };
|
|
3
|
+
export { type DiscoveredResources, type RouteConfig, type RouteRenderReason, type RouteRenderResult, type RouteRenderStatus, type SSGGenerateOptions, type SSGMetadata, type SSGMode, type SSGOptions, type SSGResult, createStaticGen };
|
package/dist/ssg/index.js
CHANGED
|
@@ -1,7 +1,2 @@
|
|
|
1
|
-
import { resolveSsgData, validateRoutes } from "./resolve-ssg-data.js";
|
|
2
|
-
import { getOutputFilePath } from "./route-utils.js";
|
|
3
|
-
import { batchRenderRoutes } from "./batch-render.js";
|
|
4
|
-
import { writeStaticFiles } from "./write-static-files.js";
|
|
5
|
-
import { generateSSGResult, resultToMetadata, writeMetadata } from "./generate-metadata.js";
|
|
6
1
|
import { createStaticGen } from "./create-static-gen.js";
|
|
7
|
-
export {
|
|
2
|
+
export { createStaticGen };
|
package/dist/ssr/context.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Route, RouteAuthOptions } from "../common/router.js";
|
|
2
|
-
import { SSRData } from "../common/ssr.js";
|
|
3
2
|
import { SSRDataMissingError } from "../common/ssr-errors.js";
|
|
3
|
+
import { SSRData } from "../common/ssr.js";
|
|
4
|
+
|
|
4
5
|
//#region src/ssr/context.d.ts
|
|
5
6
|
interface RenderContext {
|
|
6
7
|
url: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.ts","names":[],"sources":["../../src/ssr/context.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"context.d.ts","names":[],"sources":["../../src/ssr/context.ts"],"mappings":";;;;;UAgBiB,aAAA;EACf,GAAA;EACA,IAAA;EACA,IAAA,GAAO,OAAA;EACP,MAAA,GAAS,MAAA;EACT,MAAA,YAAkB,KAAA;EAClB,SAAA,GAAY,gBAAA;EACZ,MAAA,GAAS,WAAA;EACT,UAAA,GAAa,GAAA;EAEb,UAAA;EACA,UAAA,EAAY,MAAA;AAAA;AAAA,KAIF,UAAA,GAAa,aAAA;AAAA,iBAoDT,mBAAA,CACd,IAAA,WACA,IAAA;EACE,GAAA;EACA,IAAA,GAAO,OAAA;EACP,MAAA,GAAS,MAAA;EACT,MAAA,YAAkB,KAAA;EAClB,SAAA,GAAY,gBAAA;EACZ,MAAA,GAAS,WAAA;AAAA,IAEV,aAAA;;;;;iBAqBa,iBAAA,GAAA,CAAqB,GAAA,EAAK,aAAA,EAAe,EAAA,QAAU,CAAA,GAAI,CAAA;AAnFvE;;;;AAAA,iBAsGgB,gBAAA,CAAA,GAAoB,aAAA;AAAA,cASvB,aAAA,SAAa,gBAAA;AAAA,cACb,cAAA,SAAc,iBAAA;AAAA,cACd,oBAAA,SAAoB,gBAAA;AAAA,iBAEjB,iBAAA,GAAA,CAAqB,GAAA,EAAK,aAAA,EAAe,EAAA,QAAU,CAAA,GAAI,CAAA;;;;;iBASvD,mBAAA,CAAA"}
|
package/dist/ssr/context.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { __require } from "../_virtual/_rolldown/runtime.js";
|
|
2
1
|
import { SSRDataMissingError } from "../common/ssr-errors.js";
|
|
3
2
|
import "./errors.js";
|
|
4
3
|
import { clearEscapeCache } from "./escape.js";
|
|
@@ -6,16 +5,32 @@ import { clearEscapeCache } from "./escape.js";
|
|
|
6
5
|
/**
|
|
7
6
|
* SSR Context Management
|
|
8
7
|
*
|
|
9
|
-
* Provides
|
|
10
|
-
*
|
|
11
|
-
*
|
|
8
|
+
* Provides render-context storage for server-side rendering.
|
|
9
|
+
* Node SSR lazily installs AsyncLocalStorage on first use; browser builds use
|
|
10
|
+
* the fallback stack.
|
|
12
11
|
*/
|
|
13
|
-
let
|
|
14
|
-
|
|
15
|
-
const asyncHooks = __require("async_hooks");
|
|
16
|
-
if (asyncHooks?.AsyncLocalStorage) asyncLocalStorage = new asyncHooks.AsyncLocalStorage();
|
|
17
|
-
} catch {}
|
|
12
|
+
let renderContextAccessor = null;
|
|
13
|
+
let renderContextAccessorInitialized = false;
|
|
18
14
|
let fallbackStack = null;
|
|
15
|
+
function ensureRenderContextAccessor() {
|
|
16
|
+
if (renderContextAccessorInitialized) return;
|
|
17
|
+
renderContextAccessorInitialized = true;
|
|
18
|
+
if (typeof process === "undefined" || !process.versions?.node) return;
|
|
19
|
+
try {
|
|
20
|
+
const asyncHooks = new Function("return require(" + JSON.stringify("async_hooks") + ");")();
|
|
21
|
+
if (asyncHooks.AsyncLocalStorage) {
|
|
22
|
+
const asyncLocalStorage = new asyncHooks.AsyncLocalStorage();
|
|
23
|
+
renderContextAccessor = {
|
|
24
|
+
getStore() {
|
|
25
|
+
return asyncLocalStorage.getStore();
|
|
26
|
+
},
|
|
27
|
+
run(store, fn) {
|
|
28
|
+
return asyncLocalStorage.run(store, fn);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
} catch {}
|
|
33
|
+
}
|
|
19
34
|
function createRenderContext(seed = 12345, opts = {}) {
|
|
20
35
|
clearEscapeCache();
|
|
21
36
|
return {
|
|
@@ -36,7 +51,8 @@ function createRenderContext(seed = 12345, opts = {}) {
|
|
|
36
51
|
* Concurrency-safe in Node.js via AsyncLocalStorage.
|
|
37
52
|
*/
|
|
38
53
|
function withRenderContext(ctx, fn) {
|
|
39
|
-
|
|
54
|
+
ensureRenderContextAccessor();
|
|
55
|
+
if (renderContextAccessor) return renderContextAccessor.run(ctx, fn);
|
|
40
56
|
const prev = fallbackStack;
|
|
41
57
|
fallbackStack = ctx;
|
|
42
58
|
try {
|
|
@@ -50,7 +66,8 @@ function withRenderContext(ctx, fn) {
|
|
|
50
66
|
* Returns null if not inside a render.
|
|
51
67
|
*/
|
|
52
68
|
function getRenderContext() {
|
|
53
|
-
|
|
69
|
+
ensureRenderContextAccessor();
|
|
70
|
+
if (renderContextAccessor) return renderContextAccessor.getStore() ?? null;
|
|
54
71
|
return fallbackStack;
|
|
55
72
|
}
|
|
56
73
|
/**
|