@fozy-labs/rx-toolkit 0.5.4 → 0.6.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/LICENSE +1 -1
- package/README.md +22 -13
- package/dist/common/devtools/reduxDevtools.js +17 -2
- package/dist/common/react/index.d.ts +1 -0
- package/dist/common/react/index.js +1 -0
- package/dist/common/react/useIsomorphicLayoutEffect.d.ts +17 -0
- package/dist/common/react/useIsomorphicLayoutEffect.js +17 -0
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -2
- package/dist/query/api/createApi.d.ts +4 -0
- package/dist/query/api/createApi.js +9 -0
- package/dist/query/api/index.d.ts +1 -0
- package/dist/query/api/index.js +1 -0
- package/dist/query/constants.d.ts +12 -0
- package/dist/query/constants.js +15 -0
- package/dist/query/core/api/Api.d.ts +20 -0
- package/dist/query/core/api/Api.js +129 -0
- package/dist/query/core/api/constants.d.ts +2 -0
- package/dist/query/core/api/constants.js +3 -0
- package/dist/query/core/api/index.d.ts +4 -0
- package/dist/query/core/api/index.js +4 -0
- package/dist/query/core/api/mergeHooks.d.ts +2 -0
- package/dist/query/core/api/mergeHooks.js +26 -0
- package/dist/query/core/api/normalizeLinks.d.ts +2 -0
- package/dist/query/core/api/normalizeLinks.js +11 -0
- package/dist/query/core/cache/CacheEntry.d.ts +21 -0
- package/dist/query/core/cache/CacheEntry.js +54 -0
- package/dist/query/core/cache/CacheMap.d.ts +19 -0
- package/dist/query/core/cache/CacheMap.js +32 -0
- package/dist/query/core/cache/QueryCacheEntry.d.ts +21 -0
- package/dist/query/core/cache/QueryCacheEntry.js +136 -0
- package/dist/query/core/cache/index.d.ts +3 -0
- package/dist/query/core/cache/index.js +3 -0
- package/dist/query/core/command/Command.d.ts +67 -0
- package/dist/query/core/command/Command.js +253 -0
- package/dist/query/core/command/CommandAgent.d.ts +17 -0
- package/dist/query/core/command/CommandAgent.js +67 -0
- package/dist/query/core/command/LinkManager.d.ts +24 -0
- package/dist/query/core/command/LinkManager.js +71 -0
- package/dist/query/core/command/index.d.ts +2 -0
- package/dist/query/core/command/index.js +2 -0
- package/dist/query/core/errors/CacheEntryRemovedError.d.ts +7 -0
- package/dist/query/core/errors/CacheEntryRemovedError.js +9 -0
- package/dist/query/core/errors/MachineStateError.d.ts +8 -0
- package/dist/query/core/errors/MachineStateError.js +10 -0
- package/dist/query/core/errors/MachineTransitionError.d.ts +7 -0
- package/dist/query/core/errors/MachineTransitionError.js +9 -0
- package/dist/query/core/errors/index.d.ts +3 -0
- package/dist/query/core/errors/index.js +3 -0
- package/dist/query/core/index.d.ts +8 -0
- package/dist/query/core/index.js +8 -0
- package/dist/query/core/machine/Machine.d.ts +21 -0
- package/dist/query/core/machine/Machine.js +42 -0
- package/dist/query/core/machine/MachineBase.d.ts +31 -0
- package/dist/query/core/machine/MachineBase.js +176 -0
- package/dist/query/core/machine/MachineError.d.ts +10 -0
- package/dist/query/core/machine/MachineError.js +19 -0
- package/dist/query/core/machine/MachinePending.d.ts +13 -0
- package/dist/query/core/machine/MachinePending.js +32 -0
- package/dist/query/core/machine/MachineRefreshError.d.ts +12 -0
- package/dist/query/core/machine/MachineRefreshError.js +23 -0
- package/dist/query/core/machine/MachineRefreshing.d.ts +15 -0
- package/dist/query/core/machine/MachineRefreshing.js +43 -0
- package/dist/query/core/machine/MachineSuccess.d.ts +12 -0
- package/dist/query/core/machine/MachineSuccess.js +23 -0
- package/dist/query/core/machine/MachineWithData.d.ts +25 -0
- package/dist/query/core/machine/MachineWithData.js +73 -0
- package/dist/query/core/machine/index.d.ts +9 -0
- package/dist/query/core/machine/index.js +9 -0
- package/dist/query/core/machine/machine-helpers.d.ts +9 -0
- package/dist/query/core/machine/machine-helpers.js +80 -0
- package/dist/query/core/patcher/Patcher.d.ts +30 -0
- package/dist/query/core/patcher/Patcher.js +122 -0
- package/dist/query/core/patcher/index.d.ts +1 -0
- package/dist/query/core/patcher/index.js +1 -0
- package/dist/query/core/resource/Resource.d.ts +105 -0
- package/dist/query/core/resource/Resource.js +340 -0
- package/dist/query/core/resource/ResourceAgent.d.ts +37 -0
- package/dist/query/core/resource/ResourceAgent.js +179 -0
- package/dist/query/core/resource/index.d.ts +2 -0
- package/dist/query/core/resource/index.js +2 -0
- package/dist/query/core/snapshoter/Snapshoter.d.ts +22 -0
- package/dist/query/core/snapshoter/Snapshoter.js +78 -0
- package/dist/query/core/snapshoter/index.d.ts +2 -0
- package/dist/query/core/snapshoter/index.js +1 -0
- package/dist/query/core/syncer/Syncer.d.ts +32 -0
- package/dist/query/core/syncer/Syncer.js +85 -0
- package/dist/query/core/syncer/index.d.ts +2 -0
- package/dist/query/core/syncer/index.js +1 -0
- package/dist/query/index.d.ts +5 -10
- package/dist/query/index.js +5 -13
- package/dist/query/lib/broadcastSyncDriver.d.ts +5 -0
- package/dist/query/lib/broadcastSyncDriver.js +46 -0
- package/dist/query/lib/index.d.ts +3 -0
- package/dist/query/lib/index.js +3 -0
- package/dist/{query-v2 → query}/lib/stableStringify.js +5 -3
- package/dist/query/lib/toKeyed.d.ts +11 -0
- package/dist/query/lib/toKeyed.js +18 -0
- package/dist/query/react/ReactHooksPlugin.d.ts +31 -0
- package/dist/query/react/ReactHooksPlugin.js +21 -0
- package/dist/query/react/index.d.ts +3 -0
- package/dist/query/react/index.js +3 -0
- package/dist/query/react/useCommand.d.ts +2 -0
- package/dist/query/react/useCommand.js +14 -0
- package/dist/query/react/useResource.d.ts +2 -0
- package/dist/query/react/useResource.js +16 -0
- package/dist/query/types/api.d.ts +39 -0
- package/dist/query/types/cache.d.ts +51 -0
- package/dist/query/types/command.d.ts +52 -0
- package/dist/query/types/common.d.ts +65 -0
- package/dist/query/types/index.d.ts +8 -4
- package/dist/query/types/index.js +8 -5
- package/dist/query/types/plugin-hkt.d.ts +64 -0
- package/dist/query/types/resource.d.ts +49 -0
- package/dist/query/types/snapshot.d.ts +27 -0
- package/dist/query/types/snapshot.js +2 -0
- package/dist/query/types/state.d.ts +24 -0
- package/dist/signals/base/ComputeCache.js +1 -1
- package/dist/signals/base/Devtools.js +2 -6
- package/dist/signals/signals/Computed.d.ts +1 -0
- package/dist/signals/signals/Computed.js +5 -1
- package/dist/signals/signals/Effect.d.ts +0 -4
- package/dist/signals/signals/Effect.js +0 -6
- package/dist/signals/signals/LocalState.d.ts +1 -10
- package/dist/signals/signals/LocalState.js +0 -12
- package/dist/signals/signals/Signal.d.ts +2 -8
- package/dist/signals/signals/Signal.js +1 -10
- package/dist/signals/signals/State.d.ts +2 -1
- package/dist/signals/signals/State.js +9 -0
- package/dist/signals/types/SignalOptions.d.ts +0 -2
- package/dist/signals/types/normalizeSignalOptions.js +0 -3
- package/dist/signals/types/signals.types.d.ts +3 -1
- package/docs/CHANGELOG.md +48 -2
- package/docs/migrations/0.6.0.md +224 -0
- package/docs/query/README.md +52 -562
- package/docs/query/api/README.md +59 -0
- package/docs/query/api/_CacheEntry.md +39 -0
- package/docs/query/api/_CacheMap.md +30 -0
- package/docs/query/api/_QueryCacheEntry.md +81 -0
- package/docs/query/api/command-agent.md +60 -0
- package/docs/query/api/command.md +76 -0
- package/docs/query/api/resource-agent.md +68 -0
- package/docs/query/api/resource.md +77 -0
- package/docs/query/concepts/agent.md +70 -0
- package/docs/query/concepts/architecture.md +139 -0
- package/docs/query/concepts/cache.md +81 -0
- package/docs/query/concepts/dataflows.md +473 -0
- package/docs/query/concepts/keyed.md +42 -0
- package/docs/query/concepts/machine.md +106 -0
- package/docs/query/concepts/patching.md +85 -0
- package/docs/query/usage/broadcast.md +188 -0
- package/docs/query/usage/command.md +203 -0
- package/docs/query/usage/lifecycle.md +114 -0
- package/docs/query/usage/links.md +125 -0
- package/docs/query/usage/plugins.md +96 -0
- package/docs/query/usage/resource.md +206 -0
- package/docs/query/usage/snapshot.md +80 -0
- package/docs/usage/react/README.md +45 -91
- package/package.json +5 -7
- package/dist/query/SKIP_TOKEN.d.ts +0 -1
- package/dist/query/SKIP_TOKEN.js +0 -1
- package/dist/query/api/createCommand.d.ts +0 -21
- package/dist/query/api/createCommand.js +0 -20
- package/dist/query/api/createOperation.d.ts +0 -5
- package/dist/query/api/createOperation.js +0 -6
- package/dist/query/api/createResource.d.ts +0 -3
- package/dist/query/api/createResource.js +0 -2
- package/dist/query/api/createResourceDuplicator.d.ts +0 -4
- package/dist/query/api/createResourceDuplicator.js +0 -2
- package/dist/query/api/resetAllQueriesCache.d.ts +0 -1
- package/dist/query/api/resetAllQueriesCache.js +0 -4
- package/dist/query/core/Command/Command.d.ts +0 -35
- package/dist/query/core/Command/Command.js +0 -210
- package/dist/query/core/Command/CommandAgent.d.ts +0 -19
- package/dist/query/core/Command/CommandAgent.js +0 -54
- package/dist/query/core/Command/index.d.ts +0 -2
- package/dist/query/core/Command/index.js +0 -2
- package/dist/query/core/Operation/Operation.d.ts +0 -8
- package/dist/query/core/Operation/Operation.js +0 -4
- package/dist/query/core/Operation/OperationAgent.d.ts +0 -4
- package/dist/query/core/Operation/OperationAgent.js +0 -4
- package/dist/query/core/QueriesCache.d.ts +0 -9
- package/dist/query/core/QueriesCache.js +0 -28
- package/dist/query/core/QueriesLifetimeHooks.d.ts +0 -22
- package/dist/query/core/QueriesLifetimeHooks.js +0 -86
- package/dist/query/core/ResetAllQueriesSignal.d.ts +0 -6
- package/dist/query/core/ResetAllQueriesSignal.js +0 -11
- package/dist/query/core/Resource/Resource.d.ts +0 -51
- package/dist/query/core/Resource/Resource.js +0 -232
- package/dist/query/core/Resource/ResourceAgent.d.ts +0 -35
- package/dist/query/core/Resource/ResourceAgent.js +0 -110
- package/dist/query/core/Resource/ResourceDuplicator.d.ts +0 -73
- package/dist/query/core/Resource/ResourceDuplicator.js +0 -227
- package/dist/query/core/Resource/ResourceDuplicatorAgent.d.ts +0 -35
- package/dist/query/core/Resource/ResourceDuplicatorAgent.js +0 -110
- package/dist/query/core/Resource/ResourceRef.d.ts +0 -16
- package/dist/query/core/Resource/ResourceRef.js +0 -136
- package/dist/query/lib/IndirectMap.d.ts +0 -19
- package/dist/query/lib/IndirectMap.js +0 -88
- package/dist/query/lib/ReactiveCache.d.ts +0 -62
- package/dist/query/lib/ReactiveCache.js +0 -80
- package/dist/query/react/useCommandAgent.d.ts +0 -24
- package/dist/query/react/useCommandAgent.js +0 -39
- package/dist/query/react/useOperationAgent.d.ts +0 -6
- package/dist/query/react/useOperationAgent.js +0 -6
- package/dist/query/react/useResourceAgent.d.ts +0 -6
- package/dist/query/react/useResourceAgent.js +0 -31
- package/dist/query/react/useResourceRef.d.ts +0 -5
- package/dist/query/react/useResourceRef.js +0 -13
- package/dist/query/types/Command.types.d.ts +0 -154
- package/dist/query/types/Command.types.js +0 -1
- package/dist/query/types/Operation.types.d.ts +0 -13
- package/dist/query/types/Operation.types.js +0 -1
- package/dist/query/types/Resource.types.d.ts +0 -129
- package/dist/query/types/Resource.types.js +0 -1
- package/dist/query/types/shared.types.d.ts +0 -26
- package/dist/query/types/shared.types.js +0 -1
- package/dist/query-v2/api/createApi.d.ts +0 -10
- package/dist/query-v2/api/createApi.js +0 -83
- package/dist/query-v2/core/common/CacheEntry.d.ts +0 -29
- package/dist/query-v2/core/common/CacheEntry.js +0 -71
- package/dist/query-v2/core/common/CacheMap.d.ts +0 -38
- package/dist/query-v2/core/common/CacheMap.js +0 -127
- package/dist/query-v2/core/common/LifecycleHooks.d.ts +0 -22
- package/dist/query-v2/core/common/LifecycleHooks.js +0 -104
- package/dist/query-v2/core/common/index.d.ts +0 -3
- package/dist/query-v2/core/common/index.js +0 -3
- package/dist/query-v2/core/index.d.ts +0 -3
- package/dist/query-v2/core/index.js +0 -3
- package/dist/query-v2/core/machines/Machine.d.ts +0 -14
- package/dist/query-v2/core/machines/Machine.js +0 -33
- package/dist/query-v2/core/machines/MachineError.d.ts +0 -11
- package/dist/query-v2/core/machines/MachineError.js +0 -26
- package/dist/query-v2/core/machines/MachineIdle.d.ts +0 -8
- package/dist/query-v2/core/machines/MachineIdle.js +0 -19
- package/dist/query-v2/core/machines/MachinePending.d.ts +0 -12
- package/dist/query-v2/core/machines/MachinePending.js +0 -29
- package/dist/query-v2/core/machines/MachineRefreshing.d.ts +0 -14
- package/dist/query-v2/core/machines/MachineRefreshing.js +0 -46
- package/dist/query-v2/core/machines/MachineSuccess.d.ts +0 -16
- package/dist/query-v2/core/machines/MachineSuccess.js +0 -42
- package/dist/query-v2/core/machines/MachineWithData.d.ts +0 -18
- package/dist/query-v2/core/machines/MachineWithData.js +0 -40
- package/dist/query-v2/core/machines/Patcher.d.ts +0 -20
- package/dist/query-v2/core/machines/Patcher.js +0 -104
- package/dist/query-v2/core/machines/index.d.ts +0 -8
- package/dist/query-v2/core/machines/index.js +0 -8
- package/dist/query-v2/core/resource/ResourceV2.d.ts +0 -120
- package/dist/query-v2/core/resource/ResourceV2.js +0 -464
- package/dist/query-v2/core/resource/ResourceV2Agent.d.ts +0 -26
- package/dist/query-v2/core/resource/ResourceV2Agent.js +0 -132
- package/dist/query-v2/core/resource/index.d.ts +0 -2
- package/dist/query-v2/core/resource/index.js +0 -2
- package/dist/query-v2/index.d.ts +0 -11
- package/dist/query-v2/index.js +0 -17
- package/dist/query-v2/lib/NO_VALUE.d.ts +0 -2
- package/dist/query-v2/lib/NO_VALUE.js +0 -1
- package/dist/query-v2/lib/SKIP_TOKEN.d.ts +0 -2
- package/dist/query-v2/lib/SKIP_TOKEN.js +0 -1
- package/dist/query-v2/lib/index.d.ts +0 -4
- package/dist/query-v2/lib/index.js +0 -3
- package/dist/query-v2/plugins/ReactHooksPlugin.d.ts +0 -25
- package/dist/query-v2/plugins/ReactHooksPlugin.js +0 -19
- package/dist/query-v2/plugins/types.d.ts +0 -1
- package/dist/query-v2/react/__tests__/helpers.d.ts +0 -12
- package/dist/query-v2/react/__tests__/helpers.js +0 -33
- package/dist/query-v2/react/index.d.ts +0 -2
- package/dist/query-v2/react/index.js +0 -2
- package/dist/query-v2/react/useResourceV2Agent.d.ts +0 -12
- package/dist/query-v2/react/useResourceV2Agent.js +0 -36
- package/dist/query-v2/react/useResourceV2Ref.d.ts +0 -12
- package/dist/query-v2/react/useResourceV2Ref.js +0 -57
- package/dist/query-v2/snapshot/Snapshot.d.ts +0 -13
- package/dist/query-v2/snapshot/Snapshot.js +0 -76
- package/dist/query-v2/types/agent.types.d.ts +0 -54
- package/dist/query-v2/types/api.types.d.ts +0 -22
- package/dist/query-v2/types/cache.types.d.ts +0 -37
- package/dist/query-v2/types/index.d.ts +0 -9
- package/dist/query-v2/types/index.js +0 -9
- package/dist/query-v2/types/lifecycle.types.d.ts +0 -25
- package/dist/query-v2/types/machine.types.d.ts +0 -67
- package/dist/query-v2/types/plugin.types.d.ts +0 -38
- package/dist/query-v2/types/resource.types.d.ts +0 -35
- package/dist/query-v2/types/resource.types.js +0 -1
- package/dist/query-v2/types/shared.types.d.ts +0 -20
- package/dist/query-v2/types/shared.types.js +0 -1
- package/dist/query-v2/types/snapshot.types.d.ts +0 -21
- package/dist/query-v2/types/snapshot.types.js +0 -1
- package/docs/contributing/query-v2/README.md +0 -379
- package/docs/migrations/query-v2.md +0 -171
- package/docs/query-v2/README.md +0 -280
- package/docs/query-v2/api-reference.md +0 -235
- package/docs/query-v2/optimistic-updates.md +0 -148
- package/docs/query-v2/ssr.md +0 -130
- /package/dist/{query-v2 → query}/lib/stableStringify.d.ts +0 -0
- /package/dist/{query-v2/plugins/types.js → query/types/api.js} +0 -0
- /package/dist/{query-v2/types/agent.types.js → query/types/cache.js} +0 -0
- /package/dist/{query-v2/types/api.types.js → query/types/command.js} +0 -0
- /package/dist/{query-v2/types/cache.types.js → query/types/common.js} +0 -0
- /package/dist/{query-v2/types/lifecycle.types.js → query/types/plugin-hkt.js} +0 -0
- /package/dist/{query-v2/types/machine.types.js → query/types/resource.js} +0 -0
- /package/dist/{query-v2/types/plugin.types.js → query/types/state.js} +0 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./Machine";
|
|
2
|
+
export * from "./MachineBase";
|
|
3
|
+
export * from "./MachinePending";
|
|
4
|
+
export * from "./MachineSuccess";
|
|
5
|
+
export * from "./MachineError";
|
|
6
|
+
export * from "./MachineRefreshing";
|
|
7
|
+
export * from "./MachineRefreshError";
|
|
8
|
+
export * from "./MachineWithData";
|
|
9
|
+
export * from "./machine-helpers";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { TMachineState, TPatchEntry, TPatchState, TRefreshErrorState, TRefreshingState, TSuccessState } from "../../../query/types";
|
|
2
|
+
export type TDataState<TArgs, TData> = TSuccessState<TArgs, TData> | TRefreshingState<TArgs, TData> | TRefreshErrorState<TArgs, TData>;
|
|
3
|
+
export declare function hasData<TArgs, TData>(state: TMachineState<TArgs, TData>): state is TDataState<TArgs, TData>;
|
|
4
|
+
export declare function buildDataState<TArgs, TData>(status: "success" | "refreshing" | "refresh-error", base: TMachineState<TArgs, TData>, data: TData, patchState: TPatchState<TData> | null, updatedAt?: number): TDataState<TArgs, TData>;
|
|
5
|
+
export declare function withDataState<TArgs, TData>(currentState: TMachineState<TArgs, TData>, data: TData, patchState: TPatchState<TData> | null): TDataState<TArgs, TData>;
|
|
6
|
+
export declare function consistencyViolation<TArgs, TData>(currentState: TMachineState<TArgs, TData>): TDataState<TArgs, TData>;
|
|
7
|
+
export declare function replayPatches<TArgs, TData>(currentState: TMachineState<TArgs, TData>, targetStatus: "success" | "refreshing" | "refresh-error", baseData: TData, patches: TPatchEntry[], updatedAt?: number): TDataState<TArgs, TData>;
|
|
8
|
+
export declare function processPatches<TArgs, TData>(currentState: TMachineState<TArgs, TData>, patchState: TPatchState<TData>): TDataState<TArgs, TData>;
|
|
9
|
+
export declare function processAllPatches<TArgs, TData>(currentState: TMachineState<TArgs, TData>, patchState: TPatchState<TData>): TDataState<TArgs, TData>;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { processAllSettledPatches, processPatchState, replayPatchEntries } from "../patcher";
|
|
2
|
+
export function hasData(state) {
|
|
3
|
+
return state.status === "success" || state.status === "refreshing" || state.status === "refresh-error";
|
|
4
|
+
}
|
|
5
|
+
export function buildDataState(status, base, data, patchState, updatedAt) {
|
|
6
|
+
const resolvedUpdatedAt = updatedAt ?? (hasData(base) ? base.updatedAt : Date.now());
|
|
7
|
+
switch (status) {
|
|
8
|
+
case "success": {
|
|
9
|
+
const state = {
|
|
10
|
+
status: "success",
|
|
11
|
+
args: base.args,
|
|
12
|
+
data,
|
|
13
|
+
error: null,
|
|
14
|
+
updatedAt: resolvedUpdatedAt,
|
|
15
|
+
patchState,
|
|
16
|
+
};
|
|
17
|
+
return state;
|
|
18
|
+
}
|
|
19
|
+
case "refreshing": {
|
|
20
|
+
const state = {
|
|
21
|
+
status: "refreshing",
|
|
22
|
+
args: base.args,
|
|
23
|
+
data,
|
|
24
|
+
error: null,
|
|
25
|
+
updatedAt: resolvedUpdatedAt,
|
|
26
|
+
patchState,
|
|
27
|
+
};
|
|
28
|
+
return state;
|
|
29
|
+
}
|
|
30
|
+
case "refresh-error": {
|
|
31
|
+
if (!hasData(base)) {
|
|
32
|
+
throw new Error("Cannot build refresh-error from non-data state");
|
|
33
|
+
}
|
|
34
|
+
const state = {
|
|
35
|
+
status: "refresh-error",
|
|
36
|
+
args: base.args,
|
|
37
|
+
data,
|
|
38
|
+
error: base.error,
|
|
39
|
+
updatedAt: resolvedUpdatedAt,
|
|
40
|
+
patchState,
|
|
41
|
+
};
|
|
42
|
+
return state;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export function withDataState(currentState, data, patchState) {
|
|
47
|
+
if (!hasData(currentState)) {
|
|
48
|
+
throw new Error("withDataState called on non-data state");
|
|
49
|
+
}
|
|
50
|
+
return buildDataState(currentState.status, currentState, data, patchState);
|
|
51
|
+
}
|
|
52
|
+
export function consistencyViolation(currentState) {
|
|
53
|
+
if (!hasData(currentState)) {
|
|
54
|
+
throw new Error("Consistency violation in non-data state");
|
|
55
|
+
}
|
|
56
|
+
const newPatchState = {
|
|
57
|
+
originalData: currentState.patchState?.originalData ?? currentState.data,
|
|
58
|
+
patches: [],
|
|
59
|
+
isConsistencyViolation: true,
|
|
60
|
+
};
|
|
61
|
+
return withDataState(currentState, currentState.data, newPatchState);
|
|
62
|
+
}
|
|
63
|
+
export function replayPatches(currentState, targetStatus, baseData, patches, updatedAt) {
|
|
64
|
+
const result = replayPatchEntries(baseData, patches);
|
|
65
|
+
if (!result.ok)
|
|
66
|
+
return consistencyViolation(currentState);
|
|
67
|
+
return buildDataState(targetStatus, currentState, result.data, result.patchState, updatedAt);
|
|
68
|
+
}
|
|
69
|
+
export function processPatches(currentState, patchState) {
|
|
70
|
+
const result = processPatchState(patchState);
|
|
71
|
+
if (!result.ok)
|
|
72
|
+
return consistencyViolation(currentState);
|
|
73
|
+
return withDataState(currentState, result.data, result.patchState);
|
|
74
|
+
}
|
|
75
|
+
export function processAllPatches(currentState, patchState) {
|
|
76
|
+
const result = processAllSettledPatches(patchState);
|
|
77
|
+
if (!result.ok)
|
|
78
|
+
return consistencyViolation(currentState);
|
|
79
|
+
return withDataState(currentState, result.data, result.patchState);
|
|
80
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type Patch } from "immer";
|
|
2
|
+
import type { TPatchEntry, TPatchState } from "../../../query/types";
|
|
3
|
+
export type TPatchResult<TData> = {
|
|
4
|
+
ok: true;
|
|
5
|
+
data: TData;
|
|
6
|
+
patchState: TPatchState<TData> | null;
|
|
7
|
+
} | {
|
|
8
|
+
ok: false;
|
|
9
|
+
};
|
|
10
|
+
export declare function createPatches<T>(base: T, recipe: (draft: T) => void): [T, Patch[], Patch[]];
|
|
11
|
+
export declare function applyForwardPatches<T>(base: T, patches: Patch[]): T;
|
|
12
|
+
export declare function rebasePatches<T>(base: T, forwardPatches: Patch[]): [T, Patch[], Patch[]];
|
|
13
|
+
/**
|
|
14
|
+
* Replay a list of patch entries onto new base data.
|
|
15
|
+
* Pure data operation — no machine-state knowledge.
|
|
16
|
+
*/
|
|
17
|
+
export declare function replayPatchEntries<TData>(baseData: TData, patches: TPatchEntry[]): TPatchResult<TData>;
|
|
18
|
+
/**
|
|
19
|
+
* Process a patch state: fold settled independent patches, then replay remaining.
|
|
20
|
+
* Stops at the first pending patch — patches after it are left as-is.
|
|
21
|
+
* Pure data operation — no machine-state knowledge.
|
|
22
|
+
*/
|
|
23
|
+
export declare function processPatchState<TData>(patchState: TPatchState<TData>): TPatchResult<TData>;
|
|
24
|
+
/**
|
|
25
|
+
* Process ALL settled patches in a patch state, continuing past pending ones.
|
|
26
|
+
* Committed patches are rebased and folded into originalData;
|
|
27
|
+
* aborted patches are dropped; pending patches are kept and rebased.
|
|
28
|
+
* Pure data operation — no machine-state knowledge.
|
|
29
|
+
*/
|
|
30
|
+
export declare function processAllSettledPatches<TData>(patchState: TPatchState<TData>): TPatchResult<TData>;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { applyPatches, enablePatches, produceWithPatches } from "immer";
|
|
2
|
+
enablePatches();
|
|
3
|
+
// ==================== Low-level Immer Operations ====================
|
|
4
|
+
export function createPatches(base, recipe) {
|
|
5
|
+
return produceWithPatches(base, recipe);
|
|
6
|
+
}
|
|
7
|
+
export function applyForwardPatches(base, patches) {
|
|
8
|
+
return applyPatches(base, patches);
|
|
9
|
+
}
|
|
10
|
+
export function rebasePatches(base, forwardPatches) {
|
|
11
|
+
return produceWithPatches(base, (draft) => {
|
|
12
|
+
applyPatches(draft, forwardPatches);
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
// ==================== High-level Patch Operations ====================
|
|
16
|
+
/**
|
|
17
|
+
* Replay a list of patch entries onto new base data.
|
|
18
|
+
* Pure data operation — no machine-state knowledge.
|
|
19
|
+
*/
|
|
20
|
+
export function replayPatchEntries(baseData, patches) {
|
|
21
|
+
let currentData = baseData;
|
|
22
|
+
const replayedPatches = [];
|
|
23
|
+
for (const p of patches) {
|
|
24
|
+
if (p.status === "aborted") {
|
|
25
|
+
replayedPatches.push(p);
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
const [nextData, forward, inverse] = rebasePatches(currentData, p.forward);
|
|
30
|
+
currentData = nextData;
|
|
31
|
+
p.forward = forward;
|
|
32
|
+
p.inverse = inverse;
|
|
33
|
+
replayedPatches.push(p);
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return { ok: false };
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const hasPending = replayedPatches.some((p) => p.status === "pending");
|
|
40
|
+
if (!hasPending) {
|
|
41
|
+
let finalData = baseData;
|
|
42
|
+
for (const p of replayedPatches) {
|
|
43
|
+
if (p.status === "committed") {
|
|
44
|
+
try {
|
|
45
|
+
finalData = applyForwardPatches(finalData, p.forward);
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
return { ok: false };
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return { ok: true, data: finalData, patchState: null };
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
ok: true,
|
|
56
|
+
data: currentData,
|
|
57
|
+
patchState: {
|
|
58
|
+
originalData: baseData,
|
|
59
|
+
patches: replayedPatches,
|
|
60
|
+
isConsistencyViolation: false,
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Process a patch state: fold settled independent patches, then replay remaining.
|
|
66
|
+
* Stops at the first pending patch — patches after it are left as-is.
|
|
67
|
+
* Pure data operation — no machine-state knowledge.
|
|
68
|
+
*/
|
|
69
|
+
export function processPatchState(patchState) {
|
|
70
|
+
const patches = [...patchState.patches];
|
|
71
|
+
let originalData = patchState.originalData;
|
|
72
|
+
const firstPendingIdx = patches.findIndex((p) => p.status === "pending");
|
|
73
|
+
const independentEnd = firstPendingIdx === -1 ? patches.length : firstPendingIdx;
|
|
74
|
+
if (independentEnd > 0) {
|
|
75
|
+
const independentPatches = patches.slice(0, independentEnd);
|
|
76
|
+
for (const p of independentPatches) {
|
|
77
|
+
if (p.status === "committed") {
|
|
78
|
+
try {
|
|
79
|
+
originalData = applyForwardPatches(originalData, p.forward);
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
return { ok: false };
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
patches.splice(0, independentEnd);
|
|
87
|
+
}
|
|
88
|
+
if (patches.length === 0) {
|
|
89
|
+
return { ok: true, data: originalData, patchState: null };
|
|
90
|
+
}
|
|
91
|
+
return replayPatchEntries(originalData, patches);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Process ALL settled patches in a patch state, continuing past pending ones.
|
|
95
|
+
* Committed patches are rebased and folded into originalData;
|
|
96
|
+
* aborted patches are dropped; pending patches are kept and rebased.
|
|
97
|
+
* Pure data operation — no machine-state knowledge.
|
|
98
|
+
*/
|
|
99
|
+
export function processAllSettledPatches(patchState) {
|
|
100
|
+
let committedData = patchState.originalData;
|
|
101
|
+
const pendingEntries = [];
|
|
102
|
+
for (const p of patchState.patches) {
|
|
103
|
+
if (p.status === "aborted")
|
|
104
|
+
continue;
|
|
105
|
+
if (p.status === "committed") {
|
|
106
|
+
try {
|
|
107
|
+
const [nextData] = rebasePatches(committedData, p.forward);
|
|
108
|
+
committedData = nextData;
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
return { ok: false };
|
|
112
|
+
}
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
// pending
|
|
116
|
+
pendingEntries.push(p);
|
|
117
|
+
}
|
|
118
|
+
if (pendingEntries.length === 0) {
|
|
119
|
+
return { ok: true, data: committedData, patchState: null };
|
|
120
|
+
}
|
|
121
|
+
return replayPatchEntries(committedData, pendingEntries);
|
|
122
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./Patcher";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./Patcher";
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type { Args, ArgsOrVoid, IResource, IResourceAgent, IResourceConfig, Keyed } from "../../../query/types";
|
|
2
|
+
import { type ReadableSignalFnLike } from "../../../signals";
|
|
3
|
+
import { QueryCacheEntry } from "../cache/QueryCacheEntry";
|
|
4
|
+
/**
|
|
5
|
+
* Data-fetching abstraction with caching and SWR.
|
|
6
|
+
*
|
|
7
|
+
* Each unique set of serialized arguments maps to a single {@link QueryCacheEntry}.
|
|
8
|
+
* Entries are retained for `retentionTime` ms after the last subscriber unsubscribes.
|
|
9
|
+
*
|
|
10
|
+
* @template TArgs - Query argument type.
|
|
11
|
+
* @template TData - Query return data type.
|
|
12
|
+
*/
|
|
13
|
+
export declare class Resource<TArgs, TData> implements IResource<TArgs, TData> {
|
|
14
|
+
private readonly _cache;
|
|
15
|
+
/**
|
|
16
|
+
* Хранит последний добавленный кэш-энтер, для возможности реактивной подписки (с помощью getEntry$)
|
|
17
|
+
*/
|
|
18
|
+
private readonly _lastEntry$;
|
|
19
|
+
/**
|
|
20
|
+
* Определяет общий статус ресурса
|
|
21
|
+
* - "idle": ресурса не активен, записей нет, getEntry$ возвращает null
|
|
22
|
+
* - "running": ресурс активен, есть хотя бы одна запись, getEntry$ может возвращать записи
|
|
23
|
+
*/
|
|
24
|
+
private readonly _status$;
|
|
25
|
+
private readonly _queryFn;
|
|
26
|
+
readonly _key: string | undefined;
|
|
27
|
+
private readonly _retentionTime;
|
|
28
|
+
private readonly _serializeArgs;
|
|
29
|
+
private readonly _onCacheEntryAdded;
|
|
30
|
+
private readonly _onQueryStarted;
|
|
31
|
+
private readonly _beforeQuery?;
|
|
32
|
+
constructor(config: IResourceConfig<TArgs, TData>);
|
|
33
|
+
/**
|
|
34
|
+
* Execute a query with the given arguments.
|
|
35
|
+
*
|
|
36
|
+
* @param args - Query arguments.
|
|
37
|
+
* @param doForce - When `true`, forces a refresh even if data is cached.
|
|
38
|
+
*/
|
|
39
|
+
trigger(args: Args<TArgs>, doForce?: boolean): void;
|
|
40
|
+
/**
|
|
41
|
+
* Mark the entry as stale and trigger a background SWR refresh.
|
|
42
|
+
*
|
|
43
|
+
* @param args - Query arguments identifying the cache entry.
|
|
44
|
+
*/
|
|
45
|
+
refresh(args: Args<TArgs>): void;
|
|
46
|
+
/**
|
|
47
|
+
* Synchronously return the cache entry for the given arguments.
|
|
48
|
+
*
|
|
49
|
+
* @param args - Query arguments (or `void` when `TArgs` is `void`).
|
|
50
|
+
* @param doInitiate - When `true`, creates and starts the entry if absent.
|
|
51
|
+
* @returns The cache entry, or `null` if not found and `doInitiate` is `false`.
|
|
52
|
+
*/
|
|
53
|
+
getEntry(args: ArgsOrVoid<TArgs>, doInitiate?: boolean): QueryCacheEntry<TArgs, TData> | null;
|
|
54
|
+
/**
|
|
55
|
+
* Reactive variant of {@link getEntry} — establishes a signal dependency
|
|
56
|
+
* so that `Signal.compute` / `Signal.effect` callers re-evaluate when the
|
|
57
|
+
* cache map changes (entry added or removed).
|
|
58
|
+
*
|
|
59
|
+
* @param args - Query arguments (or `void` when `TArgs` is `void`).
|
|
60
|
+
* @param doInitiate - When `true`, creates and starts the entry if absent.
|
|
61
|
+
* @returns The cache entry, or `null` if not found and `doInitiate` is `false`.
|
|
62
|
+
*/
|
|
63
|
+
getEntry$(args: ArgsOrVoid<TArgs>, doInitiate: true): ReadableSignalFnLike<QueryCacheEntry<TArgs, TData>>;
|
|
64
|
+
getEntry$(args: ArgsOrVoid<TArgs>, doInitiate?: boolean): ReadableSignalFnLike<QueryCacheEntry<TArgs, TData> | null>;
|
|
65
|
+
getEntry$(args: Keyed<TArgs>, doInitiate?: boolean): ReadableSignalFnLike<QueryCacheEntry<TArgs, TData> | null>;
|
|
66
|
+
/**
|
|
67
|
+
* Create a reactive {@link ResourceAgent} that observes this resource
|
|
68
|
+
* and provides SWR-aware state transitions.
|
|
69
|
+
*/
|
|
70
|
+
createAgent(): IResourceAgent<TArgs, TData>;
|
|
71
|
+
/**
|
|
72
|
+
* Serialize arguments into a cache key string.
|
|
73
|
+
*
|
|
74
|
+
* @param args - Query arguments.
|
|
75
|
+
* @returns The serialized key used for cache lookup.
|
|
76
|
+
*/
|
|
77
|
+
serialize(args: Args<TArgs>): string;
|
|
78
|
+
/**
|
|
79
|
+
* Wrap arguments into a `{ value, key }` pair, avoiding repeated serialization.
|
|
80
|
+
*
|
|
81
|
+
* @param args - Query arguments.
|
|
82
|
+
* @returns A {@link Keyed} wrapper containing the original args and their cache key.
|
|
83
|
+
*/
|
|
84
|
+
toKeyed(args: Args<TArgs>): Keyed<TArgs>;
|
|
85
|
+
/** Iterate over all cache entries. */
|
|
86
|
+
getEntries(): IterableIterator<QueryCacheEntry<TArgs, TData>>;
|
|
87
|
+
/** Clear all cache entries. */
|
|
88
|
+
reset(): void;
|
|
89
|
+
/**
|
|
90
|
+
* Get an existing cache entry or create a new one.
|
|
91
|
+
*
|
|
92
|
+
* @internal Used by {@link ResourceAgent} and Command links.
|
|
93
|
+
* @param args - Query arguments.
|
|
94
|
+
* @param doForce - When `true`, forces a refresh on an existing entry.
|
|
95
|
+
*/
|
|
96
|
+
private _getOrCreate;
|
|
97
|
+
private _createEntry;
|
|
98
|
+
/** Standard entry creation: queryFn auto-executes in constructor. */
|
|
99
|
+
private _createEntryDirect;
|
|
100
|
+
/** Entry creation with beforeQuery intercept: starts in pending, asks other tabs first. */
|
|
101
|
+
private _createEntryWithBeforeQuery;
|
|
102
|
+
private _hydrateEntry;
|
|
103
|
+
private _fireOnCacheEntryAdded;
|
|
104
|
+
private _fireOnQueryStarted;
|
|
105
|
+
}
|