@abloatai/humans 0.37.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 +52 -0
- package/dist/Ablo.d.ts +208 -0
- package/dist/Ablo.js +120 -0
- package/dist/client.d.ts +317 -0
- package/dist/client.js +13 -0
- package/dist/core.d.ts +35 -0
- package/dist/core.js +48 -0
- package/dist/humans.d.ts +28 -0
- package/dist/humans.js +34 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +6 -0
- package/dist/local/BaseSyncedStore.d.ts +807 -0
- package/dist/local/BaseSyncedStore.js +1516 -0
- package/dist/local/Database.d.ts +322 -0
- package/dist/local/Database.js +1589 -0
- package/dist/local/InstanceCache.d.ts +255 -0
- package/dist/local/InstanceCache.js +1263 -0
- package/dist/local/LazyReferenceCollection.d.ts +177 -0
- package/dist/local/LazyReferenceCollection.js +461 -0
- package/dist/local/Model.d.ts +475 -0
- package/dist/local/Model.js +950 -0
- package/dist/local/ModelRegistry.d.ts +225 -0
- package/dist/local/ModelRegistry.js +539 -0
- package/dist/local/NetworkMonitor.d.ts +28 -0
- package/dist/local/NetworkMonitor.js +79 -0
- package/dist/local/RuntimeContext.d.ts +52 -0
- package/dist/local/RuntimeContext.js +80 -0
- package/dist/local/SyncClient.d.ts +516 -0
- package/dist/local/SyncClient.js +1754 -0
- package/dist/local/adapters/alwaysOnline.d.ts +14 -0
- package/dist/local/adapters/alwaysOnline.js +17 -0
- package/dist/local/adapters/inMemoryStorage.d.ts +37 -0
- package/dist/local/adapters/inMemoryStorage.js +122 -0
- package/dist/local/client/clientPrelude.d.ts +52 -0
- package/dist/local/client/clientPrelude.js +60 -0
- package/dist/local/client/consoleLogger.d.ts +35 -0
- package/dist/local/client/consoleLogger.js +44 -0
- package/dist/local/client/createInternalComponents.d.ts +50 -0
- package/dist/local/client/createInternalComponents.js +98 -0
- package/dist/local/client/createModelProxy.d.ts +248 -0
- package/dist/local/client/createModelProxy.js +880 -0
- package/dist/local/client/modelRegistration.d.ts +10 -0
- package/dist/local/client/modelRegistration.js +316 -0
- package/dist/local/client/options.d.ts +440 -0
- package/dist/local/client/options.js +7 -0
- package/dist/local/client/reactiveEngine.d.ts +53 -0
- package/dist/local/client/reactiveEngine.js +705 -0
- package/dist/local/client/resourceTypes.d.ts +12 -0
- package/dist/local/client/resourceTypes.js +10 -0
- package/dist/local/client/schemaConfig.d.ts +44 -0
- package/dist/local/client/schemaConfig.js +185 -0
- package/dist/local/client/storeCluster.d.ts +46 -0
- package/dist/local/client/storeCluster.js +133 -0
- package/dist/local/client/storeLifecycle.d.ts +61 -0
- package/dist/local/client/storeLifecycle.js +236 -0
- package/dist/local/client/validateAbloOptions.d.ts +42 -0
- package/dist/local/client/validateAbloOptions.js +43 -0
- package/dist/local/client/wsMutationExecutor.d.ts +27 -0
- package/dist/local/client/wsMutationExecutor.js +72 -0
- package/dist/local/context.d.ts +42 -0
- package/dist/local/context.js +81 -0
- package/dist/local/coordination/ClaimLog.d.ts +26 -0
- package/dist/local/coordination/ClaimLog.js +32 -0
- package/dist/local/interfaces/index.d.ts +311 -0
- package/dist/local/interfaces/index.js +9 -0
- package/dist/local/localModelContract.d.ts +14 -0
- package/dist/local/localModelContract.js +1 -0
- package/dist/local/logPosition.d.ts +31 -0
- package/dist/local/logPosition.js +53 -0
- package/dist/local/mutationPersistence.d.ts +6 -0
- package/dist/local/mutationPersistence.js +1 -0
- package/dist/local/mutators/RecordingMutation.d.ts +36 -0
- package/dist/local/mutators/RecordingMutation.js +182 -0
- package/dist/local/mutators/Transaction.d.ts +40 -0
- package/dist/local/mutators/Transaction.js +58 -0
- package/dist/local/mutators/UndoManager.d.ts +258 -0
- package/dist/local/mutators/UndoManager.js +665 -0
- package/dist/local/mutators/defineMutators.d.ts +60 -0
- package/dist/local/mutators/defineMutators.js +18 -0
- package/dist/local/mutators/inverseOp.d.ts +126 -0
- package/dist/local/mutators/inverseOp.js +71 -0
- package/dist/local/mutators/mutateActions.d.ts +45 -0
- package/dist/local/mutators/mutateActions.js +105 -0
- package/dist/local/mutators/readerActions.d.ts +33 -0
- package/dist/local/mutators/readerActions.js +57 -0
- package/dist/local/mutators/undoApply.d.ts +51 -0
- package/dist/local/mutators/undoApply.js +117 -0
- package/dist/local/persistence.d.ts +7 -0
- package/dist/local/persistence.js +9 -0
- package/dist/local/query/QueryProcessor.d.ts +75 -0
- package/dist/local/query/QueryProcessor.js +255 -0
- package/dist/local/query/client.d.ts +64 -0
- package/dist/local/query/client.js +138 -0
- package/dist/local/query/types.d.ts +85 -0
- package/dist/local/query/types.js +16 -0
- package/dist/local/schema/serialize.d.ts +1 -0
- package/dist/local/schema/serialize.js +1 -0
- package/dist/local/store/queryApi.d.ts +13 -0
- package/dist/local/store/queryApi.js +35 -0
- package/dist/local/storeContract.d.ts +145 -0
- package/dist/local/storeContract.js +12 -0
- package/dist/local/stores/DatabaseManager.d.ts +112 -0
- package/dist/local/stores/DatabaseManager.js +400 -0
- package/dist/local/stores/ObjectStore.d.ts +115 -0
- package/dist/local/stores/ObjectStore.js +393 -0
- package/dist/local/stores/ObjectStoreContract.d.ts +38 -0
- package/dist/local/stores/ObjectStoreContract.js +1 -0
- package/dist/local/stores/StoreManager.d.ts +114 -0
- package/dist/local/stores/StoreManager.js +304 -0
- package/dist/local/stores/SyncActionStore.d.ts +99 -0
- package/dist/local/stores/SyncActionStore.js +506 -0
- package/dist/local/stores/openIDBWithTimeout.d.ts +65 -0
- package/dist/local/stores/openIDBWithTimeout.js +153 -0
- package/dist/local/stores/persistenceCleanup.d.ts +7 -0
- package/dist/local/stores/persistenceCleanup.js +26 -0
- package/dist/local/stores/persistenceIdentity.d.ts +27 -0
- package/dist/local/stores/persistenceIdentity.js +38 -0
- package/dist/local/stores/syncAction.d.ts +26 -0
- package/dist/local/stores/syncAction.js +16 -0
- package/dist/local/stores/v1PersistenceDeletion.d.ts +8 -0
- package/dist/local/stores/v1PersistenceDeletion.js +16 -0
- package/dist/local/sync/BootstrapFetcher.d.ts +284 -0
- package/dist/local/sync/BootstrapFetcher.js +964 -0
- package/dist/local/sync/ConnectionManager.d.ts +8 -0
- package/dist/local/sync/ConnectionManager.js +8 -0
- package/dist/local/sync/OnDemandLoader.d.ts +231 -0
- package/dist/local/sync/OnDemandLoader.js +743 -0
- package/dist/local/sync/SubscriptionManager.d.ts +159 -0
- package/dist/local/sync/SubscriptionManager.js +243 -0
- package/dist/local/sync/SyncWebSocket.d.ts +173 -0
- package/dist/local/sync/SyncWebSocket.js +438 -0
- package/dist/local/sync/bootstrapApply.d.ts +73 -0
- package/dist/local/sync/bootstrapApply.js +73 -0
- package/dist/local/sync/commitFrames.d.ts +8 -0
- package/dist/local/sync/commitFrames.js +8 -0
- package/dist/local/sync/connectionManagerLifecycle.d.ts +23 -0
- package/dist/local/sync/connectionManagerLifecycle.js +126 -0
- package/dist/local/sync/contextPorts.d.ts +18 -0
- package/dist/local/sync/contextPorts.js +31 -0
- package/dist/local/sync/createClaimStream.d.ts +64 -0
- package/dist/local/sync/createClaimStream.js +475 -0
- package/dist/local/sync/createSnapshot.d.ts +29 -0
- package/dist/local/sync/createSnapshot.js +116 -0
- package/dist/local/sync/credentialLifecycle.d.ts +7 -0
- package/dist/local/sync/credentialLifecycle.js +7 -0
- package/dist/local/sync/deltaPipeline.d.ts +116 -0
- package/dist/local/sync/deltaPipeline.js +357 -0
- package/dist/local/sync/groupChange.d.ts +116 -0
- package/dist/local/sync/groupChange.js +244 -0
- package/dist/local/sync/initialize.d.ts +27 -0
- package/dist/local/sync/initialize.js +137 -0
- package/dist/local/sync/participants.d.ts +132 -0
- package/dist/local/sync/participants.js +342 -0
- package/dist/local/sync/persistedPrefix.d.ts +12 -0
- package/dist/local/sync/persistedPrefix.js +22 -0
- package/dist/local/sync/reconnect.d.ts +23 -0
- package/dist/local/sync/reconnect.js +55 -0
- package/dist/local/sync/schemaDrift.d.ts +55 -0
- package/dist/local/sync/schemaDrift.js +53 -0
- package/dist/local/sync/schemas.d.ts +71 -0
- package/dist/local/sync/schemas.js +94 -0
- package/dist/local/sync/socketEventWiring.d.ts +31 -0
- package/dist/local/sync/socketEventWiring.js +130 -0
- package/dist/local/sync/syncCursor.d.ts +40 -0
- package/dist/local/sync/syncCursor.js +55 -0
- package/dist/local/sync/syncPlan.d.ts +54 -0
- package/dist/local/sync/syncPlan.js +50 -0
- package/dist/local/sync/terminalSessionLifecycle.d.ts +20 -0
- package/dist/local/sync/terminalSessionLifecycle.js +50 -0
- package/dist/local/sync/wsFrameHandlers.d.ts +8 -0
- package/dist/local/sync/wsFrameHandlers.js +8 -0
- package/dist/local/transactions/databaseCommitOutbox.d.ts +15 -0
- package/dist/local/transactions/databaseCommitOutbox.js +16 -0
- package/dist/local/transactions/localMutation.d.ts +10 -0
- package/dist/local/transactions/localMutation.js +37 -0
- package/dist/local/transactions/mutations/MutationQueue.d.ts +511 -0
- package/dist/local/transactions/mutations/MutationQueue.js +1498 -0
- package/dist/local/transactions/mutations/MutationStore.d.ts +20 -0
- package/dist/local/transactions/mutations/MutationStore.js +53 -0
- package/dist/local/transactions/mutations/UnconfirmedWrites.d.ts +82 -0
- package/dist/local/transactions/mutations/UnconfirmedWrites.js +104 -0
- package/dist/local/transactions/mutations/batchProcessing.d.ts +64 -0
- package/dist/local/transactions/mutations/batchProcessing.js +349 -0
- package/dist/local/transactions/mutations/coalesceRules.d.ts +58 -0
- package/dist/local/transactions/mutations/coalesceRules.js +140 -0
- package/dist/local/transactions/mutations/commitApi.d.ts +19 -0
- package/dist/local/transactions/mutations/commitApi.js +74 -0
- package/dist/local/transactions/mutations/commitLane.d.ts +70 -0
- package/dist/local/transactions/mutations/commitLane.js +140 -0
- package/dist/local/transactions/mutations/commitLatency.d.ts +52 -0
- package/dist/local/transactions/mutations/commitLatency.js +130 -0
- package/dist/local/transactions/mutations/commitPayload.d.ts +165 -0
- package/dist/local/transactions/mutations/commitPayload.js +152 -0
- package/dist/local/transactions/mutations/commitTransport.d.ts +36 -0
- package/dist/local/transactions/mutations/commitTransport.js +104 -0
- package/dist/local/transactions/mutations/deltaConfirmation.d.ts +63 -0
- package/dist/local/transactions/mutations/deltaConfirmation.js +235 -0
- package/dist/local/transactions/mutations/durableCommitRestore.d.ts +17 -0
- package/dist/local/transactions/mutations/durableCommitRestore.js +95 -0
- package/dist/local/transactions/mutations/durableWriteStore.d.ts +14 -0
- package/dist/local/transactions/mutations/durableWriteStore.js +12 -0
- package/dist/local/transactions/mutations/executionSelection.d.ts +6 -0
- package/dist/local/transactions/mutations/executionSelection.js +42 -0
- package/dist/local/transactions/mutations/failureHandling.d.ts +16 -0
- package/dist/local/transactions/mutations/failureHandling.js +130 -0
- package/dist/local/transactions/mutations/failurePolicy.d.ts +13 -0
- package/dist/local/transactions/mutations/failurePolicy.js +48 -0
- package/dist/local/transactions/mutations/localMutation.d.ts +58 -0
- package/dist/local/transactions/mutations/localMutation.js +75 -0
- package/dist/local/transactions/mutations/modelOperations.d.ts +44 -0
- package/dist/local/transactions/mutations/modelOperations.js +144 -0
- package/dist/local/transactions/mutations/mutationPersistence.d.ts +25 -0
- package/dist/local/transactions/mutations/mutationPersistence.js +188 -0
- package/dist/local/transactions/mutations/pendingDrain.d.ts +33 -0
- package/dist/local/transactions/mutations/pendingDrain.js +112 -0
- package/dist/local/transactions/mutations/processingScheduler.d.ts +14 -0
- package/dist/local/transactions/mutations/processingScheduler.js +24 -0
- package/dist/local/transactions/mutations/queueCoalescing.d.ts +13 -0
- package/dist/local/transactions/mutations/queueCoalescing.js +35 -0
- package/dist/local/transactions/mutations/replayValidation.d.ts +187 -0
- package/dist/local/transactions/mutations/replayValidation.js +164 -0
- package/dist/local/transactions/reconnectDrain.d.ts +11 -0
- package/dist/local/transactions/reconnectDrain.js +13 -0
- package/dist/local/utils/mobxSetup.d.ts +53 -0
- package/dist/local/utils/mobxSetup.js +330 -0
- package/dist/local/views/QueryView.d.ts +79 -0
- package/dist/local/views/QueryView.js +218 -0
- package/dist/local/views/ViewRegistry.d.ts +20 -0
- package/dist/local/views/ViewRegistry.js +57 -0
- package/dist/local/views/incrementalView.d.ts +45 -0
- package/dist/local/views/incrementalView.js +69 -0
- package/dist/plugin.d.ts +285 -0
- package/dist/plugin.js +106 -0
- package/dist/presenceStream.d.ts +69 -0
- package/dist/presenceStream.js +200 -0
- package/dist/react/AbloProvider.d.ts +242 -0
- package/dist/react/AbloProvider.js +456 -0
- package/dist/react/ClientSideSuspense.d.ts +36 -0
- package/dist/react/ClientSideSuspense.js +17 -0
- package/dist/react/DefaultFallback.d.ts +24 -0
- package/dist/react/DefaultFallback.js +43 -0
- package/dist/react/context.d.ts +55 -0
- package/dist/react/context.js +29 -0
- package/dist/react/createAbloReact.d.ts +50 -0
- package/dist/react/createAbloReact.js +48 -0
- package/dist/react/internalContext.d.ts +33 -0
- package/dist/react/internalContext.js +3 -0
- package/dist/react/useAblo.d.ts +96 -0
- package/dist/react/useAblo.js +120 -0
- package/dist/react/useCurrentUserId.d.ts +2 -0
- package/dist/react/useCurrentUserId.js +12 -0
- package/dist/react/useErrorListener.d.ts +2 -0
- package/dist/react/useErrorListener.js +14 -0
- package/dist/react/useMutationFailureListener.d.ts +8 -0
- package/dist/react/useMutationFailureListener.js +19 -0
- package/dist/react/useMutators.d.ts +56 -0
- package/dist/react/useMutators.js +84 -0
- package/dist/react/useSyncStatus.d.ts +19 -0
- package/dist/react/useSyncStatus.js +37 -0
- package/dist/react/useUndoScope.d.ts +34 -0
- package/dist/react/useUndoScope.js +73 -0
- package/dist/react.d.ts +18 -0
- package/dist/react.js +14 -0
- package/dist/reactRuntime.d.ts +4 -0
- package/dist/reactRuntime.js +2 -0
- package/dist/surface.d.ts +36 -0
- package/dist/surface.js +77 -0
- package/dist/useReactive.d.ts +6 -0
- package/dist/useReactive.js +43 -0
- package/package.json +119 -0
- package/src/Ablo.ts +456 -0
- package/src/client.ts +374 -0
- package/src/core.ts +104 -0
- package/src/humans.ts +61 -0
- package/src/index.ts +40 -0
- package/src/local/BaseSyncedStore.ts +1991 -0
- package/src/local/Database.ts +2052 -0
- package/src/local/InstanceCache.ts +1503 -0
- package/src/local/LazyReferenceCollection.ts +563 -0
- package/src/local/Model.ts +1124 -0
- package/src/local/ModelRegistry.ts +762 -0
- package/src/local/NetworkMonitor.ts +88 -0
- package/src/local/RuntimeContext.ts +141 -0
- package/src/local/SyncClient.ts +2131 -0
- package/src/local/adapters/alwaysOnline.ts +20 -0
- package/src/local/adapters/inMemoryStorage.ts +141 -0
- package/src/local/client/clientPrelude.ts +112 -0
- package/src/local/client/consoleLogger.ts +60 -0
- package/src/local/client/createInternalComponents.ts +163 -0
- package/src/local/client/createModelProxy.ts +1390 -0
- package/src/local/client/modelRegistration.ts +350 -0
- package/src/local/client/options.ts +526 -0
- package/src/local/client/reactiveEngine.ts +935 -0
- package/src/local/client/resourceTypes.ts +37 -0
- package/src/local/client/schemaConfig.ts +194 -0
- package/src/local/client/storeCluster.ts +172 -0
- package/src/local/client/storeLifecycle.ts +343 -0
- package/src/local/client/validateAbloOptions.ts +95 -0
- package/src/local/client/wsMutationExecutor.ts +110 -0
- package/src/local/context.ts +96 -0
- package/src/local/coordination/ClaimLog.ts +39 -0
- package/src/local/interfaces/index.ts +468 -0
- package/src/local/localModelContract.ts +15 -0
- package/src/local/logPosition.ts +84 -0
- package/src/local/mutationPersistence.ts +7 -0
- package/src/local/mutators/RecordingMutation.ts +222 -0
- package/src/local/mutators/Transaction.ts +97 -0
- package/src/local/mutators/UndoManager.ts +741 -0
- package/src/local/mutators/defineMutators.ts +76 -0
- package/src/local/mutators/inverseOp.ts +83 -0
- package/src/local/mutators/mutateActions.ts +167 -0
- package/src/local/mutators/readerActions.ts +99 -0
- package/src/local/mutators/undoApply.ts +141 -0
- package/src/local/persistence.ts +16 -0
- package/src/local/query/QueryProcessor.ts +347 -0
- package/src/local/query/client.ts +197 -0
- package/src/local/query/types.ts +102 -0
- package/src/local/schema/serialize.ts +1 -0
- package/src/local/store/queryApi.ts +56 -0
- package/src/local/storeContract.ts +146 -0
- package/src/local/stores/DatabaseManager.ts +507 -0
- package/src/local/stores/ObjectStore.ts +449 -0
- package/src/local/stores/ObjectStoreContract.ts +48 -0
- package/src/local/stores/StoreManager.ts +388 -0
- package/src/local/stores/SyncActionStore.ts +579 -0
- package/src/local/stores/openIDBWithTimeout.ts +195 -0
- package/src/local/stores/persistenceCleanup.ts +43 -0
- package/src/local/stores/persistenceIdentity.ts +83 -0
- package/src/local/stores/syncAction.ts +21 -0
- package/src/local/stores/v1PersistenceDeletion.ts +21 -0
- package/src/local/sync/BootstrapFetcher.ts +1224 -0
- package/src/local/sync/ConnectionManager.ts +15 -0
- package/src/local/sync/OnDemandLoader.ts +927 -0
- package/src/local/sync/SubscriptionManager.ts +300 -0
- package/src/local/sync/SyncWebSocket.ts +584 -0
- package/src/local/sync/bootstrapApply.ts +130 -0
- package/src/local/sync/commitFrames.ts +16 -0
- package/src/local/sync/connectionManagerLifecycle.ts +158 -0
- package/src/local/sync/contextPorts.ts +37 -0
- package/src/local/sync/createClaimStream.ts +668 -0
- package/src/local/sync/createSnapshot.ts +160 -0
- package/src/local/sync/credentialLifecycle.ts +18 -0
- package/src/local/sync/deltaPipeline.ts +473 -0
- package/src/local/sync/groupChange.ts +343 -0
- package/src/local/sync/initialize.ts +205 -0
- package/src/local/sync/participants.ts +564 -0
- package/src/local/sync/persistedPrefix.ts +27 -0
- package/src/local/sync/reconnect.ts +88 -0
- package/src/local/sync/schemaDrift.ts +86 -0
- package/src/local/sync/schemas.ts +118 -0
- package/src/local/sync/socketEventWiring.ts +196 -0
- package/src/local/sync/syncCursor.ts +62 -0
- package/src/local/sync/syncPlan.ts +89 -0
- package/src/local/sync/terminalSessionLifecycle.ts +66 -0
- package/src/local/sync/wsFrameHandlers.ts +20 -0
- package/src/local/transactions/databaseCommitOutbox.ts +33 -0
- package/src/local/transactions/localMutation.ts +58 -0
- package/src/local/transactions/mutations/MutationQueue.ts +1998 -0
- package/src/local/transactions/mutations/MutationStore.ts +65 -0
- package/src/local/transactions/mutations/UnconfirmedWrites.ts +133 -0
- package/src/local/transactions/mutations/batchProcessing.ts +469 -0
- package/src/local/transactions/mutations/coalesceRules.ts +192 -0
- package/src/local/transactions/mutations/commitApi.ts +97 -0
- package/src/local/transactions/mutations/commitLane.ts +191 -0
- package/src/local/transactions/mutations/commitLatency.ts +164 -0
- package/src/local/transactions/mutations/commitPayload.ts +281 -0
- package/src/local/transactions/mutations/commitTransport.ts +174 -0
- package/src/local/transactions/mutations/deltaConfirmation.ts +298 -0
- package/src/local/transactions/mutations/durableCommitRestore.ts +128 -0
- package/src/local/transactions/mutations/durableWriteStore.ts +21 -0
- package/src/local/transactions/mutations/executionSelection.ts +42 -0
- package/src/local/transactions/mutations/failureHandling.ts +154 -0
- package/src/local/transactions/mutations/failurePolicy.ts +61 -0
- package/src/local/transactions/mutations/localMutation.ts +135 -0
- package/src/local/transactions/mutations/modelOperations.ts +210 -0
- package/src/local/transactions/mutations/mutationPersistence.ts +231 -0
- package/src/local/transactions/mutations/pendingDrain.ts +160 -0
- package/src/local/transactions/mutations/processingScheduler.ts +35 -0
- package/src/local/transactions/mutations/queueCoalescing.ts +45 -0
- package/src/local/transactions/mutations/replayValidation.ts +192 -0
- package/src/local/transactions/reconnectDrain.ts +24 -0
- package/src/local/utils/mobxSetup.ts +388 -0
- package/src/local/views/QueryView.ts +311 -0
- package/src/local/views/ViewRegistry.ts +61 -0
- package/src/local/views/incrementalView.ts +92 -0
- package/src/plugin.ts +396 -0
- package/src/presenceStream.ts +279 -0
- package/src/react/AbloProvider.tsx +744 -0
- package/src/react/ClientSideSuspense.tsx +57 -0
- package/src/react/DefaultFallback.tsx +60 -0
- package/src/react/context.ts +89 -0
- package/src/react/createAbloReact.ts +116 -0
- package/src/react/internalContext.ts +38 -0
- package/src/react/useAblo.ts +280 -0
- package/src/react/useCurrentUserId.ts +17 -0
- package/src/react/useErrorListener.ts +22 -0
- package/src/react/useMutationFailureListener.ts +34 -0
- package/src/react/useMutators.ts +184 -0
- package/src/react/useSyncStatus.ts +42 -0
- package/src/react/useUndoScope.ts +143 -0
- package/src/react.ts +69 -0
- package/src/reactRuntime.ts +10 -0
- package/src/surface.ts +106 -0
- package/src/useReactive.ts +51 -0
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
export async function remove(ctx, model, context, writeOptions, sourceMutationId) {
|
|
2
|
+
ctx.assertDurableReplayOpen();
|
|
3
|
+
const modelName = model.getModelName();
|
|
4
|
+
if (modelName === 'Activity') {
|
|
5
|
+
ctx.logger.debug('MutationQueue.delete() skipping Activity deletion - permanent audit records', { modelId: model.id });
|
|
6
|
+
const transaction = {
|
|
7
|
+
id: ctx.generateId(), type: 'delete', modelName, modelId: model.id,
|
|
8
|
+
modelKey: ctx.normalizeModelKey(modelName), priorityScore: ctx.computePriorityScore('delete', modelName),
|
|
9
|
+
previousData: model.toJSON(), context, status: 'completed', createdAt: Date.now(), attempts: 0,
|
|
10
|
+
priority: 'high', writeOptions, localOnly: true, confirmation: Promise.resolve(),
|
|
11
|
+
...(sourceMutationId ? { sourceMutationIds: [sourceMutationId] } : {}),
|
|
12
|
+
};
|
|
13
|
+
if (ctx.persistenceReady)
|
|
14
|
+
transaction.sourceMutationIds ??= [transaction.id];
|
|
15
|
+
if (ctx.enableOptimistic)
|
|
16
|
+
ctx.applyOptimisticDelete(model, transaction);
|
|
17
|
+
ctx.emit('transaction:created', transaction);
|
|
18
|
+
ctx.emit('transaction:completed', transaction);
|
|
19
|
+
return transaction;
|
|
20
|
+
}
|
|
21
|
+
const unsentCreate = ctx.takeUnsentCreateForModel(modelName, model.id);
|
|
22
|
+
if (unsentCreate) {
|
|
23
|
+
await ctx.cancelUnsentCreateForDelete(unsentCreate);
|
|
24
|
+
return ctx.completeLocalDelete(model, context, writeOptions, [
|
|
25
|
+
...(unsentCreate.sourceMutationIds ?? []),
|
|
26
|
+
...(sourceMutationId ? [sourceMutationId] : []),
|
|
27
|
+
]);
|
|
28
|
+
}
|
|
29
|
+
const transaction = {
|
|
30
|
+
id: ctx.generateId(), type: 'delete', modelName, modelId: model.id,
|
|
31
|
+
modelKey: ctx.normalizeModelKey(modelName), priorityScore: ctx.computePriorityScore('delete', modelName),
|
|
32
|
+
previousData: model.toJSON(), context, status: 'pending', createdAt: Date.now(), attempts: 0,
|
|
33
|
+
priority: 'high', writeOptions,
|
|
34
|
+
...(sourceMutationId ? { sourceMutationIds: [sourceMutationId] } : {}),
|
|
35
|
+
};
|
|
36
|
+
if (ctx.persistenceReady)
|
|
37
|
+
transaction.sourceMutationIds ??= [transaction.id];
|
|
38
|
+
ctx.attachConfirmation(transaction);
|
|
39
|
+
ctx.add(transaction);
|
|
40
|
+
const canceledUpdates = ctx.cancelTransactionsForModel(model.id, 'update');
|
|
41
|
+
const key = `${modelName}:${model.id}`;
|
|
42
|
+
const pendingMerge = ctx.pendingMergeByModel.get(key);
|
|
43
|
+
transaction.sourceMutationIds = [...new Set([
|
|
44
|
+
...(transaction.sourceMutationIds ?? []),
|
|
45
|
+
...canceledUpdates.flatMap((candidate) => candidate.commitEnvelope ? [] : (candidate.sourceMutationIds ?? [])),
|
|
46
|
+
...(pendingMerge?.sourceMutationIds ?? []),
|
|
47
|
+
])];
|
|
48
|
+
ctx.pendingMergeByModel.delete(key);
|
|
49
|
+
ctx.inFlightByModel.delete(key);
|
|
50
|
+
if (ctx.enableOptimistic)
|
|
51
|
+
ctx.applyOptimisticDelete(model, transaction);
|
|
52
|
+
await ctx.persistQueuedTransaction(transaction, model.toJSON());
|
|
53
|
+
const createBarrier = ctx.findCreateBarrierForDelete(modelName, model.id);
|
|
54
|
+
if (createBarrier)
|
|
55
|
+
ctx.deferDeleteUntilCreateSettles(createBarrier, transaction);
|
|
56
|
+
else
|
|
57
|
+
ctx.stageTransaction(transaction);
|
|
58
|
+
ctx.emit('transaction:created', transaction);
|
|
59
|
+
return transaction;
|
|
60
|
+
}
|
|
61
|
+
function stage(ctx, transaction, model) {
|
|
62
|
+
return ctx.persistenceReady
|
|
63
|
+
? ctx.persistAndStage(transaction, model.toJSON())
|
|
64
|
+
: ctx.stageTransaction(transaction);
|
|
65
|
+
}
|
|
66
|
+
export function create(ctx, model, context, writeOptions, sourceMutationId) {
|
|
67
|
+
ctx.assertDurableReplayOpen();
|
|
68
|
+
const modelName = model.getModelName();
|
|
69
|
+
const transaction = {
|
|
70
|
+
id: ctx.generateId(), type: 'create', modelName, modelId: model.id,
|
|
71
|
+
modelKey: ctx.normalizeModelKey(modelName),
|
|
72
|
+
priorityScore: ctx.computePriorityScore('create', modelName),
|
|
73
|
+
data: ctx.extractCreateData(model), previousData: null, context,
|
|
74
|
+
status: 'pending', createdAt: Date.now(), attempts: 0, priority: 'normal', writeOptions,
|
|
75
|
+
...(sourceMutationId ? { sourceMutationIds: [sourceMutationId] } : {}),
|
|
76
|
+
};
|
|
77
|
+
if (ctx.persistenceReady)
|
|
78
|
+
transaction.sourceMutationIds ??= [transaction.id];
|
|
79
|
+
ctx.attachConfirmation(transaction);
|
|
80
|
+
ctx.add(transaction);
|
|
81
|
+
if (ctx.enableOptimistic)
|
|
82
|
+
ctx.applyOptimisticCreate(model, transaction);
|
|
83
|
+
const staged = stage(ctx, transaction, model);
|
|
84
|
+
if (staged)
|
|
85
|
+
return staged.then(() => { ctx.emit('transaction:created', transaction); return transaction; });
|
|
86
|
+
ctx.emit('transaction:created', transaction);
|
|
87
|
+
return transaction;
|
|
88
|
+
}
|
|
89
|
+
export function update(ctx, model, context, precomputedChanges, writeOptions, sourceMutationId) {
|
|
90
|
+
ctx.assertDurableReplayOpen();
|
|
91
|
+
const modelName = model.getModelName();
|
|
92
|
+
const input = precomputedChanges
|
|
93
|
+
? ctx.mapChangesToInput(modelName, precomputedChanges)
|
|
94
|
+
: ctx.extractUpdateData(model);
|
|
95
|
+
const previousData = ctx.extractPreviousData(model, input);
|
|
96
|
+
model.consumeModifiedFields(Object.keys(input));
|
|
97
|
+
const transaction = {
|
|
98
|
+
id: ctx.generateId(), type: 'update', modelName, modelId: model.id,
|
|
99
|
+
modelKey: ctx.normalizeModelKey(modelName),
|
|
100
|
+
priorityScore: ctx.computePriorityScore('update', modelName),
|
|
101
|
+
data: input, previousData, context, status: 'pending', createdAt: Date.now(), attempts: 0,
|
|
102
|
+
priority: ctx.isReorderPayload(input) ? 'high' : 'normal', writeOptions,
|
|
103
|
+
...(sourceMutationId ? { sourceMutationIds: [sourceMutationId] } : {}),
|
|
104
|
+
};
|
|
105
|
+
if (ctx.persistenceReady)
|
|
106
|
+
transaction.sourceMutationIds ??= [transaction.id];
|
|
107
|
+
ctx.attachConfirmation(transaction);
|
|
108
|
+
ctx.add(transaction);
|
|
109
|
+
if (ctx.enableOptimistic)
|
|
110
|
+
ctx.applyOptimisticUpdate(model, transaction);
|
|
111
|
+
const staged = stage(ctx, transaction, model);
|
|
112
|
+
if (staged)
|
|
113
|
+
return staged.then(() => { ctx.emit('transaction:created', transaction); return transaction; });
|
|
114
|
+
ctx.emit('transaction:created', transaction);
|
|
115
|
+
return transaction;
|
|
116
|
+
}
|
|
117
|
+
export function archive(ctx, model, context, writeOptions, sourceMutationId) {
|
|
118
|
+
return simpleStatusMutation(ctx, 'archive', model, context, writeOptions, sourceMutationId);
|
|
119
|
+
}
|
|
120
|
+
export function unarchive(ctx, model, context) {
|
|
121
|
+
return simpleStatusMutation(ctx, 'unarchive', model, context);
|
|
122
|
+
}
|
|
123
|
+
function simpleStatusMutation(ctx, type, model, context, writeOptions, sourceMutationId) {
|
|
124
|
+
ctx.assertDurableReplayOpen();
|
|
125
|
+
const modelName = model.getModelName();
|
|
126
|
+
const transaction = {
|
|
127
|
+
id: ctx.generateId(), type, modelName, modelId: model.id,
|
|
128
|
+
modelKey: ctx.normalizeModelKey(modelName),
|
|
129
|
+
priorityScore: ctx.computePriorityScore(type, modelName),
|
|
130
|
+
previousData: model.toJSON(), context, status: 'pending', createdAt: Date.now(), attempts: 0,
|
|
131
|
+
priority: 'normal',
|
|
132
|
+
...(writeOptions ? { writeOptions } : {}),
|
|
133
|
+
...(sourceMutationId ? { sourceMutationIds: [sourceMutationId] } : {}),
|
|
134
|
+
};
|
|
135
|
+
if (ctx.persistenceReady)
|
|
136
|
+
transaction.sourceMutationIds ??= [transaction.id];
|
|
137
|
+
ctx.attachConfirmation(transaction);
|
|
138
|
+
ctx.add(transaction);
|
|
139
|
+
const staged = stage(ctx, transaction, model);
|
|
140
|
+
if (staged)
|
|
141
|
+
return staged.then(() => { ctx.emit('transaction:created', transaction); return transaction; });
|
|
142
|
+
ctx.emit('transaction:created', transaction);
|
|
143
|
+
return transaction;
|
|
144
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { RuntimeContext } from '../../RuntimeContext.js';
|
|
2
|
+
import type { MutationPersistencePort } from '../../mutationPersistence.js';
|
|
3
|
+
import type { MutationQueueConfig } from './MutationQueue.js';
|
|
4
|
+
import type { CommitOutboxScope } from '@abloatai/transaction/transactions/settlement/commitEnvelope';
|
|
5
|
+
import type { QueuedMutation } from './commitPayload.js';
|
|
6
|
+
import { MutationStore } from './MutationStore.js';
|
|
7
|
+
import { deserializePersistedTransaction } from './replayValidation.js';
|
|
8
|
+
export interface MutationPersistenceContext {
|
|
9
|
+
readonly runtime: RuntimeContext;
|
|
10
|
+
readonly persistence: MutationPersistencePort | null;
|
|
11
|
+
readonly commitOutboxScope: CommitOutboxScope | null;
|
|
12
|
+
readonly config: Pick<MutationQueueConfig, 'enablePersistence'>;
|
|
13
|
+
readonly store: MutationStore;
|
|
14
|
+
readonly enqueue: (transaction: QueuedMutation) => void;
|
|
15
|
+
readonly computePriorityScore: (type: QueuedMutation['type'], modelName: string) => number;
|
|
16
|
+
readonly deserializeTransaction: (data: Parameters<typeof deserializePersistedTransaction>[0]) => QueuedMutation | null;
|
|
17
|
+
}
|
|
18
|
+
export declare function persistQueuedTransaction(ctx: MutationPersistenceContext, transaction: QueuedMutation, modelData?: Record<string, unknown>): Promise<void>;
|
|
19
|
+
export declare function removePersistedTransaction(ctx: MutationPersistenceContext, transactionId: string): void;
|
|
20
|
+
export declare function settlePersistedFailure(ctx: MutationPersistenceContext, transaction: {
|
|
21
|
+
id?: string;
|
|
22
|
+
sourceMutationIds?: string[];
|
|
23
|
+
}): void;
|
|
24
|
+
export declare function deserializeLegacyPendingMutation(ctx: MutationPersistenceContext, row: object, fallbackMutationId?: string): QueuedMutation | null;
|
|
25
|
+
export declare function loadPersistedTransactions(ctx: MutationPersistenceContext, persistence: MutationPersistencePort, sealedMutationIds?: ReadonlySet<string>): Promise<void>;
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { MutationStore } from './MutationStore.js';
|
|
2
|
+
import { normalizeModelKey } from './commitPayload.js';
|
|
3
|
+
import { deserializePersistedTransaction, legacyPendingMutationRecordSchema, pendingMutationRecordId, pendingMutationRecordSchema, persistedMutationSchema, } from './replayValidation.js';
|
|
4
|
+
export async function persistQueuedTransaction(ctx, transaction, modelData) {
|
|
5
|
+
if (!ctx.persistence)
|
|
6
|
+
return;
|
|
7
|
+
const mutationId = transaction.sourceMutationIds?.[0] ?? transaction.id;
|
|
8
|
+
if (modelData && ctx.commitOutboxScope) {
|
|
9
|
+
await ctx.persistence.saveTransaction({
|
|
10
|
+
id: pendingMutationRecordId(mutationId),
|
|
11
|
+
type: 'pending_mutation',
|
|
12
|
+
storageVersion: 2,
|
|
13
|
+
mutation: {
|
|
14
|
+
mutationId,
|
|
15
|
+
type: transaction.type,
|
|
16
|
+
modelData,
|
|
17
|
+
modelName: transaction.modelName,
|
|
18
|
+
timestamp: new Date(transaction.createdAt).toISOString(),
|
|
19
|
+
...(transaction.type === 'update' && transaction.data
|
|
20
|
+
? { capturedChanges: transaction.data }
|
|
21
|
+
: {}),
|
|
22
|
+
...(transaction.writeOptions !== undefined
|
|
23
|
+
? { writeOptions: transaction.writeOptions }
|
|
24
|
+
: {}),
|
|
25
|
+
},
|
|
26
|
+
scope: ctx.commitOutboxScope,
|
|
27
|
+
timestamp: transaction.createdAt,
|
|
28
|
+
});
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
await ctx.persistence.saveTransaction({
|
|
32
|
+
id: transaction.id,
|
|
33
|
+
type: transaction.type,
|
|
34
|
+
modelName: transaction.modelName,
|
|
35
|
+
modelId: transaction.modelId,
|
|
36
|
+
modelKey: transaction.modelKey,
|
|
37
|
+
...(transaction.data !== undefined ? { data: transaction.data } : {}),
|
|
38
|
+
...(transaction.previousData !== undefined ? { previousData: transaction.previousData } : {}),
|
|
39
|
+
context: transaction.context,
|
|
40
|
+
createdAt: transaction.createdAt,
|
|
41
|
+
...(transaction.writeOptions !== undefined ? { writeOptions: transaction.writeOptions } : {}),
|
|
42
|
+
...(transaction.sourceMutationIds !== undefined
|
|
43
|
+
? { sourceMutationIds: transaction.sourceMutationIds }
|
|
44
|
+
: {}),
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
export function removePersistedTransaction(ctx, transactionId) {
|
|
48
|
+
const ids = [transactionId, pendingMutationRecordId(transactionId), ...(ctx.store.get(transactionId)?.sourceMutationIds ?? []).map(pendingMutationRecordId)];
|
|
49
|
+
for (const id of ids) {
|
|
50
|
+
void ctx.persistence?.removeTransaction(id).catch(() => undefined);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
export function settlePersistedFailure(ctx, transaction) {
|
|
54
|
+
const sourceIds = new Set(transaction.sourceMutationIds ?? []);
|
|
55
|
+
const queued = transaction.id
|
|
56
|
+
? ctx.store.get(transaction.id)
|
|
57
|
+
: undefined;
|
|
58
|
+
const match = queued ?? ctx.store.getAll().find((candidate) => candidate.id === transaction.id ||
|
|
59
|
+
sourceIds.has(candidate.id) ||
|
|
60
|
+
(candidate.sourceMutationIds ?? []).some((id) => sourceIds.has(id)));
|
|
61
|
+
if (match) {
|
|
62
|
+
ctx.store.updateStatus(match.id, 'failed');
|
|
63
|
+
removePersistedTransaction(ctx, match.id);
|
|
64
|
+
}
|
|
65
|
+
for (const sourceId of sourceIds)
|
|
66
|
+
removePersistedTransaction(ctx, sourceId);
|
|
67
|
+
}
|
|
68
|
+
export function deserializeLegacyPendingMutation(ctx, row, fallbackMutationId) {
|
|
69
|
+
const rowType = row.type;
|
|
70
|
+
const parsed = persistedMutationSchema.safeParse(rowType === 'pending_mutation'
|
|
71
|
+
? (pendingMutationRecordSchema.safeParse(row).success
|
|
72
|
+
? pendingMutationRecordSchema.parse(row).mutation
|
|
73
|
+
: legacyPendingMutationRecordSchema.safeParse(row).success
|
|
74
|
+
? legacyPendingMutationRecordSchema.parse(row).mutation
|
|
75
|
+
: null)
|
|
76
|
+
: row);
|
|
77
|
+
if (!parsed.success)
|
|
78
|
+
return null;
|
|
79
|
+
const mutation = parsed.data;
|
|
80
|
+
const writtenAt = Date.parse(mutation.timestamp);
|
|
81
|
+
if (!Number.isFinite(writtenAt) || Date.now() - writtenAt >= 23 * 60 * 60 * 1000) {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
const mutationId = mutation.mutationId ?? fallbackMutationId;
|
|
85
|
+
const modelId = mutation.modelData.id;
|
|
86
|
+
if (!mutationId || typeof modelId !== 'string')
|
|
87
|
+
return null;
|
|
88
|
+
const scope = ctx.commitOutboxScope;
|
|
89
|
+
if (!scope)
|
|
90
|
+
return null;
|
|
91
|
+
const type = mutation.type;
|
|
92
|
+
return {
|
|
93
|
+
id: mutationId,
|
|
94
|
+
type,
|
|
95
|
+
modelName: mutation.modelName,
|
|
96
|
+
modelId,
|
|
97
|
+
modelKey: normalizeModelKey(mutation.modelName),
|
|
98
|
+
...(type === 'create'
|
|
99
|
+
? { data: mutation.modelData }
|
|
100
|
+
: type === 'update'
|
|
101
|
+
? { data: mutation.capturedChanges ?? mutation.modelData }
|
|
102
|
+
: {}),
|
|
103
|
+
...(type === 'delete' || type === 'archive'
|
|
104
|
+
? { previousData: mutation.modelData }
|
|
105
|
+
: { previousData: null }),
|
|
106
|
+
context: {
|
|
107
|
+
userId: scope.participantId,
|
|
108
|
+
organizationId: scope.organizationId,
|
|
109
|
+
},
|
|
110
|
+
status: 'pending',
|
|
111
|
+
createdAt: Date.parse(mutation.timestamp) || Date.now(),
|
|
112
|
+
attempts: 0,
|
|
113
|
+
priority: 'normal',
|
|
114
|
+
priorityScore: ctx.computePriorityScore(type, mutation.modelName),
|
|
115
|
+
...(mutation.writeOptions !== undefined ? { writeOptions: mutation.writeOptions } : {}),
|
|
116
|
+
sourceMutationIds: [mutationId],
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
export async function loadPersistedTransactions(ctx, persistence, sealedMutationIds = new Set()) {
|
|
120
|
+
if (!ctx.config.enablePersistence)
|
|
121
|
+
return;
|
|
122
|
+
try {
|
|
123
|
+
const persisted = await persistence.getPersistedTransactions();
|
|
124
|
+
for (const data of persisted) {
|
|
125
|
+
if (data.type === 'pending_mutation') {
|
|
126
|
+
const legacy = deserializeLegacyPendingMutation(ctx, data);
|
|
127
|
+
if (legacy) {
|
|
128
|
+
const v1 = legacyPendingMutationRecordSchema.safeParse(data);
|
|
129
|
+
if (v1.success && ctx.commitOutboxScope) {
|
|
130
|
+
await persistence.saveTransaction({
|
|
131
|
+
...data,
|
|
132
|
+
storageVersion: 2,
|
|
133
|
+
scope: ctx.commitOutboxScope,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
legacy.id = String(data.id);
|
|
137
|
+
ctx.store.add(legacy);
|
|
138
|
+
ctx.enqueue(legacy);
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
ctx.runtime.logger.debug('[SyncClient] Dropping malformed persisted mutation', {
|
|
142
|
+
rowId: data.id,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
if (data.id === 'mutation-queue' && Array.isArray(data.mutations)) {
|
|
148
|
+
if (data.mutations.length === 0)
|
|
149
|
+
continue;
|
|
150
|
+
let migrated = true;
|
|
151
|
+
for (const [index, mutation] of data.mutations.entries()) {
|
|
152
|
+
if (typeof mutation !== 'object' || mutation === null) {
|
|
153
|
+
migrated = false;
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
const transaction = deserializeLegacyPendingMutation(ctx, mutation, `legacy_mutation_${index}`);
|
|
157
|
+
if (!transaction) {
|
|
158
|
+
migrated = false;
|
|
159
|
+
ctx.runtime.logger.debug('[SyncClient] Dropping malformed persisted mutation', {
|
|
160
|
+
rowId: `mutation-queue:${index}`,
|
|
161
|
+
});
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
await persistQueuedTransaction(ctx, transaction);
|
|
165
|
+
ctx.store.add(transaction);
|
|
166
|
+
ctx.enqueue(transaction);
|
|
167
|
+
}
|
|
168
|
+
if (migrated)
|
|
169
|
+
await persistence.removeTransaction(data.id);
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
const transaction = ctx.deserializeTransaction(data);
|
|
173
|
+
if (!transaction)
|
|
174
|
+
continue;
|
|
175
|
+
if ((transaction.sourceMutationIds ?? []).some((id) => sealedMutationIds.has(id))) {
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
ctx.store.add(transaction);
|
|
179
|
+
ctx.enqueue(transaction);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
catch (error) {
|
|
183
|
+
ctx.runtime.observability.captureMutationFailure({
|
|
184
|
+
context: 'load-persisted-transactions',
|
|
185
|
+
error: error instanceof Error ? error : String(error),
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { RuntimeContext } from '../../RuntimeContext.js';
|
|
2
|
+
import type { QueuedMutation } from './commitPayload.js';
|
|
3
|
+
import type { MutationStore } from './MutationStore.js';
|
|
4
|
+
import type { OptimisticUpdateEntry } from './localMutation.js';
|
|
5
|
+
import type { MutationCommitResult } from '@abloatai/transaction/wire/commit';
|
|
6
|
+
import type { DurableCommitEnvelope } from '@abloatai/transaction/transactions/settlement/commitEnvelope';
|
|
7
|
+
export interface PendingDrainContext {
|
|
8
|
+
readonly runtime: RuntimeContext;
|
|
9
|
+
readonly config: {
|
|
10
|
+
deltaConfirmationTimeout: number;
|
|
11
|
+
};
|
|
12
|
+
readonly store: MutationStore;
|
|
13
|
+
executionQueue: QueuedMutation[];
|
|
14
|
+
readonly optimisticUpdates: Map<string, OptimisticUpdateEntry>;
|
|
15
|
+
readonly assertDurableReplayOpen: () => void;
|
|
16
|
+
readonly processCommitLane: () => Promise<void>;
|
|
17
|
+
readonly takePendingDrainBatch: (pending: QueuedMutation[]) => QueuedMutation[];
|
|
18
|
+
readonly ensureCommitEnvelope: (batch: QueuedMutation[]) => string;
|
|
19
|
+
readonly ensureDerivedFields: (transaction: QueuedMutation) => void;
|
|
20
|
+
readonly sourceMutationIdsFor: (batch: readonly QueuedMutation[]) => string[];
|
|
21
|
+
readonly sealDurableCommit: (input: Parameters<typeof import('./commitTransport.js').sealDurableCommit>[1]) => Promise<DurableCommitEnvelope>;
|
|
22
|
+
readonly assertEnvelopeInsideReplayWindow: (envelope: DurableCommitEnvelope) => void;
|
|
23
|
+
readonly parseMutationCommitResult: (value: Awaited<ReturnType<import('../../interfaces/index.js').MutationExecutor['commit']>>) => MutationCommitResult;
|
|
24
|
+
readonly dispatchCommitBounded: (...args: Parameters<import('../../interfaces/index.js').MutationExecutor['commit']>) => ReturnType<import('../../interfaces/index.js').MutationExecutor['commit']>;
|
|
25
|
+
readonly persistDurableCommitAcceptance: (envelope: DurableCommitEnvelope, result: MutationCommitResult) => Promise<DurableCommitEnvelope>;
|
|
26
|
+
readonly removeDurableCommit: (idempotencyKey: string) => Promise<void>;
|
|
27
|
+
readonly scheduleReplicationLagTimeout: (transactionId: string, clientTxId?: string, correlationId?: string) => void;
|
|
28
|
+
readonly scheduleDeltaConfirmationTimeout: (transaction: QueuedMutation, timeoutMs: number) => void;
|
|
29
|
+
readonly enqueue: (transaction: QueuedMutation) => void;
|
|
30
|
+
readonly recentDeltaCorrelations: Map<string, number>;
|
|
31
|
+
readonly emit: (event: string, payload: object) => boolean;
|
|
32
|
+
}
|
|
33
|
+
export declare function drainPendingSettlements(ctx: PendingDrainContext): Promise<void>;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { applyWriteOptions, TX_TYPE_TO_MUTATION_OP } from './commitPayload.js';
|
|
2
|
+
export async function drainPendingSettlements(ctx) {
|
|
3
|
+
ctx.assertDurableReplayOpen();
|
|
4
|
+
// Kick the commit lane too: atomic envelopes from `commits.create()` may
|
|
5
|
+
// have been left at the head of the lane while the connection was down.
|
|
6
|
+
// Fire-and-forget; processCommitLane serializes itself.
|
|
7
|
+
void ctx.processCommitLane();
|
|
8
|
+
// Collect pending transactions in created order
|
|
9
|
+
const pending = ctx.store.getByStatus('pending').sort((a, b) => a.createdAt - b.createdAt);
|
|
10
|
+
if (pending.length === 0)
|
|
11
|
+
return;
|
|
12
|
+
const pendingIds = new Set(pending.map((tx) => tx.id));
|
|
13
|
+
// These rows may already be waiting behind the normal batch timer. The
|
|
14
|
+
// reconnect fast path takes ownership of them for this attempt so the same
|
|
15
|
+
// transaction cannot dispatch concurrently through both paths.
|
|
16
|
+
ctx.executionQueue = ctx.executionQueue.filter((tx) => !pendingIds.has(tx.id));
|
|
17
|
+
const remaining = [...pending];
|
|
18
|
+
while (remaining.length > 0) {
|
|
19
|
+
const batch = ctx.takePendingDrainBatch(remaining);
|
|
20
|
+
if (batch.length === 0)
|
|
21
|
+
break;
|
|
22
|
+
const batchIds = new Set(batch.map((tx) => tx.id));
|
|
23
|
+
const nextRemaining = remaining.filter((tx) => !batchIds.has(tx.id));
|
|
24
|
+
try {
|
|
25
|
+
const idempotencyKey = ctx.ensureCommitEnvelope(batch);
|
|
26
|
+
const projectedOperations = batch.map((tx) => {
|
|
27
|
+
ctx.ensureDerivedFields(tx);
|
|
28
|
+
return applyWriteOptions({
|
|
29
|
+
type: TX_TYPE_TO_MUTATION_OP[tx.type],
|
|
30
|
+
model: tx.modelKey,
|
|
31
|
+
id: tx.modelId,
|
|
32
|
+
input: tx.type === 'create' || tx.type === 'update' ? tx.data || {} : undefined,
|
|
33
|
+
transactionId: tx.id,
|
|
34
|
+
}, tx);
|
|
35
|
+
});
|
|
36
|
+
const durableEnvelope = await ctx.sealDurableCommit({
|
|
37
|
+
idempotencyKey,
|
|
38
|
+
origin: 'model_batch',
|
|
39
|
+
operations: projectedOperations,
|
|
40
|
+
sourceMutationIds: ctx.sourceMutationIdsFor(batch),
|
|
41
|
+
createdAt: Math.min(...batch.map((transaction) => transaction.createdAt)),
|
|
42
|
+
sealedAt: batch[0]?.commitEnvelope?.sealedAt ?? Date.now(),
|
|
43
|
+
sequence: batch[0]?.commitEnvelope?.sequence,
|
|
44
|
+
});
|
|
45
|
+
ctx.assertEnvelopeInsideReplayWindow(durableEnvelope);
|
|
46
|
+
const result = ctx.parseMutationCommitResult(await ctx.dispatchCommitBounded(durableEnvelope.operations, {
|
|
47
|
+
idempotencyKey,
|
|
48
|
+
}));
|
|
49
|
+
await ctx.persistDurableCommitAcceptance(durableEnvelope, result);
|
|
50
|
+
if (result.status === 'queued') {
|
|
51
|
+
// Reconnect flushes use the same accepted-vs-confirmed contract as
|
|
52
|
+
// the normal lane. A queued source receipt retains the envelope and
|
|
53
|
+
// waits for exact correlation; it is never promoted by the reconnect
|
|
54
|
+
// shortcut itself.
|
|
55
|
+
for (const tx of batch) {
|
|
56
|
+
tx.requiresCorrelatedDelta = true;
|
|
57
|
+
tx.syncIdNeededForCompletion = undefined;
|
|
58
|
+
tx.correlationId = result.correlationId;
|
|
59
|
+
const echoSyncId = result.correlationId
|
|
60
|
+
? ctx.recentDeltaCorrelations.get(result.correlationId)
|
|
61
|
+
: undefined;
|
|
62
|
+
if (echoSyncId !== undefined) {
|
|
63
|
+
ctx.store.updateStatus(tx.id, 'completed');
|
|
64
|
+
ctx.emit('transaction:completed', tx);
|
|
65
|
+
ctx.emit(`transaction:completed:${tx.id}`, tx);
|
|
66
|
+
ctx.optimisticUpdates.delete(tx.id);
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
ctx.store.updateStatus(tx.id, 'awaiting_delta');
|
|
70
|
+
ctx.scheduleReplicationLagTimeout(tx.id, idempotencyKey, result.correlationId);
|
|
71
|
+
ctx.scheduleDeltaConfirmationTimeout(tx, ctx.config.deltaConfirmationTimeout);
|
|
72
|
+
}
|
|
73
|
+
if (batch.every((tx) => tx.status === 'completed')) {
|
|
74
|
+
await ctx.removeDurableCommit(idempotencyKey);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
await ctx.removeDurableCommit(idempotencyKey);
|
|
79
|
+
// Mark this request envelope as completed before moving to the next.
|
|
80
|
+
for (const tx of batch) {
|
|
81
|
+
ctx.store.updateStatus(tx.id, 'completed');
|
|
82
|
+
ctx.emit('transaction:completed', tx);
|
|
83
|
+
ctx.emit(`transaction:completed:${tx.id}`, tx);
|
|
84
|
+
ctx.optimisticUpdates.delete(tx.id);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
ctx.runtime.logger.debug('txn:commit', 0, {
|
|
88
|
+
count: batch.length,
|
|
89
|
+
lastSyncId: result.lastSyncId,
|
|
90
|
+
});
|
|
91
|
+
remaining.splice(0, remaining.length, ...nextRemaining);
|
|
92
|
+
}
|
|
93
|
+
catch (err) {
|
|
94
|
+
// If one request fails, hand it and every later request back to the
|
|
95
|
+
// normal lane. Their envelopes stay attached for safe retry.
|
|
96
|
+
const networkUnavailable = !ctx.runtime.onlineStatus.isOnline();
|
|
97
|
+
const isNetworkError = err instanceof Error &&
|
|
98
|
+
(err.message.includes('Failed to fetch') ||
|
|
99
|
+
err.message.includes('Network request failed') ||
|
|
100
|
+
err.message.includes('NetworkError'));
|
|
101
|
+
if (!networkUnavailable || !isNetworkError) {
|
|
102
|
+
ctx.runtime.observability.breadcrumb('Batch flush fallback failed', 'sync.transaction', 'warning', {
|
|
103
|
+
error: err instanceof Error ? err.message : String(err),
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
for (const tx of [...batch, ...nextRemaining]) {
|
|
107
|
+
ctx.enqueue(tx);
|
|
108
|
+
}
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface ProcessingSchedulerContext {
|
|
2
|
+
readonly processScheduled: boolean;
|
|
3
|
+
readonly setProcessScheduled: (value: boolean) => void;
|
|
4
|
+
readonly processTimer: ReturnType<typeof setTimeout> | undefined;
|
|
5
|
+
readonly setProcessTimer: (timer: ReturnType<typeof setTimeout> | undefined) => void;
|
|
6
|
+
readonly executingCount: number;
|
|
7
|
+
readonly maxExecutingTransactions: number;
|
|
8
|
+
readonly batchDelay: number;
|
|
9
|
+
readonly processBatch: () => void;
|
|
10
|
+
readonly logger: {
|
|
11
|
+
debug: (message: string, fields?: Record<string, number>) => void;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export declare function scheduleProcessing(ctx: ProcessingSchedulerContext, immediate?: boolean): void;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export function scheduleProcessing(ctx, immediate = false) {
|
|
2
|
+
if (ctx.processScheduled)
|
|
3
|
+
return;
|
|
4
|
+
if (ctx.executingCount >= ctx.maxExecutingTransactions) {
|
|
5
|
+
ctx.logger.debug('[MutationQueue] Backpressure: delaying batch, too many executing', {
|
|
6
|
+
executingCount: ctx.executingCount, max: ctx.maxExecutingTransactions,
|
|
7
|
+
});
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
ctx.setProcessScheduled(true);
|
|
11
|
+
const run = () => {
|
|
12
|
+
ctx.setProcessScheduled(false);
|
|
13
|
+
ctx.processBatch();
|
|
14
|
+
};
|
|
15
|
+
if (immediate || ctx.batchDelay <= 0) {
|
|
16
|
+
(typeof queueMicrotask === 'function' ? queueMicrotask : (callback) => Promise.resolve().then(callback))(run);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const timer = setTimeout(() => {
|
|
20
|
+
ctx.setProcessTimer(undefined);
|
|
21
|
+
run();
|
|
22
|
+
}, Math.max(0, ctx.batchDelay));
|
|
23
|
+
ctx.setProcessTimer(timer);
|
|
24
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { QueuedMutation } from './commitPayload.js';
|
|
2
|
+
export interface QueueCoalescingContext {
|
|
3
|
+
readonly executionQueue: QueuedMutation[];
|
|
4
|
+
readonly inFlightByModel: Set<string>;
|
|
5
|
+
readonly pendingMergeByModel: Map<string, {
|
|
6
|
+
data: QueuedMutation['data'];
|
|
7
|
+
sourceMutationIds: string[];
|
|
8
|
+
}>;
|
|
9
|
+
readonly ensureDerivedFields: (transaction: QueuedMutation) => void;
|
|
10
|
+
readonly scheduleProcessing: (immediate: boolean) => void;
|
|
11
|
+
readonly storeRemove: (transactionId: string) => void;
|
|
12
|
+
}
|
|
13
|
+
export declare function enqueueTransaction(ctx: QueueCoalescingContext, transaction: QueuedMutation): void;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { mergeUpdateData } from './coalesceRules.js';
|
|
2
|
+
import { hasCommitCoalescingBarrier } from './commitPayload.js';
|
|
3
|
+
export function enqueueTransaction(ctx, transaction) {
|
|
4
|
+
ctx.ensureDerivedFields(transaction);
|
|
5
|
+
const modelKey = `${transaction.modelName}:${transaction.modelId}`;
|
|
6
|
+
if (transaction.type === 'update' && transaction.attempts === 0 && !transaction.commitEnvelope) {
|
|
7
|
+
const preserveWatermark = hasCommitCoalescingBarrier(transaction.writeOptions);
|
|
8
|
+
if (!preserveWatermark && ctx.inFlightByModel.has(modelKey)) {
|
|
9
|
+
const previous = ctx.pendingMergeByModel.get(modelKey);
|
|
10
|
+
const merged = mergeUpdateData(previous?.data ?? {}, transaction.data || {}, transaction.modelName);
|
|
11
|
+
ctx.pendingMergeByModel.set(modelKey, {
|
|
12
|
+
data: merged,
|
|
13
|
+
sourceMutationIds: [...new Set([...(previous?.sourceMutationIds ?? []), ...(transaction.sourceMutationIds ?? [])])],
|
|
14
|
+
});
|
|
15
|
+
ctx.storeRemove(transaction.id);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const pendingInQueue = ctx.executionQueue.find((candidate) => candidate.id !== transaction.id && candidate.type === 'update' &&
|
|
19
|
+
candidate.modelId === transaction.modelId && candidate.modelName === transaction.modelName &&
|
|
20
|
+
!hasCommitCoalescingBarrier(candidate.writeOptions));
|
|
21
|
+
if (!preserveWatermark && pendingInQueue) {
|
|
22
|
+
pendingInQueue.data = mergeUpdateData(pendingInQueue.data || {}, transaction.data || {}, transaction.modelName);
|
|
23
|
+
pendingInQueue.sourceMutationIds = [...new Set([
|
|
24
|
+
...(pendingInQueue.sourceMutationIds ?? []), ...(transaction.sourceMutationIds ?? []),
|
|
25
|
+
])];
|
|
26
|
+
ctx.storeRemove(transaction.id);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (transaction.priority === 'high')
|
|
31
|
+
ctx.executionQueue.unshift(transaction);
|
|
32
|
+
else
|
|
33
|
+
ctx.executionQueue.push(transaction);
|
|
34
|
+
ctx.scheduleProcessing(transaction.priority === 'high');
|
|
35
|
+
}
|