@effuse/store 1.0.4 → 1.0.6
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/index.cjs +82 -98
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +39 -11
- package/dist/index.d.ts +39 -11
- package/dist/index.js +83 -96
- package/dist/index.js.map +1 -1
- package/package.json +9 -8
- package/src/__tests__/actions/useConcurrency.test.ts +275 -0
- package/src/actions/async.ts +1 -128
- package/src/actions/index.ts +7 -4
- package/src/actions/useConcurrency.ts +137 -0
- package/src/index.ts +3 -4
- package/src/reactivity/derived.ts +1 -1
package/dist/index.cjs
CHANGED
|
@@ -833,99 +833,6 @@ var withRetry = (fn, config) => {
|
|
|
833
833
|
return effect.Effect.runPromise(effect$1);
|
|
834
834
|
};
|
|
835
835
|
};
|
|
836
|
-
var takeLatest = (fn) => {
|
|
837
|
-
let pending = false;
|
|
838
|
-
let currentToken = createCancellationToken();
|
|
839
|
-
let callId = 0;
|
|
840
|
-
const action = async (...args) => {
|
|
841
|
-
currentToken.cancel();
|
|
842
|
-
currentToken = createCancellationToken();
|
|
843
|
-
const myToken = currentToken;
|
|
844
|
-
const myCallId = ++callId;
|
|
845
|
-
pending = true;
|
|
846
|
-
try {
|
|
847
|
-
const result = await fn(...args);
|
|
848
|
-
if (myToken.isCancelled || myCallId !== callId) {
|
|
849
|
-
throw new CancellationError({ message: "Superseded by newer call" });
|
|
850
|
-
}
|
|
851
|
-
return result;
|
|
852
|
-
} finally {
|
|
853
|
-
if (myCallId === callId) {
|
|
854
|
-
pending = false;
|
|
855
|
-
}
|
|
856
|
-
}
|
|
857
|
-
};
|
|
858
|
-
Object.defineProperty(action, "pending", {
|
|
859
|
-
get: () => pending,
|
|
860
|
-
enumerable: true
|
|
861
|
-
});
|
|
862
|
-
Object.defineProperty(action, "cancel", {
|
|
863
|
-
value: () => {
|
|
864
|
-
currentToken.cancel();
|
|
865
|
-
pending = false;
|
|
866
|
-
},
|
|
867
|
-
enumerable: true
|
|
868
|
-
});
|
|
869
|
-
return action;
|
|
870
|
-
};
|
|
871
|
-
var takeFirst = (fn) => {
|
|
872
|
-
let pending = false;
|
|
873
|
-
const action = async (...args) => {
|
|
874
|
-
if (pending) {
|
|
875
|
-
return void 0;
|
|
876
|
-
}
|
|
877
|
-
pending = true;
|
|
878
|
-
try {
|
|
879
|
-
return await fn(...args);
|
|
880
|
-
} finally {
|
|
881
|
-
pending = false;
|
|
882
|
-
}
|
|
883
|
-
};
|
|
884
|
-
Object.defineProperty(action, "pending", {
|
|
885
|
-
get: () => pending,
|
|
886
|
-
enumerable: true
|
|
887
|
-
});
|
|
888
|
-
return action;
|
|
889
|
-
};
|
|
890
|
-
var debounceAction = (fn, delayMs) => {
|
|
891
|
-
let timeout = null;
|
|
892
|
-
let currentToken = createCancellationToken();
|
|
893
|
-
return (...args) => {
|
|
894
|
-
if (timeout) {
|
|
895
|
-
clearTimeout(timeout);
|
|
896
|
-
currentToken.cancel();
|
|
897
|
-
}
|
|
898
|
-
currentToken = createCancellationToken();
|
|
899
|
-
const myToken = currentToken;
|
|
900
|
-
return new Promise((resolve, reject) => {
|
|
901
|
-
timeout = setTimeout(() => {
|
|
902
|
-
if (myToken.isCancelled) return;
|
|
903
|
-
Promise.resolve(fn(...args)).then((result) => {
|
|
904
|
-
if (!myToken.isCancelled) resolve(result);
|
|
905
|
-
}).catch((error) => {
|
|
906
|
-
if (!myToken.isCancelled) reject(error);
|
|
907
|
-
});
|
|
908
|
-
}, delayMs);
|
|
909
|
-
});
|
|
910
|
-
};
|
|
911
|
-
};
|
|
912
|
-
var throttleAction = (fn, intervalMs) => {
|
|
913
|
-
let lastCallTime = 0;
|
|
914
|
-
let pending = false;
|
|
915
|
-
return async (...args) => {
|
|
916
|
-
const now = Date.now();
|
|
917
|
-
if (now - lastCallTime < intervalMs || pending) {
|
|
918
|
-
return void 0;
|
|
919
|
-
}
|
|
920
|
-
lastCallTime = now;
|
|
921
|
-
pending = true;
|
|
922
|
-
try {
|
|
923
|
-
return await fn(...args);
|
|
924
|
-
} finally {
|
|
925
|
-
pending = false;
|
|
926
|
-
}
|
|
927
|
-
};
|
|
928
|
-
};
|
|
929
836
|
var dispatch = (store, actionName, ...args) => {
|
|
930
837
|
const storeRecord = store;
|
|
931
838
|
const action = storeRecord[actionName];
|
|
@@ -960,6 +867,86 @@ var withAbortSignal = (fn) => {
|
|
|
960
867
|
return effect.Effect.runPromise(runWithAbortSignal(effect$1, signal6));
|
|
961
868
|
};
|
|
962
869
|
};
|
|
870
|
+
function useConcurrency(action, options = {}) {
|
|
871
|
+
const { strategy = "switch", debounceMs, throttleMs } = options;
|
|
872
|
+
let runningFiber = null;
|
|
873
|
+
let debounceTimeout = null;
|
|
874
|
+
let lastCallTime = 0;
|
|
875
|
+
const queue = strategy === "concat" ? effect.Effect.runSync(effect.Queue.unbounded()) : null;
|
|
876
|
+
if (queue) {
|
|
877
|
+
effect.Effect.runFork(
|
|
878
|
+
effect.Effect.forever(
|
|
879
|
+
effect.Effect.gen(function* () {
|
|
880
|
+
const args = yield* effect.Queue.take(queue);
|
|
881
|
+
yield* effect.Effect.catchAll(action(...args), () => effect.Effect.void);
|
|
882
|
+
})
|
|
883
|
+
)
|
|
884
|
+
);
|
|
885
|
+
}
|
|
886
|
+
const execute = (...args) => {
|
|
887
|
+
switch (strategy) {
|
|
888
|
+
case "switch":
|
|
889
|
+
if (runningFiber) {
|
|
890
|
+
effect.Effect.runFork(effect.Fiber.interrupt(runningFiber));
|
|
891
|
+
}
|
|
892
|
+
runningFiber = effect.Effect.runFork(
|
|
893
|
+
effect.Effect.match(action(...args), {
|
|
894
|
+
onFailure: () => {
|
|
895
|
+
runningFiber = null;
|
|
896
|
+
},
|
|
897
|
+
onSuccess: () => {
|
|
898
|
+
runningFiber = null;
|
|
899
|
+
}
|
|
900
|
+
})
|
|
901
|
+
);
|
|
902
|
+
break;
|
|
903
|
+
case "exhaust":
|
|
904
|
+
if (runningFiber) {
|
|
905
|
+
return;
|
|
906
|
+
}
|
|
907
|
+
runningFiber = effect.Effect.runFork(
|
|
908
|
+
effect.Effect.match(action(...args), {
|
|
909
|
+
onFailure: () => {
|
|
910
|
+
runningFiber = null;
|
|
911
|
+
},
|
|
912
|
+
onSuccess: () => {
|
|
913
|
+
runningFiber = null;
|
|
914
|
+
}
|
|
915
|
+
})
|
|
916
|
+
);
|
|
917
|
+
break;
|
|
918
|
+
case "merge":
|
|
919
|
+
effect.Effect.runFork(
|
|
920
|
+
effect.Effect.catchAll(action(...args), () => effect.Effect.void)
|
|
921
|
+
);
|
|
922
|
+
break;
|
|
923
|
+
case "concat":
|
|
924
|
+
if (queue) {
|
|
925
|
+
effect.Effect.runFork(effect.Queue.offer(queue, args));
|
|
926
|
+
}
|
|
927
|
+
break;
|
|
928
|
+
}
|
|
929
|
+
};
|
|
930
|
+
return (...args) => {
|
|
931
|
+
const now = Date.now();
|
|
932
|
+
if (throttleMs !== void 0 && throttleMs > 0) {
|
|
933
|
+
if (now - lastCallTime < throttleMs) {
|
|
934
|
+
return;
|
|
935
|
+
}
|
|
936
|
+
lastCallTime = now;
|
|
937
|
+
}
|
|
938
|
+
if (debounceMs !== void 0 && debounceMs > 0) {
|
|
939
|
+
if (debounceTimeout) {
|
|
940
|
+
clearTimeout(debounceTimeout);
|
|
941
|
+
}
|
|
942
|
+
debounceTimeout = setTimeout(() => {
|
|
943
|
+
execute(...args);
|
|
944
|
+
}, debounceMs);
|
|
945
|
+
return;
|
|
946
|
+
}
|
|
947
|
+
execute(...args);
|
|
948
|
+
};
|
|
949
|
+
}
|
|
963
950
|
var validateState = (schema, state) => {
|
|
964
951
|
const result = effect.Effect.runSync(
|
|
965
952
|
effect.Schema.decodeUnknown(schema)(state).pipe(
|
|
@@ -1198,7 +1185,7 @@ var hydrateStores = (serialized) => effect.Effect.try({
|
|
|
1198
1185
|
);
|
|
1199
1186
|
}
|
|
1200
1187
|
},
|
|
1201
|
-
catch: () => new HydrationError(
|
|
1188
|
+
catch: () => new HydrationError()
|
|
1202
1189
|
}).pipe(effect.Effect.catchAll(() => effect.Effect.void));
|
|
1203
1190
|
var hydrateStoresSync = (serialized) => {
|
|
1204
1191
|
effect.Effect.runSync(
|
|
@@ -1507,7 +1494,6 @@ exports.createSelectorAsync = createSelectorAsync;
|
|
|
1507
1494
|
exports.createStore = createStore;
|
|
1508
1495
|
exports.createStoreStream = createStoreStream;
|
|
1509
1496
|
exports.createValidatedSetter = createValidatedSetter;
|
|
1510
|
-
exports.debounceAction = debounceAction;
|
|
1511
1497
|
exports.defineSlice = defineSlice;
|
|
1512
1498
|
exports.deriveFrom = deriveFrom;
|
|
1513
1499
|
exports.deriveFromAsync = deriveFromAsync;
|
|
@@ -1538,9 +1524,7 @@ exports.runInScope = runInScope;
|
|
|
1538
1524
|
exports.serializeStores = serializeStores;
|
|
1539
1525
|
exports.shallowEqual = shallowEqual;
|
|
1540
1526
|
exports.streamAll = streamAll;
|
|
1541
|
-
exports.
|
|
1542
|
-
exports.takeLatest = takeLatest;
|
|
1543
|
-
exports.throttleAction = throttleAction;
|
|
1527
|
+
exports.useConcurrency = useConcurrency;
|
|
1544
1528
|
exports.validateState = validateState;
|
|
1545
1529
|
exports.validateStateAsync = validateStateAsync;
|
|
1546
1530
|
exports.withAbortSignal = withAbortSignal;
|