storion 0.7.9 → 0.8.0
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/async/index.js +1 -2
- package/dist/core/container.d.ts.map +1 -1
- package/dist/core/createResolver.d.ts.map +1 -1
- package/dist/core/middleware.d.ts +3 -8
- package/dist/core/middleware.d.ts.map +1 -1
- package/dist/core/store.d.ts +1 -1
- package/dist/core/store.d.ts.map +1 -1
- package/dist/devtools/index.js +12 -31
- package/dist/devtools/middleware.d.ts.map +1 -1
- package/dist/{effect-ByI1oRBq.js → effect-C6h0PDDI.js} +160 -4
- package/dist/{emitter-XwTUpyGv.js → emitter-j4rC71vY.js} +1 -159
- package/dist/{store-DGyskBrf.js → index-CIFKRy71.js} +536 -16
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/isPromiseLike-bFkfHAbm.js +6 -0
- package/dist/meta/createMetaQuery.d.ts +10 -0
- package/dist/meta/createMetaQuery.d.ts.map +1 -0
- package/dist/meta/index.d.ts +4 -0
- package/dist/meta/index.d.ts.map +1 -0
- package/dist/meta/meta.d.ts +71 -0
- package/dist/meta/meta.d.ts.map +1 -0
- package/dist/meta/withMeta.d.ts +48 -0
- package/dist/meta/withMeta.d.ts.map +1 -0
- package/dist/meta-u3yOx5Kh.js +29 -0
- package/dist/persist/index.d.ts +9 -0
- package/dist/persist/index.d.ts.map +1 -0
- package/dist/persist/index.js +70 -0
- package/dist/persist/persist.d.ts +128 -0
- package/dist/persist/persist.d.ts.map +1 -0
- package/dist/react/context.d.ts.map +1 -1
- package/dist/react/create.d.ts +37 -7
- package/dist/react/create.d.ts.map +1 -1
- package/dist/react/index.d.ts +2 -2
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +126 -107
- package/dist/react/withStore.d.ts +73 -8
- package/dist/react/withStore.d.ts.map +1 -1
- package/dist/storion.js +29 -474
- package/dist/types.d.ts +149 -34
- package/dist/types.d.ts.map +1 -1
- package/package.json +5 -1
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { a as SetupPhaseError, c as LifetimeMismatchError,
|
|
1
|
+
import { a as SetupPhaseError, c as LifetimeMismatchError, w as withHooks, I as InvalidActionError, h as hasReadHook, t as trackRead, f as hasWriteHook, g as trackWrite, d as StoreDisposedError, s as scheduleNotification, b as batch, u as untrack, i as getHooks, H as HooksContextError } from "./effect-C6h0PDDI.js";
|
|
2
|
+
import { e as emitter } from "./emitter-j4rC71vY.js";
|
|
3
|
+
import "./meta-u3yOx5Kh.js";
|
|
2
4
|
const STORION_TYPE = Symbol("STORION");
|
|
3
5
|
function is$1(value, kind) {
|
|
4
6
|
return value !== null && (typeof value === "object" || typeof value === "function") && STORION_TYPE in value && value[STORION_TYPE] === kind;
|
|
@@ -1231,8 +1233,8 @@ function arraySome$1(array, predicate) {
|
|
|
1231
1233
|
return false;
|
|
1232
1234
|
}
|
|
1233
1235
|
var _arraySome = arraySome$1;
|
|
1234
|
-
function cacheHas$1(
|
|
1235
|
-
return
|
|
1236
|
+
function cacheHas$1(cache2, key) {
|
|
1237
|
+
return cache2.has(key);
|
|
1236
1238
|
}
|
|
1237
1239
|
var _cacheHas = cacheHas$1;
|
|
1238
1240
|
var SetCache = _SetCache, arraySome = _arraySome, cacheHas = _cacheHas;
|
|
@@ -2034,10 +2036,12 @@ function store(options) {
|
|
|
2034
2036
|
{}
|
|
2035
2037
|
);
|
|
2036
2038
|
};
|
|
2039
|
+
const metaEntries = Array.isArray(options.meta) ? options.meta : options.meta ? [options.meta] : [];
|
|
2037
2040
|
Object.defineProperties(spec, {
|
|
2038
2041
|
[STORION_TYPE]: { value: "store.spec", enumerable: false },
|
|
2039
2042
|
displayName: { value: displayName, enumerable: true, writable: false },
|
|
2040
|
-
options: { value: options, enumerable: true, writable: false }
|
|
2043
|
+
options: { value: options, enumerable: true, writable: false },
|
|
2044
|
+
meta: { value: metaEntries, enumerable: true, writable: false }
|
|
2041
2045
|
});
|
|
2042
2046
|
return spec;
|
|
2043
2047
|
}
|
|
@@ -2241,11 +2245,12 @@ function createStoreInstance(spec, resolver, instanceOptions = {}) {
|
|
|
2241
2245
|
}
|
|
2242
2246
|
return { ...currentState };
|
|
2243
2247
|
},
|
|
2244
|
-
hydrate(data) {
|
|
2248
|
+
hydrate(data, hydrateOptions) {
|
|
2245
2249
|
const denormalizer = options.denormalize;
|
|
2246
2250
|
const newState = denormalizer ? denormalizer(data) : data;
|
|
2251
|
+
const force = (hydrateOptions == null ? void 0 : hydrateOptions.force) ?? false;
|
|
2247
2252
|
for (const key of Object.keys(newState)) {
|
|
2248
|
-
if (currentState[key] !== initialState[key]) {
|
|
2253
|
+
if (!force && currentState[key] !== initialState[key]) {
|
|
2249
2254
|
continue;
|
|
2250
2255
|
}
|
|
2251
2256
|
const oldValue = currentState[key];
|
|
@@ -2368,7 +2373,10 @@ function createStoreInstance(spec, resolver, instanceOptions = {}) {
|
|
|
2368
2373
|
scheduledEffects.push(runEffect);
|
|
2369
2374
|
}
|
|
2370
2375
|
}),
|
|
2371
|
-
() =>
|
|
2376
|
+
() => {
|
|
2377
|
+
var _a2;
|
|
2378
|
+
return ((_a2 = options.setup) == null ? void 0 : _a2.call(options, setupContext)) ?? {};
|
|
2379
|
+
}
|
|
2372
2380
|
);
|
|
2373
2381
|
} catch (error) {
|
|
2374
2382
|
disposeEffectsEmitter.emitAndClear();
|
|
@@ -2445,11 +2453,518 @@ function createStoreInstance(spec, resolver, instanceOptions = {}) {
|
|
|
2445
2453
|
instanceActions = wrappedActions;
|
|
2446
2454
|
return instance;
|
|
2447
2455
|
}
|
|
2456
|
+
function createMetaQuery(entries) {
|
|
2457
|
+
const entryArray = Array.isArray(entries) ? entries : entries ? [entries] : [];
|
|
2458
|
+
const single = (type) => {
|
|
2459
|
+
const result = { store: void 0, fields: {} };
|
|
2460
|
+
for (const entry of entryArray) {
|
|
2461
|
+
if (entry.type !== type) continue;
|
|
2462
|
+
if (entry.fields && entry.fields.length > 0) {
|
|
2463
|
+
for (const field of entry.fields) {
|
|
2464
|
+
const fieldKey = field;
|
|
2465
|
+
if (!(fieldKey in result.fields)) {
|
|
2466
|
+
result.fields[fieldKey] = entry.value;
|
|
2467
|
+
}
|
|
2468
|
+
}
|
|
2469
|
+
} else {
|
|
2470
|
+
if (result.store === void 0) {
|
|
2471
|
+
result.store = entry.value;
|
|
2472
|
+
}
|
|
2473
|
+
}
|
|
2474
|
+
}
|
|
2475
|
+
return result;
|
|
2476
|
+
};
|
|
2477
|
+
const all = (type) => {
|
|
2478
|
+
const result = { store: [], fields: {} };
|
|
2479
|
+
for (const entry of entryArray) {
|
|
2480
|
+
if (entry.type !== type) continue;
|
|
2481
|
+
if (entry.fields && entry.fields.length > 0) {
|
|
2482
|
+
for (const field of entry.fields) {
|
|
2483
|
+
const fieldKey = field;
|
|
2484
|
+
let arr = result.fields[fieldKey];
|
|
2485
|
+
if (!arr) {
|
|
2486
|
+
arr = [];
|
|
2487
|
+
result.fields[fieldKey] = arr;
|
|
2488
|
+
}
|
|
2489
|
+
arr.push(entry.value);
|
|
2490
|
+
}
|
|
2491
|
+
} else {
|
|
2492
|
+
result.store.push(entry.value);
|
|
2493
|
+
}
|
|
2494
|
+
}
|
|
2495
|
+
return result;
|
|
2496
|
+
};
|
|
2497
|
+
const any = (...types) => {
|
|
2498
|
+
return entryArray.some((entry) => types.includes(entry.type));
|
|
2499
|
+
};
|
|
2500
|
+
const query = Object.assign(
|
|
2501
|
+
(type) => single(type),
|
|
2502
|
+
{ single, all, any }
|
|
2503
|
+
);
|
|
2504
|
+
return query;
|
|
2505
|
+
}
|
|
2506
|
+
function extractDisplayName(factory) {
|
|
2507
|
+
if (isSpec(factory)) {
|
|
2508
|
+
return factory.displayName;
|
|
2509
|
+
}
|
|
2510
|
+
if (typeof factory.displayName === "string" && factory.displayName) {
|
|
2511
|
+
return factory.displayName;
|
|
2512
|
+
}
|
|
2513
|
+
if (factory.name && factory.name !== "") {
|
|
2514
|
+
return factory.name;
|
|
2515
|
+
}
|
|
2516
|
+
return void 0;
|
|
2517
|
+
}
|
|
2518
|
+
function createResolver(options = {}) {
|
|
2519
|
+
const {
|
|
2520
|
+
middleware = [],
|
|
2521
|
+
parent,
|
|
2522
|
+
invokeResolver: invokeResolverOption
|
|
2523
|
+
} = options;
|
|
2524
|
+
const cache2 = /* @__PURE__ */ new Map();
|
|
2525
|
+
const overrides = /* @__PURE__ */ new Map();
|
|
2526
|
+
const resolve = (factory) => overrides.get(factory) ?? factory;
|
|
2527
|
+
const invoke = (factory, resolverForCtx) => {
|
|
2528
|
+
const isStoreSpec = isSpec(factory);
|
|
2529
|
+
const displayName = extractDisplayName(factory);
|
|
2530
|
+
const meta = createMetaQuery(factory.meta);
|
|
2531
|
+
const chain = middleware.reduceRight(
|
|
2532
|
+
(next, mw) => () => {
|
|
2533
|
+
if (isStoreSpec) {
|
|
2534
|
+
const ctx = {
|
|
2535
|
+
type: "store",
|
|
2536
|
+
factory,
|
|
2537
|
+
resolver: resolverForCtx,
|
|
2538
|
+
next,
|
|
2539
|
+
displayName,
|
|
2540
|
+
spec: factory,
|
|
2541
|
+
meta
|
|
2542
|
+
};
|
|
2543
|
+
return mw(ctx);
|
|
2544
|
+
} else {
|
|
2545
|
+
const ctx = {
|
|
2546
|
+
type: "factory",
|
|
2547
|
+
factory,
|
|
2548
|
+
resolver: resolverForCtx,
|
|
2549
|
+
next,
|
|
2550
|
+
displayName,
|
|
2551
|
+
meta
|
|
2552
|
+
};
|
|
2553
|
+
return mw(ctx);
|
|
2554
|
+
}
|
|
2555
|
+
},
|
|
2556
|
+
() => factory(resolverForCtx)
|
|
2557
|
+
);
|
|
2558
|
+
return chain();
|
|
2559
|
+
};
|
|
2560
|
+
const resolver = {
|
|
2561
|
+
get(factory) {
|
|
2562
|
+
if (cache2.has(factory)) {
|
|
2563
|
+
return cache2.get(factory);
|
|
2564
|
+
}
|
|
2565
|
+
if (parent && overrides.size === 0 && parent.has(factory)) {
|
|
2566
|
+
return parent.get(factory);
|
|
2567
|
+
}
|
|
2568
|
+
const instance = invoke(
|
|
2569
|
+
resolve(factory),
|
|
2570
|
+
invokeResolverOption ?? resolver
|
|
2571
|
+
);
|
|
2572
|
+
cache2.set(factory, instance);
|
|
2573
|
+
return instance;
|
|
2574
|
+
},
|
|
2575
|
+
create(factory, ...args) {
|
|
2576
|
+
const resolverForCtx = invokeResolverOption ?? resolver;
|
|
2577
|
+
if (args.length > 0) {
|
|
2578
|
+
return factory(resolverForCtx, ...args);
|
|
2579
|
+
}
|
|
2580
|
+
return invoke(resolve(factory), resolverForCtx);
|
|
2581
|
+
},
|
|
2582
|
+
set(factory, override) {
|
|
2583
|
+
overrides.set(factory, override);
|
|
2584
|
+
cache2.delete(factory);
|
|
2585
|
+
},
|
|
2586
|
+
has(factory) {
|
|
2587
|
+
const inParent = parent && overrides.size === 0 && parent.has(factory);
|
|
2588
|
+
return cache2.has(factory) || (inParent ?? false);
|
|
2589
|
+
},
|
|
2590
|
+
tryGet(factory) {
|
|
2591
|
+
if (cache2.has(factory)) {
|
|
2592
|
+
return cache2.get(factory);
|
|
2593
|
+
}
|
|
2594
|
+
if (parent && overrides.size === 0) {
|
|
2595
|
+
return parent.tryGet(factory);
|
|
2596
|
+
}
|
|
2597
|
+
return void 0;
|
|
2598
|
+
},
|
|
2599
|
+
delete(factory) {
|
|
2600
|
+
const instance = cache2.get(factory);
|
|
2601
|
+
if (instance) {
|
|
2602
|
+
tryDispose(instance);
|
|
2603
|
+
cache2.delete(factory);
|
|
2604
|
+
return true;
|
|
2605
|
+
}
|
|
2606
|
+
return false;
|
|
2607
|
+
},
|
|
2608
|
+
clear() {
|
|
2609
|
+
for (const instance of cache2.values()) {
|
|
2610
|
+
tryDispose(instance);
|
|
2611
|
+
}
|
|
2612
|
+
cache2.clear();
|
|
2613
|
+
},
|
|
2614
|
+
scope(scopeOptions = {}) {
|
|
2615
|
+
return createResolver({
|
|
2616
|
+
middleware: scopeOptions.middleware ?? middleware,
|
|
2617
|
+
parent: resolver,
|
|
2618
|
+
...scopeOptions
|
|
2619
|
+
});
|
|
2620
|
+
}
|
|
2621
|
+
};
|
|
2622
|
+
return resolver;
|
|
2623
|
+
}
|
|
2624
|
+
function when(predicate, middleware) {
|
|
2625
|
+
return (ctx) => {
|
|
2626
|
+
if (predicate(ctx.factory)) {
|
|
2627
|
+
return middleware(ctx);
|
|
2628
|
+
}
|
|
2629
|
+
return ctx.next();
|
|
2630
|
+
};
|
|
2631
|
+
}
|
|
2632
|
+
function createLoggingMiddleware(prefix = "Resolver") {
|
|
2633
|
+
return (ctx) => {
|
|
2634
|
+
const name = ctx.factory.name || "anonymous";
|
|
2635
|
+
console.log(`[${prefix}] Creating: ${name}`);
|
|
2636
|
+
const start = performance.now();
|
|
2637
|
+
const result = ctx.next();
|
|
2638
|
+
const duration = (performance.now() - start).toFixed(2);
|
|
2639
|
+
console.log(`[${prefix}] Created: ${name} (${duration}ms)`);
|
|
2640
|
+
return result;
|
|
2641
|
+
};
|
|
2642
|
+
}
|
|
2643
|
+
function createValidationMiddleware(validate) {
|
|
2644
|
+
return (ctx) => {
|
|
2645
|
+
const result = ctx.next();
|
|
2646
|
+
validate(result, ctx.factory);
|
|
2647
|
+
return result;
|
|
2648
|
+
};
|
|
2649
|
+
}
|
|
2650
|
+
let defaultMiddlewareConfig = {};
|
|
2651
|
+
const container = function(options = {}) {
|
|
2652
|
+
const middleware = [
|
|
2653
|
+
...defaultMiddlewareConfig.pre ?? [],
|
|
2654
|
+
...Array.isArray(options.middleware) ? options.middleware : options.middleware ? [options.middleware] : [],
|
|
2655
|
+
...defaultMiddlewareConfig.post ?? []
|
|
2656
|
+
];
|
|
2657
|
+
const instancesById = /* @__PURE__ */ new Map();
|
|
2658
|
+
const creationOrder = [];
|
|
2659
|
+
const createEmitter = emitter();
|
|
2660
|
+
const disposeEmitter = emitter();
|
|
2661
|
+
const parent = options._parent;
|
|
2662
|
+
let containerApi;
|
|
2663
|
+
const internalResolver = createResolver({
|
|
2664
|
+
middleware,
|
|
2665
|
+
parent,
|
|
2666
|
+
get invokeResolver() {
|
|
2667
|
+
return containerApi;
|
|
2668
|
+
}
|
|
2669
|
+
});
|
|
2670
|
+
function trackStore(spec, instance) {
|
|
2671
|
+
instancesById.set(instance.id, instance);
|
|
2672
|
+
creationOrder.push(spec);
|
|
2673
|
+
if (typeof instance.onDispose === "function") {
|
|
2674
|
+
instance.onDispose(() => {
|
|
2675
|
+
disposeEmitter.emit(instance);
|
|
2676
|
+
instancesById.delete(instance.id);
|
|
2677
|
+
internalResolver.delete(spec);
|
|
2678
|
+
const index = creationOrder.indexOf(spec);
|
|
2679
|
+
if (index !== -1) {
|
|
2680
|
+
creationOrder.splice(index, 1);
|
|
2681
|
+
}
|
|
2682
|
+
});
|
|
2683
|
+
}
|
|
2684
|
+
createEmitter.emit(instance);
|
|
2685
|
+
}
|
|
2686
|
+
containerApi = {
|
|
2687
|
+
[STORION_TYPE]: "container",
|
|
2688
|
+
// Get by ID or factory/spec
|
|
2689
|
+
get(specOrIdOrFactory) {
|
|
2690
|
+
if (typeof specOrIdOrFactory === "string") {
|
|
2691
|
+
return instancesById.get(specOrIdOrFactory);
|
|
2692
|
+
}
|
|
2693
|
+
const wasCached = internalResolver.has(specOrIdOrFactory);
|
|
2694
|
+
const instance = untrack(() => internalResolver.get(specOrIdOrFactory));
|
|
2695
|
+
if (!wasCached && isSpec(specOrIdOrFactory)) {
|
|
2696
|
+
trackStore(specOrIdOrFactory, instance);
|
|
2697
|
+
}
|
|
2698
|
+
return instance;
|
|
2699
|
+
},
|
|
2700
|
+
create(specOrFactory, ...args) {
|
|
2701
|
+
const instance = untrack(
|
|
2702
|
+
() => internalResolver.create(specOrFactory, ...args)
|
|
2703
|
+
);
|
|
2704
|
+
if (isSpec(specOrFactory)) {
|
|
2705
|
+
const storeInstance = instance;
|
|
2706
|
+
instancesById.set(storeInstance.id, storeInstance);
|
|
2707
|
+
if (typeof storeInstance.onDispose === "function") {
|
|
2708
|
+
storeInstance.onDispose(() => {
|
|
2709
|
+
disposeEmitter.emit(storeInstance);
|
|
2710
|
+
instancesById.delete(storeInstance.id);
|
|
2711
|
+
});
|
|
2712
|
+
}
|
|
2713
|
+
}
|
|
2714
|
+
return instance;
|
|
2715
|
+
},
|
|
2716
|
+
set(spec, override) {
|
|
2717
|
+
const existing = internalResolver.tryGet(spec);
|
|
2718
|
+
tryDispose(existing);
|
|
2719
|
+
internalResolver.set(spec, override);
|
|
2720
|
+
},
|
|
2721
|
+
has(spec) {
|
|
2722
|
+
return internalResolver.has(spec);
|
|
2723
|
+
},
|
|
2724
|
+
tryGet(spec) {
|
|
2725
|
+
return internalResolver.tryGet(spec);
|
|
2726
|
+
},
|
|
2727
|
+
delete(spec) {
|
|
2728
|
+
const instance = internalResolver.tryGet(spec);
|
|
2729
|
+
if (instance) {
|
|
2730
|
+
tryDispose(instance);
|
|
2731
|
+
return true;
|
|
2732
|
+
}
|
|
2733
|
+
return false;
|
|
2734
|
+
},
|
|
2735
|
+
clear() {
|
|
2736
|
+
const specs = [...creationOrder].reverse();
|
|
2737
|
+
for (const spec of specs) {
|
|
2738
|
+
const instance = internalResolver.tryGet(spec);
|
|
2739
|
+
tryDispose(instance);
|
|
2740
|
+
}
|
|
2741
|
+
internalResolver.clear();
|
|
2742
|
+
instancesById.clear();
|
|
2743
|
+
creationOrder.length = 0;
|
|
2744
|
+
},
|
|
2745
|
+
dispose(spec) {
|
|
2746
|
+
return containerApi.delete(spec);
|
|
2747
|
+
},
|
|
2748
|
+
scope(scopeOptions = {}) {
|
|
2749
|
+
return container({
|
|
2750
|
+
middleware: scopeOptions.middleware ?? middleware,
|
|
2751
|
+
_parent: containerApi
|
|
2752
|
+
});
|
|
2753
|
+
},
|
|
2754
|
+
onCreate: createEmitter.on,
|
|
2755
|
+
onDispose: disposeEmitter.on
|
|
2756
|
+
};
|
|
2757
|
+
return containerApi;
|
|
2758
|
+
};
|
|
2759
|
+
const defaultsFn = (config = {}) => {
|
|
2760
|
+
defaultMiddlewareConfig = {
|
|
2761
|
+
pre: [...defaultMiddlewareConfig.pre ?? [], ...config.pre ?? []],
|
|
2762
|
+
post: [...defaultMiddlewareConfig.post ?? [], ...config.post ?? []]
|
|
2763
|
+
};
|
|
2764
|
+
};
|
|
2765
|
+
defaultsFn.clear = () => {
|
|
2766
|
+
defaultMiddlewareConfig = {};
|
|
2767
|
+
};
|
|
2768
|
+
container.defaults = defaultsFn;
|
|
2769
|
+
let pickIdCounter = 0;
|
|
2770
|
+
function pick(selector, equality2) {
|
|
2771
|
+
var _a;
|
|
2772
|
+
const parentHooks = getHooks();
|
|
2773
|
+
if (!parentHooks.onRead) {
|
|
2774
|
+
throw new HooksContextError("pick", "an effect or useStore selector");
|
|
2775
|
+
}
|
|
2776
|
+
const equalityFn = resolveEquality(equality2);
|
|
2777
|
+
const currentReads = [];
|
|
2778
|
+
const evaluate = () => {
|
|
2779
|
+
currentReads.length = 0;
|
|
2780
|
+
const value = withHooks(
|
|
2781
|
+
{
|
|
2782
|
+
onRead: (event) => {
|
|
2783
|
+
currentReads.push(event);
|
|
2784
|
+
}
|
|
2785
|
+
},
|
|
2786
|
+
selector
|
|
2787
|
+
);
|
|
2788
|
+
return value;
|
|
2789
|
+
};
|
|
2790
|
+
let currentValue = evaluate();
|
|
2791
|
+
if (!currentReads.length) {
|
|
2792
|
+
return currentValue;
|
|
2793
|
+
}
|
|
2794
|
+
const pickKey = `pick:${++pickIdCounter}`;
|
|
2795
|
+
const subscribe = (listener) => {
|
|
2796
|
+
const onCleanup = emitter();
|
|
2797
|
+
const setupSubscriptions = () => {
|
|
2798
|
+
for (const read of currentReads) {
|
|
2799
|
+
const unsub = read.subscribe(handleChange);
|
|
2800
|
+
onCleanup.on(unsub);
|
|
2801
|
+
}
|
|
2802
|
+
};
|
|
2803
|
+
const clearSubscriptions = () => {
|
|
2804
|
+
onCleanup.emitAndClear();
|
|
2805
|
+
};
|
|
2806
|
+
const handleChange = () => {
|
|
2807
|
+
try {
|
|
2808
|
+
const prevValue = currentValue;
|
|
2809
|
+
clearSubscriptions();
|
|
2810
|
+
currentValue = evaluate();
|
|
2811
|
+
setupSubscriptions();
|
|
2812
|
+
if (!equalityFn(prevValue, currentValue)) {
|
|
2813
|
+
listener();
|
|
2814
|
+
}
|
|
2815
|
+
} catch (error) {
|
|
2816
|
+
clearSubscriptions();
|
|
2817
|
+
listener();
|
|
2818
|
+
}
|
|
2819
|
+
};
|
|
2820
|
+
setupSubscriptions();
|
|
2821
|
+
return clearSubscriptions;
|
|
2822
|
+
};
|
|
2823
|
+
(_a = parentHooks.onRead) == null ? void 0 : _a.call(parentHooks, {
|
|
2824
|
+
key: pickKey,
|
|
2825
|
+
value: currentValue,
|
|
2826
|
+
subscribe
|
|
2827
|
+
});
|
|
2828
|
+
return currentValue;
|
|
2829
|
+
}
|
|
2830
|
+
function patternToPredicate(pattern) {
|
|
2831
|
+
if (pattern instanceof RegExp) {
|
|
2832
|
+
return (name) => pattern.test(name);
|
|
2833
|
+
}
|
|
2834
|
+
const startsWithWildcard = pattern.startsWith("*");
|
|
2835
|
+
const endsWithWildcard = pattern.endsWith("*");
|
|
2836
|
+
if (startsWithWildcard && endsWithWildcard) {
|
|
2837
|
+
const substr = pattern.slice(1, -1);
|
|
2838
|
+
return (name) => name.includes(substr);
|
|
2839
|
+
}
|
|
2840
|
+
if (startsWithWildcard) {
|
|
2841
|
+
const suffix = pattern.slice(1);
|
|
2842
|
+
return (name) => name.endsWith(suffix);
|
|
2843
|
+
}
|
|
2844
|
+
if (endsWithWildcard) {
|
|
2845
|
+
const prefix = pattern.slice(0, -1);
|
|
2846
|
+
return (name) => name.startsWith(prefix);
|
|
2847
|
+
}
|
|
2848
|
+
return (name) => name === pattern;
|
|
2849
|
+
}
|
|
2850
|
+
function patternsToPredicate(patterns) {
|
|
2851
|
+
if (Array.isArray(patterns)) {
|
|
2852
|
+
const predicates = patterns.map(patternToPredicate);
|
|
2853
|
+
return (ctx) => ctx.displayName !== void 0 && predicates.some((p) => p(ctx.displayName));
|
|
2854
|
+
}
|
|
2855
|
+
const predicate = patternToPredicate(patterns);
|
|
2856
|
+
return (ctx) => ctx.displayName !== void 0 && predicate(ctx.displayName);
|
|
2857
|
+
}
|
|
2858
|
+
function compose(...middlewares) {
|
|
2859
|
+
if (middlewares.length === 0) {
|
|
2860
|
+
return (ctx) => ctx.next();
|
|
2861
|
+
}
|
|
2862
|
+
if (middlewares.length === 1) {
|
|
2863
|
+
return middlewares[0];
|
|
2864
|
+
}
|
|
2865
|
+
return (ctx) => {
|
|
2866
|
+
let index = 0;
|
|
2867
|
+
const executeNext = () => {
|
|
2868
|
+
if (index >= middlewares.length) {
|
|
2869
|
+
return ctx.next();
|
|
2870
|
+
}
|
|
2871
|
+
const currentMiddleware = middlewares[index];
|
|
2872
|
+
index++;
|
|
2873
|
+
const wrappedCtx = ctx.type === "store" ? { ...ctx, next: executeNext } : { ...ctx, next: executeNext };
|
|
2874
|
+
return currentMiddleware(wrappedCtx);
|
|
2875
|
+
};
|
|
2876
|
+
return executeNext();
|
|
2877
|
+
};
|
|
2878
|
+
}
|
|
2879
|
+
function applyFor(predicateOrPatterns, middleware) {
|
|
2880
|
+
const predicate = typeof predicateOrPatterns === "function" ? predicateOrPatterns : patternsToPredicate(predicateOrPatterns);
|
|
2881
|
+
const composedMiddleware = Array.isArray(middleware) ? compose(...middleware) : middleware;
|
|
2882
|
+
return (ctx) => {
|
|
2883
|
+
if (predicate(ctx)) {
|
|
2884
|
+
return composedMiddleware(ctx);
|
|
2885
|
+
}
|
|
2886
|
+
return ctx.next();
|
|
2887
|
+
};
|
|
2888
|
+
}
|
|
2889
|
+
function applyExcept(predicateOrPatterns, middleware) {
|
|
2890
|
+
const matchPredicate = typeof predicateOrPatterns === "function" ? predicateOrPatterns : patternsToPredicate(predicateOrPatterns);
|
|
2891
|
+
const invertedPredicate = (ctx) => !matchPredicate(ctx);
|
|
2892
|
+
return applyFor(invertedPredicate, middleware);
|
|
2893
|
+
}
|
|
2894
|
+
function forStores(storeMiddleware) {
|
|
2895
|
+
const composedMiddleware = Array.isArray(storeMiddleware) ? compose(...storeMiddleware) : storeMiddleware;
|
|
2896
|
+
return (ctx) => {
|
|
2897
|
+
if (ctx.type === "store") {
|
|
2898
|
+
return composedMiddleware(ctx);
|
|
2899
|
+
}
|
|
2900
|
+
return ctx.next();
|
|
2901
|
+
};
|
|
2902
|
+
}
|
|
2903
|
+
const DEFAULT_KEY = {};
|
|
2904
|
+
const cache = /* @__PURE__ */ new Map();
|
|
2905
|
+
function getKeyCache(key) {
|
|
2906
|
+
let keyCache = cache.get(key);
|
|
2907
|
+
if (!keyCache) {
|
|
2908
|
+
keyCache = /* @__PURE__ */ new WeakMap();
|
|
2909
|
+
cache.set(key, keyCache);
|
|
2910
|
+
}
|
|
2911
|
+
return keyCache;
|
|
2912
|
+
}
|
|
2913
|
+
function trigger(keyOrFn, fnOrDepsOrOptions, depsOrFirstArg, ...restArgs) {
|
|
2914
|
+
let key;
|
|
2915
|
+
let fn;
|
|
2916
|
+
let deps;
|
|
2917
|
+
let equality2;
|
|
2918
|
+
let args;
|
|
2919
|
+
if (typeof fnOrDepsOrOptions === "function") {
|
|
2920
|
+
key = keyOrFn;
|
|
2921
|
+
fn = fnOrDepsOrOptions;
|
|
2922
|
+
deps = depsOrFirstArg ?? [];
|
|
2923
|
+
args = restArgs;
|
|
2924
|
+
} else if (Array.isArray(fnOrDepsOrOptions)) {
|
|
2925
|
+
key = DEFAULT_KEY;
|
|
2926
|
+
fn = keyOrFn;
|
|
2927
|
+
deps = fnOrDepsOrOptions;
|
|
2928
|
+
args = depsOrFirstArg !== void 0 ? [depsOrFirstArg, ...restArgs] : [];
|
|
2929
|
+
} else {
|
|
2930
|
+
const options = fnOrDepsOrOptions ?? {};
|
|
2931
|
+
key = options.key ?? DEFAULT_KEY;
|
|
2932
|
+
fn = keyOrFn;
|
|
2933
|
+
deps = options.deps ?? [];
|
|
2934
|
+
equality2 = options.equality ? resolveEquality(options.equality) : void 0;
|
|
2935
|
+
args = depsOrFirstArg !== void 0 ? [depsOrFirstArg, ...restArgs] : [];
|
|
2936
|
+
}
|
|
2937
|
+
const cacheKey = unwrapFn(fn);
|
|
2938
|
+
const keyCache = getKeyCache(key);
|
|
2939
|
+
const cached = keyCache.get(cacheKey);
|
|
2940
|
+
const eq2 = equality2 ?? (cached == null ? void 0 : cached.equality) ?? shallowEqual;
|
|
2941
|
+
if (cached && eq2(cached.deps, deps)) {
|
|
2942
|
+
return cached.result;
|
|
2943
|
+
}
|
|
2944
|
+
const result = fn(...args);
|
|
2945
|
+
keyCache.set(cacheKey, { deps, result, equality: equality2 });
|
|
2946
|
+
return result;
|
|
2947
|
+
}
|
|
2948
|
+
trigger.clear = (key) => {
|
|
2949
|
+
cache.delete(key);
|
|
2950
|
+
};
|
|
2951
|
+
trigger.clearAll = () => {
|
|
2952
|
+
cache.clear();
|
|
2953
|
+
};
|
|
2954
|
+
function withMeta(factory, meta) {
|
|
2955
|
+
return Object.assign(factory, {
|
|
2956
|
+
meta: Array.isArray(meta) ? meta : [meta]
|
|
2957
|
+
});
|
|
2958
|
+
}
|
|
2448
2959
|
export {
|
|
2960
|
+
wrapFn as A,
|
|
2961
|
+
unwrapFn as B,
|
|
2962
|
+
isWrappedFn as C,
|
|
2963
|
+
withMeta as D,
|
|
2449
2964
|
STORION_TYPE as S,
|
|
2450
2965
|
is$1 as a,
|
|
2451
2966
|
isStorion as b,
|
|
2452
|
-
|
|
2967
|
+
container as c,
|
|
2453
2968
|
isContainer as d,
|
|
2454
2969
|
isStore as e,
|
|
2455
2970
|
isFocus as f,
|
|
@@ -2458,14 +2973,19 @@ export {
|
|
|
2458
2973
|
isSpec as i,
|
|
2459
2974
|
isStoreContext as j,
|
|
2460
2975
|
isSelectorContext as k,
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2976
|
+
createResolver as l,
|
|
2977
|
+
createLoggingMiddleware as m,
|
|
2978
|
+
createValidationMiddleware as n,
|
|
2979
|
+
applyFor as o,
|
|
2980
|
+
pick as p,
|
|
2981
|
+
applyExcept as q,
|
|
2466
2982
|
resolveEquality as r,
|
|
2467
2983
|
store as s,
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2984
|
+
forStores as t,
|
|
2985
|
+
equality as u,
|
|
2986
|
+
shallowEqual as v,
|
|
2987
|
+
when as w,
|
|
2988
|
+
deepEqual as x,
|
|
2989
|
+
strictEqual as y,
|
|
2990
|
+
trigger as z
|
|
2471
2991
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @packageDocumentation
|
|
5
5
|
*/
|
|
6
|
-
export { STORION_TYPE, type StorionKind, type StorionObject, type StateBase, type ActionsBase, type Equality, type EqualityMap, type Lifetime, type DispatchEvent, type ActionDispatchEvent, type ReactiveAction, type ReactiveActions, type
|
|
6
|
+
export { STORION_TYPE, type StorionKind, type StorionObject, type StateBase, type ActionsBase, type Equality, type EqualityMap, type Lifetime, type DispatchEvent, type ActionDispatchEvent, type ReactiveAction, type ReactiveActions, type StoreSpec, type StoreContext, type StoreMixin, type StoreOptions, type StoreInstance, type StoreContainer, type ContainerOptions, type StoreMiddleware, type SelectorContext, type SelectorMixin, type Selector, type StoreTuple, type StableResult, type PickEquality, type StatePath, type PathValue, type Focus, type FocusOptions, type FocusChangeEvent, type NonNullish, type Factory, type Middleware, type MiddlewareContext, type Resolver, type ResolverOptions, type MetaType, type MetaInfo, } from './types';
|
|
7
7
|
export { is, isStorion, getKind, isSpec, isContainer, isStore, isFocus, isAction, isStoreContext, isSelectorContext, } from './is';
|
|
8
8
|
export { store } from './core/store';
|
|
9
9
|
export { container } from './core/container';
|
|
@@ -11,9 +11,11 @@ export { batch, untrack } from './core/tracking';
|
|
|
11
11
|
export { createResolver, when, createLoggingMiddleware, createValidationMiddleware, } from './core/createResolver';
|
|
12
12
|
export { pick } from './core/pick';
|
|
13
13
|
export { effect, type EffectFn, type EffectContext, type EffectOptions, type EffectErrorStrategy, type EffectErrorContext, type EffectRetryConfig, } from './core/effect';
|
|
14
|
-
export { applyFor, applyExcept,
|
|
14
|
+
export { applyFor, applyExcept, forStores, type SpecPattern, } from './core/middleware';
|
|
15
15
|
export { equality, shallowEqual, deepEqual, strictEqual, } from './core/equality';
|
|
16
16
|
export { trigger, type TriggerOptions } from './trigger';
|
|
17
17
|
export { wrapFn as wrapFn, unwrapFn, isWrappedFn } from './core/fnWrapper';
|
|
18
18
|
export { StorionError, SetupPhaseError, LifetimeMismatchError, AsyncFunctionError, StoreDisposedError, InvalidActionError, HooksContextError, ProviderMissingError, LocalStoreDependencyError, EffectRefreshError, } from './errors';
|
|
19
|
+
export { meta } from './meta/meta';
|
|
20
|
+
export { withMeta } from './meta/withMeta';
|
|
19
21
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EACL,YAAY,EACZ,KAAK,WAAW,EAChB,KAAK,aAAa,EAClB,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,KAAK,QAAQ,EACb,KAAK,WAAW,EAChB,KAAK,QAAQ,EACb,KAAK,aAAa,EAClB,KAAK,mBAAmB,EACxB,KAAK,cAAc,EACnB,KAAK,eAAe,EACpB,KAAK,SAAS,EACd,KAAK,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EACL,YAAY,EACZ,KAAK,WAAW,EAChB,KAAK,aAAa,EAClB,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,KAAK,QAAQ,EACb,KAAK,WAAW,EAChB,KAAK,QAAQ,EACb,KAAK,aAAa,EAClB,KAAK,mBAAmB,EACxB,KAAK,cAAc,EACnB,KAAK,eAAe,EACpB,KAAK,SAAS,EACd,KAAK,YAAY,EACjB,KAAK,UAAU,EACf,KAAK,YAAY,EACjB,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,KAAK,eAAe,EACpB,KAAK,aAAa,EAClB,KAAK,QAAQ,EACb,KAAK,UAAU,EACf,KAAK,YAAY,EACjB,KAAK,YAAY,EAEjB,KAAK,SAAS,EACd,KAAK,SAAS,EACd,KAAK,KAAK,EACV,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACrB,KAAK,UAAU,EAEf,KAAK,OAAO,EACZ,KAAK,UAAU,EACf,KAAK,iBAAiB,EACtB,KAAK,QAAQ,EACb,KAAK,eAAe,EAEpB,KAAK,QAAQ,EACb,KAAK,QAAQ,GACd,MAAM,SAAS,CAAC;AAGjB,OAAO,EACL,EAAE,EACF,SAAS,EACT,OAAO,EACP,MAAM,EACN,WAAW,EACX,OAAO,EACP,OAAO,EACP,QAAQ,EACR,cAAc,EACd,iBAAiB,GAClB,MAAM,MAAM,CAAC;AAGd,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AACrC,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAGjD,OAAO,EACL,cAAc,EACd,IAAI,EACJ,uBAAuB,EACvB,0BAA0B,GAC3B,MAAM,uBAAuB,CAAC;AAG/B,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAEnC,OAAO,EACL,MAAM,EACN,KAAK,QAAQ,EACb,KAAK,aAAa,EAClB,KAAK,aAAa,EAClB,KAAK,mBAAmB,EACxB,KAAK,kBAAkB,EACvB,KAAK,iBAAiB,GACvB,MAAM,eAAe,CAAC;AAGvB,OAAO,EACL,QAAQ,EACR,WAAW,EACX,SAAS,EACT,KAAK,WAAW,GACjB,MAAM,mBAAmB,CAAC;AAG3B,OAAO,EACL,QAAQ,EACR,YAAY,EACZ,SAAS,EACT,WAAW,GACZ,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EAAE,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,WAAW,CAAC;AAGzD,OAAO,EAAE,MAAM,IAAI,MAAM,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAG3E,OAAO,EACL,YAAY,EACZ,eAAe,EACf,qBAAqB,EACrB,kBAAkB,EAClB,kBAAkB,EAClB,kBAAkB,EAClB,iBAAiB,EACjB,oBAAoB,EACpB,yBAAyB,EACzB,kBAAkB,GACnB,MAAM,UAAU,CAAC;AAGlB,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AACnC,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { MetaEntry, MetaQuery } from '../types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates a MetaQuery object for querying store metadata.
|
|
5
|
+
*
|
|
6
|
+
* @param entries - Array of MetaEntry objects from store options
|
|
7
|
+
* @returns MetaQuery interface with single/all/any methods
|
|
8
|
+
*/
|
|
9
|
+
export declare function createMetaQuery(entries: undefined | MetaEntry | MetaEntry[]): MetaQuery;
|
|
10
|
+
//# sourceMappingURL=createMetaQuery.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createMetaQuery.d.ts","sourceRoot":"","sources":["../../src/meta/createMetaQuery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,SAAS,EAAY,SAAS,EAAE,MAAM,UAAU,CAAC;AAEhF;;;;;GAKG;AACH,wBAAgB,eAAe,CAC7B,OAAO,EAAE,SAAS,GAAG,SAAS,GAAG,SAAS,EAAE,GAC3C,SAAS,CAiFX"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/meta/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC"}
|