akanjs 3.2.0-rc.2 → 3.2.0-rc.21
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/ARCHITECTURE.md +42 -41
- package/CHANGELOG.md +593 -3
- package/CONTRIBUTING.md +26 -0
- package/base/base.ts +2 -1
- package/client/clientRuntime.ts +1 -0
- package/client/cn.ts +2 -2
- package/client/csrTypes.ts +15 -1
- package/client/frameConfig.ts +5 -2
- package/client/index.ts +249 -26
- package/client/locale.ts +1 -0
- package/client/makePageProto.tsx +33 -82
- package/client/nativeThemeColor.ts +51 -0
- package/client/route/RouteDefinition.ts +24 -7
- package/client/route/RouteGuards.ts +2 -1
- package/client/route/RouteHead.ts +11 -2
- package/client/route/routeArgs.ts +7 -0
- package/client/router.ts +25 -6
- package/client/rscNavigation.ts +3 -3
- package/client/rscRoutePatch.ts +42 -0
- package/client/viewportMeta.ts +34 -4
- package/common/CodeTranscript.ts +1 -1
- package/common/PipelinePlanSchema.ts +2 -2
- package/common/akanGraph.ts +1 -1
- package/common/err.ts +122 -0
- package/common/facetBarrel.ts +38 -0
- package/common/failureCode.ts +18 -0
- package/common/index.ts +9 -0
- package/common/isGuardRefusal.ts +14 -0
- package/common/mcpExposure.ts +17 -4
- package/common/mcpFailure.ts +9 -0
- package/common/pipelinePlan.ts +1 -1
- package/common/restartSchedule.ts +55 -0
- package/common/routeConvention.ts +3 -1
- package/common/types.ts +18 -1
- package/constant/constantRegistry.ts +4 -8
- package/constant/crystalize.ts +1 -2
- package/constant/getDefault.ts +1 -1
- package/constant/immerify.ts +1 -1
- package/constant/index.ts +78 -21
- package/constant/purify.ts +4 -9
- package/constant/serialize.ts +4 -2
- package/constant/types.ts +1 -0
- package/constant/via.ts +1 -2
- package/dictionary/agent.dictionary.ts +4 -0
- package/dictionary/base.dictionary.ts +1 -0
- package/dictionary/locale.ts +1 -1
- package/dictionary/trans.ts +14 -97
- package/document/databaseRegistry.ts +2 -1
- package/document/documentSchema.ts +1 -0
- package/document/filterMeta.ts +4 -1
- package/document/into.ts +2 -1
- package/document/loaderInfo.ts +2 -1
- package/document/noDocumentError.ts +4 -1
- package/document/queryEvaluator.ts +1 -1
- package/document/types.ts +1 -1
- package/fetch/client/fetchClient.ts +87 -270
- package/fetch/client/fetchClientTypes.ts +49 -0
- package/fetch/client/fetchHandle.ts +11 -1
- package/fetch/client/httpClient.ts +14 -2
- package/fetch/client/inflightQueries.ts +17 -0
- package/fetch/client/remoteError.ts +4 -20
- package/fetch/client/requestScope.ts +32 -0
- package/fetch/client/signalCatalog.ts +131 -0
- package/fetch/client/wsClient.ts +11 -0
- package/fetch/index.ts +1 -0
- package/fetch/staleReads.ts +27 -0
- package/fetch/streamedRead.ts +23 -0
- package/package.json +1 -1
- package/server/agentHooks.ts +16 -0
- package/server/akanApp.ts +107 -1272
- package/server/akanOption.ts +17 -4
- package/server/akanServer.ts +167 -675
- package/server/artifact/builderRpc.ts +33 -10
- package/server/artifact/ipcTypes.ts +9 -3
- package/server/artifact/routeSeedIndexStore.ts +4 -2
- package/server/assetHeaders.ts +6 -0
- package/server/console.ts +6 -4
- package/server/decorators.ts +2 -2
- package/server/devtools/README.md +4 -3
- package/server/devtools/devtoolsRouter.ts +9 -5
- package/server/devtools/studio/studioRoute.ts +18 -11
- package/server/di/diLifecycle.ts +24 -35
- package/server/di/predefinedAdaptor.ts +24 -1
- package/server/di/resolveHierarchy.ts +19 -6
- package/server/di/utils.ts +7 -3
- package/server/ed25519Key.ts +31 -15
- package/server/flightRowReader.ts +95 -18
- package/server/gateway/childSupervisor.ts +326 -0
- package/server/gateway/devHostChannel.ts +47 -0
- package/server/gateway/gatewayChildren.ts +135 -0
- package/server/gateway/gatewayConfig.ts +182 -0
- package/server/gateway/gatewayHttpProxy.ts +231 -0
- package/server/gateway/gatewayLogging.ts +111 -0
- package/server/gateway/gatewayRooms.ts +141 -0
- package/server/gateway/gatewayStatus.ts +116 -0
- package/server/gateway/gatewayStatusPage.ts +54 -0
- package/server/gateway/gatewayWebSocketRelay.ts +149 -0
- package/server/head.tsx +4 -19
- package/server/hmr/devHmrController.ts +10 -1
- package/server/htmlResultCache.ts +234 -0
- package/server/imageOptimizer.ts +4 -1
- package/server/index.ts +257 -17
- package/server/lifecycle/parentChannel.ts +78 -0
- package/server/lifecycle/shutdownManager.ts +6 -3
- package/server/lifecycle/startGuard.ts +90 -0
- package/server/logging/logControlSocket.ts +42 -3
- package/server/logging/serverLogging.ts +74 -0
- package/server/machine/machineToken.ts +10 -3
- package/server/mcp/AgentTools.ts +149 -0
- package/server/mcp/AgentToolsAccount.ts +26 -0
- package/server/mcp/McpDispatcher.ts +48 -19
- package/server/mcp/McpPages.ts +21 -6
- package/server/mcp/McpRateLimiter.ts +14 -0
- package/server/mcp/McpRouter.ts +52 -23
- package/server/mcp/McpUiSession.ts +2 -1
- package/server/mcp/index.ts +1 -0
- package/server/nativeFile.ts +30 -11
- package/server/nativeSecret.ts +12 -3
- package/server/oauth/OAuthClientIdMetadata.ts +2 -2
- package/server/ops/opsRoute.ts +5 -1
- package/server/ops/opsToken.ts +3 -1
- package/server/resolver/CascadeRunner.ts +10 -4
- package/server/resolver/database.resolver.ts +10 -417
- package/server/resolver/databaseModelInstance.ts +200 -0
- package/server/resolver/invalidSignal.ts +7 -0
- package/server/resolver/livePause.ts +34 -0
- package/server/resolver/modelFacade.ts +136 -0
- package/server/resolver/modelLoaders.ts +52 -0
- package/server/resolver/modelSchema.ts +52 -0
- package/server/resolver/signal.resolver.ts +92 -116
- package/server/resolver/timedQuery.ts +16 -0
- package/server/resolver/websocketOf.ts +23 -0
- package/server/routeElementComposer.tsx +131 -9
- package/server/routeState.ts +5 -1
- package/server/routeTreeBuilder.ts +22 -9
- package/server/routing/apiRouter.ts +44 -25
- package/server/routing/builtinRoutes.ts +194 -0
- package/server/routing/localRooms.ts +34 -0
- package/server/routing/roomReplay.ts +2 -2
- package/server/routing/serverLimits.ts +4 -1
- package/server/routing/serverListener.ts +194 -0
- package/server/rscClient.tsx +210 -227
- package/server/rscClientCache.ts +165 -0
- package/server/rscClientChunks.ts +209 -0
- package/server/rscClientFetch.ts +15 -3
- package/server/rscClientPatch.ts +78 -15
- package/server/rscDocumentHead.tsx +170 -0
- package/server/rscFlightRenderer.ts +83 -0
- package/server/rscFlightStream.ts +257 -0
- package/server/rscHeadPatch.ts +17 -56
- package/server/rscHostReloads.ts +238 -0
- package/server/rscHostRenderStream.ts +297 -0
- package/server/rscHostRequests.ts +158 -0
- package/server/rscHttp.ts +18 -6
- package/server/rscInlineStream.ts +34 -0
- package/server/rscNavigationState.ts +21 -1
- package/server/rscPagePrompts.ts +11 -5
- package/server/rscParkedRedirect.ts +23 -0
- package/server/rscPatchPlanner.ts +220 -0
- package/server/rscPrefetchCache.ts +68 -32
- package/server/rscRenderControl.ts +33 -0
- package/server/rscRenderFallbacks.ts +297 -0
- package/server/rscRenderHandler.ts +439 -0
- package/server/rscRenderStats.ts +49 -0
- package/server/rscResponse.ts +78 -0
- package/server/rscResultCaches.ts +74 -0
- package/server/rscRouteDocument.tsx +299 -0
- package/server/rscSegmentOutlet.tsx +24 -5
- package/server/rscSegmentOutletReference.ts +14 -12
- package/server/rscWorker.tsx +86 -1392
- package/server/rscWorkerCache.ts +56 -0
- package/server/rscWorkerHost.ts +145 -811
- package/server/rscWorkerProcess.ts +155 -0
- package/server/rscWorkerRecycler.ts +128 -0
- package/server/serverConfig.ts +128 -0
- package/server/serverMetrics.ts +52 -0
- package/server/sitemap.ts +1 -0
- package/server/ssrFromRscRenderer.tsx +20 -20
- package/server/ssrTypes.ts +9 -0
- package/server/webArtifact.ts +86 -0
- package/server/webAssetRoutes.ts +84 -0
- package/server/webCsrSurface.ts +172 -0
- package/server/webDocumentRoute.ts +180 -0
- package/server/webRouteContext.ts +232 -0
- package/server/webRouter.ts +126 -1242
- package/server/webRscRoute.ts +155 -0
- package/server/webSiteFiles.ts +139 -0
- package/server/wellKnownRoutes.ts +73 -0
- package/service/agent.service.ts +59 -5
- package/service/agentAccounts.ts +19 -0
- package/service/agentLoop/agentLoop.ts +387 -0
- package/service/agentLoop/agentLoopTypes.ts +112 -0
- package/service/agentLoop/deltaStream.ts +40 -0
- package/service/agentLoop/loopCompaction.ts +135 -0
- package/service/agentLoop/loopRequest.ts +62 -0
- package/service/agentLoop/loopToolOutput.ts +54 -0
- package/service/agentLoop/loopToolkits.ts +107 -0
- package/service/agentLoop/loopTranscript.ts +102 -0
- package/{signal → service}/agentMeter.ts +83 -15
- package/{signal → service}/agentRelayPolicy.ts +30 -7
- package/service/documentStoreError.ts +16 -0
- package/service/index.ts +7 -0
- package/service/injectError.ts +5 -0
- package/service/injectInfo.ts +30 -18
- package/service/insightQueryError.ts +19 -0
- package/service/ipcTypes.ts +3 -1
- package/service/liveSyncHub.ts +5 -2
- package/service/predefinedAdaptor/agentTools.adaptor.ts +34 -0
- package/service/predefinedAdaptor/anthropicLlm.ts +52 -21
- package/service/predefinedAdaptor/cache.adaptor.ts +3 -2
- package/service/predefinedAdaptor/compress.adaptor.ts +7 -3
- package/service/predefinedAdaptor/database.adaptor.ts +3 -1
- package/service/predefinedAdaptor/documentTransfer.ts +2 -1
- package/service/predefinedAdaptor/index.ts +2 -0
- package/service/predefinedAdaptor/insightQuery.ts +35 -11
- package/service/predefinedAdaptor/llm.adaptor.ts +22 -2
- package/service/predefinedAdaptor/llmSpend.ts +80 -0
- package/service/predefinedAdaptor/openaiDialect.ts +52 -17
- package/service/predefinedAdaptor/openaiLlm.ts +17 -9
- package/service/predefinedAdaptor/role.adaptor.ts +2 -0
- package/service/predefinedAdaptor/searchIndex.ts +5 -1
- package/service/predefinedAdaptor/solidPubsub.adaptor.ts +35 -2
- package/service/predefinedAdaptor/solidQueue.adaptor.ts +2 -2
- package/service/predefinedAdaptor/solidSqlite.ts +3 -2
- package/service/predefinedAdaptor/sql/DocumentHydrator.ts +95 -0
- package/service/predefinedAdaptor/sql/DocumentRowCodec.ts +192 -0
- package/service/predefinedAdaptor/sql/DocumentWriter.ts +292 -0
- package/service/predefinedAdaptor/sql/PostgresInsightSession.ts +15 -5
- package/service/predefinedAdaptor/sql/QueryCompiler.ts +29 -13
- package/service/predefinedAdaptor/sql/QueryScope.ts +70 -0
- package/service/predefinedAdaptor/sql/QueryWriter.ts +200 -0
- package/service/predefinedAdaptor/sql/RawDocumentTable.ts +20 -8
- package/service/predefinedAdaptor/sql/SqlDocumentStore.ts +83 -1124
- package/service/predefinedAdaptor/sql/SqlTableSchema.ts +319 -0
- package/service/predefinedAdaptor/sql/SqliteInsightSession.ts +2 -1
- package/service/predefinedAdaptor/sql/StoredRows.ts +95 -0
- package/service/predefinedAdaptor/sql/UpdateCompiler.ts +6 -2
- package/service/predefinedAdaptor/sql/driver/bunSqlite.ts +3 -3
- package/service/predefinedAdaptor/sql/driver/postgres.ts +1 -1
- package/service/predefinedAdaptor/sql/fieldValues.ts +74 -0
- package/service/predefinedAdaptor/sql/search/fts5.ts +4 -1
- package/service/predefinedAdaptor/sql/search/postgres.ts +6 -2
- package/service/predefinedAdaptor/sql/values.ts +8 -4
- package/service/predefinedAdaptor/sseData.ts +14 -0
- package/service/predefinedAdaptor/storage.adaptor.ts +9 -4
- package/service/predefinedAdaptor/websocket.adaptor.ts +199 -43
- package/service/serve.ts +3 -2
- package/service/serviceModule.ts +2 -2
- package/service/serviceRegistry.ts +1 -0
- package/signal/SignalFailure.ts +30 -1
- package/signal/agent.signal.ts +30 -9
- package/signal/agentTurnStream.ts +12 -8
- package/signal/byIdRead.ts +18 -0
- package/signal/carriedClientIps.ts +1 -0
- package/signal/endpointErrorContract.ts +20 -2
- package/signal/endpointInfo.ts +6 -5
- package/signal/exception.ts +1 -0
- package/signal/index.ts +314 -30
- package/signal/internalInfo.ts +6 -4
- package/signal/mcp/McpDocument.ts +8 -1
- package/signal/mcp/Msg.ts +23 -10
- package/signal/mcp/index.ts +1 -0
- package/signal/mcp/promptError.ts +9 -0
- package/signal/middleware.ts +12 -3
- package/signal/openapi/openapi.ts +10 -1
- package/signal/resolveFieldContext.ts +34 -0
- package/signal/serializer/fetch.serializer.ts +23 -15
- package/signal/signalContext.ts +24 -57
- package/signal/signalContractError.ts +10 -0
- package/signal/signalDeclarationError.ts +13 -0
- package/signal/signalRegistry.ts +3 -1
- package/signal/slice.ts +24 -20
- package/signal/sliceInfo.ts +12 -9
- package/signal/trace.ts +3 -0
- package/signal/types.ts +35 -6
- package/store/action.ts +44 -1086
- package/store/actionErrorKey.ts +9 -0
- package/store/actionOption.ts +22 -0
- package/store/baseSt.ts +1 -0
- package/store/draftFlush.ts +5 -0
- package/store/draftStore.ts +2 -0
- package/store/formDrafts.ts +115 -0
- package/store/formFieldSetters.ts +229 -0
- package/store/index.ts +125 -17
- package/store/modelActionContext.ts +65 -0
- package/store/modelListPatch.ts +53 -0
- package/store/modelOpenActions.ts +158 -0
- package/store/modelWriteActions.ts +240 -0
- package/store/modelWriteGuard.ts +82 -0
- package/store/openSeq.ts +34 -0
- package/store/rootStore.ts +2 -0
- package/store/sliceListActions.ts +370 -0
- package/store/sliceLiveActions.ts +130 -0
- package/store/stateBuilder.ts +2 -0
- package/store/storeInstance.ts +9 -10
- package/store/uploadPoll.ts +52 -0
- package/store/withSelector.ts +6 -4
- package/types/base/base.d.ts +2 -1
- package/types/client/clientRuntime.d.ts +1 -0
- package/types/client/cn.d.ts +2 -2
- package/types/client/csrTypes.d.ts +15 -1
- package/types/client/frameConfig.d.ts +1 -0
- package/types/client/index.d.ts +25 -26
- package/types/client/locale.d.ts +1 -0
- package/types/client/makePageProto.d.ts +28 -26091
- package/types/client/nativeThemeColor.d.ts +6 -0
- package/types/client/route/RouteDefinition.d.ts +2 -2
- package/types/client/route/RouteHead.d.ts +5 -1
- package/types/client/route/routeArgs.d.ts +6 -0
- package/types/client/router.d.ts +6 -3
- package/types/client/rscNavigation.d.ts +3 -2
- package/types/client/rscRoutePatch.d.ts +22 -0
- package/types/client/viewportMeta.d.ts +3 -1
- package/types/common/CodeTranscript.d.ts +1 -1
- package/types/common/akanGraph.d.ts +1 -1
- package/types/common/err.d.ts +48 -0
- package/types/common/facetBarrel.d.ts +4 -0
- package/types/common/failureCode.d.ts +6 -0
- package/types/common/index.d.ts +8 -2
- package/types/common/isGuardRefusal.d.ts +3 -0
- package/types/common/mcpExposure.d.ts +9 -2
- package/types/common/mcpFailure.d.ts +6 -0
- package/types/common/restartSchedule.d.ts +16 -0
- package/types/common/routeConvention.d.ts +2 -0
- package/types/common/types.d.ts +18 -1
- package/types/constant/constantRegistry.d.ts +3 -1
- package/types/constant/crystalize.d.ts +1 -1
- package/types/constant/getDefault.d.ts +1 -1
- package/types/constant/immerify.d.ts +1 -1
- package/types/constant/index.d.ts +21 -21
- package/types/constant/purify.d.ts +2 -1
- package/types/constant/serialize.d.ts +1 -1
- package/types/constant/types.d.ts +1 -0
- package/types/dictionary/agent.dictionary.d.ts +1 -1
- package/types/dictionary/base.dictionary.d.ts +1 -1
- package/types/dictionary/dictionary.d.ts +9 -9
- package/types/dictionary/locale.d.ts +1 -1
- package/types/dictionary/trans.d.ts +3 -26
- package/types/document/databaseRegistry.d.ts +2 -1
- package/types/document/documentSchema.d.ts +1 -0
- package/types/document/filterMeta.d.ts +4 -1
- package/types/document/into.d.ts +1 -1
- package/types/document/loaderInfo.d.ts +2 -1
- package/types/document/noDocumentError.d.ts +8 -1
- package/types/document/queryEvaluator.d.ts +1 -1
- package/types/document/types.d.ts +1 -1
- package/types/fetch/client/fetchClient.d.ts +6 -36
- package/types/fetch/client/fetchClientTypes.d.ts +40 -0
- package/types/fetch/client/fetchHandle.d.ts +1 -0
- package/types/fetch/client/httpClient.d.ts +2 -0
- package/types/fetch/client/inflightQueries.d.ts +2 -0
- package/types/fetch/client/remoteError.d.ts +0 -4
- package/types/fetch/client/requestScope.d.ts +6 -0
- package/types/fetch/client/signalCatalog.d.ts +25 -0
- package/types/fetch/client/wsClient.d.ts +1 -0
- package/types/fetch/index.d.ts +1 -0
- package/types/fetch/staleReads.d.ts +8 -0
- package/types/fetch/streamedRead.d.ts +8 -0
- package/types/server/agentHooks.d.ts +4 -0
- package/types/server/akanApp.d.ts +2 -20
- package/types/server/akanOption.d.ts +9 -3
- package/types/server/akanServer.d.ts +1 -1
- package/types/server/artifact/builderRpc.d.ts +8 -0
- package/types/server/artifact/ipcTypes.d.ts +8 -0
- package/types/server/assetHeaders.d.ts +1 -0
- package/types/server/console.d.ts +4 -3
- package/types/server/devtools/devtoolsRouter.d.ts +4 -1
- package/types/server/devtools/studio/studioRoute.d.ts +2 -1
- package/types/server/di/predefinedAdaptor.d.ts +1 -0
- package/types/server/ed25519Key.d.ts +1 -0
- package/types/server/flightRowReader.d.ts +3 -1
- package/types/server/gateway/childSupervisor.d.ts +33 -0
- package/types/server/gateway/devHostChannel.d.ts +14 -0
- package/types/server/gateway/gatewayChildren.d.ts +47 -0
- package/types/server/gateway/gatewayConfig.d.ts +82 -0
- package/types/server/gateway/gatewayHttpProxy.d.ts +22 -0
- package/types/server/gateway/gatewayLogging.d.ts +16 -0
- package/types/server/gateway/gatewayRooms.d.ts +25 -0
- package/types/server/gateway/gatewayStatus.d.ts +62 -0
- package/types/server/gateway/gatewayStatusPage.d.ts +9 -0
- package/types/server/gateway/gatewayWebSocketRelay.d.ts +17 -0
- package/types/server/head.d.ts +2 -2
- package/types/server/htmlResultCache.d.ts +51 -0
- package/types/server/index.d.ts +17 -17
- package/types/server/lifecycle/parentChannel.d.ts +13 -0
- package/types/server/lifecycle/startGuard.d.ts +10 -0
- package/types/server/logging/rotatingLogWriter.d.ts +1 -1
- package/types/server/logging/serverLogging.d.ts +12 -0
- package/types/server/mcp/AgentTools.d.ts +31 -0
- package/types/server/mcp/AgentToolsAccount.d.ts +7 -0
- package/types/server/mcp/McpDispatcher.d.ts +10 -1
- package/types/server/mcp/McpPages.d.ts +2 -1
- package/types/server/mcp/McpRateLimiter.d.ts +3 -0
- package/types/server/mcp/index.d.ts +1 -0
- package/types/server/resolver/database.resolver.d.ts +2 -2
- package/types/server/resolver/databaseModelInstance.d.ts +4 -0
- package/types/server/resolver/invalidSignal.d.ts +2 -0
- package/types/server/resolver/livePause.d.ts +5 -0
- package/types/server/resolver/modelFacade.d.ts +5 -0
- package/types/server/resolver/modelLoaders.d.ts +9 -0
- package/types/server/resolver/modelSchema.d.ts +6 -0
- package/types/server/resolver/signal.resolver.d.ts +2 -2
- package/types/server/resolver/timedQuery.d.ts +8 -0
- package/types/server/resolver/websocketOf.d.ts +2 -0
- package/types/server/routeElementComposer.d.ts +45 -6
- package/types/server/routeState.d.ts +2 -0
- package/types/server/routing/builtinRoutes.d.ts +24 -0
- package/types/server/routing/localRooms.d.ts +14 -0
- package/types/server/routing/roomReplay.d.ts +2 -2
- package/types/server/routing/serverListener.d.ts +31 -0
- package/types/server/rscClient.d.ts +1 -5
- package/types/server/rscClientCache.d.ts +34 -0
- package/types/server/rscClientChunks.d.ts +18 -0
- package/types/server/rscClientFetch.d.ts +1 -1
- package/types/server/rscClientPatch.d.ts +12 -1
- package/types/server/rscDocumentHead.d.ts +32 -0
- package/types/server/rscFlightRenderer.d.ts +14 -0
- package/types/server/rscFlightStream.d.ts +52 -0
- package/types/server/rscHeadPatch.d.ts +5 -4
- package/types/server/rscHostReloads.d.ts +54 -0
- package/types/server/rscHostRenderStream.d.ts +53 -0
- package/types/server/rscHostRequests.d.ts +29 -0
- package/types/server/rscHttp.d.ts +4 -2
- package/types/server/rscInlineStream.d.ts +9 -0
- package/types/server/rscNavigationState.d.ts +8 -3
- package/types/server/rscParkedRedirect.d.ts +12 -0
- package/types/server/rscPatchPlanner.d.ts +49 -0
- package/types/server/rscPrefetchCache.d.ts +8 -7
- package/types/server/rscRenderControl.d.ts +12 -0
- package/types/server/rscRenderFallbacks.d.ts +41 -0
- package/types/server/rscRenderHandler.d.ts +29 -0
- package/types/server/rscRenderStats.d.ts +32 -0
- package/types/server/rscResponse.d.ts +9 -0
- package/types/server/rscResultCaches.d.ts +18 -0
- package/types/server/rscRouteDocument.d.ts +52 -0
- package/types/server/rscSegmentOutlet.d.ts +5 -0
- package/types/server/rscSegmentOutletReference.d.ts +2 -1
- package/types/server/rscWorker.d.ts +1 -3
- package/types/server/rscWorkerCache.d.ts +33 -1
- package/types/server/rscWorkerHost.d.ts +9 -76
- package/types/server/rscWorkerProcess.d.ts +31 -0
- package/types/server/rscWorkerRecycler.d.ts +22 -0
- package/types/server/serverConfig.d.ts +30 -0
- package/types/server/serverMetrics.d.ts +11 -0
- package/types/server/sitemap.d.ts +1 -0
- package/types/server/ssrFromRscRenderer.d.ts +2 -2
- package/types/server/ssrTypes.d.ts +8 -0
- package/types/server/webArtifact.d.ts +7 -0
- package/types/server/webAssetRoutes.d.ts +11 -0
- package/types/server/webCsrSurface.d.ts +18 -0
- package/types/server/webDocumentRoute.d.ts +19 -0
- package/types/server/webRouteContext.d.ts +45 -0
- package/types/server/webRouter.d.ts +4 -35
- package/types/server/webRscRoute.d.ts +24 -0
- package/types/server/webSiteFiles.d.ts +12 -0
- package/types/server/wellKnownRoutes.d.ts +5 -0
- package/types/service/agent.service.d.ts +13 -4
- package/types/service/agentAccounts.d.ts +8 -0
- package/types/service/agentLoop/agentLoop.d.ts +31 -0
- package/types/service/agentLoop/agentLoopTypes.d.ts +126 -0
- package/types/service/agentLoop/deltaStream.d.ts +3 -0
- package/types/service/agentLoop/loopCompaction.d.ts +11 -0
- package/types/service/agentLoop/loopRequest.d.ts +6 -0
- package/types/service/agentLoop/loopToolOutput.d.ts +7 -0
- package/types/service/agentLoop/loopToolkits.d.ts +14 -0
- package/types/service/agentLoop/loopTranscript.d.ts +12 -0
- package/types/service/agentMeter.d.ts +50 -0
- package/types/service/agentRelayPolicy.d.ts +45 -0
- package/types/service/documentStoreError.d.ts +7 -0
- package/types/service/index.d.ts +7 -0
- package/types/service/injectError.d.ts +4 -0
- package/types/service/injectInfo.d.ts +2 -1
- package/types/service/insightQueryError.d.ts +9 -0
- package/types/service/ipcTypes.d.ts +6 -0
- package/types/service/predefinedAdaptor/agentTools.adaptor.d.ts +31 -0
- package/types/service/predefinedAdaptor/anthropicLlm.d.ts +2 -2
- package/types/service/predefinedAdaptor/index.d.ts +2 -0
- package/types/service/predefinedAdaptor/llm.adaptor.d.ts +18 -2
- package/types/service/predefinedAdaptor/llmSpend.d.ts +22 -0
- package/types/service/predefinedAdaptor/openaiDialect.d.ts +2 -1
- package/types/service/predefinedAdaptor/openaiLlm.d.ts +1 -1
- package/types/service/predefinedAdaptor/role.adaptor.d.ts +2 -0
- package/types/service/predefinedAdaptor/solidPubsub.adaptor.d.ts +3 -0
- package/types/service/predefinedAdaptor/solidSqlite.d.ts +1 -1
- package/types/service/predefinedAdaptor/sql/DocumentHydrator.d.ts +11 -0
- package/types/service/predefinedAdaptor/sql/DocumentRowCodec.d.ts +26 -0
- package/types/service/predefinedAdaptor/sql/DocumentWriter.d.ts +24 -0
- package/types/service/predefinedAdaptor/sql/QueryScope.d.ts +16 -0
- package/types/service/predefinedAdaptor/sql/QueryWriter.d.ts +20 -0
- package/types/service/predefinedAdaptor/sql/SqlDocumentStore.d.ts +1 -32
- package/types/service/predefinedAdaptor/sql/SqlTableSchema.d.ts +20 -0
- package/types/service/predefinedAdaptor/sql/StoredRows.d.ts +27 -0
- package/types/service/predefinedAdaptor/sql/driver/bunSqlite.d.ts +1 -14
- package/types/service/predefinedAdaptor/sql/driver/postgres.d.ts +0 -8
- package/types/service/predefinedAdaptor/sql/fieldValues.d.ts +5 -0
- package/types/service/predefinedAdaptor/sseData.d.ts +5 -0
- package/types/service/predefinedAdaptor/websocket.adaptor.d.ts +14 -5
- package/types/service/serviceModule.d.ts +1 -1
- package/types/service/serviceRegistry.d.ts +1 -0
- package/types/signal/SignalFailure.d.ts +2 -0
- package/types/signal/agentTurnStream.d.ts +2 -1
- package/types/signal/byIdRead.d.ts +7 -0
- package/types/signal/carriedClientIps.d.ts +4 -0
- package/types/signal/endpointErrorContract.d.ts +3 -0
- package/types/signal/exception.d.ts +1 -0
- package/types/signal/index.d.ts +32 -30
- package/types/signal/mcp/McpDocument.d.ts +2 -0
- package/types/signal/mcp/index.d.ts +1 -0
- package/types/signal/mcp/promptError.d.ts +5 -0
- package/types/signal/resolveFieldContext.d.ts +15 -0
- package/types/signal/serializer/fetch.serializer.d.ts +3 -1
- package/types/signal/signalContext.d.ts +1 -13
- package/types/signal/signalContractError.d.ts +4 -0
- package/types/signal/signalDeclarationError.d.ts +4 -0
- package/types/signal/slice.d.ts +15 -13
- package/types/signal/trace.d.ts +3 -0
- package/types/signal/types.d.ts +33 -6
- package/types/store/action.d.ts +8 -27
- package/types/store/actionErrorKey.d.ts +1 -0
- package/types/store/actionOption.d.ts +24 -0
- package/types/store/baseSt.d.ts +2 -295
- package/types/store/draftFlush.d.ts +4 -0
- package/types/store/formDrafts.d.ts +21 -0
- package/types/store/formFieldSetters.d.ts +80 -0
- package/types/store/index.d.ts +17 -16
- package/types/store/modelActionContext.d.ts +189 -0
- package/types/store/modelListPatch.d.ts +34 -0
- package/types/store/modelOpenActions.d.ts +12 -0
- package/types/store/modelWriteActions.d.ts +16 -0
- package/types/store/modelWriteGuard.d.ts +42 -0
- package/types/store/openSeq.d.ts +17 -0
- package/types/store/rootStore.d.ts +6 -0
- package/types/store/sliceListActions.d.ts +295 -0
- package/types/store/sliceLiveActions.d.ts +6 -0
- package/types/store/stateBuilder.d.ts +2 -0
- package/types/store/uploadPoll.d.ts +16 -0
- package/types/store/withSelector.d.ts +1 -1
- package/types/ui/Agent/Chat.d.ts +5 -2
- package/types/ui/Agent/ChatHeader.d.ts +13 -0
- package/types/ui/Agent/ChatPendingCards.d.ts +23 -0
- package/types/ui/Field/Choice.d.ts +2 -1
- package/types/ui/Field/index.d.ts +1 -1
- package/types/ui/Link/inputModality.d.ts +5 -0
- package/types/ui/Link/viewportPrefetch.d.ts +4 -0
- package/types/ui/Load/Edit_Client.d.ts +2 -0
- package/types/ui/Load/Pagination.d.ts +4 -1
- package/types/ui/Load/Stream.d.ts +7 -1
- package/types/ui/Load/StreamBoundary.d.ts +20 -0
- package/types/ui/Load/Units.d.ts +2 -0
- package/types/ui/Load/View.d.ts +2 -0
- package/types/ui/Reference/origin.d.ts +1 -0
- package/types/ui/index.d.ts +1 -1
- package/types/ui/overlayLayer.d.ts +6 -1
- package/types/ui/recipe/badgeRecipe.d.ts +3 -3
- package/types/ui/recipe/buttonRecipe.d.ts +3 -3
- package/types/ui/recipe/inputRecipe.d.ts +1 -1
- package/types/vendor/@akanjs/native/packages/cli/src/lib/project.d.ts +4 -0
- package/types/vendor/@akanjs/native/packages/desktop/src/relaunch.d.ts +1 -0
- package/types/vendor/@akanjs/native/plugins/appearance/src/index.d.ts +9 -0
- package/types/vendor/@akanjs/native/plugins/external-view/src/index.d.ts +18 -11
- package/types/vendor/use-agentic/AgentClientTools.d.ts +19 -0
- package/types/vendor/use-agentic/failureCode.d.ts +4 -0
- package/types/vendor/use-agentic/index.d.ts +1 -0
- package/types/vendor/use-agentic/types.d.ts +11 -0
- package/types/webkit/CsrGuards.d.ts +2 -0
- package/types/webkit/RouteLevelBoundary.d.ts +17 -0
- package/types/webkit/nativeCore.d.ts +1 -0
- package/types/webkit/nativePurchase.d.ts +2 -1
- package/types/webkit/useCsrKeyboard.d.ts +19 -0
- package/types/webkit/useCsrLocationState.d.ts +21 -0
- package/types/webkit/useCsrNavigation.d.ts +25 -0
- package/types/webkit/useCsrRefs.d.ts +33 -0
- package/types/webkit/useCsrRenderTrace.d.ts +13 -0
- package/types/webkit/useCsrStackLifecycle.d.ts +12 -0
- package/types/webkit/useCsrTransitions.d.ts +2 -0
- package/types/webkit/useFrameRuntime.d.ts +1 -0
- package/types/webkit/useGeoLocation.d.ts +1 -1
- package/types/webkit/useHistory.d.ts +2 -3
- package/types/webkit/usePurchase.d.ts +1 -1
- package/types/webkit/useResolvedFrame.d.ts +38 -0
- package/types/webkit/useSyncNavigation.d.ts +22 -0
- package/types/webkit/useTransitionStore.d.ts +1 -0
- package/ui/Agent/Chat.tsx +392 -235
- package/ui/Agent/ChatHeader.tsx +59 -0
- package/ui/Agent/ChatPendingCards.tsx +49 -0
- package/ui/Agent/fetchRunner.ts +8 -1
- package/ui/BottomSheet.tsx +1 -1
- package/ui/Button.tsx +1 -0
- package/ui/Data/Item.tsx +2 -2
- package/ui/Data/ListContainer.tsx +348 -181
- package/ui/Data/useViewModal.ts +10 -4
- package/ui/Dialog/LegacyModal.tsx +1 -1
- package/ui/Dialog/Modal.tsx +1 -1
- package/ui/Field/Choice.tsx +3 -1
- package/ui/Link/inputModality.ts +21 -0
- package/ui/Link/prefetchIntent.ts +7 -25
- package/ui/Link/viewportPrefetch.ts +30 -0
- package/ui/Load/Edit_Client.tsx +7 -1
- package/ui/Load/Pagination.tsx +10 -2
- package/ui/Load/Stream.tsx +24 -4
- package/ui/Load/StreamBoundary.tsx +65 -0
- package/ui/Load/Units.tsx +3 -0
- package/ui/Load/View.tsx +5 -1
- package/ui/Model/EditModal.tsx +2 -1
- package/ui/Popconfirm.tsx +1 -1
- package/ui/Reference/origin.ts +1 -0
- package/ui/Select.tsx +43 -51
- package/ui/Signal/RestApi.tsx +3 -2
- package/ui/System/Client.tsx +32 -8
- package/ui/index.ts +4 -0
- package/ui/overlayLayer.ts +22 -1
- package/vendor/@akanjs/native/CHANGELOG.md +7 -1
- package/vendor/@akanjs/native/native/android/src/com/akanjs/runtime/AkanNativeActivity.kt +27 -12
- package/vendor/@akanjs/native/native/android/src/com/akanjs/runtime/AkanNativePlugin.kt +4 -0
- package/vendor/@akanjs/native/native/desktop/src/external.rs +213 -111
- package/vendor/@akanjs/native/native/desktop/src/lib.rs +12 -4
- package/vendor/@akanjs/native/native/desktop/src/linux/external.rs +509 -0
- package/vendor/@akanjs/native/native/desktop/src/linux/mod.rs +1 -0
- package/vendor/@akanjs/native/native/ios/Sources/AkanNativeViewController.swift +6 -1
- package/vendor/@akanjs/native/packages/cli/src/lib/native-plugins.ts +5 -3
- package/vendor/@akanjs/native/packages/cli/src/lib/project.ts +25 -1
- package/vendor/@akanjs/native/packages/desktop/src/deeplinks.ts +2 -2
- package/vendor/@akanjs/native/packages/desktop/src/host.ts +2 -2
- package/vendor/@akanjs/native/packages/desktop/src/relaunch.ts +8 -3
- package/vendor/@akanjs/native/plugins/app/src/desktop.ts +2 -2
- package/vendor/@akanjs/native/plugins/appearance/android/AppearancePlugin.kt +13 -0
- package/vendor/@akanjs/native/plugins/appearance/ios/AppearancePlugin.swift +27 -0
- package/vendor/@akanjs/native/plugins/appearance/native-plugin.json +2 -2
- package/vendor/@akanjs/native/plugins/appearance/src/index.ts +8 -1
- package/vendor/@akanjs/native/plugins/autostart/src/desktop.ts +2 -1
- package/vendor/@akanjs/native/plugins/external-view/native-plugin.json +4 -1
- package/vendor/@akanjs/native/plugins/external-view/src/index.ts +18 -11
- package/vendor/use-agentic/AgentClientTools.ts +33 -0
- package/vendor/use-agentic/AgentSession.ts +11 -11
- package/vendor/use-agentic/CHANGELOG.md +46 -1
- package/vendor/use-agentic/Compaction.ts +2 -1
- package/vendor/use-agentic/ToolRunner.ts +6 -3
- package/vendor/use-agentic/Transcript.ts +6 -3
- package/vendor/use-agentic/WIRE.md +29 -7
- package/vendor/use-agentic/failureCode.ts +44 -0
- package/vendor/use-agentic/httpRunner.ts +15 -4
- package/vendor/use-agentic/index.ts +1 -0
- package/vendor/use-agentic/types.ts +11 -0
- package/webkit/CsrGuards.ts +7 -2
- package/webkit/McpAppAgent.ts +8 -3
- package/webkit/McpAppFetchBridge.ts +11 -5
- package/webkit/RouteLevelBoundary.tsx +37 -0
- package/webkit/nativeCore.ts +1 -0
- package/webkit/nativePurchase.ts +6 -3
- package/webkit/useCamera.tsx +22 -21
- package/webkit/useCsrKeyboard.ts +155 -0
- package/webkit/useCsrLocationState.ts +58 -0
- package/webkit/useCsrNavigation.ts +302 -0
- package/webkit/useCsrRefs.ts +52 -0
- package/webkit/useCsrRenderTrace.ts +68 -0
- package/webkit/useCsrStackLifecycle.ts +120 -0
- package/webkit/useCsrTransitions.ts +552 -0
- package/webkit/useCsrValues.ts +69 -1234
- package/webkit/useGeoLocation.tsx +4 -1
- package/webkit/useHistory.ts +2 -2
- package/webkit/useLocation.ts +3 -2
- package/webkit/usePurchase.tsx +4 -2
- package/webkit/useResolvedFrame.ts +116 -0
- package/webkit/useSyncNavigation.ts +109 -0
- package/webkit/useTransitionStore.ts +13 -0
- package/types/signal/agentMeter.d.ts +0 -27
- package/types/signal/agentRelayPolicy.d.ts +0 -25
package/store/action.ts
CHANGED
|
@@ -1,34 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
type FetchPolicy,
|
|
7
|
-
formSetterNames,
|
|
8
|
-
isQueryEqual,
|
|
9
|
-
Logger,
|
|
10
|
-
modelNames,
|
|
11
|
-
pathSet,
|
|
12
|
-
plainFieldsOf,
|
|
13
|
-
resolveFileUploadCapability,
|
|
14
|
-
sliceNames,
|
|
15
|
-
} from "akanjs/common";
|
|
16
|
-
import {
|
|
17
|
-
type BaseInsight,
|
|
18
|
-
type BaseObject,
|
|
19
|
-
ConstantRegistry,
|
|
20
|
-
type DefaultOf,
|
|
21
|
-
type FieldState,
|
|
22
|
-
immerify,
|
|
23
|
-
MAX_PAGE_SIZE,
|
|
24
|
-
type ProtoFile,
|
|
25
|
-
type ProtoLightFile,
|
|
26
|
-
withSharedInstances,
|
|
27
|
-
} from "akanjs/constant";
|
|
28
|
-
import type { BaseFilterSortKey, ExtractSort, FilterInstance } from "akanjs/document";
|
|
29
|
-
import { type FetchInitForm, type FetchProxy, restoreRemoteError } from "akanjs/fetch";
|
|
1
|
+
import { FIELD_META, type GetStateObject } from "akanjs/base";
|
|
2
|
+
import { type FetchPolicy, pathSet } from "akanjs/common";
|
|
3
|
+
import type { DefaultOf, FieldState, ProtoLightFile } from "akanjs/constant";
|
|
4
|
+
import type { ExtractSort, FilterInstance } from "akanjs/document";
|
|
5
|
+
import type { FetchInitForm, FetchProxy } from "akanjs/fetch";
|
|
30
6
|
import type {
|
|
31
|
-
LiveEventPayload,
|
|
32
7
|
SerializedSlice,
|
|
33
8
|
SlceCnstCapitalizedRefName,
|
|
34
9
|
SlceCnstDefault,
|
|
@@ -41,14 +16,13 @@ import type {
|
|
|
41
16
|
SlceDbSort,
|
|
42
17
|
SliceCls,
|
|
43
18
|
} from "akanjs/signal";
|
|
44
|
-
import {
|
|
45
|
-
import {
|
|
46
|
-
import {
|
|
47
|
-
import {
|
|
48
|
-
import {
|
|
49
|
-
import {
|
|
50
|
-
import {
|
|
51
|
-
import type { DraftState } from "./state";
|
|
19
|
+
import type { CreateOption, EditOption, NewOption } from "./actionOption";
|
|
20
|
+
import { fieldSettersOf, formSetterContextOf } from "./formFieldSetters";
|
|
21
|
+
import { modelActionContextOf, type ReportingFetchPolicy } from "./modelActionContext";
|
|
22
|
+
import { modelOpenActionsOf, modelStateActionsOf } from "./modelOpenActions";
|
|
23
|
+
import { formSaveActionsOf, modelWriteActionsOf } from "./modelWriteActions";
|
|
24
|
+
import { sliceListContextOf, sliceLoadActionsOf, slicePageActionsOf, sliceQueryActionsOf } from "./sliceListActions";
|
|
25
|
+
import { sliceLiveActionsOf } from "./sliceLiveActions";
|
|
52
26
|
import type { SetGet, StoreSliceArgs, StoreSliceMap, StoreSliceSuffixCap } from "./types";
|
|
53
27
|
|
|
54
28
|
type _SliceMap<S extends SliceCls> = StoreSliceMap<S>;
|
|
@@ -66,28 +40,6 @@ type SliceRefreshForm<S extends SliceCls, Suffix extends keyof _SliceMap<S>> = S
|
|
|
66
40
|
queryArgs?: StoreSliceArgs<S, Suffix>;
|
|
67
41
|
};
|
|
68
42
|
|
|
69
|
-
const UPLOAD_POLL_INTERVAL_MS = 3000;
|
|
70
|
-
const UPLOAD_POLL_ATTEMPTS = 40;
|
|
71
|
-
|
|
72
|
-
export interface CreateOption<Full extends { id: string }> {
|
|
73
|
-
path?: string;
|
|
74
|
-
modal?: string;
|
|
75
|
-
sliceName?: string;
|
|
76
|
-
onError?: (e: string) => void;
|
|
77
|
-
onSuccess?: (model: Full) => void | Promise<void>;
|
|
78
|
-
}
|
|
79
|
-
export interface NewOption {
|
|
80
|
-
modal?: string;
|
|
81
|
-
setDefault?: boolean;
|
|
82
|
-
sliceName?: string;
|
|
83
|
-
/** Built by the shell from the raw seed (merged date defaults would rotate the key). Absent means no draft. */
|
|
84
|
-
draftScope?: string;
|
|
85
|
-
}
|
|
86
|
-
export interface EditOption {
|
|
87
|
-
modal?: string | null;
|
|
88
|
-
/** Built by the shell from the record id. Absent means no draft. */
|
|
89
|
-
draftScope?: string;
|
|
90
|
-
}
|
|
91
43
|
type PartialOrNull<O> = { [K in keyof O]?: O[K] | null };
|
|
92
44
|
|
|
93
45
|
type BaseAction<
|
|
@@ -109,7 +61,7 @@ type BaseAction<
|
|
|
109
61
|
} & {
|
|
110
62
|
[K in `remove${_CapitalizedRefName}`]: (
|
|
111
63
|
id: string,
|
|
112
|
-
options?:
|
|
64
|
+
options?: ReportingFetchPolicy & { modal?: string | null },
|
|
113
65
|
) => Promise<void>;
|
|
114
66
|
} & {
|
|
115
67
|
[K in `check${_CapitalizedRefName}Submitable`]: (disabled?: boolean) => Promise<void>;
|
|
@@ -118,7 +70,10 @@ type BaseAction<
|
|
|
118
70
|
} & {
|
|
119
71
|
[K in `new${_CapitalizedRefName}`]: (partial?: PartialOrNull<Full>, options?: NewOption) => void;
|
|
120
72
|
} & {
|
|
121
|
-
[K in `edit${_CapitalizedRefName}`]: (
|
|
73
|
+
[K in `edit${_CapitalizedRefName}`]: (
|
|
74
|
+
model: Full | string,
|
|
75
|
+
options?: EditOption & ReportingFetchPolicy,
|
|
76
|
+
) => Promise<void>;
|
|
122
77
|
} & {
|
|
123
78
|
[K in `load${_CapitalizedRefName}FormDraft`]: (draftScope?: string) => Promise<void>;
|
|
124
79
|
} & {
|
|
@@ -129,12 +84,12 @@ type BaseAction<
|
|
|
129
84
|
[K in `merge${_CapitalizedRefName}`]: (
|
|
130
85
|
model: Full | string,
|
|
131
86
|
data: Partial<Full>,
|
|
132
|
-
options?:
|
|
87
|
+
options?: ReportingFetchPolicy,
|
|
133
88
|
) => Promise<void>;
|
|
134
89
|
} & {
|
|
135
90
|
[K in `view${_CapitalizedRefName}`]: (
|
|
136
91
|
model: Full | string,
|
|
137
|
-
options?: { modal?: string | null } &
|
|
92
|
+
options?: { modal?: string | null } & ReportingFetchPolicy,
|
|
138
93
|
) => Promise<void>;
|
|
139
94
|
} & { [K in `set${_CapitalizedRefName}`]: (...models: (Full | Light)[]) => void } & {
|
|
140
95
|
[K in `reset${_CapitalizedRefName}`]: (model?: Full) => void;
|
|
@@ -305,1037 +260,40 @@ export type DefaultAction<
|
|
|
305
260
|
DefaultSliceActionFields<SlceCls, _CapitalizedRefName, _FetchInitFormWithFetchPolicy, _Light, _Sort>;
|
|
306
261
|
|
|
307
262
|
export const makeFormSetter = (refName: string, fetch: FetchProxy<any>) => {
|
|
308
|
-
|
|
309
|
-
const
|
|
310
|
-
const modelRef = ConstantRegistry.getDatabase(refName).full;
|
|
311
|
-
const fileUploadRefName = resolveFileUploadCapability(fetch.serializedSignal)?.refName;
|
|
312
|
-
|
|
313
|
-
const names = modelNames(refName);
|
|
314
|
-
type UploadState = { [key: string]: { [field: string]: string[] } };
|
|
315
|
-
let uploadSeq = 0;
|
|
316
|
-
const releaseUpload = (state: UploadState, field: string, ticket?: string) => {
|
|
317
|
-
const pending = state[names.modelFormUploading][field];
|
|
318
|
-
if (!pending || (ticket && !pending.includes(ticket))) return;
|
|
319
|
-
const { [field]: tickets = [], ...rest } = state[names.modelFormUploading];
|
|
320
|
-
const left = ticket ? tickets.filter((pending) => pending !== ticket) : [];
|
|
321
|
-
state[names.modelFormUploading] = left.length ? { ...rest, [field]: left } : rest;
|
|
322
|
-
};
|
|
263
|
+
const ctx = formSetterContextOf(refName, fetch);
|
|
264
|
+
const { names, formPatchOf } = ctx;
|
|
323
265
|
const baseSetAction = {
|
|
324
266
|
[names.writeOnModel]: function (this: SetGet, path: string | (string | number)[], value: any) {
|
|
325
|
-
this.set(
|
|
326
|
-
|
|
327
|
-
|
|
267
|
+
this.set(
|
|
268
|
+
formPatchOf(this.get(), (form) => {
|
|
269
|
+
pathSet(form, path, value);
|
|
270
|
+
}),
|
|
271
|
+
);
|
|
328
272
|
},
|
|
329
273
|
};
|
|
330
|
-
const fieldSetAction = Object.entries(modelRef[FIELD_META]).reduce(
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
const singleFieldSetAction = {
|
|
335
|
-
[namesOfField.setFieldOnModel]: function (this: SetGet, value: any | null) {
|
|
336
|
-
this.set((state: { [key: string]: any }) => {
|
|
337
|
-
|
|
338
|
-
if (isFileField && !field.isArray) releaseUpload(state as UploadState, namesOfField.field);
|
|
339
|
-
const setValue =
|
|
340
|
-
value === null
|
|
341
|
-
? null
|
|
342
|
-
: field.isClass
|
|
343
|
-
? immerify<object>(field.modelRef, value as object)
|
|
344
|
-
: (value as object);
|
|
345
|
-
(state[names.modelForm] as { [key: string]: any })[namesOfField.field] = setValue;
|
|
346
|
-
});
|
|
347
|
-
|
|
348
|
-
const postSet = (this as unknown as { [key: string]: ((value: unknown) => unknown) | undefined })[
|
|
349
|
-
namesOfField.postSetField
|
|
350
|
-
];
|
|
351
|
-
if (postSet) void postSet.call(this, value);
|
|
352
|
-
},
|
|
353
|
-
...(field.isArray
|
|
354
|
-
? {
|
|
355
|
-
[namesOfField.addFieldOnModel]: function (
|
|
356
|
-
this: SetGet,
|
|
357
|
-
value: Light | Light[],
|
|
358
|
-
options: { idx?: number; limit?: number } = {},
|
|
359
|
-
) {
|
|
360
|
-
const form = (this.get() as { [key: string]: any })[names.modelForm] as { [key: string]: any };
|
|
361
|
-
const length = (form[namesOfField.field] as unknown[]).length;
|
|
362
|
-
if (options.limit && options.limit <= length) return;
|
|
363
|
-
const idx = options.idx ?? length;
|
|
364
|
-
const setValue = field.isClass ? immerify<Light | Light[]>(field.modelRef, value) : value;
|
|
365
|
-
this.set((state: { [key: string]: any }) => {
|
|
366
|
-
(state[names.modelForm] as { [key: string]: any })[namesOfField.field] = [
|
|
367
|
-
...(form[namesOfField.field] as object[]).slice(0, idx),
|
|
368
|
-
...(Array.isArray(setValue) ? setValue : [setValue]),
|
|
369
|
-
...(form[namesOfField.field] as object[]).slice(idx),
|
|
370
|
-
];
|
|
371
|
-
});
|
|
372
|
-
},
|
|
373
|
-
[namesOfField.subFieldOnModel]: function (this: SetGet, idx: number | number[]) {
|
|
374
|
-
const form = (this.get() as { [key: string]: any })[names.modelForm] as { [key: string]: object[] };
|
|
375
|
-
this.set((state: { [key: string]: any }) => {
|
|
376
|
-
(state[names.modelForm] as { [key: string]: any })[namesOfField.field] =
|
|
377
|
-
typeof idx === "number"
|
|
378
|
-
? form[namesOfField.field].filter((_, i) => i !== idx)
|
|
379
|
-
: form[namesOfField.field].filter((_, i) => !idx.includes(i));
|
|
380
|
-
});
|
|
381
|
-
},
|
|
382
|
-
[namesOfField.addOrSubFieldOnModel]: function (
|
|
383
|
-
this: SetGet,
|
|
384
|
-
value: any,
|
|
385
|
-
options: { idx?: number; limit?: number } = {},
|
|
386
|
-
) {
|
|
387
|
-
const { [names.modelForm]: form } = this.get() as { [key: string]: { [key: string]: any[] } };
|
|
388
|
-
const index = form[namesOfField.field].indexOf(value);
|
|
389
|
-
if (index === -1)
|
|
390
|
-
((this as unknown as DynamicRecord)[namesOfField.addFieldOnModel] as (...args: any) => void)(
|
|
391
|
-
value,
|
|
392
|
-
options,
|
|
393
|
-
);
|
|
394
|
-
else ((this as unknown as DynamicRecord)[namesOfField.subFieldOnModel] as (...args: any) => void)(index);
|
|
395
|
-
},
|
|
396
|
-
}
|
|
397
|
-
: {}),
|
|
398
|
-
...(isFileField && fileUploadRefName
|
|
399
|
-
? {
|
|
400
|
-
[namesOfField.uploadFieldOnModel]: async function (
|
|
401
|
-
this: SetGet,
|
|
402
|
-
fileList: FileList | File[],
|
|
403
|
-
index?: number,
|
|
404
|
-
) {
|
|
405
|
-
const formId = ((this.get() as { [key: string]: any })[names.modelForm] as { id?: string }).id;
|
|
406
|
-
if (!fileList.length) return;
|
|
407
|
-
|
|
408
|
-
const ticket = `${++uploadSeq}`;
|
|
409
|
-
this.set((state: UploadState) => {
|
|
410
|
-
const tickets = state[names.modelFormUploading][namesOfField.field] ?? [];
|
|
411
|
-
state[names.modelFormUploading] = {
|
|
412
|
-
...state[names.modelFormUploading],
|
|
413
|
-
[namesOfField.field]: field.isArray ? [...tickets, ticket] : [ticket],
|
|
414
|
-
};
|
|
415
|
-
});
|
|
416
|
-
const held = (state: { [key: string]: any }) =>
|
|
417
|
-
!!(state as UploadState)[names.modelFormUploading][namesOfField.field]?.includes(ticket) &&
|
|
418
|
-
(state[names.modelForm] as { id?: string }).id === formId;
|
|
419
|
-
let files: ProtoFile[];
|
|
420
|
-
try {
|
|
421
|
-
files = await (fetch[names.addModelFiles] as (...args: any) => Promise<ProtoFile[]>)(fileList, formId);
|
|
422
|
-
} catch (error) {
|
|
423
|
-
this.set((state: UploadState) => releaseUpload(state, namesOfField.field, ticket));
|
|
424
|
-
throw error;
|
|
425
|
-
}
|
|
426
|
-
let isWritten = false;
|
|
427
|
-
|
|
428
|
-
this.set((state: { [key: string]: any }) => {
|
|
429
|
-
isWritten = held(state);
|
|
430
|
-
releaseUpload(state as UploadState, namesOfField.field, ticket);
|
|
431
|
-
if (!isWritten) return;
|
|
432
|
-
const form = state[names.modelForm] as { [key: string]: ProtoLightFile[] | ProtoLightFile | null };
|
|
433
|
-
if (field.isArray) {
|
|
434
|
-
const current = (form[namesOfField.field] as ProtoLightFile[] | null) ?? [];
|
|
435
|
-
const idx = Math.min(index ?? current.length, current.length);
|
|
436
|
-
form[namesOfField.field] = [...current.slice(0, idx), ...files, ...current.slice(idx)];
|
|
437
|
-
} else form[namesOfField.field] = files[0] ?? null;
|
|
438
|
-
});
|
|
439
|
-
if (!isWritten) return;
|
|
440
|
-
const postSet = (this as unknown as { [key: string]: ((value: unknown) => unknown) | undefined })[
|
|
441
|
-
namesOfField.postSetField
|
|
442
|
-
];
|
|
443
|
-
if (postSet)
|
|
444
|
-
void postSet.call(this, (this.get() as { [key: string]: any })[names.modelForm][namesOfField.field]);
|
|
445
|
-
|
|
446
|
-
const fileReadName = ConstantRegistry.isLight(field.modelRef)
|
|
447
|
-
? modelNames(fileUploadRefName).lightModel
|
|
448
|
-
: fileUploadRefName;
|
|
449
|
-
const holdsFile = (id: string) => {
|
|
450
|
-
const value = (this.get() as { [key: string]: { [key: string]: unknown } })[names.modelForm][
|
|
451
|
-
namesOfField.field
|
|
452
|
-
] as ProtoLightFile | ProtoLightFile[] | null;
|
|
453
|
-
return Array.isArray(value) ? value.some((heldFile) => heldFile.id === id) : value?.id === id;
|
|
454
|
-
};
|
|
455
|
-
files.forEach((file) => {
|
|
456
|
-
let attemptsLeft = UPLOAD_POLL_ATTEMPTS;
|
|
457
|
-
const intervalKey = setInterval(() => {
|
|
458
|
-
void (async () => {
|
|
459
|
-
if (!holdsFile(file.id)) {
|
|
460
|
-
clearInterval(intervalKey);
|
|
461
|
-
return;
|
|
462
|
-
}
|
|
463
|
-
attemptsLeft -= 1;
|
|
464
|
-
try {
|
|
465
|
-
const currentFile = await (
|
|
466
|
-
(fetch as { [key: string]: any })[fileReadName] as (id: string) => Promise<ProtoLightFile>
|
|
467
|
-
)(file.id);
|
|
468
|
-
if (!holdsFile(currentFile.id)) {
|
|
469
|
-
clearInterval(intervalKey);
|
|
470
|
-
return;
|
|
471
|
-
}
|
|
472
|
-
this.set(
|
|
473
|
-
(state: { [key: string]: { [key: string]: ProtoLightFile[] | ProtoLightFile | null } }) => {
|
|
474
|
-
const value = state[names.modelForm][namesOfField.field];
|
|
475
|
-
state[names.modelForm][namesOfField.field] = Array.isArray(value)
|
|
476
|
-
? value.map((heldFile) => (heldFile.id === currentFile.id ? currentFile : heldFile))
|
|
477
|
-
: currentFile;
|
|
478
|
-
},
|
|
479
|
-
);
|
|
480
|
-
if (currentFile.status !== "uploading" || attemptsLeft <= 0) clearInterval(intervalKey);
|
|
481
|
-
} catch (error) {
|
|
482
|
-
clearInterval(intervalKey);
|
|
483
|
-
Logger.warn(
|
|
484
|
-
`Upload poll for ${fileReadName} ${file.id} stopped: ${
|
|
485
|
-
error instanceof Error ? error.message : String(error)
|
|
486
|
-
}`,
|
|
487
|
-
);
|
|
488
|
-
}
|
|
489
|
-
})();
|
|
490
|
-
}, UPLOAD_POLL_INTERVAL_MS);
|
|
491
|
-
});
|
|
492
|
-
},
|
|
493
|
-
}
|
|
494
|
-
: {}),
|
|
495
|
-
};
|
|
496
|
-
|
|
497
|
-
tagAction(singleFieldSetAction[namesOfField.setFieldOnModel] as (...args: never[]) => unknown, {
|
|
498
|
-
action: namesOfField.setFieldOnModel,
|
|
499
|
-
state: `${names.modelForm}.${namesOfField.field}`,
|
|
500
|
-
});
|
|
501
|
-
return Object.assign(acc, singleFieldSetAction);
|
|
502
|
-
}, {});
|
|
274
|
+
const fieldSetAction = Object.entries(ctx.modelRef[FIELD_META]).reduce(
|
|
275
|
+
(acc, [key, field]) => Object.assign(acc, fieldSettersOf(ctx, key, field)),
|
|
276
|
+
{},
|
|
277
|
+
);
|
|
503
278
|
return Object.assign(fieldSetAction, baseSetAction);
|
|
504
279
|
};
|
|
505
280
|
export const makeActions = (refName: string, slice: { [key: string]: SerializedSlice }, fetch: FetchProxy<any>) => {
|
|
506
|
-
|
|
507
|
-
interface Insight {
|
|
508
|
-
count: number;
|
|
509
|
-
}
|
|
510
|
-
type Full = BaseObject;
|
|
511
|
-
type Light = BaseObject;
|
|
512
|
-
type Filter = FilterInstance;
|
|
513
|
-
type Sort = BaseFilterSortKey;
|
|
514
|
-
const className = capitalize(refName);
|
|
515
|
-
const cnst = ConstantRegistry.getDatabase(refName);
|
|
516
|
-
const modelRef = cnst.full;
|
|
517
|
-
|
|
518
|
-
let openFormBase: object | null = null;
|
|
519
|
-
const slices = Object.entries(slice).map(([suffix, serializedSlice]) => {
|
|
520
|
-
const keys = sliceKeysOf(refName, suffix);
|
|
521
|
-
return { sliceName: keys.slice, keys, slice: serializedSlice };
|
|
522
|
-
});
|
|
523
|
-
const { modelInsight, modelList, modelListLoading } = sliceNames(refName).state;
|
|
524
|
-
const names = { ...modelNames(refName), Model: className, modelInsight, modelList, modelListLoading };
|
|
525
|
-
|
|
526
|
-
const isUploadingIn = (state: { [key: string]: any }) =>
|
|
527
|
-
Object.keys(state[names.modelFormUploading] as object).length > 0;
|
|
528
|
-
const defaultKeyOf = (sliceName: string) => capitalize(sliceName).replace(names.Model, names.defaultModel);
|
|
529
|
-
|
|
530
|
-
const staleAtOfSlices = () => {
|
|
531
|
-
const staleAt = new Date();
|
|
532
|
-
return Object.fromEntries(slices.map(({ keys }) => [keys.state.modelStaleAt, staleAt]));
|
|
533
|
-
};
|
|
534
|
-
const updatedAtOf = (model: Full): string | null => {
|
|
535
|
-
const updatedAt = (model as unknown as { updatedAt?: { toISOString?: () => string } }).updatedAt;
|
|
536
|
-
return typeof updatedAt?.toISOString === "function" ? updatedAt.toISOString() : null;
|
|
537
|
-
};
|
|
538
|
-
const servedFetch = <T>(key: string) => {
|
|
539
|
-
const fn = servedEndpointOf<(...args: any[]) => Promise<T>>(fetch, key);
|
|
540
|
-
if (fn) return fn;
|
|
541
|
-
throw restoreRemoteError({ error: "base.error.endpointClosed", data: { endpoint: key } }, 404);
|
|
542
|
-
};
|
|
543
|
-
function armDraft(this: SetGet, draftScope: string | undefined, form: object, baseUpdatedAt: string | null) {
|
|
544
|
-
openFormBase = draftScope ? form : null;
|
|
545
|
-
if (!draftScope) return null;
|
|
546
|
-
|
|
547
|
-
void DraftStore.reconcileIdentity();
|
|
548
|
-
return {
|
|
549
|
-
key: DraftStore.keyOf(refName, draftScope),
|
|
550
|
-
baseHash: DraftStore.formHash(refName, form),
|
|
551
|
-
baseUpdatedAt,
|
|
552
|
-
pending: null,
|
|
553
|
-
appliedAt: null,
|
|
554
|
-
};
|
|
555
|
-
}
|
|
556
|
-
async function applyDraft(this: SetGet, { auto }: { auto: boolean }) {
|
|
557
|
-
const draft = (this.get() as { [key: string]: any })[names.modelDraft] as DraftState | null;
|
|
558
|
-
if (!draft) return;
|
|
559
|
-
const record = await DraftStore.read(draft.key);
|
|
560
|
-
if (!record) return;
|
|
561
|
-
|
|
562
|
-
const current = (this.get() as { [key: string]: any })[names.modelDraft] as DraftState | null;
|
|
563
|
-
if (current?.key !== draft.key) return;
|
|
564
|
-
|
|
565
|
-
const openedForm = (this.get() as { [key: string]: any })[names.modelForm] as object;
|
|
566
|
-
if (DraftStore.contentHash(record.form) === DraftStore.contentHash(DraftStore.encodeForm(refName, openedForm))) {
|
|
567
|
-
await DraftStore.remove(draft.key);
|
|
568
|
-
return;
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
const draftedId = record.form.id;
|
|
572
|
-
if (typeof draftedId === "string" && draftedId && draftedId !== (openedForm as { id?: unknown }).id) {
|
|
573
|
-
await DraftStore.remove(draft.key);
|
|
574
|
-
return;
|
|
575
|
-
}
|
|
576
|
-
let form: object;
|
|
577
|
-
try {
|
|
578
|
-
form = DraftStore.decodeForm(refName, record.form);
|
|
579
|
-
} catch {
|
|
580
|
-
|
|
581
|
-
await DraftStore.remove(draft.key);
|
|
582
|
-
return;
|
|
583
|
-
}
|
|
584
|
-
const savedAt = new Date(record.savedAt);
|
|
585
|
-
if (auto || record.baseUpdatedAt === draft.baseUpdatedAt) {
|
|
586
|
-
this.set({ [names.modelForm]: form, [names.modelDraft]: { ...draft, appliedAt: savedAt } });
|
|
587
|
-
return;
|
|
588
|
-
}
|
|
589
|
-
this.set({ [names.modelDraft]: { ...draft, pending: { savedAt, form } } });
|
|
590
|
-
}
|
|
591
|
-
const clearDraft = (draft: DraftState | null) => {
|
|
592
|
-
openFormBase = null;
|
|
593
|
-
if (draft) void DraftStore.remove(draft.key);
|
|
594
|
-
return null;
|
|
595
|
-
};
|
|
596
|
-
const isLiveSlice = (sliceName: string) => !!slices.find((entry) => entry.sliceName === sliceName)?.slice.live;
|
|
597
|
-
|
|
598
|
-
const createdListPatchOf = (sliceName: string, model: Full, seen: { [key: string]: unknown }) => {
|
|
599
|
-
const listKey = sliceName.replace(names.model, names.modelList);
|
|
600
|
-
if (isLiveSlice(sliceName) || seen[sliceName.replace(names.model, names.modelListLoading)] || !seen[listKey])
|
|
601
|
-
return {};
|
|
602
|
-
const insightKey = sliceName.replace(names.model, names.modelInsight);
|
|
603
|
-
const modelInsight = seen[insightKey] as Insight & BaseInsight;
|
|
604
|
-
return {
|
|
605
|
-
[listKey]: new DataList([model, ...(seen[listKey] as DataList<Light>)]),
|
|
606
|
-
[insightKey]: new cnst.insight().set({ ...modelInsight, count: modelInsight.count + 1 }),
|
|
607
|
-
};
|
|
608
|
-
};
|
|
609
|
-
function applyLiveCreate(this: SetGet, sliceName: string, model: Full) {
|
|
610
|
-
const target = slices.find((entry) => entry.sliceName === sliceName);
|
|
611
|
-
if (!target?.slice.live) return;
|
|
612
|
-
const applyLive = (this as unknown as DynamicRecord)[target.keys.action.applyLiveModel] as
|
|
613
|
-
| ((event: LiveEventPayload) => void)
|
|
614
|
-
| undefined;
|
|
615
|
-
applyLive?.({ op: "enter", id: model.id, light: new cnst.light().set(model) as unknown as object });
|
|
616
|
-
}
|
|
617
|
-
const updatedListPatchOf = (state: { [key: string]: unknown }, ...updatedModels: Full[]) => {
|
|
618
|
-
const lightModels = withSharedInstances(() =>
|
|
619
|
-
updatedModels.map((updatedModel) => new cnst.light().set(updatedModel) as unknown as Light),
|
|
620
|
-
);
|
|
621
|
-
const patch: { [key: string]: DataList<Light> } = {};
|
|
622
|
-
for (const { keys } of slices) {
|
|
623
|
-
const namesOfSlice = keys.state;
|
|
624
|
-
const modelList = state[namesOfSlice.modelList] as DataList<Light>;
|
|
625
|
-
if (state[namesOfSlice.modelListLoading] || !lightModels.some(({ id }) => modelList.has(id))) continue;
|
|
626
|
-
const nextList = new DataList(modelList);
|
|
627
|
-
for (const lightModel of lightModels) if (nextList.has(lightModel.id)) nextList.set(lightModel);
|
|
628
|
-
patch[namesOfSlice.modelList] = nextList;
|
|
629
|
-
}
|
|
630
|
-
return patch;
|
|
631
|
-
};
|
|
281
|
+
const model = modelActionContextOf(refName, slice, fetch);
|
|
632
282
|
const baseAction = {
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
)
|
|
637
|
-
const currentState = this.get() as { [key: string]: any };
|
|
638
|
-
const modelForm = currentState[names.modelForm] as Input;
|
|
639
|
-
const defaultModel = currentState[defaultKeyOf(sliceName)] as Full;
|
|
640
|
-
const modelInput = (cnst.input.purify as (form: any) => DefaultOf<Input> | null)(modelForm);
|
|
641
|
-
|
|
642
|
-
if (!modelInput || isUploadingIn(currentState)) return;
|
|
643
|
-
this.set({ [names.modelLoading]: true });
|
|
644
|
-
const model = await servedFetch<Full>(names.createModel)(modelInput, { onError });
|
|
645
|
-
this.set({
|
|
646
|
-
[names.modelForm]: immerify(modelRef, defaultModel),
|
|
647
|
-
[names.modelDraft]: clearDraft(currentState[names.modelDraft] as DraftState | null),
|
|
648
|
-
[names.model]: model,
|
|
649
|
-
[names.modelLoading]: false,
|
|
650
|
-
[names.modelViewAt]: new Date(),
|
|
651
|
-
[names.modelModal]: modal ?? null,
|
|
652
|
-
...staleAtOfSlices(),
|
|
653
|
-
...(typeof path === "string" && path ? { [path]: model } : {}),
|
|
654
|
-
...createdListPatchOf(sliceName, model, this.get()),
|
|
655
|
-
});
|
|
656
|
-
applyLiveCreate.call(this, sliceName, model);
|
|
657
|
-
await onSuccess?.(model);
|
|
658
|
-
},
|
|
659
|
-
[names.updateModelInForm]: async function (
|
|
660
|
-
this: SetGet,
|
|
661
|
-
{ path, modal, sliceName = names.model, onError, onSuccess }: CreateOption<Full> = {},
|
|
662
|
-
) {
|
|
663
|
-
const currentState = this.get() as { [key: string]: any };
|
|
664
|
-
const model = currentState[names.model] as Full | null;
|
|
665
|
-
const modelForm = currentState[names.modelForm] as Input & { id: string };
|
|
666
|
-
const defaultModel = currentState[defaultKeyOf(sliceName)] as Full;
|
|
667
|
-
const modelInput = (cnst.input.purify as (form: any) => DefaultOf<Input> | null)(modelForm);
|
|
668
|
-
if (!modelInput || isUploadingIn(currentState)) return;
|
|
669
|
-
if (model?.id === modelForm.id) this.set({ [names.modelLoading]: modelForm.id });
|
|
670
|
-
const updatedModel = await servedFetch<Full>(names.updateModel)(modelForm.id, modelInput, { onError });
|
|
671
|
-
this.set({
|
|
672
|
-
...(model?.id === updatedModel.id
|
|
673
|
-
? { [names.model]: updatedModel, [names.modelLoading]: false, [names.modelViewAt]: new Date() }
|
|
674
|
-
: {}),
|
|
675
|
-
[names.modelForm]: immerify(modelRef, defaultModel),
|
|
676
|
-
[names.modelDraft]: clearDraft(currentState[names.modelDraft] as DraftState | null),
|
|
677
|
-
[names.modelModal]: modal ?? null,
|
|
678
|
-
...staleAtOfSlices(),
|
|
679
|
-
...(typeof path === "string" && path ? { [path]: updatedModel } : {}),
|
|
680
|
-
...updatedListPatchOf(this.get(), updatedModel),
|
|
681
|
-
});
|
|
682
|
-
await onSuccess?.(updatedModel);
|
|
683
|
-
},
|
|
684
|
-
[names.createModel]: async function (
|
|
685
|
-
this: SetGet,
|
|
686
|
-
data: GetStateObject<Input>,
|
|
687
|
-
{ path, modal, sliceName = names.model, onError, onSuccess }: CreateOption<Full> = {},
|
|
688
|
-
) {
|
|
689
|
-
const modelInput = (cnst.input.purify as (data: any) => Input | null)(data);
|
|
690
|
-
if (!modelInput) return;
|
|
691
|
-
this.set({ [names.modelLoading]: true });
|
|
692
|
-
const model = await servedFetch<Full>(names.createModel)(modelInput, { onError });
|
|
693
|
-
this.set({
|
|
694
|
-
[names.model]: model,
|
|
695
|
-
[names.modelLoading]: false,
|
|
696
|
-
[names.modelViewAt]: new Date(),
|
|
697
|
-
[names.modelModal]: modal ?? null,
|
|
698
|
-
...staleAtOfSlices(),
|
|
699
|
-
...(typeof path === "string" && path ? { [path]: model } : {}),
|
|
700
|
-
...createdListPatchOf(sliceName, model, this.get()),
|
|
701
|
-
});
|
|
702
|
-
applyLiveCreate.call(this, sliceName, model);
|
|
703
|
-
await onSuccess?.(model);
|
|
704
|
-
},
|
|
705
|
-
[names.updateModel]: async function (
|
|
706
|
-
this: SetGet,
|
|
707
|
-
id: string,
|
|
708
|
-
data: GetStateObject<Input>,
|
|
709
|
-
{ path, modal, onError, onSuccess }: CreateOption<Full> = {},
|
|
710
|
-
) {
|
|
711
|
-
const currentState = this.get() as { [key: string]: any };
|
|
712
|
-
const model = currentState[names.model] as Full | null;
|
|
713
|
-
const modelInput = (cnst.input.purify as (data: any) => DefaultOf<Input> | null)(data);
|
|
714
|
-
if (!modelInput) return;
|
|
715
|
-
if (model?.id === id) this.set({ [names.modelLoading]: id });
|
|
716
|
-
const updatedModel = await servedFetch<Full>(names.updateModel)(id, modelInput, {
|
|
717
|
-
onError,
|
|
718
|
-
});
|
|
719
|
-
this.set({
|
|
720
|
-
...(model?.id === updatedModel.id
|
|
721
|
-
? { [names.model]: updatedModel, [names.modelLoading]: false, [names.modelViewAt]: new Date() }
|
|
722
|
-
: {}),
|
|
723
|
-
[names.modelModal]: modal ?? null,
|
|
724
|
-
...staleAtOfSlices(),
|
|
725
|
-
...(typeof path === "string" && path ? { [path]: updatedModel } : {}),
|
|
726
|
-
...updatedListPatchOf(this.get(), updatedModel),
|
|
727
|
-
});
|
|
728
|
-
await onSuccess?.(updatedModel);
|
|
729
|
-
},
|
|
730
|
-
[names.removeModel]: async function (this: SetGet, id: string, options?: FetchPolicy & { modal?: string | null }) {
|
|
731
|
-
const { modal, ...fetchPolicyOptions } = options ?? {};
|
|
732
|
-
const model = await servedFetch<Full & { removedAt: Dayjs | null }>(names.removeModel)(id, fetchPolicyOptions);
|
|
733
|
-
const currentState = this.get() as { [key: string]: unknown };
|
|
734
|
-
if (!model.removedAt) {
|
|
735
|
-
const listPatch = updatedListPatchOf(currentState, model);
|
|
736
|
-
this.set({
|
|
737
|
-
...listPatch,
|
|
738
|
-
...(modal !== undefined && Object.keys(listPatch).length ? { [names.modelModal]: modal } : {}),
|
|
739
|
-
...staleAtOfSlices(),
|
|
740
|
-
});
|
|
741
|
-
return;
|
|
742
|
-
}
|
|
743
|
-
const removedPatch: { [key: string]: unknown } = {};
|
|
744
|
-
for (const { keys } of slices) {
|
|
745
|
-
const namesOfSlice = keys.state;
|
|
746
|
-
const modelList = currentState[namesOfSlice.modelList] as DataList<Light>;
|
|
747
|
-
const modelSelection = currentState[namesOfSlice.modelSelection] as DataList<Light>;
|
|
748
|
-
const modelInsight = currentState[namesOfSlice.modelInsight] as Insight & BaseInsight;
|
|
749
|
-
if (currentState[namesOfSlice.modelListLoading] || !modelList.has(model.id)) continue;
|
|
750
|
-
removedPatch[namesOfSlice.modelList] = new DataList(modelList).delete(id);
|
|
751
|
-
removedPatch[namesOfSlice.modelInsight] = new cnst.insight().set({
|
|
752
|
-
...modelInsight,
|
|
753
|
-
count: modelInsight.count - 1,
|
|
754
|
-
});
|
|
755
|
-
if (modelSelection.has(model.id))
|
|
756
|
-
removedPatch[namesOfSlice.modelSelection] = new DataList(modelSelection).delete(id);
|
|
757
|
-
if (modal !== undefined) removedPatch[names.modelModal] = modal;
|
|
758
|
-
}
|
|
759
|
-
this.set({ ...removedPatch, ...staleAtOfSlices() });
|
|
760
|
-
},
|
|
761
|
-
[names.checkModelSubmitable]: function (this: SetGet, disabled?: boolean) {
|
|
762
|
-
const currentState = this.get() as { [key: string]: any };
|
|
763
|
-
const modelForm = currentState[names.modelForm] as Input;
|
|
764
|
-
const modelSubmit = currentState[names.modelSubmit] as { disabled: boolean };
|
|
765
|
-
const modelInput = (cnst.input.purify as (obj: any) => DefaultOf<Input> | null)(modelForm);
|
|
766
|
-
const isDisabled = !modelInput || isUploadingIn(currentState) || disabled;
|
|
767
|
-
this.set({ [names.modelSubmit]: { ...modelSubmit, disabled: isDisabled } });
|
|
768
|
-
},
|
|
769
|
-
[names.submitModel]: async function (this: SetGet, option?: CreateOption<Full>) {
|
|
770
|
-
const currentState = this.get() as { [key: string]: any };
|
|
771
|
-
const modelForm = currentState[names.modelForm] as Input & { id: string };
|
|
772
|
-
const modelSubmit = currentState[names.modelSubmit] as { loading: boolean; times: number };
|
|
773
|
-
this.set({ [names.modelSubmit]: { ...modelSubmit, loading: true } });
|
|
774
|
-
if (modelForm.id)
|
|
775
|
-
await ((this as unknown as DynamicRecord)[names.updateModelInForm] as (...args: any[]) => Promise<Full>)(
|
|
776
|
-
option,
|
|
777
|
-
);
|
|
778
|
-
else
|
|
779
|
-
await ((this as unknown as DynamicRecord)[names.createModelInForm] as (...args: any[]) => Promise<Full>)(
|
|
780
|
-
option,
|
|
781
|
-
);
|
|
782
|
-
this.set({ [names.modelSubmit]: { ...modelSubmit, loading: false, times: modelSubmit.times + 1 } });
|
|
783
|
-
},
|
|
784
|
-
[names.newModel]: function (
|
|
785
|
-
this: SetGet,
|
|
786
|
-
partial: Partial<Full> = {},
|
|
787
|
-
{ modal, setDefault, sliceName = names.model, draftScope }: NewOption = {},
|
|
788
|
-
) {
|
|
789
|
-
const defaultModelKey = defaultKeyOf(sliceName);
|
|
790
|
-
const currentState = this.get() as { [key: string]: any };
|
|
791
|
-
const defaultModel = currentState[defaultModelKey] as Full;
|
|
792
|
-
const merged = { ...plainFieldsOf(defaultModel), ...partial };
|
|
793
|
-
const modelForm = immerify(modelRef, merged);
|
|
794
|
-
this.set({
|
|
795
|
-
[names.modelForm]: modelForm,
|
|
796
|
-
[defaultModelKey]: setDefault ? immerify(modelRef, merged) : defaultModel,
|
|
797
|
-
[names.model]: null,
|
|
798
|
-
[names.modelModal]: modal ?? "edit",
|
|
799
|
-
[names.modelFormLoading]: false,
|
|
800
|
-
[names.modelDraft]: armDraft.call(this, draftScope, modelForm, null),
|
|
801
|
-
});
|
|
802
|
-
|
|
803
|
-
if (draftScope) void applyDraft.call(this, { auto: true });
|
|
804
|
-
},
|
|
805
|
-
[names.editModel]: async function (
|
|
806
|
-
this: SetGet,
|
|
807
|
-
modelOrId: Full | string,
|
|
808
|
-
{ modal, onError, draftScope }: EditOption & FetchPolicy = {},
|
|
809
|
-
) {
|
|
810
|
-
const id = typeof modelOrId === "string" ? modelOrId : modelOrId.id;
|
|
811
|
-
this.set({ [names.modelFormLoading]: id, [names.modelModal]: modal ?? "edit" });
|
|
812
|
-
const model = await servedFetch<Full>(names.model)(id, { onError });
|
|
813
|
-
const modelForm = immerify(modelRef, deepObjectify<Input>(model as unknown as Input) as object) as Input;
|
|
814
|
-
this.set({
|
|
815
|
-
[names.model]: model,
|
|
816
|
-
[names.modelFormLoading]: false,
|
|
817
|
-
[names.modelViewAt]: new Date(),
|
|
818
|
-
[names.modelForm]: modelForm,
|
|
819
|
-
[names.modelDraft]: armDraft.call(this, draftScope, modelForm, updatedAtOf(model)),
|
|
820
|
-
});
|
|
821
|
-
|
|
822
|
-
if (draftScope) void applyDraft.call(this, { auto: false });
|
|
823
|
-
},
|
|
824
|
-
[names.mergeModel]: async function (
|
|
825
|
-
this: SetGet,
|
|
826
|
-
modelOrId: Full | string,
|
|
827
|
-
data: Partial<Full>,
|
|
828
|
-
options?: FetchPolicy,
|
|
829
|
-
) {
|
|
830
|
-
const id = typeof modelOrId === "string" ? modelOrId : modelOrId.id;
|
|
831
|
-
const currentState = this.get() as { [key: string]: any };
|
|
832
|
-
const model = currentState[names.model] as Full | null;
|
|
833
|
-
if (id === model?.id) this.set({ modelLoading: id });
|
|
834
|
-
const updatedModel = await servedFetch<Full>(names.mergeModel)(modelOrId, data, options);
|
|
835
|
-
this.set({
|
|
836
|
-
[names.model]: id === model?.id ? updatedModel : model,
|
|
837
|
-
[names.modelLoading]: false,
|
|
838
|
-
...staleAtOfSlices(),
|
|
839
|
-
...updatedListPatchOf(this.get(), updatedModel),
|
|
840
|
-
});
|
|
841
|
-
},
|
|
842
|
-
[names.viewModel]: async function (
|
|
843
|
-
this: SetGet,
|
|
844
|
-
modelOrId: Full | string,
|
|
845
|
-
{ modal, onError }: { modal?: string | null } & FetchPolicy = {},
|
|
846
|
-
) {
|
|
847
|
-
const id = typeof modelOrId === "string" ? modelOrId : modelOrId.id;
|
|
848
|
-
this.set({ [names.modelModal]: modal ?? "view", [names.modelLoading]: id });
|
|
849
|
-
const model = await servedFetch<Full>(names.model)(id, { onError });
|
|
850
|
-
this.set({ [names.model]: model, [names.modelViewAt]: new Date(), [names.modelLoading]: false });
|
|
851
|
-
},
|
|
852
|
-
[names.setModel]: function (this: SetGet, ...fullOrLightModels: Full[]) {
|
|
853
|
-
const currentState = this.get() as { [key: string]: any };
|
|
854
|
-
if (fullOrLightModels.length === 0) return;
|
|
855
|
-
const firstModel = fullOrLightModels[0];
|
|
856
|
-
const model = currentState[names.model] as Full | null;
|
|
857
|
-
const isFull = firstModel instanceof modelRef;
|
|
858
|
-
const crystalizedModel = isFull
|
|
859
|
-
? (new cnst.full().set(firstModel) as unknown as Full)
|
|
860
|
-
: model?.id === firstModel.id
|
|
861
|
-
? (new cnst.full().set(model).set(firstModel) as unknown as Full)
|
|
862
|
-
: null;
|
|
863
|
-
this.set({
|
|
864
|
-
...(crystalizedModel ? { [names.model]: crystalizedModel } : {}),
|
|
865
|
-
...updatedListPatchOf(currentState, ...fullOrLightModels),
|
|
866
|
-
...staleAtOfSlices(),
|
|
867
|
-
});
|
|
868
|
-
},
|
|
869
|
-
[names.resetModel]: function (this: SetGet, model?: Full) {
|
|
870
|
-
const currentState = this.get() as { [key: string]: any };
|
|
871
|
-
const defaultModel = currentState[names.defaultModel] as Full;
|
|
872
|
-
this.set({
|
|
873
|
-
[names.model]: model ?? null,
|
|
874
|
-
[names.modelViewAt]: new Date(0),
|
|
875
|
-
[names.modelForm]: immerify(modelRef, defaultModel),
|
|
876
|
-
[names.modelModal]: null,
|
|
877
|
-
[names.modelDraft]: clearDraft(currentState[names.modelDraft] as DraftState | null),
|
|
878
|
-
});
|
|
879
|
-
return model ?? null;
|
|
880
|
-
},
|
|
881
|
-
[names.invalidateModel]: function (this: SetGet) {
|
|
882
|
-
this.set(staleAtOfSlices());
|
|
883
|
-
},
|
|
884
|
-
|
|
885
|
-
[names.loadModelFormDraft]: async function (this: SetGet, draftScope?: string) {
|
|
886
|
-
const currentState = this.get() as { [key: string]: any };
|
|
887
|
-
const model = currentState[names.model] as Full | null;
|
|
888
|
-
const modelForm = currentState[names.modelForm] as object;
|
|
889
|
-
this.set({
|
|
890
|
-
[names.modelDraft]: armDraft.call(this, draftScope, modelForm, model ? updatedAtOf(model) : null),
|
|
891
|
-
});
|
|
892
|
-
if (draftScope) await applyDraft.call(this, { auto: false });
|
|
893
|
-
},
|
|
894
|
-
[names.restoreModelFormDraft]: function (this: SetGet) {
|
|
895
|
-
const draft = (this.get() as { [key: string]: any })[names.modelDraft] as DraftState | null;
|
|
896
|
-
if (!draft?.pending) return;
|
|
897
|
-
this.set({
|
|
898
|
-
[names.modelForm]: draft.pending.form,
|
|
899
|
-
[names.modelDraft]: { ...draft, pending: null, appliedAt: draft.pending.savedAt },
|
|
900
|
-
});
|
|
901
|
-
},
|
|
902
|
-
[names.discardModelFormDraft]: function (this: SetGet) {
|
|
903
|
-
const draft = (this.get() as { [key: string]: any })[names.modelDraft] as DraftState | null;
|
|
904
|
-
if (!draft) return;
|
|
905
|
-
void DraftStore.remove(draft.key);
|
|
906
|
-
|
|
907
|
-
this.set({
|
|
908
|
-
...(draft.appliedAt && openFormBase ? { [names.modelForm]: openFormBase } : {}),
|
|
909
|
-
[names.modelDraft]: { ...draft, pending: null, appliedAt: null },
|
|
910
|
-
});
|
|
911
|
-
},
|
|
283
|
+
...formSaveActionsOf(model),
|
|
284
|
+
...modelWriteActionsOf(model),
|
|
285
|
+
...modelOpenActionsOf(model),
|
|
286
|
+
...modelStateActionsOf(model),
|
|
912
287
|
};
|
|
913
|
-
const sliceAction = slices.reduce((acc,
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
liveWatch.dispose();
|
|
923
|
-
liveClosed = { signature: liveWatch.signature, at: new Date() };
|
|
924
|
-
liveWatch = null;
|
|
925
|
-
};
|
|
926
|
-
const livePauseIdxs = (slice.live?.pauseOn ?? [])
|
|
927
|
-
.map((name) => slice.args.findIndex((arg) => arg.name === name))
|
|
928
|
-
.filter((idx) => idx >= 0);
|
|
929
|
-
const namesOfSlice = { ...keys.state, ...keys.action };
|
|
930
|
-
|
|
931
|
-
const hasMoreFrom = (batch: unknown[], askedFor: number) => ({
|
|
932
|
-
[namesOfSlice.hasMoreOfModel]: batch.length >= (askedFor > 0 ? Math.min(askedFor, MAX_PAGE_SIZE) : MAX_PAGE_SIZE),
|
|
933
|
-
});
|
|
934
|
-
|
|
935
|
-
const lastPageFrom = (count: number, limit: number) => ({
|
|
936
|
-
[namesOfSlice.lastPageOfModel]: limit > 0 ? Math.max(Math.floor((count - 1) / limit) + 1, 1) : 1,
|
|
937
|
-
});
|
|
938
|
-
const fetchList = (queryArgs: unknown[], skip: number, limit: number, sort: Sort, options?: object) =>
|
|
939
|
-
(fetch[keys.endpoint.list] as (...args: any[]) => Promise<Light[]>)(
|
|
940
|
-
...expandQueryArgs(queryArgs, slice.args),
|
|
941
|
-
skip,
|
|
942
|
-
limit,
|
|
943
|
-
sort,
|
|
944
|
-
options,
|
|
945
|
-
);
|
|
946
|
-
const fetchInsight = (queryArgs: unknown[], options?: object) =>
|
|
947
|
-
(fetch[keys.endpoint.insight] as (...args: any[]) => Promise<Insight & BaseInsight>)(
|
|
948
|
-
...expandQueryArgs(queryArgs, slice.args),
|
|
949
|
-
options,
|
|
950
|
-
);
|
|
951
|
-
const insightOf = (modelObjInsight: (Insight & BaseInsight) | null, modelDataList: Light[]) =>
|
|
952
|
-
modelObjInsight ?? (new cnst.insight().set({ count: modelDataList.length }) as unknown as Insight & BaseInsight);
|
|
953
|
-
const queryArgsIn = (self: SetGet) =>
|
|
954
|
-
(self.get() as { [key: string]: any })[namesOfSlice.queryArgsOfModel] as object[];
|
|
955
|
-
const freshLoad = Symbol("freshLoad");
|
|
956
|
-
|
|
957
|
-
const loadList = async <T>(
|
|
958
|
-
self: SetGet,
|
|
959
|
-
callerSignal: AbortSignal | undefined,
|
|
960
|
-
load: (signal: AbortSignal) => Promise<T>,
|
|
961
|
-
commit: (loaded: T, requestedAt: Date) => object,
|
|
962
|
-
) => {
|
|
963
|
-
if (callerSignal?.aborted) return requests.endSpent(callerSignal);
|
|
964
|
-
self.set({ [namesOfSlice.modelListLoading]: true });
|
|
965
|
-
const { ticket, signal, dispose, endsQuietly } = requests.claim(callerSignal);
|
|
966
|
-
try {
|
|
967
|
-
for (let round = 0; round < 2; round += 1) {
|
|
968
|
-
|
|
969
|
-
const requestedAt = new Date();
|
|
970
|
-
const queryArgs = queryArgsIn(self);
|
|
971
|
-
const loaded = await load(signal);
|
|
972
|
-
|
|
973
|
-
if (!requests.isCurrent(ticket) || queryArgsIn(self) !== queryArgs) return;
|
|
974
|
-
self.set(commit(loaded, requestedAt));
|
|
975
|
-
const staleAt = (self.get() as { [key: string]: any })[namesOfSlice.modelStaleAt] as Date;
|
|
976
|
-
if (staleAt.getTime() <= requestedAt.getTime()) return;
|
|
977
|
-
}
|
|
978
|
-
} catch (error) {
|
|
979
|
-
if (endsQuietly()) return;
|
|
980
|
-
throw error;
|
|
981
|
-
} finally {
|
|
982
|
-
dispose();
|
|
983
|
-
if (requests.isCurrent(ticket)) self.set({ [namesOfSlice.modelListLoading]: false });
|
|
984
|
-
}
|
|
985
|
-
};
|
|
986
|
-
const singleSliceAction = {
|
|
987
|
-
[namesOfSlice.initModel]: async function (
|
|
988
|
-
this: SetGet,
|
|
989
|
-
...args: [...args: any[], initForm: FetchInitForm<Input, Filter> & FetchPolicy]
|
|
990
|
-
) {
|
|
991
|
-
const initArgLength = Math.min(args.length, slice.args.length);
|
|
992
|
-
const initForm = {
|
|
993
|
-
invalidate: false,
|
|
994
|
-
insight: true,
|
|
995
|
-
...(args[slice.args.length] ?? {}),
|
|
996
|
-
} as FetchInitForm<Input, Filter> & FetchPolicy;
|
|
997
|
-
const queryArgs = new Array(initArgLength).fill(null).map((_, i) => args[i] as object);
|
|
998
|
-
const defaultModel = new cnst.full().set(initForm.default ?? {}) as unknown as Full;
|
|
999
|
-
if (!initForm.signal?.aborted) this.set({ [names.defaultModel]: defaultModel });
|
|
1000
|
-
await ((this as unknown as DynamicRecord)[namesOfSlice.refreshModel] as (...args: any[]) => Promise<void>)({
|
|
1001
|
-
...initForm,
|
|
1002
|
-
queryArgs,
|
|
1003
|
-
[freshLoad]: true,
|
|
1004
|
-
});
|
|
1005
|
-
},
|
|
1006
|
-
[namesOfSlice.refreshModel]: async function (
|
|
1007
|
-
this: SetGet,
|
|
1008
|
-
initForm: FetchInitForm<Input, Filter> & FetchPolicy & { queryArgs?: any[]; [freshLoad]?: boolean } = {},
|
|
1009
|
-
) {
|
|
1010
|
-
const args = initForm.queryArgs ?? [];
|
|
1011
|
-
const refreshArgLength = Math.min(args.length, slice.args.length);
|
|
1012
|
-
const currentState = this.get() as { [key: string]: any };
|
|
1013
|
-
const existingQueryArgs = currentState[namesOfSlice.queryArgsOfModel] as object[];
|
|
1014
|
-
const queryArgs = normalizeQueryArgs(
|
|
1015
|
-
[
|
|
1016
|
-
...new Array(refreshArgLength).fill(null).map((_, i) => args[i] as object),
|
|
1017
|
-
...existingQueryArgs.slice(refreshArgLength, slice.args.length),
|
|
1018
|
-
],
|
|
1019
|
-
slice.args,
|
|
1020
|
-
);
|
|
1021
|
-
const {
|
|
1022
|
-
default: defaultFromInitForm,
|
|
1023
|
-
insight = currentState[namesOfSlice.hasInsightOfModel] as boolean,
|
|
1024
|
-
page = currentState[namesOfSlice.pageOfModel] as number,
|
|
1025
|
-
limit = currentState[namesOfSlice.limitOfModel] as number,
|
|
1026
|
-
sort = currentState[namesOfSlice.sortOfModel] as Sort,
|
|
1027
|
-
invalidate = true,
|
|
1028
|
-
queryArgs: queryArgsFromInitForm,
|
|
1029
|
-
[freshLoad]: fresh = false,
|
|
1030
|
-
...fetchPolicy
|
|
1031
|
-
} = initForm;
|
|
1032
|
-
const modelOperation = currentState[names.modelOperation] as string;
|
|
1033
|
-
const isCumulative = !fresh && (currentState[namesOfSlice.isCumulativeOfModel] as boolean);
|
|
1034
|
-
const loadedLength = (currentState[namesOfSlice.modelList] as DataList<Light>).length;
|
|
1035
|
-
|
|
1036
|
-
const fetchLimit = isCumulative ? Math.max(limit, loadedLength) : limit;
|
|
1037
|
-
const queryArgsOfModel = currentState[namesOfSlice.queryArgsOfModel] as object[];
|
|
1038
|
-
const pageOfModel = currentState[namesOfSlice.pageOfModel] as number;
|
|
1039
|
-
const limitOfModel = currentState[namesOfSlice.limitOfModel] as number;
|
|
1040
|
-
const sortOfModel = currentState[namesOfSlice.sortOfModel] as Sort;
|
|
1041
|
-
if (
|
|
1042
|
-
!invalidate &&
|
|
1043
|
-
!["sleep", "reset"].includes(modelOperation) &&
|
|
1044
|
-
isQueryEqual(queryArgs, queryArgsOfModel) &&
|
|
1045
|
-
page === pageOfModel &&
|
|
1046
|
-
limit === limitOfModel &&
|
|
1047
|
-
isQueryEqual(sort as unknown as object, sortOfModel as unknown as object)
|
|
1048
|
-
)
|
|
1049
|
-
return;
|
|
1050
|
-
await loadList(
|
|
1051
|
-
this,
|
|
1052
|
-
fetchPolicy.signal,
|
|
1053
|
-
(signal) =>
|
|
1054
|
-
Promise.all([
|
|
1055
|
-
fetchList(queryArgs, (page - 1) * limit, fetchLimit, sort, { ...fetchPolicy, signal }),
|
|
1056
|
-
insight === false ? null : fetchInsight(queryArgs, { ...fetchPolicy, signal }),
|
|
1057
|
-
]),
|
|
1058
|
-
([modelDataList, modelObjInsight], requestedAt) => {
|
|
1059
|
-
const modelList = new DataList(modelDataList);
|
|
1060
|
-
const modelInsight = insightOf(modelObjInsight, modelDataList);
|
|
1061
|
-
return {
|
|
1062
|
-
[namesOfSlice.modelList]: modelList,
|
|
1063
|
-
[namesOfSlice.modelInsight]: modelInsight,
|
|
1064
|
-
[namesOfSlice.hasInsightOfModel]: insight !== false,
|
|
1065
|
-
[namesOfSlice.modelInitList]: modelList,
|
|
1066
|
-
[namesOfSlice.modelInitAt]: requestedAt,
|
|
1067
|
-
...lastPageFrom(modelInsight.count, limit),
|
|
1068
|
-
...hasMoreFrom(modelDataList, fetchLimit),
|
|
1069
|
-
[namesOfSlice.limitOfModel]: limit,
|
|
1070
|
-
[namesOfSlice.queryArgsOfModel]: queryArgs,
|
|
1071
|
-
[namesOfSlice.sortOfModel]: sort,
|
|
1072
|
-
[namesOfSlice.pageOfModel]: page,
|
|
1073
|
-
[names.modelOperation]: "idle",
|
|
1074
|
-
...(fresh ? { [namesOfSlice.isCumulativeOfModel]: false } : {}),
|
|
1075
|
-
};
|
|
1076
|
-
},
|
|
1077
|
-
);
|
|
1078
|
-
},
|
|
1079
|
-
[namesOfSlice.selectModel]: function (
|
|
1080
|
-
this: SetGet,
|
|
1081
|
-
model: Light | Light[],
|
|
1082
|
-
{ refresh, remove }: { refresh?: boolean; remove?: boolean } = {},
|
|
1083
|
-
) {
|
|
1084
|
-
const models = Array.isArray(model) ? model : [model];
|
|
1085
|
-
const currentState = this.get() as { [key: string]: any };
|
|
1086
|
-
const modelSelection = currentState[namesOfSlice.modelSelection] as DataList<Light>;
|
|
1087
|
-
if (refresh) this.set({ [namesOfSlice.modelSelection]: new DataList(models) });
|
|
1088
|
-
else if (remove) {
|
|
1089
|
-
const newModelSelection = new DataList(modelSelection);
|
|
1090
|
-
models.map((model) => newModelSelection.delete(model.id));
|
|
1091
|
-
this.set({ [namesOfSlice.modelSelection]: newModelSelection });
|
|
1092
|
-
} else {
|
|
1093
|
-
this.set({ [namesOfSlice.modelSelection]: new DataList([...modelSelection.values, ...models]) });
|
|
1094
|
-
}
|
|
1095
|
-
},
|
|
1096
|
-
[namesOfSlice.setPageOfModel]: async function (this: SetGet, page: number, options?: FetchPolicy) {
|
|
1097
|
-
const currentState = this.get() as { [key: string]: any };
|
|
1098
|
-
const queryArgsOfModel = currentState[namesOfSlice.queryArgsOfModel] as object[];
|
|
1099
|
-
const pageOfModel = currentState[namesOfSlice.pageOfModel] as number;
|
|
1100
|
-
const limitOfModel = currentState[namesOfSlice.limitOfModel] as number;
|
|
1101
|
-
const sortOfModel = currentState[namesOfSlice.sortOfModel] as Sort;
|
|
1102
|
-
if (pageOfModel === page) return;
|
|
1103
|
-
await loadList(
|
|
1104
|
-
this,
|
|
1105
|
-
options?.signal,
|
|
1106
|
-
(signal) =>
|
|
1107
|
-
fetchList(queryArgsOfModel, (page - 1) * limitOfModel, limitOfModel, sortOfModel, { ...options, signal }),
|
|
1108
|
-
(modelDataList) => ({
|
|
1109
|
-
[namesOfSlice.modelList]: new DataList(modelDataList),
|
|
1110
|
-
[namesOfSlice.pageOfModel]: page,
|
|
1111
|
-
[namesOfSlice.isCumulativeOfModel]: false,
|
|
1112
|
-
...hasMoreFrom(modelDataList, limitOfModel),
|
|
1113
|
-
}),
|
|
1114
|
-
);
|
|
1115
|
-
},
|
|
1116
|
-
|
|
1117
|
-
[namesOfSlice.loadMoreOfModel]: async function (this: SetGet, options?: FetchPolicy) {
|
|
1118
|
-
const currentState = this.get() as { [key: string]: any };
|
|
1119
|
-
|
|
1120
|
-
if (currentState[namesOfSlice.modelListLoading] as boolean) return;
|
|
1121
|
-
if (!(currentState[namesOfSlice.hasMoreOfModel] as boolean)) return;
|
|
1122
|
-
const modelList = currentState[namesOfSlice.modelList] as DataList<Light>;
|
|
1123
|
-
const queryArgsOfModel = currentState[namesOfSlice.queryArgsOfModel] as object[];
|
|
1124
|
-
const pageOfModel = currentState[namesOfSlice.pageOfModel] as number;
|
|
1125
|
-
const limitOfModel = (currentState[namesOfSlice.limitOfModel] as number) || 20;
|
|
1126
|
-
const sortOfModel = currentState[namesOfSlice.sortOfModel] as Sort;
|
|
1127
|
-
if (options?.signal?.aborted) return requests.endSpent(options.signal);
|
|
1128
|
-
const { ticket, signal, dispose, endsQuietly } = requests.claim(options?.signal);
|
|
1129
|
-
const modelDataList = await fetchList(
|
|
1130
|
-
queryArgsOfModel,
|
|
1131
|
-
(pageOfModel - 1) * limitOfModel + modelList.length,
|
|
1132
|
-
limitOfModel,
|
|
1133
|
-
sortOfModel,
|
|
1134
|
-
{ ...options, signal },
|
|
1135
|
-
)
|
|
1136
|
-
.catch((error: unknown) => {
|
|
1137
|
-
if (endsQuietly()) return null;
|
|
1138
|
-
throw error;
|
|
1139
|
-
})
|
|
1140
|
-
.finally(dispose);
|
|
1141
|
-
if (!modelDataList || !requests.isCurrent(ticket) || queryArgsIn(this) !== queryArgsOfModel) return;
|
|
1142
|
-
|
|
1143
|
-
const currentModelList = (this.get() as { [key: string]: any })[namesOfSlice.modelList] as DataList<Light>;
|
|
1144
|
-
this.set({
|
|
1145
|
-
[namesOfSlice.modelList]: new DataList([...currentModelList.values, ...modelDataList]),
|
|
1146
|
-
[namesOfSlice.isCumulativeOfModel]: true,
|
|
1147
|
-
...hasMoreFrom(modelDataList, limitOfModel),
|
|
1148
|
-
});
|
|
1149
|
-
},
|
|
1150
|
-
[namesOfSlice.setLimitOfModel]: async function (this: SetGet, limit: number, options?: FetchPolicy) {
|
|
1151
|
-
const currentState = this.get() as { [key: string]: any };
|
|
1152
|
-
const modelInsight = currentState[namesOfSlice.modelInsight] as Insight & BaseInsight;
|
|
1153
|
-
const queryArgsOfModel = currentState[namesOfSlice.queryArgsOfModel] as object[];
|
|
1154
|
-
const pageOfModel = currentState[namesOfSlice.pageOfModel] as number;
|
|
1155
|
-
const limitOfModel = currentState[namesOfSlice.limitOfModel] as number;
|
|
1156
|
-
const sortOfModel = currentState[namesOfSlice.sortOfModel] as Sort;
|
|
1157
|
-
if (limitOfModel === limit) return;
|
|
1158
|
-
const skip = (pageOfModel - 1) * limitOfModel;
|
|
1159
|
-
const page = limit > 0 ? Math.max(Math.floor((skip - 1) / limit) + 1, 1) : 1;
|
|
1160
|
-
await loadList(
|
|
1161
|
-
this,
|
|
1162
|
-
options?.signal,
|
|
1163
|
-
(signal) => fetchList(queryArgsOfModel, (page - 1) * limit, limit, sortOfModel, { ...options, signal }),
|
|
1164
|
-
(modelDataList) => ({
|
|
1165
|
-
[namesOfSlice.modelList]: new DataList(modelDataList),
|
|
1166
|
-
...lastPageFrom(modelInsight.count, limit),
|
|
1167
|
-
[namesOfSlice.limitOfModel]: limit,
|
|
1168
|
-
[namesOfSlice.pageOfModel]: page,
|
|
1169
|
-
[namesOfSlice.isCumulativeOfModel]: false,
|
|
1170
|
-
...hasMoreFrom(modelDataList, limit),
|
|
1171
|
-
}),
|
|
1172
|
-
);
|
|
1173
|
-
},
|
|
1174
|
-
[namesOfSlice.setQueryArgsOfModel]: async function (
|
|
1175
|
-
this: SetGet,
|
|
1176
|
-
...args:
|
|
1177
|
-
| [...queryArgs: any, options?: FetchPolicy]
|
|
1178
|
-
| [setQueryArgs: (...prevQueryArgs: object[]) => object[], options?: FetchPolicy]
|
|
1179
|
-
) {
|
|
1180
|
-
const isSetQueryAsFunction = typeof args[0] === "function";
|
|
1181
|
-
const currentState = this.get() as { [key: string]: any };
|
|
1182
|
-
const options = (isSetQueryAsFunction ? args[1] : args[slice.args.length]) as FetchPolicy | undefined;
|
|
1183
|
-
const queryArgsOfModel = currentState[namesOfSlice.queryArgsOfModel] as object[];
|
|
1184
|
-
const queryArgs = normalizeQueryArgs(
|
|
1185
|
-
isSetQueryAsFunction
|
|
1186
|
-
? (args[0] as (...prevQueryArgs: object[]) => object[])(...queryArgsOfModel)
|
|
1187
|
-
: new Array(slice.args.length).fill(null).map((_, i) => args[i] as object),
|
|
1188
|
-
slice.args,
|
|
1189
|
-
);
|
|
1190
|
-
const limitOfModel = currentState[namesOfSlice.limitOfModel] as number;
|
|
1191
|
-
const sortOfModel = currentState[namesOfSlice.sortOfModel] as Sort;
|
|
1192
|
-
const hasInsightOfModel = currentState[namesOfSlice.hasInsightOfModel] as boolean;
|
|
1193
|
-
if (isQueryEqual(queryArgsOfModel, queryArgs)) {
|
|
1194
|
-
Logger.trace(`${namesOfSlice.queryArgsOfModel} store-level cache hit`);
|
|
1195
|
-
return;
|
|
1196
|
-
}
|
|
1197
|
-
await loadList(
|
|
1198
|
-
this,
|
|
1199
|
-
options?.signal,
|
|
1200
|
-
(signal) =>
|
|
1201
|
-
Promise.all([
|
|
1202
|
-
fetchList(queryArgs, 0, limitOfModel, sortOfModel, { ...options, signal }),
|
|
1203
|
-
hasInsightOfModel === false ? null : fetchInsight(queryArgs, { ...options, signal }),
|
|
1204
|
-
]),
|
|
1205
|
-
([modelDataList, modelObjInsight]) => {
|
|
1206
|
-
const modelInsight = insightOf(modelObjInsight, modelDataList);
|
|
1207
|
-
return {
|
|
1208
|
-
[namesOfSlice.queryArgsOfModel]: queryArgs,
|
|
1209
|
-
[namesOfSlice.modelList]: new DataList(modelDataList),
|
|
1210
|
-
[namesOfSlice.modelInsight]: modelInsight,
|
|
1211
|
-
...lastPageFrom(modelInsight.count, limitOfModel),
|
|
1212
|
-
[namesOfSlice.pageOfModel]: 1,
|
|
1213
|
-
[namesOfSlice.isCumulativeOfModel]: false,
|
|
1214
|
-
...hasMoreFrom(modelDataList, limitOfModel),
|
|
1215
|
-
[namesOfSlice.modelSelection]: new Map(),
|
|
1216
|
-
};
|
|
1217
|
-
},
|
|
1218
|
-
);
|
|
1219
|
-
},
|
|
1220
|
-
[namesOfSlice.setSortOfModel]: async function (this: SetGet, sort: Sort, options?: FetchPolicy) {
|
|
1221
|
-
const currentState = this.get() as { [key: string]: any };
|
|
1222
|
-
const queryArgsOfModel = currentState[namesOfSlice.queryArgsOfModel] as object[];
|
|
1223
|
-
const limitOfModel = currentState[namesOfSlice.limitOfModel] as number;
|
|
1224
|
-
const sortOfModel = currentState[namesOfSlice.sortOfModel] as Sort;
|
|
1225
|
-
if (sortOfModel === sort) return;
|
|
1226
|
-
await loadList(
|
|
1227
|
-
this,
|
|
1228
|
-
options?.signal,
|
|
1229
|
-
(signal) => fetchList(queryArgsOfModel, 0, limitOfModel, sort, { ...options, signal }),
|
|
1230
|
-
(modelDataList) => ({
|
|
1231
|
-
[namesOfSlice.modelList]: new DataList(modelDataList),
|
|
1232
|
-
[namesOfSlice.sortOfModel]: sort,
|
|
1233
|
-
[namesOfSlice.pageOfModel]: 1,
|
|
1234
|
-
[namesOfSlice.isCumulativeOfModel]: false,
|
|
1235
|
-
...hasMoreFrom(modelDataList, limitOfModel),
|
|
1236
|
-
}),
|
|
1237
|
-
);
|
|
1238
|
-
},
|
|
1239
|
-
|
|
1240
|
-
[namesOfSlice.applyLiveModel]: function (this: SetGet, event: LiveEventPayload) {
|
|
1241
|
-
const currentState = this.get() as { [key: string]: any };
|
|
1242
|
-
const modelList = currentState[namesOfSlice.modelList] as DataList<Light>;
|
|
1243
|
-
const staleAt = { [namesOfSlice.modelStaleAt]: new Date() };
|
|
1244
|
-
|
|
1245
|
-
if (event.op === "invalidate" || (currentState[namesOfSlice.modelListLoading] as boolean)) {
|
|
1246
|
-
this.set(staleAt);
|
|
1247
|
-
return;
|
|
1248
|
-
}
|
|
1249
|
-
const modelInsight = currentState[namesOfSlice.modelInsight] as Insight & BaseInsight;
|
|
1250
|
-
const limit = currentState[namesOfSlice.limitOfModel] as number;
|
|
1251
|
-
const isCumulative = currentState[namesOfSlice.isCumulativeOfModel] as boolean;
|
|
1252
|
-
const countedTo = (count: number) => ({
|
|
1253
|
-
[namesOfSlice.modelInsight]: new cnst.insight().set({ ...modelInsight, count }),
|
|
1254
|
-
...lastPageFrom(count, limit),
|
|
1255
|
-
});
|
|
1256
|
-
if (event.op === "leave") {
|
|
1257
|
-
if (!modelList.has(event.id)) return;
|
|
1258
|
-
this.set({
|
|
1259
|
-
[namesOfSlice.modelList]: new DataList(modelList).delete(event.id).save(),
|
|
1260
|
-
...countedTo(Math.max(modelInsight.count - 1, 0)),
|
|
1261
|
-
...staleAt,
|
|
1262
|
-
});
|
|
1263
|
-
return;
|
|
1264
|
-
}
|
|
1265
|
-
const light = event.light ? (new cnst.light().set(event.light) as unknown as Light) : null;
|
|
1266
|
-
if (event.op === "update") {
|
|
1267
|
-
|
|
1268
|
-
if (!light || !modelList.has(event.id)) return;
|
|
1269
|
-
this.set({ [namesOfSlice.modelList]: new DataList(modelList).set(light).save() });
|
|
1270
|
-
return;
|
|
1271
|
-
}
|
|
1272
|
-
|
|
1273
|
-
const placement =
|
|
1274
|
-
light && !modelList.has(event.id)
|
|
1275
|
-
? livePlacementIndex({
|
|
1276
|
-
list: modelList as unknown as LiveSortableRow[],
|
|
1277
|
-
row: light as unknown as LiveSortableRow,
|
|
1278
|
-
page: currentState[namesOfSlice.pageOfModel] as number,
|
|
1279
|
-
limit,
|
|
1280
|
-
count: modelInsight.count,
|
|
1281
|
-
cumulative: isCumulative,
|
|
1282
|
-
hasMore: currentState[namesOfSlice.hasMoreOfModel] as boolean,
|
|
1283
|
-
sortKey: String(currentState[namesOfSlice.sortOfModel]),
|
|
1284
|
-
allowedSorts: slice.live?.sort ?? [],
|
|
1285
|
-
sorts: fetch.sortValueMap?.get(refName),
|
|
1286
|
-
})
|
|
1287
|
-
: null;
|
|
1288
|
-
if (!light || placement === null) {
|
|
1289
|
-
this.set({ ...(modelList.has(event.id) ? {} : countedTo(modelInsight.count + 1)), ...staleAt });
|
|
1290
|
-
return;
|
|
1291
|
-
}
|
|
1292
|
-
|
|
1293
|
-
const inserted = [...modelList.slice(0, placement), light, ...modelList.slice(placement)];
|
|
1294
|
-
const placed = isCumulative || !limit ? inserted : inserted.slice(0, limit);
|
|
1295
|
-
this.set({
|
|
1296
|
-
[namesOfSlice.modelList]: new DataList(placed as Light[]),
|
|
1297
|
-
...countedTo(modelInsight.count + 1),
|
|
1298
|
-
...staleAt,
|
|
1299
|
-
});
|
|
1300
|
-
},
|
|
1301
|
-
|
|
1302
|
-
[namesOfSlice.watchLiveModel]: function (this: SetGet, queryArgs: unknown[] | null) {
|
|
1303
|
-
if (!slice.live) return;
|
|
1304
|
-
if (!queryArgs) {
|
|
1305
|
-
liveWatchers = Math.max(liveWatchers - 1, 0);
|
|
1306
|
-
|
|
1307
|
-
if (!liveWatchers)
|
|
1308
|
-
queueMicrotask(() => {
|
|
1309
|
-
if (!liveWatchers) closeLiveWatch();
|
|
1310
|
-
});
|
|
1311
|
-
return;
|
|
1312
|
-
}
|
|
1313
|
-
liveWatchers += 1;
|
|
1314
|
-
const args = expandQueryArgs(normalizeQueryArgs(queryArgs, slice.args), slice.args);
|
|
1315
|
-
|
|
1316
|
-
const paused = livePauseIdxs.some((idx) => args[idx] != null);
|
|
1317
|
-
const signature = paused ? null : JSON.stringify(args);
|
|
1318
|
-
if (liveWatch && signature !== liveWatch.signature) closeLiveWatch();
|
|
1319
|
-
if (!signature || liveWatch) return;
|
|
1320
|
-
const self = this as unknown as DynamicRecord;
|
|
1321
|
-
const apply = self[namesOfSlice.applyLiveModel] as (event: unknown) => void;
|
|
1322
|
-
const refresh = self[namesOfSlice.refreshModel] as (form: object) => Promise<void>;
|
|
1323
|
-
|
|
1324
|
-
const subscribe = fetch[keys.endpoint.subscribeLive] as ((...args: unknown[]) => () => void) | undefined;
|
|
1325
|
-
if (!subscribe) return;
|
|
1326
|
-
const dispose = subscribe(...args, (event: unknown) => apply(event), {
|
|
1327
|
-
crystalize: false,
|
|
1328
|
-
|
|
1329
|
-
onResync: () => void refresh({ invalidate: true }),
|
|
1330
|
-
});
|
|
1331
|
-
liveWatch = { signature, dispose };
|
|
1332
|
-
|
|
1333
|
-
const modelInitAt = (this.get() as { [key: string]: any })[namesOfSlice.modelInitAt] as Date;
|
|
1334
|
-
if (liveClosed?.signature === signature && modelInitAt < liveClosed.at)
|
|
1335
|
-
this.set({ [namesOfSlice.modelStaleAt]: new Date() });
|
|
1336
|
-
},
|
|
1337
|
-
};
|
|
1338
|
-
return Object.assign(acc, singleSliceAction);
|
|
288
|
+
const sliceAction = model.slices.reduce((acc, entry) => {
|
|
289
|
+
const list = sliceListContextOf(model, entry);
|
|
290
|
+
return Object.assign(
|
|
291
|
+
acc,
|
|
292
|
+
sliceLoadActionsOf(list),
|
|
293
|
+
slicePageActionsOf(list),
|
|
294
|
+
sliceQueryActionsOf(list),
|
|
295
|
+
sliceLiveActionsOf(list),
|
|
296
|
+
);
|
|
1339
297
|
}, {});
|
|
1340
298
|
return { ...baseAction, ...sliceAction };
|
|
1341
299
|
};
|