storion 0.5.0 → 0.7.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/README.md +318 -25
- package/dist/async/async.d.ts +49 -0
- package/dist/async/async.d.ts.map +1 -1
- package/dist/async/index.js +50 -0
- package/dist/collection.d.ts +13 -21
- package/dist/collection.d.ts.map +1 -1
- package/dist/core/container.d.ts.map +1 -1
- package/dist/core/createResolver.d.ts.map +1 -1
- package/dist/core/disposable.d.ts +18 -0
- package/dist/core/disposable.d.ts.map +1 -0
- package/dist/core/effect.d.ts +20 -0
- package/dist/core/effect.d.ts.map +1 -1
- package/dist/core/storeContext.d.ts.map +1 -1
- package/dist/devtools/index.js +2 -1
- package/dist/effect-DPAYSuW3.js +294 -0
- package/dist/emitter-BA44OHdL.js +228 -0
- package/dist/react/index.js +27 -20
- package/dist/react/useStore.d.ts.map +1 -1
- package/dist/{store-CwA4YTVb.js → store-BroaE7p4.js} +46 -241
- package/dist/storion.js +32 -304
- package/dist/test/util.d.ts +2 -0
- package/dist/test/util.d.ts.map +1 -0
- package/dist/types.d.ts +63 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"effect.d.ts","sourceRoot":"","sources":["../../src/core/effect.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAgBH;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,uCAAuC;IACvC,UAAU,EAAE,MAAM,CAAC;IACnB,mEAAmE;IACnE,KAAK,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC,OAAO,EAAE,MAAM,KAAK,MAAM,CAAC,CAAC;CAChD;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,gCAAgC;IAChC,KAAK,EAAE,OAAO,CAAC;IACf,2CAA2C;IAC3C,KAAK,EAAE,MAAM,IAAI,CAAC;IAClB,4DAA4D;IAC5D,UAAU,EAAE,MAAM,CAAC;CACpB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AAM,MAAM,QAAQ,GAAG,CAAC,GAAG,EAAE,aAAa,KAAK,IAAI,CAAC;AAsBpD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AACH,wBAAgB,MAAM,CAAC,EAAE,EAAE,QAAQ,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,YAAY,CAyS1E"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storeContext.d.ts","sourceRoot":"","sources":["../../src/core/storeContext.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAEL,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,KAAK,SAAS,EACd,KAAK,aAAa,EAClB,KAAK,QAAQ,EACb,KAAK,YAAY,EAGjB,KAAK,KAAK,EACV,KAAK,YAAY,EAElB,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"storeContext.d.ts","sourceRoot":"","sources":["../../src/core/storeContext.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAEL,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,KAAK,SAAS,EACd,KAAK,aAAa,EAClB,KAAK,QAAQ,EACb,KAAK,YAAY,EAGjB,KAAK,KAAK,EACV,KAAK,YAAY,EAElB,MAAM,UAAU,CAAC;AAUlB;;GAEG;AACH,MAAM,WAAW,YAAY,CAAC,MAAM,SAAS,SAAS;IACpD,wBAAwB;IACxB,QAAQ,EAAE,MAAM,MAAM,CAAC;IACvB,4CAA4C;IAC5C,MAAM,EAAE,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,KAAK,IAAI,CAAC;IACnD,iCAAiC;IACjC,SAAS,EAAE,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,YAAY,CAAC;CACnD;AAED;;GAEG;AACH,MAAM,WAAW,yBAAyB,CACxC,MAAM,SAAS,SAAS,EACxB,QAAQ,SAAS,WAAW;IAE5B,8BAA8B;IAC9B,IAAI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CAAC,MAAM,EAAE,QAAQ,CAAC,GAAG,YAAY,CAAC,MAAM,CAAC,CAwK5E"}
|
package/dist/devtools/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { c as createStoreInstance, S as STORION_TYPE } from "../store-BroaE7p4.js";
|
|
2
|
+
import { e as emitter } from "../emitter-BA44OHdL.js";
|
|
2
3
|
let snapshotIdCounter = 0;
|
|
3
4
|
let eventIdCounter = 0;
|
|
4
5
|
const DEFAULT_MAX_EVENTS = 200;
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { g as getHooks, w as withHooks, s as scheduleNotification, e as emitter } from "./emitter-BA44OHdL.js";
|
|
2
|
+
function isPromiseLike(value) {
|
|
3
|
+
return typeof value === "object" && value !== null && "then" in value && typeof value.then === "function";
|
|
4
|
+
}
|
|
5
|
+
function createEffectContext(nth, onRefresh) {
|
|
6
|
+
let cleanupEmitter = null;
|
|
7
|
+
let abortController = null;
|
|
8
|
+
let isStale = false;
|
|
9
|
+
const runCleanups = () => {
|
|
10
|
+
if (isStale) return;
|
|
11
|
+
isStale = true;
|
|
12
|
+
if (abortController) {
|
|
13
|
+
abortController.abort();
|
|
14
|
+
abortController = null;
|
|
15
|
+
}
|
|
16
|
+
if (cleanupEmitter && cleanupEmitter.size > 0) {
|
|
17
|
+
cleanupEmitter.emitAndClearLifo();
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
function wrapPromise(promise) {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
promise.then(
|
|
23
|
+
(value) => {
|
|
24
|
+
if (!isStale) {
|
|
25
|
+
resolve(value);
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
(error) => {
|
|
29
|
+
if (!isStale) {
|
|
30
|
+
reject(error);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
const context = {
|
|
37
|
+
nth,
|
|
38
|
+
get signal() {
|
|
39
|
+
if (!abortController) {
|
|
40
|
+
abortController = new AbortController();
|
|
41
|
+
}
|
|
42
|
+
return abortController.signal;
|
|
43
|
+
},
|
|
44
|
+
onCleanup(listener) {
|
|
45
|
+
if (!cleanupEmitter) {
|
|
46
|
+
cleanupEmitter = emitter();
|
|
47
|
+
}
|
|
48
|
+
return cleanupEmitter.on(listener);
|
|
49
|
+
},
|
|
50
|
+
safe(promiseOrCallback) {
|
|
51
|
+
if (promiseOrCallback instanceof Promise) {
|
|
52
|
+
return wrapPromise(promiseOrCallback);
|
|
53
|
+
}
|
|
54
|
+
return (...args) => {
|
|
55
|
+
if (!isStale) {
|
|
56
|
+
const result = promiseOrCallback(
|
|
57
|
+
...args
|
|
58
|
+
);
|
|
59
|
+
if (isPromiseLike(result)) {
|
|
60
|
+
return wrapPromise(result);
|
|
61
|
+
}
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
return void 0;
|
|
65
|
+
};
|
|
66
|
+
},
|
|
67
|
+
refresh() {
|
|
68
|
+
if (!isStale) {
|
|
69
|
+
onRefresh();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
return Object.assign(context, { _runCleanups: runCleanups });
|
|
74
|
+
}
|
|
75
|
+
class CannotRefreshError extends Error {
|
|
76
|
+
constructor() {
|
|
77
|
+
super("Effect is already running, cannot refresh");
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function resolveErrorStrategy(effectOptions) {
|
|
81
|
+
return (effectOptions == null ? void 0 : effectOptions.onError) ?? "keepAlive";
|
|
82
|
+
}
|
|
83
|
+
function getRetryDelay(config, attempt) {
|
|
84
|
+
if (typeof config.delay === "function") {
|
|
85
|
+
return config.delay(attempt);
|
|
86
|
+
}
|
|
87
|
+
return config.delay ?? 100 * Math.pow(2, attempt);
|
|
88
|
+
}
|
|
89
|
+
function effect(fn, options) {
|
|
90
|
+
let currentContext = null;
|
|
91
|
+
let subscriptionEmitter = null;
|
|
92
|
+
let isStarted = false;
|
|
93
|
+
let isRunning = false;
|
|
94
|
+
let isDisposed = false;
|
|
95
|
+
let runGeneration = 0;
|
|
96
|
+
let retryCount = 0;
|
|
97
|
+
let retryTimeout = null;
|
|
98
|
+
let errorStrategy = "keepAlive";
|
|
99
|
+
let onErrorCallback = null;
|
|
100
|
+
let prevTrackedDeps = /* @__PURE__ */ new Map();
|
|
101
|
+
let prevSubscriptionEmitter = null;
|
|
102
|
+
let trackedDeps = /* @__PURE__ */ new Map();
|
|
103
|
+
const writtenProps = /* @__PURE__ */ new Set();
|
|
104
|
+
let newTrackedDeps = null;
|
|
105
|
+
const getSubscriptionEmitter = () => {
|
|
106
|
+
if (!subscriptionEmitter) {
|
|
107
|
+
subscriptionEmitter = emitter();
|
|
108
|
+
}
|
|
109
|
+
return subscriptionEmitter;
|
|
110
|
+
};
|
|
111
|
+
const clearSubscriptions = () => {
|
|
112
|
+
if (subscriptionEmitter && subscriptionEmitter.size > 0) {
|
|
113
|
+
subscriptionEmitter.emitAndClear();
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
const areDepsChanged = (prev, next) => {
|
|
117
|
+
if (prev.size !== next.size) return true;
|
|
118
|
+
for (const key of next.keys()) {
|
|
119
|
+
if (!prev.has(key)) return true;
|
|
120
|
+
}
|
|
121
|
+
return false;
|
|
122
|
+
};
|
|
123
|
+
const subscribeToTrackedDeps = () => {
|
|
124
|
+
for (const [key, dep] of trackedDeps) {
|
|
125
|
+
if (writtenProps.has(key)) continue;
|
|
126
|
+
const unsub = dep.subscribe(() => {
|
|
127
|
+
scheduleNotification(execute, fn);
|
|
128
|
+
});
|
|
129
|
+
getSubscriptionEmitter().on(unsub);
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
const handleError = (error) => {
|
|
133
|
+
onErrorCallback == null ? void 0 : onErrorCallback(error);
|
|
134
|
+
if (errorStrategy === "failFast") {
|
|
135
|
+
throw error;
|
|
136
|
+
}
|
|
137
|
+
if (errorStrategy === "keepAlive") {
|
|
138
|
+
console.error("Effect error (keepAlive):", error);
|
|
139
|
+
if (prevSubscriptionEmitter && prevSubscriptionEmitter.size > 0) {
|
|
140
|
+
trackedDeps = new Map(prevTrackedDeps);
|
|
141
|
+
subscriptionEmitter = prevSubscriptionEmitter;
|
|
142
|
+
prevSubscriptionEmitter = null;
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
if (newTrackedDeps && newTrackedDeps.size > 0) {
|
|
146
|
+
trackedDeps = newTrackedDeps;
|
|
147
|
+
}
|
|
148
|
+
subscribeToTrackedDeps();
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
if (typeof errorStrategy === "function") {
|
|
152
|
+
const retry = () => {
|
|
153
|
+
retryCount++;
|
|
154
|
+
execute();
|
|
155
|
+
};
|
|
156
|
+
errorStrategy({ error, retry, retryCount });
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
const retryConfig = errorStrategy;
|
|
160
|
+
if (retryCount < retryConfig.maxRetries) {
|
|
161
|
+
const delay = getRetryDelay(retryConfig, retryCount);
|
|
162
|
+
retryCount++;
|
|
163
|
+
retryTimeout = setTimeout(() => {
|
|
164
|
+
retryTimeout = null;
|
|
165
|
+
execute();
|
|
166
|
+
}, delay);
|
|
167
|
+
} else {
|
|
168
|
+
console.error(
|
|
169
|
+
`Effect failed after ${retryConfig.maxRetries} retries:`,
|
|
170
|
+
error
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
let cachedHooks = null;
|
|
175
|
+
const getTrackingHooks = (current) => {
|
|
176
|
+
if (!cachedHooks) {
|
|
177
|
+
cachedHooks = {
|
|
178
|
+
...current,
|
|
179
|
+
onRead: (event) => {
|
|
180
|
+
var _a;
|
|
181
|
+
(_a = current.onRead) == null ? void 0 : _a.call(current, event);
|
|
182
|
+
if (!newTrackedDeps.has(event.key)) {
|
|
183
|
+
newTrackedDeps.set(event.key, {
|
|
184
|
+
key: event.key,
|
|
185
|
+
value: event.value,
|
|
186
|
+
subscribe: event.subscribe
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
onWrite: (event) => {
|
|
191
|
+
var _a;
|
|
192
|
+
(_a = current.onWrite) == null ? void 0 : _a.call(current, event);
|
|
193
|
+
writtenProps.add(event.key);
|
|
194
|
+
},
|
|
195
|
+
scheduleNotification: current.scheduleNotification,
|
|
196
|
+
scheduleEffect: current.scheduleEffect
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
return cachedHooks;
|
|
200
|
+
};
|
|
201
|
+
const execute = () => {
|
|
202
|
+
if (isDisposed || isRunning) return;
|
|
203
|
+
isRunning = true;
|
|
204
|
+
let shouldRefresh = false;
|
|
205
|
+
const currentGeneration = ++runGeneration;
|
|
206
|
+
try {
|
|
207
|
+
currentContext == null ? void 0 : currentContext._runCleanups();
|
|
208
|
+
currentContext = null;
|
|
209
|
+
if (subscriptionEmitter && subscriptionEmitter.size > 0) {
|
|
210
|
+
prevTrackedDeps = new Map(trackedDeps);
|
|
211
|
+
prevSubscriptionEmitter = subscriptionEmitter;
|
|
212
|
+
subscriptionEmitter = null;
|
|
213
|
+
}
|
|
214
|
+
newTrackedDeps = /* @__PURE__ */ new Map();
|
|
215
|
+
writtenProps.clear();
|
|
216
|
+
let lazyContext = null;
|
|
217
|
+
if (fn.length) {
|
|
218
|
+
lazyContext = createEffectContext(currentGeneration, () => {
|
|
219
|
+
if (isRunning) {
|
|
220
|
+
throw new CannotRefreshError();
|
|
221
|
+
}
|
|
222
|
+
execute();
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
withHooks(getTrackingHooks, () => {
|
|
226
|
+
const result = fn(lazyContext);
|
|
227
|
+
if (result !== null && result !== void 0 && typeof result.then === "function") {
|
|
228
|
+
throw new Error(
|
|
229
|
+
"Effect function must be synchronous. Use ctx.safe(promise) for async operations instead of returning a Promise."
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
if (lazyContext && lazyContext.refresh === result) {
|
|
233
|
+
shouldRefresh = true;
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
currentContext = lazyContext;
|
|
237
|
+
retryCount = 0;
|
|
238
|
+
const depsChanged = areDepsChanged(trackedDeps, newTrackedDeps);
|
|
239
|
+
if (depsChanged) {
|
|
240
|
+
if (prevSubscriptionEmitter && prevSubscriptionEmitter.size > 0) {
|
|
241
|
+
prevSubscriptionEmitter.emitAndClear();
|
|
242
|
+
}
|
|
243
|
+
trackedDeps = newTrackedDeps;
|
|
244
|
+
subscribeToTrackedDeps();
|
|
245
|
+
} else {
|
|
246
|
+
if (prevSubscriptionEmitter) {
|
|
247
|
+
subscriptionEmitter = prevSubscriptionEmitter;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
prevTrackedDeps.clear();
|
|
251
|
+
prevSubscriptionEmitter = null;
|
|
252
|
+
} catch (error) {
|
|
253
|
+
if (error instanceof Error && error instanceof CannotRefreshError) {
|
|
254
|
+
throw error;
|
|
255
|
+
}
|
|
256
|
+
handleError(error);
|
|
257
|
+
} finally {
|
|
258
|
+
isRunning = false;
|
|
259
|
+
}
|
|
260
|
+
if (shouldRefresh) {
|
|
261
|
+
execute();
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
const dispose = () => {
|
|
265
|
+
if (isDisposed) return;
|
|
266
|
+
isDisposed = true;
|
|
267
|
+
runGeneration++;
|
|
268
|
+
if (retryTimeout) {
|
|
269
|
+
clearTimeout(retryTimeout);
|
|
270
|
+
retryTimeout = null;
|
|
271
|
+
}
|
|
272
|
+
currentContext == null ? void 0 : currentContext._runCleanups();
|
|
273
|
+
currentContext = null;
|
|
274
|
+
clearSubscriptions();
|
|
275
|
+
if (prevSubscriptionEmitter && prevSubscriptionEmitter.size > 0) {
|
|
276
|
+
prevSubscriptionEmitter.emitAndClear();
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
const start = (runOptions) => {
|
|
280
|
+
if (isStarted) return;
|
|
281
|
+
isStarted = true;
|
|
282
|
+
errorStrategy = resolveErrorStrategy(options);
|
|
283
|
+
onErrorCallback = (runOptions == null ? void 0 : runOptions.onError) ?? null;
|
|
284
|
+
execute();
|
|
285
|
+
};
|
|
286
|
+
getHooks().scheduleEffect((runOptions) => {
|
|
287
|
+
start(runOptions);
|
|
288
|
+
return dispose;
|
|
289
|
+
});
|
|
290
|
+
return dispose;
|
|
291
|
+
}
|
|
292
|
+
export {
|
|
293
|
+
effect as e
|
|
294
|
+
};
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
let globalHooks = {
|
|
2
|
+
scheduleNotification(notify) {
|
|
3
|
+
notify();
|
|
4
|
+
},
|
|
5
|
+
scheduleEffect(runEffect) {
|
|
6
|
+
runEffect();
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
function getHooks() {
|
|
10
|
+
return globalHooks;
|
|
11
|
+
}
|
|
12
|
+
function hasReadHook() {
|
|
13
|
+
return globalHooks.onRead !== void 0;
|
|
14
|
+
}
|
|
15
|
+
function hasWriteHook() {
|
|
16
|
+
return globalHooks.onWrite !== void 0;
|
|
17
|
+
}
|
|
18
|
+
function withHooks(hooksOrSetup, fn, onFinish) {
|
|
19
|
+
const prev = globalHooks;
|
|
20
|
+
if (typeof hooksOrSetup === "function") {
|
|
21
|
+
globalHooks = {
|
|
22
|
+
...globalHooks,
|
|
23
|
+
...hooksOrSetup(prev)
|
|
24
|
+
};
|
|
25
|
+
} else {
|
|
26
|
+
globalHooks = { ...prev, ...hooksOrSetup };
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
return fn();
|
|
30
|
+
} finally {
|
|
31
|
+
globalHooks = prev;
|
|
32
|
+
onFinish == null ? void 0 : onFinish();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function trackRead(storeId, prop, value, subscribe) {
|
|
36
|
+
var _a;
|
|
37
|
+
const key = `${storeId}.${prop}`;
|
|
38
|
+
(_a = globalHooks.onRead) == null ? void 0 : _a.call(globalHooks, { key, value, subscribe });
|
|
39
|
+
}
|
|
40
|
+
function trackWrite(storeId, prop, next, prev) {
|
|
41
|
+
var _a;
|
|
42
|
+
const key = `${storeId}.${prop}`;
|
|
43
|
+
(_a = globalHooks.onWrite) == null ? void 0 : _a.call(globalHooks, { key, next, prev });
|
|
44
|
+
}
|
|
45
|
+
function untrack(fn) {
|
|
46
|
+
return withHooks({ onRead: void 0, onWrite: void 0 }, fn);
|
|
47
|
+
}
|
|
48
|
+
function scheduleNotification(notify, key) {
|
|
49
|
+
globalHooks.scheduleNotification(notify, key);
|
|
50
|
+
}
|
|
51
|
+
function batch(fn) {
|
|
52
|
+
const pending = /* @__PURE__ */ new Map();
|
|
53
|
+
return withHooks(
|
|
54
|
+
(current) => ({
|
|
55
|
+
...current,
|
|
56
|
+
scheduleNotification: (notify, key) => {
|
|
57
|
+
const actualKey = key ?? notify;
|
|
58
|
+
pending.set(actualKey, notify);
|
|
59
|
+
}
|
|
60
|
+
}),
|
|
61
|
+
fn,
|
|
62
|
+
// Flush on finish
|
|
63
|
+
() => {
|
|
64
|
+
for (const notify of pending.values()) {
|
|
65
|
+
notify();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
function emitter(initialListeners) {
|
|
71
|
+
const listeners = /* @__PURE__ */ new Set([]);
|
|
72
|
+
let settledPayload;
|
|
73
|
+
let isSettled = false;
|
|
74
|
+
const noop = () => {
|
|
75
|
+
};
|
|
76
|
+
const doEmit = (payload, clear, lifo = false) => {
|
|
77
|
+
const copy = Array.from(listeners);
|
|
78
|
+
if (clear) {
|
|
79
|
+
listeners.clear();
|
|
80
|
+
}
|
|
81
|
+
const len = copy.length;
|
|
82
|
+
if (lifo) {
|
|
83
|
+
for (let i = len - 1; i >= 0; i--) {
|
|
84
|
+
copy[i](payload);
|
|
85
|
+
}
|
|
86
|
+
} else {
|
|
87
|
+
for (let i = 0; i < len; i++) {
|
|
88
|
+
copy[i](payload);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
return {
|
|
93
|
+
get size() {
|
|
94
|
+
return listeners.size;
|
|
95
|
+
},
|
|
96
|
+
get settled() {
|
|
97
|
+
return isSettled;
|
|
98
|
+
},
|
|
99
|
+
/**
|
|
100
|
+
* Adds one or more listeners to the emitter.
|
|
101
|
+
*
|
|
102
|
+
* The listener(s) will be called whenever `emit()` is called.
|
|
103
|
+
* Returns an unsubscribe function that removes the listener(s).
|
|
104
|
+
*
|
|
105
|
+
* **Important**: The unsubscribe function is idempotent - calling it multiple
|
|
106
|
+
* times is safe and won't cause errors. If the same listener is added multiple
|
|
107
|
+
* times, it will only be called once per emit (Set deduplication).
|
|
108
|
+
*
|
|
109
|
+
* **Settled behavior**: If the emitter is settled, listeners are called
|
|
110
|
+
* immediately with the settled payload and a no-op unsubscribe is returned.
|
|
111
|
+
*
|
|
112
|
+
* @param newListeners - Single listener or array of listeners to add
|
|
113
|
+
* @returns An unsubscribe function that removes the listener(s)
|
|
114
|
+
*/
|
|
115
|
+
on(...args) {
|
|
116
|
+
let newListeners = [];
|
|
117
|
+
if (args.length < 2) {
|
|
118
|
+
newListeners = Array.isArray(args[0]) ? args[0] : [args[0]];
|
|
119
|
+
} else {
|
|
120
|
+
const map = args[0];
|
|
121
|
+
const sourceListeners = Array.isArray(args[1]) ? args[1] : [args[1]];
|
|
122
|
+
newListeners = [
|
|
123
|
+
(value) => {
|
|
124
|
+
const mappedValue = map(value);
|
|
125
|
+
if (mappedValue) {
|
|
126
|
+
for (const listener of sourceListeners) {
|
|
127
|
+
listener(mappedValue.value);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
];
|
|
132
|
+
}
|
|
133
|
+
if (isSettled) {
|
|
134
|
+
for (const listener of newListeners) {
|
|
135
|
+
listener(settledPayload);
|
|
136
|
+
}
|
|
137
|
+
return noop;
|
|
138
|
+
}
|
|
139
|
+
for (const listener of newListeners) {
|
|
140
|
+
listeners.add(listener);
|
|
141
|
+
}
|
|
142
|
+
return () => {
|
|
143
|
+
for (const listener of newListeners) {
|
|
144
|
+
listeners.delete(listener);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
},
|
|
148
|
+
/**
|
|
149
|
+
* Emits an event to all registered listeners.
|
|
150
|
+
*
|
|
151
|
+
* **Important**: Creates a snapshot of listeners before iterating to ensure
|
|
152
|
+
* that modifications during emission (adding/removing listeners) don't affect
|
|
153
|
+
* the current emission cycle. This prevents:
|
|
154
|
+
* - New listeners added during emission from being called immediately
|
|
155
|
+
* - Issues with listeners that unsubscribe during emission
|
|
156
|
+
*
|
|
157
|
+
* Performance: For typical use cases (< 20 listeners), Array.from() overhead
|
|
158
|
+
* is negligible compared to calling the listener functions themselves.
|
|
159
|
+
*
|
|
160
|
+
* **Settled behavior**: After `settle()` is called, `emit()` becomes a no-op.
|
|
161
|
+
*
|
|
162
|
+
* @param payload - The value to pass to all listeners
|
|
163
|
+
*/
|
|
164
|
+
emit(payload) {
|
|
165
|
+
if (isSettled) return;
|
|
166
|
+
doEmit(payload, false);
|
|
167
|
+
},
|
|
168
|
+
emitLifo(payload) {
|
|
169
|
+
if (isSettled) return;
|
|
170
|
+
doEmit(payload, false, true);
|
|
171
|
+
},
|
|
172
|
+
/**
|
|
173
|
+
* Removes all registered listeners.
|
|
174
|
+
*
|
|
175
|
+
* After calling `clear()`, no listeners will be notified until new ones
|
|
176
|
+
* are added via `on()`.
|
|
177
|
+
*/
|
|
178
|
+
clear() {
|
|
179
|
+
listeners.clear();
|
|
180
|
+
},
|
|
181
|
+
/**
|
|
182
|
+
* Emits an event to all registered listeners and then clears all listeners.
|
|
183
|
+
*
|
|
184
|
+
* **Settled behavior**: After `settle()` is called, `emitAndClear()` becomes a no-op.
|
|
185
|
+
*
|
|
186
|
+
* @param payload - The value to pass to all listeners
|
|
187
|
+
*/
|
|
188
|
+
emitAndClear(payload) {
|
|
189
|
+
if (isSettled) return;
|
|
190
|
+
doEmit(payload, true);
|
|
191
|
+
},
|
|
192
|
+
emitAndClearLifo(payload) {
|
|
193
|
+
if (isSettled) return;
|
|
194
|
+
doEmit(payload, true, true);
|
|
195
|
+
},
|
|
196
|
+
/**
|
|
197
|
+
* Emit to all listeners, clear, and "settle" the emitter.
|
|
198
|
+
*
|
|
199
|
+
* After settling:
|
|
200
|
+
* - Any new `on()` call immediately invokes the listener with the settled payload
|
|
201
|
+
* - Returns a no-op unsubscribe function
|
|
202
|
+
* - `emit()` and `emitAndClear()` become no-ops
|
|
203
|
+
*
|
|
204
|
+
* **Important**: `isSettled` is set BEFORE emitting so that listeners
|
|
205
|
+
* added during emission see the settled state and get called immediately.
|
|
206
|
+
*
|
|
207
|
+
* @param payload - The final value to pass to all listeners
|
|
208
|
+
*/
|
|
209
|
+
settle(payload) {
|
|
210
|
+
if (isSettled) return;
|
|
211
|
+
settledPayload = payload;
|
|
212
|
+
isSettled = true;
|
|
213
|
+
doEmit(payload, true);
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
export {
|
|
218
|
+
hasWriteHook as a,
|
|
219
|
+
batch as b,
|
|
220
|
+
trackWrite as c,
|
|
221
|
+
emitter as e,
|
|
222
|
+
getHooks as g,
|
|
223
|
+
hasReadHook as h,
|
|
224
|
+
scheduleNotification as s,
|
|
225
|
+
trackRead as t,
|
|
226
|
+
untrack as u,
|
|
227
|
+
withHooks as w
|
|
228
|
+
};
|
package/dist/react/index.js
CHANGED
|
@@ -3,10 +3,13 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
|
|
|
3
3
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
4
|
import { memo, useMemo, createElement, createContext, useContext, useReducer, useRef, useEffect, useLayoutEffect, useState, forwardRef } from "react";
|
|
5
5
|
import { container } from "../storion.js";
|
|
6
|
-
import { applyExcept, applyFor, compose, createLoggingMiddleware, createResolver, createValidationMiddleware,
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
6
|
+
import { applyExcept, applyFor, compose, createLoggingMiddleware, createResolver, createValidationMiddleware, forStores, pick, trigger, when } from "../storion.js";
|
|
7
|
+
import { i as isSpec, S as STORION_TYPE, r as resolveEquality, s as store } from "../store-BroaE7p4.js";
|
|
8
|
+
import { n, l, g, a, h, d, f, k, e, j, b, p, m, o, u, w } from "../store-BroaE7p4.js";
|
|
9
|
+
import { w as withHooks } from "../emitter-BA44OHdL.js";
|
|
10
|
+
import { b as b2, u as u2 } from "../emitter-BA44OHdL.js";
|
|
9
11
|
import { jsx } from "react/jsx-runtime";
|
|
12
|
+
import { e as e2 } from "../effect-DPAYSuW3.js";
|
|
10
13
|
const StoreContext = createContext(null);
|
|
11
14
|
const StoreProvider = memo(
|
|
12
15
|
({ container: value, children }) => {
|
|
@@ -78,7 +81,7 @@ const useIsomorphicLayoutEffect = isServer ? useEffect : useLayoutEffect;
|
|
|
78
81
|
const shouldScheduleDispose = !isServer && typeof useLayoutEffect === "function" && dev();
|
|
79
82
|
function useLocalStore(spec) {
|
|
80
83
|
var _a;
|
|
81
|
-
const [, forceUpdate] = useReducer((
|
|
84
|
+
const [, forceUpdate] = useReducer((x) => x + 1, 0);
|
|
82
85
|
const prevControllerRef = useRef(null);
|
|
83
86
|
const controller = useMemo(() => new LocalStoreController(spec), [spec]);
|
|
84
87
|
if (prevControllerRef.current !== controller) {
|
|
@@ -188,7 +191,7 @@ class LocalStoreController {
|
|
|
188
191
|
}
|
|
189
192
|
}
|
|
190
193
|
function useStoreWithContainer(selector, container2) {
|
|
191
|
-
const [, forceUpdate] = useReducer((
|
|
194
|
+
const [, forceUpdate] = useReducer((x) => x + 1, 0);
|
|
192
195
|
const [refs] = useState(() => ({
|
|
193
196
|
fresh: void 0,
|
|
194
197
|
stableFns: /* @__PURE__ */ new Map(),
|
|
@@ -215,6 +218,10 @@ function useStoreWithContainer(selector, container2) {
|
|
|
215
218
|
actions: instance.actions
|
|
216
219
|
});
|
|
217
220
|
},
|
|
221
|
+
// Create a fresh instance from a parameterized factory (bypasses cache)
|
|
222
|
+
create(factory, ...args) {
|
|
223
|
+
return container2.create(factory, ...args);
|
|
224
|
+
},
|
|
218
225
|
mixin(mixin, ...args) {
|
|
219
226
|
return mixin(ctx, ...args);
|
|
220
227
|
},
|
|
@@ -382,38 +389,38 @@ export {
|
|
|
382
389
|
StoreProvider,
|
|
383
390
|
applyExcept,
|
|
384
391
|
applyFor,
|
|
385
|
-
|
|
392
|
+
b2 as batch,
|
|
386
393
|
compose,
|
|
387
394
|
container,
|
|
388
395
|
create,
|
|
389
396
|
createLoggingMiddleware,
|
|
390
397
|
createResolver,
|
|
391
398
|
createValidationMiddleware,
|
|
392
|
-
|
|
393
|
-
effect,
|
|
394
|
-
|
|
399
|
+
n as deepEqual,
|
|
400
|
+
e2 as effect,
|
|
401
|
+
l as equality,
|
|
395
402
|
forStores,
|
|
396
403
|
g as getKind,
|
|
397
404
|
a as is,
|
|
398
|
-
|
|
405
|
+
h as isAction,
|
|
399
406
|
d as isContainer,
|
|
400
|
-
|
|
401
|
-
|
|
407
|
+
f as isFocus,
|
|
408
|
+
k as isSelectorContext,
|
|
402
409
|
isSpec,
|
|
403
|
-
|
|
404
|
-
|
|
410
|
+
e as isStore,
|
|
411
|
+
j as isStoreContext,
|
|
405
412
|
b as isStorion,
|
|
406
|
-
|
|
413
|
+
p as isWrappedFn,
|
|
407
414
|
pick,
|
|
408
|
-
|
|
415
|
+
m as shallowEqual,
|
|
409
416
|
store,
|
|
410
|
-
|
|
417
|
+
o as strictEqual,
|
|
411
418
|
trigger,
|
|
412
|
-
|
|
413
|
-
|
|
419
|
+
u2 as untrack,
|
|
420
|
+
u as unwrapFn,
|
|
414
421
|
useContainer,
|
|
415
422
|
useStore,
|
|
416
423
|
when,
|
|
417
424
|
withStore,
|
|
418
|
-
|
|
425
|
+
w as wrapFn
|
|
419
426
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStore.d.ts","sourceRoot":"","sources":["../../src/react/useStore.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,EAEL,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,KAAK,SAAS,EACd,KAAK,cAAc,EAEnB,KAAK,QAAQ,EACb,KAAK,YAAY,EAClB,MAAM,UAAU,CAAC;AAGlB,OAAO,EAAiB,KAAK,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAuBvE;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,SAAS,MAAM,EACpD,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EACrB,SAAS,EAAE,cAAc,GACxB,YAAY,CAAC,CAAC,CAAC,
|
|
1
|
+
{"version":3,"file":"useStore.d.ts","sourceRoot":"","sources":["../../src/react/useStore.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,EAEL,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,KAAK,SAAS,EACd,KAAK,cAAc,EAEnB,KAAK,QAAQ,EACb,KAAK,YAAY,EAClB,MAAM,UAAU,CAAC;AAGlB,OAAO,EAAiB,KAAK,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAuBvE;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,SAAS,MAAM,EACpD,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EACrB,SAAS,EAAE,cAAc,GACxB,YAAY,CAAC,CAAC,CAAC,CAmJjB;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,QAAQ,CAAC,CAAC,SAAS,MAAM,EACvC,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,GACpB,YAAY,CAAC,CAAC,CAAC,CAAC;AACnB,wBAAgB,QAAQ,CACtB,MAAM,SAAS,SAAS,EACxB,QAAQ,SAAS,WAAW,EAC5B,IAAI,EAAE,SAAS,CAAC,MAAM,EAAE,QAAQ,CAAC,GAAG,gBAAgB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC"}
|