storion 0.11.1 → 0.11.2
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/async.d.ts +31 -0
- package/dist/async/async.d.ts.map +1 -1
- package/dist/async/index.js +8 -601
- package/dist/async/safe.d.ts +0 -12
- package/dist/async/safe.d.ts.map +1 -1
- package/dist/{effect-1beiYe_c.js → effect-BfoYEdFF.js} +686 -61
- package/dist/isPromiseLike-bFkfHAbm.js +6 -0
- package/dist/persist/index.js +1 -3
- package/dist/react/index.js +11 -11
- package/dist/storion.js +15 -15
- package/package.json +1 -1
|
@@ -2,6 +2,7 @@ var __defProp = Object.defineProperty;
|
|
|
2
2
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
3
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
4
|
import { e as emitter } from "./emitter-j4rC71vY.js";
|
|
5
|
+
import { i as isPromiseLike$1 } from "./isPromiseLike-bFkfHAbm.js";
|
|
5
6
|
const STORION_TYPE = Symbol("STORION");
|
|
6
7
|
function is$1(value, kind) {
|
|
7
8
|
return value !== null && (typeof value === "object" || typeof value === "function") && STORION_TYPE in value && value[STORION_TYPE] === kind;
|
|
@@ -3137,10 +3138,72 @@ function abortable(fn) {
|
|
|
3137
3138
|
});
|
|
3138
3139
|
return wrapper;
|
|
3139
3140
|
}
|
|
3140
|
-
|
|
3141
|
-
|
|
3141
|
+
const retryStrategy = {
|
|
3142
|
+
/** Exponential backoff: 1s, 2s, 4s, 8s... (max 30s) */
|
|
3143
|
+
backoff: (attempt) => Math.min(1e3 * 2 ** attempt, 3e4),
|
|
3144
|
+
/** Linear: 1s, 2s, 3s, 4s... (max 30s) */
|
|
3145
|
+
linear: (attempt) => Math.min(1e3 * (attempt + 1), 3e4),
|
|
3146
|
+
/** Fixed 1 second delay */
|
|
3147
|
+
fixed: () => 1e3,
|
|
3148
|
+
/** Fibonacci: 1s, 1s, 2s, 3s, 5s, 8s... (max 30s) */
|
|
3149
|
+
fibonacci: (attempt) => {
|
|
3150
|
+
const fib = [1, 1, 2, 3, 5, 8, 13, 21, 30];
|
|
3151
|
+
return Math.min(fib[attempt] ?? 30, 30) * 1e3;
|
|
3152
|
+
},
|
|
3153
|
+
/** Immediate retry (no delay) */
|
|
3154
|
+
immediate: () => 0,
|
|
3155
|
+
/** Add jitter (±30%) to any strategy */
|
|
3156
|
+
withJitter: (strategy) => (attempt) => {
|
|
3157
|
+
const base = strategy(attempt);
|
|
3158
|
+
const jitter = base * 0.3 * (Math.random() * 2 - 1);
|
|
3159
|
+
return Math.max(0, Math.round(base + jitter));
|
|
3160
|
+
}
|
|
3161
|
+
};
|
|
3162
|
+
class AsyncNotReadyError extends Error {
|
|
3163
|
+
constructor(message, status) {
|
|
3164
|
+
super(message);
|
|
3165
|
+
this.status = status;
|
|
3166
|
+
this.name = "AsyncNotReadyError";
|
|
3167
|
+
}
|
|
3168
|
+
}
|
|
3169
|
+
class AsyncAggregateError extends Error {
|
|
3170
|
+
constructor(message, errors2) {
|
|
3171
|
+
super(message);
|
|
3172
|
+
this.errors = errors2;
|
|
3173
|
+
this.name = "AsyncAggregateError";
|
|
3174
|
+
}
|
|
3175
|
+
}
|
|
3176
|
+
function createAsyncContext(abortController, isCancelledOrAborted, cancel, resolver) {
|
|
3177
|
+
const safe = createSafe(
|
|
3178
|
+
() => abortController.signal,
|
|
3179
|
+
isCancelledOrAborted
|
|
3180
|
+
);
|
|
3181
|
+
return {
|
|
3182
|
+
signal: abortController.signal,
|
|
3183
|
+
get(specOrFactory) {
|
|
3184
|
+
const instance = resolver.get(specOrFactory);
|
|
3185
|
+
if (isSpec(specOrFactory)) {
|
|
3186
|
+
const store2 = instance;
|
|
3187
|
+
return storeTuple(store2);
|
|
3188
|
+
}
|
|
3189
|
+
return instance;
|
|
3190
|
+
},
|
|
3191
|
+
safe,
|
|
3192
|
+
cancel
|
|
3193
|
+
};
|
|
3194
|
+
}
|
|
3195
|
+
const pendingPromises = /* @__PURE__ */ new WeakMap();
|
|
3196
|
+
const promiseStates = /* @__PURE__ */ new WeakMap();
|
|
3197
|
+
function getPendingPromise(state) {
|
|
3198
|
+
if (state.status === "pending" && "__key" in state && state.__key) {
|
|
3199
|
+
return pendingPromises.get(state.__key);
|
|
3200
|
+
}
|
|
3201
|
+
return void 0;
|
|
3142
3202
|
}
|
|
3143
3203
|
function toPromise(value) {
|
|
3204
|
+
if (isPromiseLike$1(value)) {
|
|
3205
|
+
return Promise.resolve(value);
|
|
3206
|
+
}
|
|
3144
3207
|
if (typeof value === "function") {
|
|
3145
3208
|
try {
|
|
3146
3209
|
const result = value();
|
|
@@ -3149,11 +3212,607 @@ function toPromise(value) {
|
|
|
3149
3212
|
return Promise.reject(e);
|
|
3150
3213
|
}
|
|
3151
3214
|
}
|
|
3152
|
-
if (isPromiseLike(value)) {
|
|
3153
|
-
return Promise.resolve(value);
|
|
3154
|
-
}
|
|
3155
3215
|
return Promise.resolve(value);
|
|
3156
3216
|
}
|
|
3217
|
+
function promiseTry(fn) {
|
|
3218
|
+
return new Promise((resolve) => {
|
|
3219
|
+
resolve(fn());
|
|
3220
|
+
});
|
|
3221
|
+
}
|
|
3222
|
+
function createCancellablePromise(promise, cancel) {
|
|
3223
|
+
const cancellable = promise;
|
|
3224
|
+
cancellable.cancel = cancel;
|
|
3225
|
+
return cancellable;
|
|
3226
|
+
}
|
|
3227
|
+
function stateToJSON() {
|
|
3228
|
+
if (this.mode === "stale") {
|
|
3229
|
+
return { status: "success", mode: "stale", data: this.data };
|
|
3230
|
+
}
|
|
3231
|
+
if (this.status === "success") {
|
|
3232
|
+
return { status: "success", mode: "fresh", data: this.data };
|
|
3233
|
+
}
|
|
3234
|
+
return null;
|
|
3235
|
+
}
|
|
3236
|
+
function wrapAbortable(fn) {
|
|
3237
|
+
return (ctx, ...args) => {
|
|
3238
|
+
return fn.withSignal(ctx.signal, ...args);
|
|
3239
|
+
};
|
|
3240
|
+
}
|
|
3241
|
+
function asyncWithFocus(focus, handler, options) {
|
|
3242
|
+
if (!focus._storeContext.isSetupPhase()) {
|
|
3243
|
+
throw new SetupPhaseError(
|
|
3244
|
+
"async.action",
|
|
3245
|
+
"async.action() must be called during store setup phase."
|
|
3246
|
+
);
|
|
3247
|
+
}
|
|
3248
|
+
const [getState, setState] = focus;
|
|
3249
|
+
const asyncKey = {};
|
|
3250
|
+
let lastCancel = null;
|
|
3251
|
+
let lastArgs = null;
|
|
3252
|
+
let invocationCount = 0;
|
|
3253
|
+
function dispatch(...args) {
|
|
3254
|
+
return untrack(() => {
|
|
3255
|
+
if (lastCancel && (options == null ? void 0 : options.autoCancel) !== false) {
|
|
3256
|
+
lastCancel();
|
|
3257
|
+
}
|
|
3258
|
+
const abortController = new AbortController();
|
|
3259
|
+
let isCancelled = false;
|
|
3260
|
+
const requestId = {};
|
|
3261
|
+
let rejectOnCancel = null;
|
|
3262
|
+
const cancelPromise = new Promise((_, reject) => {
|
|
3263
|
+
rejectOnCancel = reject;
|
|
3264
|
+
});
|
|
3265
|
+
const cancel2 = () => {
|
|
3266
|
+
if (!isCancelled) {
|
|
3267
|
+
isCancelled = true;
|
|
3268
|
+
abortController.abort();
|
|
3269
|
+
pendingPromises.delete(asyncKey);
|
|
3270
|
+
rejectOnCancel == null ? void 0 : rejectOnCancel(new DOMException("Aborted", "AbortError"));
|
|
3271
|
+
}
|
|
3272
|
+
};
|
|
3273
|
+
lastCancel = cancel2;
|
|
3274
|
+
lastArgs = args;
|
|
3275
|
+
invocationCount++;
|
|
3276
|
+
const prevState = getState();
|
|
3277
|
+
const mode = prevState.mode;
|
|
3278
|
+
const staleData = mode === "stale" ? prevState.data : prevState.status === "success" ? prevState.data : void 0;
|
|
3279
|
+
const autoCancel = (options == null ? void 0 : options.autoCancel) !== false;
|
|
3280
|
+
const isStateExternallyModified = () => {
|
|
3281
|
+
if (!autoCancel) {
|
|
3282
|
+
const currentState2 = getState();
|
|
3283
|
+
return currentState2.__requestId === void 0;
|
|
3284
|
+
}
|
|
3285
|
+
const currentState = getState();
|
|
3286
|
+
return currentState.__requestId !== requestId;
|
|
3287
|
+
};
|
|
3288
|
+
const execute = async () => {
|
|
3289
|
+
try {
|
|
3290
|
+
const isCancelledOrAborted = () => isCancelled || abortController.signal.aborted;
|
|
3291
|
+
const asyncContext = createAsyncContext(
|
|
3292
|
+
abortController,
|
|
3293
|
+
isCancelledOrAborted,
|
|
3294
|
+
cancel2,
|
|
3295
|
+
focus._resolver
|
|
3296
|
+
);
|
|
3297
|
+
const result = await promiseTry(() => handler(asyncContext, ...args));
|
|
3298
|
+
if (isCancelled) {
|
|
3299
|
+
throw new DOMException("Aborted", "AbortError");
|
|
3300
|
+
}
|
|
3301
|
+
if (isStateExternallyModified()) {
|
|
3302
|
+
return result;
|
|
3303
|
+
}
|
|
3304
|
+
setState({
|
|
3305
|
+
status: "success",
|
|
3306
|
+
mode,
|
|
3307
|
+
data: result,
|
|
3308
|
+
error: void 0,
|
|
3309
|
+
timestamp: Date.now(),
|
|
3310
|
+
__requestId: requestId,
|
|
3311
|
+
toJSON: stateToJSON
|
|
3312
|
+
});
|
|
3313
|
+
if (lastCancel === cancel2) {
|
|
3314
|
+
lastCancel = null;
|
|
3315
|
+
}
|
|
3316
|
+
return result;
|
|
3317
|
+
} catch (error) {
|
|
3318
|
+
if (isCancelled || abortController.signal.aborted) {
|
|
3319
|
+
throw error instanceof Error ? error : new DOMException("Aborted", "AbortError");
|
|
3320
|
+
}
|
|
3321
|
+
const errorObj = error instanceof Error ? error : new Error(String(error));
|
|
3322
|
+
if (!abortController.signal.aborted) {
|
|
3323
|
+
abortController.abort();
|
|
3324
|
+
}
|
|
3325
|
+
if (isStateExternallyModified()) {
|
|
3326
|
+
throw errorObj;
|
|
3327
|
+
}
|
|
3328
|
+
setState({
|
|
3329
|
+
status: "error",
|
|
3330
|
+
mode,
|
|
3331
|
+
data: mode === "stale" ? staleData : void 0,
|
|
3332
|
+
error: errorObj,
|
|
3333
|
+
timestamp: void 0,
|
|
3334
|
+
__requestId: requestId,
|
|
3335
|
+
toJSON: stateToJSON
|
|
3336
|
+
});
|
|
3337
|
+
if (lastCancel === cancel2) {
|
|
3338
|
+
lastCancel = null;
|
|
3339
|
+
}
|
|
3340
|
+
throw errorObj;
|
|
3341
|
+
}
|
|
3342
|
+
};
|
|
3343
|
+
const executionPromise = execute();
|
|
3344
|
+
const promise = Promise.race([executionPromise, cancelPromise]);
|
|
3345
|
+
pendingPromises.set(asyncKey, executionPromise);
|
|
3346
|
+
setState({
|
|
3347
|
+
status: "pending",
|
|
3348
|
+
mode,
|
|
3349
|
+
data: mode === "stale" ? staleData : void 0,
|
|
3350
|
+
error: void 0,
|
|
3351
|
+
timestamp: void 0,
|
|
3352
|
+
__key: asyncKey,
|
|
3353
|
+
__requestId: requestId,
|
|
3354
|
+
toJSON: stateToJSON
|
|
3355
|
+
});
|
|
3356
|
+
promise.then(
|
|
3357
|
+
() => pendingPromises.delete(asyncKey),
|
|
3358
|
+
() => pendingPromises.delete(asyncKey)
|
|
3359
|
+
);
|
|
3360
|
+
return createCancellablePromise(promise, cancel2);
|
|
3361
|
+
});
|
|
3362
|
+
}
|
|
3363
|
+
function refresh() {
|
|
3364
|
+
if (lastArgs === null) {
|
|
3365
|
+
return void 0;
|
|
3366
|
+
}
|
|
3367
|
+
return dispatch(...lastArgs);
|
|
3368
|
+
}
|
|
3369
|
+
function cancel() {
|
|
3370
|
+
if (lastCancel) {
|
|
3371
|
+
lastCancel();
|
|
3372
|
+
lastCancel = null;
|
|
3373
|
+
}
|
|
3374
|
+
}
|
|
3375
|
+
function reset() {
|
|
3376
|
+
cancel();
|
|
3377
|
+
lastArgs = null;
|
|
3378
|
+
const currentState = getState();
|
|
3379
|
+
const mode = currentState.mode;
|
|
3380
|
+
const resetRequestId = {};
|
|
3381
|
+
if (mode === "stale") {
|
|
3382
|
+
setState({
|
|
3383
|
+
status: "idle",
|
|
3384
|
+
mode: "stale",
|
|
3385
|
+
data: currentState.data,
|
|
3386
|
+
error: void 0,
|
|
3387
|
+
timestamp: void 0,
|
|
3388
|
+
__requestId: resetRequestId,
|
|
3389
|
+
toJSON: stateToJSON
|
|
3390
|
+
});
|
|
3391
|
+
} else {
|
|
3392
|
+
setState({
|
|
3393
|
+
status: "idle",
|
|
3394
|
+
mode: "fresh",
|
|
3395
|
+
data: void 0,
|
|
3396
|
+
error: void 0,
|
|
3397
|
+
timestamp: void 0,
|
|
3398
|
+
__requestId: resetRequestId,
|
|
3399
|
+
toJSON: stateToJSON
|
|
3400
|
+
});
|
|
3401
|
+
}
|
|
3402
|
+
}
|
|
3403
|
+
function last() {
|
|
3404
|
+
const hasDispatched = !!lastArgs;
|
|
3405
|
+
if (!hasDispatched) {
|
|
3406
|
+
return void 0;
|
|
3407
|
+
}
|
|
3408
|
+
return {
|
|
3409
|
+
args: lastArgs,
|
|
3410
|
+
nth: invocationCount,
|
|
3411
|
+
// Read state to trigger reactivity (via focus getter) - this is the only tracked read
|
|
3412
|
+
state: getState()
|
|
3413
|
+
};
|
|
3414
|
+
}
|
|
3415
|
+
if (options == null ? void 0 : options.autoCancel) {
|
|
3416
|
+
focus._storeContext.onDispose(() => {
|
|
3417
|
+
cancel();
|
|
3418
|
+
});
|
|
3419
|
+
}
|
|
3420
|
+
return {
|
|
3421
|
+
dispatch,
|
|
3422
|
+
refresh,
|
|
3423
|
+
cancel,
|
|
3424
|
+
reset,
|
|
3425
|
+
last
|
|
3426
|
+
};
|
|
3427
|
+
}
|
|
3428
|
+
function asyncState(mode, status, dataOrError, errorOrExtra, extra) {
|
|
3429
|
+
let state;
|
|
3430
|
+
if (mode === "fresh") {
|
|
3431
|
+
switch (status) {
|
|
3432
|
+
case "idle":
|
|
3433
|
+
state = {
|
|
3434
|
+
status: "idle",
|
|
3435
|
+
mode: "fresh",
|
|
3436
|
+
data: void 0,
|
|
3437
|
+
error: void 0,
|
|
3438
|
+
timestamp: void 0,
|
|
3439
|
+
toJSON: stateToJSON
|
|
3440
|
+
};
|
|
3441
|
+
break;
|
|
3442
|
+
case "pending":
|
|
3443
|
+
state = {
|
|
3444
|
+
status: "pending",
|
|
3445
|
+
mode: "fresh",
|
|
3446
|
+
data: void 0,
|
|
3447
|
+
error: void 0,
|
|
3448
|
+
timestamp: void 0,
|
|
3449
|
+
...dataOrError,
|
|
3450
|
+
toJSON: stateToJSON
|
|
3451
|
+
};
|
|
3452
|
+
break;
|
|
3453
|
+
case "success":
|
|
3454
|
+
state = {
|
|
3455
|
+
status: "success",
|
|
3456
|
+
mode: "fresh",
|
|
3457
|
+
data: dataOrError,
|
|
3458
|
+
error: void 0,
|
|
3459
|
+
timestamp: Date.now(),
|
|
3460
|
+
toJSON: stateToJSON
|
|
3461
|
+
};
|
|
3462
|
+
break;
|
|
3463
|
+
case "error":
|
|
3464
|
+
state = {
|
|
3465
|
+
status: "error",
|
|
3466
|
+
mode: "fresh",
|
|
3467
|
+
data: void 0,
|
|
3468
|
+
error: dataOrError,
|
|
3469
|
+
timestamp: void 0,
|
|
3470
|
+
...errorOrExtra,
|
|
3471
|
+
toJSON: stateToJSON
|
|
3472
|
+
};
|
|
3473
|
+
break;
|
|
3474
|
+
}
|
|
3475
|
+
} else {
|
|
3476
|
+
switch (status) {
|
|
3477
|
+
case "idle":
|
|
3478
|
+
state = {
|
|
3479
|
+
status: "idle",
|
|
3480
|
+
mode: "stale",
|
|
3481
|
+
data: dataOrError,
|
|
3482
|
+
error: void 0,
|
|
3483
|
+
timestamp: void 0,
|
|
3484
|
+
toJSON: stateToJSON
|
|
3485
|
+
};
|
|
3486
|
+
break;
|
|
3487
|
+
case "pending":
|
|
3488
|
+
state = {
|
|
3489
|
+
status: "pending",
|
|
3490
|
+
mode: "stale",
|
|
3491
|
+
data: dataOrError,
|
|
3492
|
+
error: void 0,
|
|
3493
|
+
timestamp: void 0,
|
|
3494
|
+
...errorOrExtra,
|
|
3495
|
+
toJSON: stateToJSON
|
|
3496
|
+
};
|
|
3497
|
+
break;
|
|
3498
|
+
case "success":
|
|
3499
|
+
state = {
|
|
3500
|
+
status: "success",
|
|
3501
|
+
mode: "stale",
|
|
3502
|
+
data: dataOrError,
|
|
3503
|
+
error: void 0,
|
|
3504
|
+
timestamp: Date.now(),
|
|
3505
|
+
toJSON: stateToJSON
|
|
3506
|
+
};
|
|
3507
|
+
break;
|
|
3508
|
+
case "error":
|
|
3509
|
+
state = {
|
|
3510
|
+
status: "error",
|
|
3511
|
+
mode: "stale",
|
|
3512
|
+
data: dataOrError,
|
|
3513
|
+
error: errorOrExtra,
|
|
3514
|
+
timestamp: void 0,
|
|
3515
|
+
...extra,
|
|
3516
|
+
toJSON: stateToJSON
|
|
3517
|
+
};
|
|
3518
|
+
break;
|
|
3519
|
+
}
|
|
3520
|
+
}
|
|
3521
|
+
return Object.freeze(state);
|
|
3522
|
+
}
|
|
3523
|
+
function asyncStateFrom(prev, status, dataOrError) {
|
|
3524
|
+
const mode = prev.mode;
|
|
3525
|
+
const staleData = mode === "stale" ? prev.data : void 0;
|
|
3526
|
+
if (mode === "stale") {
|
|
3527
|
+
switch (status) {
|
|
3528
|
+
case "idle":
|
|
3529
|
+
return asyncState("stale", "idle", staleData);
|
|
3530
|
+
case "pending":
|
|
3531
|
+
return asyncState("stale", "pending", staleData);
|
|
3532
|
+
case "success":
|
|
3533
|
+
return asyncState("stale", "success", dataOrError);
|
|
3534
|
+
case "error":
|
|
3535
|
+
return asyncState(
|
|
3536
|
+
"stale",
|
|
3537
|
+
"error",
|
|
3538
|
+
staleData,
|
|
3539
|
+
dataOrError
|
|
3540
|
+
);
|
|
3541
|
+
}
|
|
3542
|
+
} else {
|
|
3543
|
+
switch (status) {
|
|
3544
|
+
case "idle":
|
|
3545
|
+
return asyncState("fresh", "idle");
|
|
3546
|
+
case "pending":
|
|
3547
|
+
return asyncState("fresh", "pending");
|
|
3548
|
+
case "success":
|
|
3549
|
+
return asyncState("fresh", "success", dataOrError);
|
|
3550
|
+
case "error":
|
|
3551
|
+
return asyncState("fresh", "error", dataOrError);
|
|
3552
|
+
}
|
|
3553
|
+
}
|
|
3554
|
+
}
|
|
3555
|
+
asyncState.from = asyncStateFrom;
|
|
3556
|
+
var async;
|
|
3557
|
+
((async2) => {
|
|
3558
|
+
function fresh() {
|
|
3559
|
+
return asyncState("fresh", "idle");
|
|
3560
|
+
}
|
|
3561
|
+
async2.fresh = fresh;
|
|
3562
|
+
function stale(initialData) {
|
|
3563
|
+
return asyncState("stale", "idle", initialData);
|
|
3564
|
+
}
|
|
3565
|
+
async2.stale = stale;
|
|
3566
|
+
function action(focus, handlerOrAbortable, options) {
|
|
3567
|
+
const handler = isAbortable(handlerOrAbortable) ? wrapAbortable(handlerOrAbortable) : handlerOrAbortable;
|
|
3568
|
+
return asyncWithFocus(focus, handler, options);
|
|
3569
|
+
}
|
|
3570
|
+
async2.action = action;
|
|
3571
|
+
function mixin(handlerOrAbortable, options) {
|
|
3572
|
+
const handler = isAbortable(handlerOrAbortable) ? wrapAbortable(handlerOrAbortable) : handlerOrAbortable;
|
|
3573
|
+
const initialState = (options == null ? void 0 : options.initial) ?? asyncState("fresh", "idle");
|
|
3574
|
+
const asyncSpec = store({
|
|
3575
|
+
name: (options == null ? void 0 : options.name) ?? `async:${handler.name || "anonymous"}`,
|
|
3576
|
+
state: { result: initialState },
|
|
3577
|
+
meta: options == null ? void 0 : options.meta,
|
|
3578
|
+
setup(storeContext) {
|
|
3579
|
+
const { focus } = storeContext;
|
|
3580
|
+
const actions = asyncWithFocus(
|
|
3581
|
+
focus("result"),
|
|
3582
|
+
(asyncContext, ...args) => {
|
|
3583
|
+
return handler(asyncContext, ...args);
|
|
3584
|
+
},
|
|
3585
|
+
options
|
|
3586
|
+
);
|
|
3587
|
+
return actions;
|
|
3588
|
+
}
|
|
3589
|
+
});
|
|
3590
|
+
return (context) => {
|
|
3591
|
+
const [state2, actions] = context.scoped(asyncSpec);
|
|
3592
|
+
return [state2.result, actions];
|
|
3593
|
+
};
|
|
3594
|
+
}
|
|
3595
|
+
async2.mixin = mixin;
|
|
3596
|
+
function delay(ms, resolved) {
|
|
3597
|
+
let timeout;
|
|
3598
|
+
return createCancellablePromise(
|
|
3599
|
+
new Promise((resolve) => {
|
|
3600
|
+
timeout = setTimeout(resolve, ms, resolved);
|
|
3601
|
+
}),
|
|
3602
|
+
() => {
|
|
3603
|
+
clearTimeout(timeout);
|
|
3604
|
+
}
|
|
3605
|
+
);
|
|
3606
|
+
}
|
|
3607
|
+
async2.delay = delay;
|
|
3608
|
+
async2.invoke = promiseTry;
|
|
3609
|
+
function wait(state2) {
|
|
3610
|
+
if (state2.status === "success") {
|
|
3611
|
+
return state2.data;
|
|
3612
|
+
}
|
|
3613
|
+
if (state2.mode === "stale" && state2.data !== void 0) {
|
|
3614
|
+
return state2.data;
|
|
3615
|
+
}
|
|
3616
|
+
if (state2.status === "error") {
|
|
3617
|
+
throw state2.error;
|
|
3618
|
+
}
|
|
3619
|
+
if (state2.status === "pending") {
|
|
3620
|
+
const promise = getPendingPromise(state2);
|
|
3621
|
+
if (promise) {
|
|
3622
|
+
throw promise;
|
|
3623
|
+
}
|
|
3624
|
+
}
|
|
3625
|
+
const message = state2.status === "idle" ? `Cannot wait: state is idle. Call dispatch() or use trigger() to start the async operation before calling async.wait().` : `Cannot wait: state is ${state2.status}`;
|
|
3626
|
+
throw new AsyncNotReadyError(message, state2.status);
|
|
3627
|
+
}
|
|
3628
|
+
async2.wait = wait;
|
|
3629
|
+
function race(states) {
|
|
3630
|
+
for (const key in states) {
|
|
3631
|
+
if (Object.prototype.hasOwnProperty.call(states, key)) {
|
|
3632
|
+
const state2 = states[key];
|
|
3633
|
+
if (state2.status === "success") {
|
|
3634
|
+
return [key, state2.data];
|
|
3635
|
+
}
|
|
3636
|
+
}
|
|
3637
|
+
}
|
|
3638
|
+
for (const key in states) {
|
|
3639
|
+
if (Object.prototype.hasOwnProperty.call(states, key)) {
|
|
3640
|
+
const state2 = states[key];
|
|
3641
|
+
if (state2.mode === "stale" && state2.data !== void 0) {
|
|
3642
|
+
return [key, state2.data];
|
|
3643
|
+
}
|
|
3644
|
+
}
|
|
3645
|
+
}
|
|
3646
|
+
for (const key in states) {
|
|
3647
|
+
if (Object.prototype.hasOwnProperty.call(states, key)) {
|
|
3648
|
+
const state2 = states[key];
|
|
3649
|
+
if (state2.status === "error") {
|
|
3650
|
+
throw state2.error;
|
|
3651
|
+
}
|
|
3652
|
+
}
|
|
3653
|
+
}
|
|
3654
|
+
throw new AsyncNotReadyError(
|
|
3655
|
+
"No async state has resolved successfully",
|
|
3656
|
+
"pending"
|
|
3657
|
+
);
|
|
3658
|
+
}
|
|
3659
|
+
async2.race = race;
|
|
3660
|
+
function withState(promise) {
|
|
3661
|
+
const s = state(promise);
|
|
3662
|
+
return Object.assign(promise, { state: s });
|
|
3663
|
+
}
|
|
3664
|
+
async2.withState = withState;
|
|
3665
|
+
function state(promise) {
|
|
3666
|
+
const state2 = promiseStates.get(promise);
|
|
3667
|
+
if (state2) {
|
|
3668
|
+
return state2;
|
|
3669
|
+
}
|
|
3670
|
+
const newState = {
|
|
3671
|
+
status: "pending",
|
|
3672
|
+
resolved: void 0,
|
|
3673
|
+
rejected: void 0
|
|
3674
|
+
};
|
|
3675
|
+
promise.then(
|
|
3676
|
+
(value) => {
|
|
3677
|
+
newState.status = "fulfilled";
|
|
3678
|
+
newState.resolved = value;
|
|
3679
|
+
},
|
|
3680
|
+
(error) => {
|
|
3681
|
+
newState.status = "rejected";
|
|
3682
|
+
newState.rejected = error;
|
|
3683
|
+
}
|
|
3684
|
+
);
|
|
3685
|
+
promiseStates.set(promise, newState);
|
|
3686
|
+
return newState;
|
|
3687
|
+
}
|
|
3688
|
+
async2.state = state;
|
|
3689
|
+
function all(...states) {
|
|
3690
|
+
const results = [];
|
|
3691
|
+
for (let i = 0; i < states.length; i++) {
|
|
3692
|
+
const state2 = states[i];
|
|
3693
|
+
if (state2.status === "success") {
|
|
3694
|
+
results.push(state2.data);
|
|
3695
|
+
continue;
|
|
3696
|
+
}
|
|
3697
|
+
if (state2.mode === "stale" && state2.data !== void 0) {
|
|
3698
|
+
results.push(state2.data);
|
|
3699
|
+
continue;
|
|
3700
|
+
}
|
|
3701
|
+
if (state2.status === "error") {
|
|
3702
|
+
throw state2.error;
|
|
3703
|
+
}
|
|
3704
|
+
throw new AsyncNotReadyError(
|
|
3705
|
+
`State at index ${i} is ${state2.status}`,
|
|
3706
|
+
state2.status
|
|
3707
|
+
);
|
|
3708
|
+
}
|
|
3709
|
+
return results;
|
|
3710
|
+
}
|
|
3711
|
+
async2.all = all;
|
|
3712
|
+
function any(...states) {
|
|
3713
|
+
const errors2 = [];
|
|
3714
|
+
for (const state2 of states) {
|
|
3715
|
+
if (state2.status === "success") {
|
|
3716
|
+
return state2.data;
|
|
3717
|
+
}
|
|
3718
|
+
}
|
|
3719
|
+
for (const state2 of states) {
|
|
3720
|
+
if (state2.mode === "stale" && state2.data !== void 0) {
|
|
3721
|
+
return state2.data;
|
|
3722
|
+
}
|
|
3723
|
+
if (state2.status === "error") {
|
|
3724
|
+
errors2.push(state2.error);
|
|
3725
|
+
}
|
|
3726
|
+
}
|
|
3727
|
+
if (errors2.length === states.length) {
|
|
3728
|
+
throw new AsyncAggregateError("All async states have errors", errors2);
|
|
3729
|
+
}
|
|
3730
|
+
throw new AsyncNotReadyError(
|
|
3731
|
+
"No async state has resolved successfully",
|
|
3732
|
+
"pending"
|
|
3733
|
+
);
|
|
3734
|
+
}
|
|
3735
|
+
async2.any = any;
|
|
3736
|
+
function settled(...states) {
|
|
3737
|
+
const results = [];
|
|
3738
|
+
for (const state2 of states) {
|
|
3739
|
+
switch (state2.status) {
|
|
3740
|
+
case "success":
|
|
3741
|
+
results.push({ status: "success", data: state2.data });
|
|
3742
|
+
break;
|
|
3743
|
+
case "error":
|
|
3744
|
+
results.push({
|
|
3745
|
+
status: "error",
|
|
3746
|
+
error: state2.error,
|
|
3747
|
+
data: state2.mode === "stale" ? state2.data : void 0
|
|
3748
|
+
});
|
|
3749
|
+
break;
|
|
3750
|
+
case "pending":
|
|
3751
|
+
results.push({
|
|
3752
|
+
status: "pending",
|
|
3753
|
+
data: state2.mode === "stale" ? state2.data : void 0
|
|
3754
|
+
});
|
|
3755
|
+
break;
|
|
3756
|
+
case "idle":
|
|
3757
|
+
results.push({
|
|
3758
|
+
status: "idle",
|
|
3759
|
+
data: state2.mode === "stale" ? state2.data : void 0
|
|
3760
|
+
});
|
|
3761
|
+
break;
|
|
3762
|
+
}
|
|
3763
|
+
}
|
|
3764
|
+
return results;
|
|
3765
|
+
}
|
|
3766
|
+
async2.settled = settled;
|
|
3767
|
+
function hasData(state2) {
|
|
3768
|
+
if (state2.status === "success") return true;
|
|
3769
|
+
if (state2.mode === "stale" && state2.data !== void 0) return true;
|
|
3770
|
+
return false;
|
|
3771
|
+
}
|
|
3772
|
+
async2.hasData = hasData;
|
|
3773
|
+
function isLoading(state2) {
|
|
3774
|
+
return state2.status === "pending";
|
|
3775
|
+
}
|
|
3776
|
+
async2.isLoading = isLoading;
|
|
3777
|
+
function isError(state2) {
|
|
3778
|
+
return state2.status === "error";
|
|
3779
|
+
}
|
|
3780
|
+
async2.isError = isError;
|
|
3781
|
+
function derive(focus, computeFn) {
|
|
3782
|
+
const [getState, setState] = focus;
|
|
3783
|
+
let hasSetPending = false;
|
|
3784
|
+
return effect((ctx) => {
|
|
3785
|
+
const currentState = untrack(getState);
|
|
3786
|
+
try {
|
|
3787
|
+
const result = computeFn();
|
|
3788
|
+
if (result !== null && result !== void 0 && typeof result.then === "function") {
|
|
3789
|
+
throw new AsyncFunctionError(
|
|
3790
|
+
"async.derive computeFn",
|
|
3791
|
+
"Use async.wait() for async values, not async/await or returning promises."
|
|
3792
|
+
);
|
|
3793
|
+
}
|
|
3794
|
+
hasSetPending = false;
|
|
3795
|
+
setState(asyncState.from(currentState, "success", result));
|
|
3796
|
+
} catch (ex) {
|
|
3797
|
+
if (ex !== null && ex !== void 0 && typeof ex.then === "function") {
|
|
3798
|
+
if (!hasSetPending) {
|
|
3799
|
+
hasSetPending = true;
|
|
3800
|
+
setState(asyncState.from(currentState, "pending"));
|
|
3801
|
+
}
|
|
3802
|
+
ctx.safe(ex).then(ctx.refresh, ctx.refresh);
|
|
3803
|
+
} else {
|
|
3804
|
+
hasSetPending = false;
|
|
3805
|
+
const error = ex instanceof Error ? ex : new Error(String(ex));
|
|
3806
|
+
setState(asyncState.from(currentState, "error", error));
|
|
3807
|
+
}
|
|
3808
|
+
}
|
|
3809
|
+
});
|
|
3810
|
+
}
|
|
3811
|
+
async2.derive = derive;
|
|
3812
|
+
})(async || (async = {}));
|
|
3813
|
+
function isPromiseLike(value) {
|
|
3814
|
+
return value !== null && typeof value === "object" && "then" in value && typeof value.then === "function";
|
|
3815
|
+
}
|
|
3157
3816
|
class AggregateErrorImpl extends Error {
|
|
3158
3817
|
constructor(errors2, message) {
|
|
3159
3818
|
super(message);
|
|
@@ -3317,41 +3976,6 @@ function createSafe(getSignal, isCancelled) {
|
|
|
3317
3976
|
delay
|
|
3318
3977
|
});
|
|
3319
3978
|
}
|
|
3320
|
-
const retryStrategy = {
|
|
3321
|
-
/** Exponential backoff: 1s, 2s, 4s, 8s... (max 30s) */
|
|
3322
|
-
backoff: (attempt) => Math.min(1e3 * 2 ** attempt, 3e4),
|
|
3323
|
-
/** Linear: 1s, 2s, 3s, 4s... (max 30s) */
|
|
3324
|
-
linear: (attempt) => Math.min(1e3 * (attempt + 1), 3e4),
|
|
3325
|
-
/** Fixed 1 second delay */
|
|
3326
|
-
fixed: () => 1e3,
|
|
3327
|
-
/** Fibonacci: 1s, 1s, 2s, 3s, 5s, 8s... (max 30s) */
|
|
3328
|
-
fibonacci: (attempt) => {
|
|
3329
|
-
const fib = [1, 1, 2, 3, 5, 8, 13, 21, 30];
|
|
3330
|
-
return Math.min(fib[attempt] ?? 30, 30) * 1e3;
|
|
3331
|
-
},
|
|
3332
|
-
/** Immediate retry (no delay) */
|
|
3333
|
-
immediate: () => 0,
|
|
3334
|
-
/** Add jitter (±30%) to any strategy */
|
|
3335
|
-
withJitter: (strategy) => (attempt) => {
|
|
3336
|
-
const base = strategy(attempt);
|
|
3337
|
-
const jitter = base * 0.3 * (Math.random() * 2 - 1);
|
|
3338
|
-
return Math.max(0, Math.round(base + jitter));
|
|
3339
|
-
}
|
|
3340
|
-
};
|
|
3341
|
-
class AsyncNotReadyError extends Error {
|
|
3342
|
-
constructor(message, status) {
|
|
3343
|
-
super(message);
|
|
3344
|
-
this.status = status;
|
|
3345
|
-
this.name = "AsyncNotReadyError";
|
|
3346
|
-
}
|
|
3347
|
-
}
|
|
3348
|
-
class AsyncAggregateError extends Error {
|
|
3349
|
-
constructor(message, errors2) {
|
|
3350
|
-
super(message);
|
|
3351
|
-
this.errors = errors2;
|
|
3352
|
-
this.name = "AsyncAggregateError";
|
|
3353
|
-
}
|
|
3354
|
-
}
|
|
3355
3979
|
function createEffectContext(nth, onRefresh) {
|
|
3356
3980
|
let cleanupEmitter = null;
|
|
3357
3981
|
let abortController = null;
|
|
@@ -3613,40 +4237,41 @@ function effect(fn, options) {
|
|
|
3613
4237
|
}
|
|
3614
4238
|
export {
|
|
3615
4239
|
AsyncNotReadyError as A,
|
|
3616
|
-
|
|
3617
|
-
|
|
3618
|
-
|
|
4240
|
+
StoreDisposedError as B,
|
|
4241
|
+
LocalStoreDependencyError as C,
|
|
4242
|
+
pool as D,
|
|
3619
4243
|
EffectRefreshError as E,
|
|
3620
|
-
|
|
4244
|
+
tryDispose as F,
|
|
4245
|
+
unwrapFn as G,
|
|
3621
4246
|
HooksContextError as H,
|
|
3622
4247
|
InvalidActionError as I,
|
|
3623
4248
|
LifetimeMismatchError as L,
|
|
3624
4249
|
ProviderMissingError as P,
|
|
3625
|
-
|
|
3626
|
-
|
|
3627
|
-
|
|
4250
|
+
ScopedOutsideSelectorError as S,
|
|
4251
|
+
async as a,
|
|
4252
|
+
abortable as b,
|
|
3628
4253
|
createSafe as c,
|
|
3629
|
-
|
|
3630
|
-
|
|
3631
|
-
|
|
3632
|
-
|
|
3633
|
-
|
|
3634
|
-
|
|
3635
|
-
|
|
3636
|
-
|
|
3637
|
-
|
|
3638
|
-
|
|
3639
|
-
|
|
3640
|
-
|
|
4254
|
+
AsyncAggregateError as d,
|
|
4255
|
+
AsyncFunctionError as e,
|
|
4256
|
+
storeTuple as f,
|
|
4257
|
+
isSpec as g,
|
|
4258
|
+
STORION_TYPE as h,
|
|
4259
|
+
isAbortable as i,
|
|
4260
|
+
dev as j,
|
|
4261
|
+
resolveEquality as k,
|
|
4262
|
+
store as l,
|
|
4263
|
+
is$1 as m,
|
|
4264
|
+
batch as n,
|
|
4265
|
+
effect as o,
|
|
3641
4266
|
pick as p,
|
|
3642
4267
|
equality as q,
|
|
3643
4268
|
retryStrategy as r,
|
|
3644
|
-
|
|
4269
|
+
strictEqual as s,
|
|
3645
4270
|
tryStabilize as t,
|
|
3646
4271
|
untrack as u,
|
|
3647
4272
|
shallowEqual as v,
|
|
3648
4273
|
withHooks as w,
|
|
3649
4274
|
deepEqual as x,
|
|
3650
4275
|
StorionError as y,
|
|
3651
|
-
|
|
4276
|
+
SetupPhaseError as z
|
|
3652
4277
|
};
|