@fluixi/reactive 1.0.0-alpha.91 → 1.0.0-alpha.93
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/dist/cdn/reactive.cjs +105 -35
- package/dist/cdn/reactive.global.js +2 -2
- package/dist/cdn/reactive.mjs +105 -35
- package/dist/cdn/signal.cjs +1 -1
- package/dist/cdn/signal.global.js +2 -2
- package/dist/cdn/signal.mjs +1 -1
- package/dist/index.cjs +105 -35
- package/dist/index.mjs +105 -35
- package/dist/lib/index.cjs +106 -36
- package/dist/lib/index.mjs +106 -36
- package/dist/lib/observable/index.cjs +1 -1
- package/dist/lib/observable/index.mjs +1 -1
- package/dist/lib/signal/api.cjs +11 -10
- package/dist/lib/signal/api.d.ts +5 -1
- package/dist/lib/signal/api.d.ts.map +1 -1
- package/dist/lib/signal/api.js +4 -2
- package/dist/lib/signal/api.mjs +11 -10
- package/dist/lib/signal/cached-resource.cjs +77 -34
- package/dist/lib/signal/cached-resource.d.ts +48 -68
- package/dist/lib/signal/cached-resource.d.ts.map +1 -1
- package/dist/lib/signal/cached-resource.js +59 -72
- package/dist/lib/signal/cached-resource.mjs +77 -34
- package/dist/lib/signal/graph/children.cjs +3 -2
- package/dist/lib/signal/graph/children.mjs +3 -2
- package/dist/lib/signal/graph/compat.cjs +10 -9
- package/dist/lib/signal/graph/compat.d.ts +3 -1
- package/dist/lib/signal/graph/compat.d.ts.map +1 -1
- package/dist/lib/signal/graph/compat.js +2 -2
- package/dist/lib/signal/graph/compat.mjs +10 -9
- package/dist/lib/signal/graph/core.cjs +2 -2
- package/dist/lib/signal/graph/core.mjs +2 -2
- package/dist/lib/signal/graph/debounced.cjs +9 -8
- package/dist/lib/signal/graph/debounced.mjs +9 -8
- package/dist/lib/signal/graph/derived.cjs +9 -8
- package/dist/lib/signal/graph/derived.mjs +9 -8
- package/dist/lib/signal/graph/error-boundary.cjs +3 -2
- package/dist/lib/signal/graph/error-boundary.mjs +3 -2
- package/dist/lib/signal/graph/index.cjs +2 -2
- package/dist/lib/signal/graph/index.mjs +2 -2
- package/dist/lib/signal/graph/observe.cjs +38 -3
- package/dist/lib/signal/graph/observe.d.ts +44 -0
- package/dist/lib/signal/graph/observe.d.ts.map +1 -1
- package/dist/lib/signal/graph/observe.js +50 -1
- package/dist/lib/signal/graph/observe.mjs +38 -3
- package/dist/lib/signal/graph/resource.cjs +9 -8
- package/dist/lib/signal/graph/resource.mjs +9 -8
- package/dist/lib/signal/graph/runtime.cjs +7 -6
- package/dist/lib/signal/graph/runtime.d.ts +1 -1
- package/dist/lib/signal/graph/runtime.d.ts.map +1 -1
- package/dist/lib/signal/graph/runtime.js +4 -4
- package/dist/lib/signal/graph/runtime.mjs +7 -6
- package/dist/lib/signal/graph/signal-next.cjs +39 -9
- package/dist/lib/signal/graph/signal-next.d.ts +1 -1
- package/dist/lib/signal/graph/signal-next.d.ts.map +1 -1
- package/dist/lib/signal/graph/signal-next.js +1 -1
- package/dist/lib/signal/graph/signal-next.mjs +39 -9
- package/dist/lib/signal/graph/ssr-hooks.cjs +29 -0
- package/dist/lib/signal/graph/ssr-hooks.d.ts +24 -0
- package/dist/lib/signal/graph/ssr-hooks.d.ts.map +1 -1
- package/dist/lib/signal/graph/ssr-hooks.js +36 -0
- package/dist/lib/signal/graph/ssr-hooks.mjs +29 -0
- package/dist/lib/signal/graph/state.cjs +1 -1
- package/dist/lib/signal/graph/state.mjs +1 -1
- package/dist/lib/signal/graph/suspense.cjs +3 -2
- package/dist/lib/signal/graph/suspense.mjs +3 -2
- package/dist/lib/signal/index-legacy.cjs +65 -34
- package/dist/lib/signal/index-legacy.mjs +65 -34
- package/dist/lib/signal/index.cjs +76 -12
- package/dist/lib/signal/index.d.ts +2 -1
- package/dist/lib/signal/index.d.ts.map +1 -1
- package/dist/lib/signal/index.js +1 -1
- package/dist/lib/signal/index.mjs +76 -12
- package/dist/lib/signal/list.cjs +3 -2
- package/dist/lib/signal/list.mjs +3 -2
- package/dist/lib/signal/resource-api.cjs +9 -8
- package/dist/lib/signal/resource-api.mjs +9 -8
- package/dist/lib/signal/signal.cjs +9 -8
- package/dist/lib/signal/signal.mjs +9 -8
- package/dist/lib/signal/utilities.cjs +10 -9
- package/dist/lib/signal/utilities.mjs +10 -9
- package/dist/lib/store/api.cjs +3 -2
- package/dist/lib/store/api.mjs +3 -2
- package/dist/lib/store/index.cjs +4 -3
- package/dist/lib/store/index.mjs +4 -3
- package/dist/lib/store/store.cjs +3 -2
- package/dist/lib/store/store.mjs +3 -2
- package/dist/tsconfig.lib.tsbuildinfo +1 -1
- package/dist/version.generated.cjs +2 -2
- package/dist/version.generated.d.ts +1 -1
- package/dist/version.generated.js +1 -1
- package/dist/version.generated.mjs +2 -2
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -39,15 +39,16 @@ __export(index_exports, {
|
|
|
39
39
|
SuspenseContext: () => SuspenseContext,
|
|
40
40
|
VERSION: () => VERSION,
|
|
41
41
|
__DEV__: () => __DEV__,
|
|
42
|
+
adoptReactiveScope: () => adoptReactiveScope,
|
|
42
43
|
batch: () => batch2,
|
|
44
|
+
cachedResource: () => cachedResource,
|
|
43
45
|
childOwner: () => childOwner,
|
|
44
|
-
|
|
45
|
-
|
|
46
|
+
clearCachedResources: () => clearCachedResources,
|
|
47
|
+
closeReactiveScope: () => closeReactiveScope,
|
|
46
48
|
combineSignal: () => combineSignal,
|
|
47
49
|
combineSignals: () => combineSignals,
|
|
48
50
|
computed: () => computed,
|
|
49
51
|
context: () => context,
|
|
50
|
-
createCachedResource: () => createCachedResource,
|
|
51
52
|
createChildOwner: () => createChildOwner,
|
|
52
53
|
createComputed: () => createComputed,
|
|
53
54
|
createComputedStore: () => createComputedStore,
|
|
@@ -62,6 +63,7 @@ __export(index_exports, {
|
|
|
62
63
|
createMutableStore: () => createMutableStore,
|
|
63
64
|
createRenderEffect: () => createRenderEffect,
|
|
64
65
|
createResource: () => createResource,
|
|
66
|
+
createResourceCache: () => createResourceCache,
|
|
65
67
|
createRoot: () => createRoot,
|
|
66
68
|
createSelector: () => createSelector,
|
|
67
69
|
createSignal: () => createSignal,
|
|
@@ -69,6 +71,7 @@ __export(index_exports, {
|
|
|
69
71
|
createStoreEffect: () => createStoreEffect,
|
|
70
72
|
createStoreMemo: () => createStoreMemo,
|
|
71
73
|
createSuspenseBoundary: () => createSuspenseBoundary,
|
|
74
|
+
currentResourceCache: () => currentResourceCache,
|
|
72
75
|
debounceSignal: () => debounceSignal,
|
|
73
76
|
disposeScope: () => disposeScope,
|
|
74
77
|
distinctSignal: () => distinctSignal,
|
|
@@ -97,6 +100,7 @@ __export(index_exports, {
|
|
|
97
100
|
makeArrayFlat: () => makeArrayFlat,
|
|
98
101
|
mapArray: () => mapArray,
|
|
99
102
|
mapSignal: () => mapSignal,
|
|
103
|
+
markReactiveScope: () => markReactiveScope,
|
|
100
104
|
memo: () => memo,
|
|
101
105
|
mergeSignal: () => mergeSignal,
|
|
102
106
|
modifiable: () => modifiable,
|
|
@@ -105,12 +109,14 @@ __export(index_exports, {
|
|
|
105
109
|
omitSignal: () => omitSignal,
|
|
106
110
|
on: () => on,
|
|
107
111
|
onCleanup: () => onCleanup,
|
|
112
|
+
openReactiveScope: () => openReactiveScope,
|
|
108
113
|
operate: () => operate,
|
|
109
114
|
pickSignal: () => pickSignal,
|
|
110
115
|
produceUtil: () => produce,
|
|
111
116
|
provide: () => provide,
|
|
112
117
|
provideErrorBoundary: () => provideErrorBoundary,
|
|
113
118
|
provideSuspense: () => provideSuspense,
|
|
119
|
+
reactiveScopeOf: () => reactiveScopeOf,
|
|
114
120
|
readChildren: () => readChildren,
|
|
115
121
|
readSignal: () => readSignal,
|
|
116
122
|
reconcileUtil: () => reconcile,
|
|
@@ -127,6 +133,7 @@ __export(index_exports, {
|
|
|
127
133
|
runWithOwner: () => runWithOwner,
|
|
128
134
|
selectSignal: () => selectSignal,
|
|
129
135
|
setOwner: () => setOwner,
|
|
136
|
+
setResourceCacheScope: () => setResourceCacheScope,
|
|
130
137
|
setResourceDataSink: () => setResourceDataSink,
|
|
131
138
|
setResourceIdSource: () => setResourceIdSource,
|
|
132
139
|
setResourceTracker: () => setResourceTracker,
|
|
@@ -144,13 +151,14 @@ __export(index_exports, {
|
|
|
144
151
|
useContext: () => useContext,
|
|
145
152
|
useTransition: () => useTransition,
|
|
146
153
|
whenSignal: () => whenSignal,
|
|
154
|
+
withReactiveScope: () => withReactiveScope,
|
|
147
155
|
wrap: () => wrap,
|
|
148
156
|
zipSignal: () => zipSignal
|
|
149
157
|
});
|
|
150
158
|
module.exports = __toCommonJS(index_exports);
|
|
151
159
|
|
|
152
160
|
// src/version.generated.ts
|
|
153
|
-
var VERSION = "1.0.0-alpha.
|
|
161
|
+
var VERSION = "1.0.0-alpha.93";
|
|
154
162
|
|
|
155
163
|
// src/lib/predicates.ts
|
|
156
164
|
function isFunction(value) {
|
|
@@ -641,11 +649,40 @@ function withReactiveOrigin(owner, fn) {
|
|
|
641
649
|
origin = previous;
|
|
642
650
|
}
|
|
643
651
|
}
|
|
652
|
+
var currentScope;
|
|
653
|
+
function openReactiveScope(info) {
|
|
654
|
+
const scope = {};
|
|
655
|
+
G.observer?.scope?.(scope, info, currentScope);
|
|
656
|
+
return scope;
|
|
657
|
+
}
|
|
658
|
+
function withReactiveScope(scope, fn) {
|
|
659
|
+
if (!G.observer) return fn();
|
|
660
|
+
const previous = currentScope;
|
|
661
|
+
currentScope = scope;
|
|
662
|
+
try {
|
|
663
|
+
return fn();
|
|
664
|
+
} finally {
|
|
665
|
+
currentScope = previous;
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
function closeReactiveScope(scope) {
|
|
669
|
+
G.observer?.scopeEnded?.(scope);
|
|
670
|
+
}
|
|
671
|
+
function adoptReactiveScope(scope, parent) {
|
|
672
|
+
G.observer?.scopeAdopted?.(scope, parent);
|
|
673
|
+
}
|
|
674
|
+
var SCOPE_OF = /* @__PURE__ */ Symbol.for("fluixi.reactiveScope");
|
|
675
|
+
function markReactiveScope(value, scope) {
|
|
676
|
+
Object.defineProperty(value, SCOPE_OF, { value: scope, configurable: true, enumerable: false });
|
|
677
|
+
}
|
|
678
|
+
function reactiveScopeOf(value) {
|
|
679
|
+
return value !== null && (typeof value === "object" || typeof value === "function") ? value[SCOPE_OF] ?? void 0 : void 0;
|
|
680
|
+
}
|
|
644
681
|
function reportReactiveNode(report) {
|
|
645
682
|
const observer = G.observer;
|
|
646
683
|
if (observer) {
|
|
647
684
|
observer.created(
|
|
648
|
-
report.parents === void 0 ? { ...report, parents: running, origin: report.origin ?? origin } : report
|
|
685
|
+
report.parents === void 0 ? { ...report, parents: running, origin: report.origin ?? origin, scope: report.scope ?? currentScope } : { ...report, scope: report.scope ?? currentScope }
|
|
649
686
|
);
|
|
650
687
|
}
|
|
651
688
|
}
|
|
@@ -1065,7 +1102,7 @@ function batch(fn) {
|
|
|
1065
1102
|
}
|
|
1066
1103
|
}
|
|
1067
1104
|
var EffectNode = class {
|
|
1068
|
-
constructor(fn, report = false) {
|
|
1105
|
+
constructor(fn, report = false, name) {
|
|
1069
1106
|
this.queued = false;
|
|
1070
1107
|
this.disposed = false;
|
|
1071
1108
|
this.owner = makeOwner(G.owner);
|
|
@@ -1077,7 +1114,7 @@ var EffectNode = class {
|
|
|
1077
1114
|
if (typeof r === "function") (this.owner.cleanups ??= []).push(r);
|
|
1078
1115
|
});
|
|
1079
1116
|
});
|
|
1080
|
-
reportReactiveNode({ kind: "effect", node: this.computed, handle: this, internal: !report });
|
|
1117
|
+
reportReactiveNode({ kind: "effect", node: this.computed, handle: this, internal: !report, ...name ? { name } : {} });
|
|
1081
1118
|
this.watcher = new Watcher(() => enqueue(this));
|
|
1082
1119
|
this.run();
|
|
1083
1120
|
this.watcher.watch(this.computed);
|
|
@@ -1104,8 +1141,8 @@ var EffectNode = class {
|
|
|
1104
1141
|
disposeOwner(this.owner);
|
|
1105
1142
|
}
|
|
1106
1143
|
};
|
|
1107
|
-
function effect(fn, report = false) {
|
|
1108
|
-
const node = new EffectNode(fn, report);
|
|
1144
|
+
function effect(fn, report = false, name) {
|
|
1145
|
+
const node = new EffectNode(fn, report, name);
|
|
1109
1146
|
return () => node.dispose();
|
|
1110
1147
|
}
|
|
1111
1148
|
|
|
@@ -1173,12 +1210,12 @@ function createMemo(fn, value, options) {
|
|
|
1173
1210
|
if (isObserved()) onCleanup(() => reportReactiveDispose(c));
|
|
1174
1211
|
return read;
|
|
1175
1212
|
}
|
|
1176
|
-
function createEffect(fn, value) {
|
|
1213
|
+
function createEffect(fn, value, options) {
|
|
1177
1214
|
let prev = value;
|
|
1178
1215
|
return effect(() => {
|
|
1179
1216
|
const r = fn(prev);
|
|
1180
1217
|
prev = r;
|
|
1181
|
-
}, true);
|
|
1218
|
+
}, true, options?.name);
|
|
1182
1219
|
}
|
|
1183
1220
|
function createComputed(fn, value) {
|
|
1184
1221
|
let prev = value;
|
|
@@ -1424,6 +1461,32 @@ function setResourceDataSink(fn) {
|
|
|
1424
1461
|
function reportResourceData(id, value) {
|
|
1425
1462
|
if (_resourceDataSink) _resourceDataSink(id, value);
|
|
1426
1463
|
}
|
|
1464
|
+
function createResourceCache() {
|
|
1465
|
+
const entries = /* @__PURE__ */ new Map();
|
|
1466
|
+
const inflight = /* @__PURE__ */ new Map();
|
|
1467
|
+
return {
|
|
1468
|
+
get: (key) => entries.get(key),
|
|
1469
|
+
set: (key, entry) => void entries.set(key, entry),
|
|
1470
|
+
delete: (key) => void entries.delete(key),
|
|
1471
|
+
keys: () => entries.keys(),
|
|
1472
|
+
clear: () => {
|
|
1473
|
+
entries.clear();
|
|
1474
|
+
inflight.clear();
|
|
1475
|
+
},
|
|
1476
|
+
inflight
|
|
1477
|
+
};
|
|
1478
|
+
}
|
|
1479
|
+
var _cacheScope = null;
|
|
1480
|
+
function setResourceCacheScope(fn) {
|
|
1481
|
+
_cacheScope = fn;
|
|
1482
|
+
}
|
|
1483
|
+
var _pageCache;
|
|
1484
|
+
function currentResourceCache() {
|
|
1485
|
+
const scoped = _cacheScope?.();
|
|
1486
|
+
if (scoped) return scoped;
|
|
1487
|
+
if (typeof window === "undefined") return createResourceCache();
|
|
1488
|
+
return _pageCache ??= createResourceCache();
|
|
1489
|
+
}
|
|
1427
1490
|
|
|
1428
1491
|
// src/lib/signal/graph/derived.ts
|
|
1429
1492
|
function createSelector(source, fn = (k, v) => k === v) {
|
|
@@ -1743,8 +1806,8 @@ function signal(value, options) {
|
|
|
1743
1806
|
function memo(fn, value, options) {
|
|
1744
1807
|
return createMemo(fn, value, options);
|
|
1745
1808
|
}
|
|
1746
|
-
function effect2(fn, value) {
|
|
1747
|
-
return createEffect(fn, value);
|
|
1809
|
+
function effect2(fn, value, options) {
|
|
1810
|
+
return createEffect(fn, value, options);
|
|
1748
1811
|
}
|
|
1749
1812
|
function root(fn) {
|
|
1750
1813
|
return createRoot(fn);
|
|
@@ -2527,12 +2590,10 @@ function readSignal(signal2) {
|
|
|
2527
2590
|
}
|
|
2528
2591
|
|
|
2529
2592
|
// src/lib/signal/cached-resource.ts
|
|
2530
|
-
var ttlCache = /* @__PURE__ */ new Map();
|
|
2531
|
-
var inflight = /* @__PURE__ */ new Map();
|
|
2532
2593
|
var _autoId = 0;
|
|
2533
2594
|
var $SIGNAL4 = /* @__PURE__ */ Symbol.for("signal");
|
|
2534
2595
|
var $RESOURCE2 = /* @__PURE__ */ Symbol.for("resource");
|
|
2535
|
-
function
|
|
2596
|
+
function cachedResource(sourceOrFetcher, fetcherOrOptions, opts = {}) {
|
|
2536
2597
|
let sourceFn;
|
|
2537
2598
|
let fetchFn;
|
|
2538
2599
|
let options;
|
|
@@ -2551,7 +2612,13 @@ function createCachedResource(sourceOrFetcher, fetcherOrOptions, opts = {}) {
|
|
|
2551
2612
|
staleWhileRevalidate = true
|
|
2552
2613
|
} = options;
|
|
2553
2614
|
const ns = name;
|
|
2554
|
-
const
|
|
2615
|
+
const cache = options.cache ?? currentResourceCache();
|
|
2616
|
+
const inflight = cache.inflight;
|
|
2617
|
+
const ssrId = options.transport === false ? void 0 : nextResourceId();
|
|
2618
|
+
const seed = ssrId !== void 0 ? getServerData(ssrId) : void 0;
|
|
2619
|
+
const deps = options.deps ?? [];
|
|
2620
|
+
const depValues = () => deps.map((dep) => dep());
|
|
2621
|
+
const buildKey = (key, values = untrack2(depValues)) => `${ns}:${JSON.stringify(key)}` + (deps.length ? `:${JSON.stringify(values)}` : "");
|
|
2555
2622
|
const isFresh = (entry) => ttl === Infinity || Date.now() - entry.timestamp <= ttl;
|
|
2556
2623
|
const [value, setValue] = createSignal(void 0, { equals: false });
|
|
2557
2624
|
const [resourceState, setResourceState] = createSignal("unresolved", { equals: false });
|
|
@@ -2564,8 +2631,9 @@ function createCachedResource(sourceOrFetcher, fetcherOrOptions, opts = {}) {
|
|
|
2564
2631
|
let flagRefetch = false;
|
|
2565
2632
|
let flagInvalidate = false;
|
|
2566
2633
|
function doFetch(key, ck, background, isCancelled) {
|
|
2567
|
-
|
|
2568
|
-
|
|
2634
|
+
const running2 = inflight.get(ck);
|
|
2635
|
+
if (running2) {
|
|
2636
|
+
running2.then(
|
|
2569
2637
|
(v) => {
|
|
2570
2638
|
if (!isCancelled()) settle(v, background);
|
|
2571
2639
|
},
|
|
@@ -2573,12 +2641,15 @@ function createCachedResource(sourceOrFetcher, fetcherOrOptions, opts = {}) {
|
|
|
2573
2641
|
if (!isCancelled() && !background) fail(e);
|
|
2574
2642
|
}
|
|
2575
2643
|
);
|
|
2576
|
-
if (!background)
|
|
2644
|
+
if (!background) {
|
|
2645
|
+
trackResourcePromise(running2);
|
|
2646
|
+
setPendingP(running2);
|
|
2647
|
+
}
|
|
2577
2648
|
return;
|
|
2578
2649
|
}
|
|
2579
2650
|
const promise = Promise.resolve(fetchFn(key)).then(
|
|
2580
2651
|
(v) => {
|
|
2581
|
-
|
|
2652
|
+
cache.set(ck, { value: v, timestamp: Date.now() });
|
|
2582
2653
|
inflight.delete(ck);
|
|
2583
2654
|
return v;
|
|
2584
2655
|
},
|
|
@@ -2601,7 +2672,8 @@ function createCachedResource(sourceOrFetcher, fetcherOrOptions, opts = {}) {
|
|
|
2601
2672
|
}
|
|
2602
2673
|
);
|
|
2603
2674
|
}
|
|
2604
|
-
function settle(v,
|
|
2675
|
+
function settle(v, _background) {
|
|
2676
|
+
if (ssrId !== void 0) reportResourceData(ssrId, v);
|
|
2605
2677
|
batch2(() => {
|
|
2606
2678
|
setValue(() => v);
|
|
2607
2679
|
setResourceState("ready");
|
|
@@ -2619,6 +2691,7 @@ function createCachedResource(sourceOrFetcher, fetcherOrOptions, opts = {}) {
|
|
|
2619
2691
|
}
|
|
2620
2692
|
createEffect(() => {
|
|
2621
2693
|
const key = sourceFn();
|
|
2694
|
+
const values = depValues();
|
|
2622
2695
|
tick();
|
|
2623
2696
|
if (key === false || key === null || key === void 0) {
|
|
2624
2697
|
prevKey = UNSET2;
|
|
@@ -2633,16 +2706,18 @@ function createCachedResource(sourceOrFetcher, fetcherOrOptions, opts = {}) {
|
|
|
2633
2706
|
const doInvalidate = flagInvalidate;
|
|
2634
2707
|
flagRefetch = false;
|
|
2635
2708
|
flagInvalidate = false;
|
|
2636
|
-
const ck = buildKey(key);
|
|
2637
|
-
if (
|
|
2709
|
+
const ck = buildKey(key, values);
|
|
2710
|
+
if (seed && prevKey === UNSET2 && !cache.get(ck)) cache.set(ck, { value: seed.value, timestamp: Date.now() });
|
|
2711
|
+
if (doInvalidate) cache.delete(ck);
|
|
2638
2712
|
const keyChanged = prevKey === UNSET2 || key !== prevKey;
|
|
2639
2713
|
prevKey = key;
|
|
2640
2714
|
let cancelled = false;
|
|
2641
2715
|
onCleanup(() => {
|
|
2642
2716
|
cancelled = true;
|
|
2643
2717
|
});
|
|
2644
|
-
const cached =
|
|
2718
|
+
const cached = cache.get(ck);
|
|
2645
2719
|
if (cached && isFresh(cached) && !doRefetch && !doInvalidate) {
|
|
2720
|
+
if (ssrId !== void 0) reportResourceData(ssrId, cached.value);
|
|
2646
2721
|
const cur2 = untrack2(resourceState);
|
|
2647
2722
|
if (cur2 !== "ready" || untrack2(value) !== cached.value) {
|
|
2648
2723
|
batch2(() => {
|
|
@@ -2703,20 +2778,15 @@ function createCachedResource(sourceOrFetcher, fetcherOrOptions, opts = {}) {
|
|
|
2703
2778
|
const invalidate = () => {
|
|
2704
2779
|
const key = untrack2(sourceFn);
|
|
2705
2780
|
if (key !== null && key !== void 0 && key !== false) {
|
|
2706
|
-
|
|
2781
|
+
cache.delete(buildKey(key));
|
|
2707
2782
|
}
|
|
2708
2783
|
flagInvalidate = true;
|
|
2709
2784
|
setTick((v) => v + 1);
|
|
2710
2785
|
};
|
|
2711
|
-
return
|
|
2786
|
+
return Object.assign(resource2, { mutate: setValue, refetch, invalidate });
|
|
2712
2787
|
}
|
|
2713
|
-
function
|
|
2788
|
+
function clearCachedResources(name, cache = currentResourceCache()) {
|
|
2789
|
+
if (name === void 0) return cache.clear();
|
|
2714
2790
|
const prefix = `${name}:`;
|
|
2715
|
-
for (const key of
|
|
2716
|
-
if (key.startsWith(prefix)) ttlCache.delete(key);
|
|
2717
|
-
}
|
|
2718
|
-
}
|
|
2719
|
-
function clearAllCachedResources() {
|
|
2720
|
-
ttlCache.clear();
|
|
2721
|
-
inflight.clear();
|
|
2791
|
+
for (const key of [...cache.keys()]) if (key.startsWith(prefix)) cache.delete(key);
|
|
2722
2792
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/version.generated.ts
|
|
2
|
-
var VERSION = "1.0.0-alpha.
|
|
2
|
+
var VERSION = "1.0.0-alpha.93";
|
|
3
3
|
|
|
4
4
|
// src/lib/predicates.ts
|
|
5
5
|
function isFunction(value) {
|
|
@@ -490,11 +490,40 @@ function withReactiveOrigin(owner, fn) {
|
|
|
490
490
|
origin = previous;
|
|
491
491
|
}
|
|
492
492
|
}
|
|
493
|
+
var currentScope;
|
|
494
|
+
function openReactiveScope(info) {
|
|
495
|
+
const scope = {};
|
|
496
|
+
G.observer?.scope?.(scope, info, currentScope);
|
|
497
|
+
return scope;
|
|
498
|
+
}
|
|
499
|
+
function withReactiveScope(scope, fn) {
|
|
500
|
+
if (!G.observer) return fn();
|
|
501
|
+
const previous = currentScope;
|
|
502
|
+
currentScope = scope;
|
|
503
|
+
try {
|
|
504
|
+
return fn();
|
|
505
|
+
} finally {
|
|
506
|
+
currentScope = previous;
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
function closeReactiveScope(scope) {
|
|
510
|
+
G.observer?.scopeEnded?.(scope);
|
|
511
|
+
}
|
|
512
|
+
function adoptReactiveScope(scope, parent) {
|
|
513
|
+
G.observer?.scopeAdopted?.(scope, parent);
|
|
514
|
+
}
|
|
515
|
+
var SCOPE_OF = /* @__PURE__ */ Symbol.for("fluixi.reactiveScope");
|
|
516
|
+
function markReactiveScope(value, scope) {
|
|
517
|
+
Object.defineProperty(value, SCOPE_OF, { value: scope, configurable: true, enumerable: false });
|
|
518
|
+
}
|
|
519
|
+
function reactiveScopeOf(value) {
|
|
520
|
+
return value !== null && (typeof value === "object" || typeof value === "function") ? value[SCOPE_OF] ?? void 0 : void 0;
|
|
521
|
+
}
|
|
493
522
|
function reportReactiveNode(report) {
|
|
494
523
|
const observer = G.observer;
|
|
495
524
|
if (observer) {
|
|
496
525
|
observer.created(
|
|
497
|
-
report.parents === void 0 ? { ...report, parents: running, origin: report.origin ?? origin } : report
|
|
526
|
+
report.parents === void 0 ? { ...report, parents: running, origin: report.origin ?? origin, scope: report.scope ?? currentScope } : { ...report, scope: report.scope ?? currentScope }
|
|
498
527
|
);
|
|
499
528
|
}
|
|
500
529
|
}
|
|
@@ -914,7 +943,7 @@ function batch(fn) {
|
|
|
914
943
|
}
|
|
915
944
|
}
|
|
916
945
|
var EffectNode = class {
|
|
917
|
-
constructor(fn, report = false) {
|
|
946
|
+
constructor(fn, report = false, name) {
|
|
918
947
|
this.queued = false;
|
|
919
948
|
this.disposed = false;
|
|
920
949
|
this.owner = makeOwner(G.owner);
|
|
@@ -926,7 +955,7 @@ var EffectNode = class {
|
|
|
926
955
|
if (typeof r === "function") (this.owner.cleanups ??= []).push(r);
|
|
927
956
|
});
|
|
928
957
|
});
|
|
929
|
-
reportReactiveNode({ kind: "effect", node: this.computed, handle: this, internal: !report });
|
|
958
|
+
reportReactiveNode({ kind: "effect", node: this.computed, handle: this, internal: !report, ...name ? { name } : {} });
|
|
930
959
|
this.watcher = new Watcher(() => enqueue(this));
|
|
931
960
|
this.run();
|
|
932
961
|
this.watcher.watch(this.computed);
|
|
@@ -953,8 +982,8 @@ var EffectNode = class {
|
|
|
953
982
|
disposeOwner(this.owner);
|
|
954
983
|
}
|
|
955
984
|
};
|
|
956
|
-
function effect(fn, report = false) {
|
|
957
|
-
const node = new EffectNode(fn, report);
|
|
985
|
+
function effect(fn, report = false, name) {
|
|
986
|
+
const node = new EffectNode(fn, report, name);
|
|
958
987
|
return () => node.dispose();
|
|
959
988
|
}
|
|
960
989
|
|
|
@@ -1022,12 +1051,12 @@ function createMemo(fn, value, options) {
|
|
|
1022
1051
|
if (isObserved()) onCleanup(() => reportReactiveDispose(c));
|
|
1023
1052
|
return read;
|
|
1024
1053
|
}
|
|
1025
|
-
function createEffect(fn, value) {
|
|
1054
|
+
function createEffect(fn, value, options) {
|
|
1026
1055
|
let prev = value;
|
|
1027
1056
|
return effect(() => {
|
|
1028
1057
|
const r = fn(prev);
|
|
1029
1058
|
prev = r;
|
|
1030
|
-
}, true);
|
|
1059
|
+
}, true, options?.name);
|
|
1031
1060
|
}
|
|
1032
1061
|
function createComputed(fn, value) {
|
|
1033
1062
|
let prev = value;
|
|
@@ -1273,6 +1302,32 @@ function setResourceDataSink(fn) {
|
|
|
1273
1302
|
function reportResourceData(id, value) {
|
|
1274
1303
|
if (_resourceDataSink) _resourceDataSink(id, value);
|
|
1275
1304
|
}
|
|
1305
|
+
function createResourceCache() {
|
|
1306
|
+
const entries = /* @__PURE__ */ new Map();
|
|
1307
|
+
const inflight = /* @__PURE__ */ new Map();
|
|
1308
|
+
return {
|
|
1309
|
+
get: (key) => entries.get(key),
|
|
1310
|
+
set: (key, entry) => void entries.set(key, entry),
|
|
1311
|
+
delete: (key) => void entries.delete(key),
|
|
1312
|
+
keys: () => entries.keys(),
|
|
1313
|
+
clear: () => {
|
|
1314
|
+
entries.clear();
|
|
1315
|
+
inflight.clear();
|
|
1316
|
+
},
|
|
1317
|
+
inflight
|
|
1318
|
+
};
|
|
1319
|
+
}
|
|
1320
|
+
var _cacheScope = null;
|
|
1321
|
+
function setResourceCacheScope(fn) {
|
|
1322
|
+
_cacheScope = fn;
|
|
1323
|
+
}
|
|
1324
|
+
var _pageCache;
|
|
1325
|
+
function currentResourceCache() {
|
|
1326
|
+
const scoped = _cacheScope?.();
|
|
1327
|
+
if (scoped) return scoped;
|
|
1328
|
+
if (typeof window === "undefined") return createResourceCache();
|
|
1329
|
+
return _pageCache ??= createResourceCache();
|
|
1330
|
+
}
|
|
1276
1331
|
|
|
1277
1332
|
// src/lib/signal/graph/derived.ts
|
|
1278
1333
|
function createSelector(source, fn = (k, v) => k === v) {
|
|
@@ -1592,8 +1647,8 @@ function signal(value, options) {
|
|
|
1592
1647
|
function memo(fn, value, options) {
|
|
1593
1648
|
return createMemo(fn, value, options);
|
|
1594
1649
|
}
|
|
1595
|
-
function effect2(fn, value) {
|
|
1596
|
-
return createEffect(fn, value);
|
|
1650
|
+
function effect2(fn, value, options) {
|
|
1651
|
+
return createEffect(fn, value, options);
|
|
1597
1652
|
}
|
|
1598
1653
|
function root(fn) {
|
|
1599
1654
|
return createRoot(fn);
|
|
@@ -2383,12 +2438,10 @@ function readSignal(signal2) {
|
|
|
2383
2438
|
}
|
|
2384
2439
|
|
|
2385
2440
|
// src/lib/signal/cached-resource.ts
|
|
2386
|
-
var ttlCache = /* @__PURE__ */ new Map();
|
|
2387
|
-
var inflight = /* @__PURE__ */ new Map();
|
|
2388
2441
|
var _autoId = 0;
|
|
2389
2442
|
var $SIGNAL4 = /* @__PURE__ */ Symbol.for("signal");
|
|
2390
2443
|
var $RESOURCE2 = /* @__PURE__ */ Symbol.for("resource");
|
|
2391
|
-
function
|
|
2444
|
+
function cachedResource(sourceOrFetcher, fetcherOrOptions, opts = {}) {
|
|
2392
2445
|
let sourceFn;
|
|
2393
2446
|
let fetchFn;
|
|
2394
2447
|
let options;
|
|
@@ -2407,7 +2460,13 @@ function createCachedResource(sourceOrFetcher, fetcherOrOptions, opts = {}) {
|
|
|
2407
2460
|
staleWhileRevalidate = true
|
|
2408
2461
|
} = options;
|
|
2409
2462
|
const ns = name;
|
|
2410
|
-
const
|
|
2463
|
+
const cache = options.cache ?? currentResourceCache();
|
|
2464
|
+
const inflight = cache.inflight;
|
|
2465
|
+
const ssrId = options.transport === false ? void 0 : nextResourceId();
|
|
2466
|
+
const seed = ssrId !== void 0 ? getServerData(ssrId) : void 0;
|
|
2467
|
+
const deps = options.deps ?? [];
|
|
2468
|
+
const depValues = () => deps.map((dep) => dep());
|
|
2469
|
+
const buildKey = (key, values = untrack2(depValues)) => `${ns}:${JSON.stringify(key)}` + (deps.length ? `:${JSON.stringify(values)}` : "");
|
|
2411
2470
|
const isFresh = (entry) => ttl === Infinity || Date.now() - entry.timestamp <= ttl;
|
|
2412
2471
|
const [value, setValue] = createSignal(void 0, { equals: false });
|
|
2413
2472
|
const [resourceState, setResourceState] = createSignal("unresolved", { equals: false });
|
|
@@ -2420,8 +2479,9 @@ function createCachedResource(sourceOrFetcher, fetcherOrOptions, opts = {}) {
|
|
|
2420
2479
|
let flagRefetch = false;
|
|
2421
2480
|
let flagInvalidate = false;
|
|
2422
2481
|
function doFetch(key, ck, background, isCancelled) {
|
|
2423
|
-
|
|
2424
|
-
|
|
2482
|
+
const running2 = inflight.get(ck);
|
|
2483
|
+
if (running2) {
|
|
2484
|
+
running2.then(
|
|
2425
2485
|
(v) => {
|
|
2426
2486
|
if (!isCancelled()) settle(v, background);
|
|
2427
2487
|
},
|
|
@@ -2429,12 +2489,15 @@ function createCachedResource(sourceOrFetcher, fetcherOrOptions, opts = {}) {
|
|
|
2429
2489
|
if (!isCancelled() && !background) fail(e);
|
|
2430
2490
|
}
|
|
2431
2491
|
);
|
|
2432
|
-
if (!background)
|
|
2492
|
+
if (!background) {
|
|
2493
|
+
trackResourcePromise(running2);
|
|
2494
|
+
setPendingP(running2);
|
|
2495
|
+
}
|
|
2433
2496
|
return;
|
|
2434
2497
|
}
|
|
2435
2498
|
const promise = Promise.resolve(fetchFn(key)).then(
|
|
2436
2499
|
(v) => {
|
|
2437
|
-
|
|
2500
|
+
cache.set(ck, { value: v, timestamp: Date.now() });
|
|
2438
2501
|
inflight.delete(ck);
|
|
2439
2502
|
return v;
|
|
2440
2503
|
},
|
|
@@ -2457,7 +2520,8 @@ function createCachedResource(sourceOrFetcher, fetcherOrOptions, opts = {}) {
|
|
|
2457
2520
|
}
|
|
2458
2521
|
);
|
|
2459
2522
|
}
|
|
2460
|
-
function settle(v,
|
|
2523
|
+
function settle(v, _background) {
|
|
2524
|
+
if (ssrId !== void 0) reportResourceData(ssrId, v);
|
|
2461
2525
|
batch2(() => {
|
|
2462
2526
|
setValue(() => v);
|
|
2463
2527
|
setResourceState("ready");
|
|
@@ -2475,6 +2539,7 @@ function createCachedResource(sourceOrFetcher, fetcherOrOptions, opts = {}) {
|
|
|
2475
2539
|
}
|
|
2476
2540
|
createEffect(() => {
|
|
2477
2541
|
const key = sourceFn();
|
|
2542
|
+
const values = depValues();
|
|
2478
2543
|
tick();
|
|
2479
2544
|
if (key === false || key === null || key === void 0) {
|
|
2480
2545
|
prevKey = UNSET2;
|
|
@@ -2489,16 +2554,18 @@ function createCachedResource(sourceOrFetcher, fetcherOrOptions, opts = {}) {
|
|
|
2489
2554
|
const doInvalidate = flagInvalidate;
|
|
2490
2555
|
flagRefetch = false;
|
|
2491
2556
|
flagInvalidate = false;
|
|
2492
|
-
const ck = buildKey(key);
|
|
2493
|
-
if (
|
|
2557
|
+
const ck = buildKey(key, values);
|
|
2558
|
+
if (seed && prevKey === UNSET2 && !cache.get(ck)) cache.set(ck, { value: seed.value, timestamp: Date.now() });
|
|
2559
|
+
if (doInvalidate) cache.delete(ck);
|
|
2494
2560
|
const keyChanged = prevKey === UNSET2 || key !== prevKey;
|
|
2495
2561
|
prevKey = key;
|
|
2496
2562
|
let cancelled = false;
|
|
2497
2563
|
onCleanup(() => {
|
|
2498
2564
|
cancelled = true;
|
|
2499
2565
|
});
|
|
2500
|
-
const cached =
|
|
2566
|
+
const cached = cache.get(ck);
|
|
2501
2567
|
if (cached && isFresh(cached) && !doRefetch && !doInvalidate) {
|
|
2568
|
+
if (ssrId !== void 0) reportResourceData(ssrId, cached.value);
|
|
2502
2569
|
const cur2 = untrack2(resourceState);
|
|
2503
2570
|
if (cur2 !== "ready" || untrack2(value) !== cached.value) {
|
|
2504
2571
|
batch2(() => {
|
|
@@ -2559,22 +2626,17 @@ function createCachedResource(sourceOrFetcher, fetcherOrOptions, opts = {}) {
|
|
|
2559
2626
|
const invalidate = () => {
|
|
2560
2627
|
const key = untrack2(sourceFn);
|
|
2561
2628
|
if (key !== null && key !== void 0 && key !== false) {
|
|
2562
|
-
|
|
2629
|
+
cache.delete(buildKey(key));
|
|
2563
2630
|
}
|
|
2564
2631
|
flagInvalidate = true;
|
|
2565
2632
|
setTick((v) => v + 1);
|
|
2566
2633
|
};
|
|
2567
|
-
return
|
|
2634
|
+
return Object.assign(resource2, { mutate: setValue, refetch, invalidate });
|
|
2568
2635
|
}
|
|
2569
|
-
function
|
|
2636
|
+
function clearCachedResources(name, cache = currentResourceCache()) {
|
|
2637
|
+
if (name === void 0) return cache.clear();
|
|
2570
2638
|
const prefix = `${name}:`;
|
|
2571
|
-
for (const key of
|
|
2572
|
-
if (key.startsWith(prefix)) ttlCache.delete(key);
|
|
2573
|
-
}
|
|
2574
|
-
}
|
|
2575
|
-
function clearAllCachedResources() {
|
|
2576
|
-
ttlCache.clear();
|
|
2577
|
-
inflight.clear();
|
|
2639
|
+
for (const key of [...cache.keys()]) if (key.startsWith(prefix)) cache.delete(key);
|
|
2578
2640
|
}
|
|
2579
2641
|
export {
|
|
2580
2642
|
$NAME,
|
|
@@ -2596,15 +2658,16 @@ export {
|
|
|
2596
2658
|
SuspenseContext,
|
|
2597
2659
|
VERSION,
|
|
2598
2660
|
__DEV__,
|
|
2661
|
+
adoptReactiveScope,
|
|
2599
2662
|
batch2 as batch,
|
|
2663
|
+
cachedResource,
|
|
2600
2664
|
childOwner,
|
|
2601
|
-
|
|
2602
|
-
|
|
2665
|
+
clearCachedResources,
|
|
2666
|
+
closeReactiveScope,
|
|
2603
2667
|
combineSignal,
|
|
2604
2668
|
combineSignals,
|
|
2605
2669
|
computed,
|
|
2606
2670
|
context,
|
|
2607
|
-
createCachedResource,
|
|
2608
2671
|
createChildOwner,
|
|
2609
2672
|
createComputed,
|
|
2610
2673
|
createComputedStore,
|
|
@@ -2619,6 +2682,7 @@ export {
|
|
|
2619
2682
|
createMutableStore,
|
|
2620
2683
|
createRenderEffect,
|
|
2621
2684
|
createResource,
|
|
2685
|
+
createResourceCache,
|
|
2622
2686
|
createRoot,
|
|
2623
2687
|
createSelector,
|
|
2624
2688
|
createSignal,
|
|
@@ -2626,6 +2690,7 @@ export {
|
|
|
2626
2690
|
createStoreEffect,
|
|
2627
2691
|
createStoreMemo,
|
|
2628
2692
|
createSuspenseBoundary,
|
|
2693
|
+
currentResourceCache,
|
|
2629
2694
|
debounceSignal,
|
|
2630
2695
|
disposeScope,
|
|
2631
2696
|
distinctSignal,
|
|
@@ -2654,6 +2719,7 @@ export {
|
|
|
2654
2719
|
makeArrayFlat,
|
|
2655
2720
|
mapArray,
|
|
2656
2721
|
mapSignal,
|
|
2722
|
+
markReactiveScope,
|
|
2657
2723
|
memo,
|
|
2658
2724
|
mergeSignal,
|
|
2659
2725
|
modifiable,
|
|
@@ -2662,12 +2728,14 @@ export {
|
|
|
2662
2728
|
omitSignal,
|
|
2663
2729
|
on,
|
|
2664
2730
|
onCleanup,
|
|
2731
|
+
openReactiveScope,
|
|
2665
2732
|
operate,
|
|
2666
2733
|
pickSignal,
|
|
2667
2734
|
produce as produceUtil,
|
|
2668
2735
|
provide,
|
|
2669
2736
|
provideErrorBoundary,
|
|
2670
2737
|
provideSuspense,
|
|
2738
|
+
reactiveScopeOf,
|
|
2671
2739
|
readChildren,
|
|
2672
2740
|
readSignal,
|
|
2673
2741
|
reconcile as reconcileUtil,
|
|
@@ -2684,6 +2752,7 @@ export {
|
|
|
2684
2752
|
runWithOwner,
|
|
2685
2753
|
selectSignal,
|
|
2686
2754
|
setOwner,
|
|
2755
|
+
setResourceCacheScope,
|
|
2687
2756
|
setResourceDataSink,
|
|
2688
2757
|
setResourceIdSource,
|
|
2689
2758
|
setResourceTracker,
|
|
@@ -2701,6 +2770,7 @@ export {
|
|
|
2701
2770
|
useContext,
|
|
2702
2771
|
useTransition,
|
|
2703
2772
|
whenSignal,
|
|
2773
|
+
withReactiveScope,
|
|
2704
2774
|
wrap,
|
|
2705
2775
|
zipSignal
|
|
2706
2776
|
};
|