@platforma-sdk/model 1.51.6 → 1.52.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bconfig/lambdas.d.ts +26 -4
- package/dist/bconfig/lambdas.d.ts.map +1 -1
- package/dist/bconfig/v3.d.ts +4 -2
- package/dist/bconfig/v3.d.ts.map +1 -1
- package/dist/block_api_v3.d.ts +32 -0
- package/dist/block_api_v3.d.ts.map +1 -0
- package/dist/block_migrations.cjs +138 -0
- package/dist/block_migrations.cjs.map +1 -0
- package/dist/block_migrations.d.ts +79 -0
- package/dist/block_migrations.d.ts.map +1 -0
- package/dist/block_migrations.js +136 -0
- package/dist/block_migrations.js.map +1 -0
- package/dist/block_model.cjs +222 -0
- package/dist/block_model.cjs.map +1 -0
- package/dist/block_model.d.ts +132 -0
- package/dist/block_model.d.ts.map +1 -0
- package/dist/block_model.js +220 -0
- package/dist/block_model.js.map +1 -0
- package/dist/block_storage.cjs +244 -0
- package/dist/block_storage.cjs.map +1 -0
- package/dist/block_storage.d.ts +208 -0
- package/dist/block_storage.d.ts.map +1 -0
- package/dist/block_storage.js +225 -0
- package/dist/block_storage.js.map +1 -0
- package/dist/block_storage_vm.cjs +264 -0
- package/dist/block_storage_vm.cjs.map +1 -0
- package/dist/block_storage_vm.d.ts +67 -0
- package/dist/block_storage_vm.d.ts.map +1 -0
- package/dist/block_storage_vm.js +260 -0
- package/dist/block_storage_vm.js.map +1 -0
- package/dist/builder.cjs +9 -6
- package/dist/builder.cjs.map +1 -1
- package/dist/builder.d.ts +15 -30
- package/dist/builder.d.ts.map +1 -1
- package/dist/builder.js +10 -7
- package/dist/builder.js.map +1 -1
- package/dist/components/PlDataTable.cjs.map +1 -1
- package/dist/components/PlDataTable.d.ts +2 -2
- package/dist/components/PlDataTable.d.ts.map +1 -1
- package/dist/components/PlDataTable.js.map +1 -1
- package/dist/index.cjs +25 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/internal.cjs +38 -0
- package/dist/internal.cjs.map +1 -1
- package/dist/internal.d.ts +21 -0
- package/dist/internal.d.ts.map +1 -1
- package/dist/internal.js +36 -1
- package/dist/internal.js.map +1 -1
- package/dist/package.json.cjs +1 -1
- package/dist/package.json.js +1 -1
- package/dist/platforma.d.ts +18 -3
- package/dist/platforma.d.ts.map +1 -1
- package/dist/render/api.cjs +43 -16
- package/dist/render/api.cjs.map +1 -1
- package/dist/render/api.d.ts +19 -7
- package/dist/render/api.d.ts.map +1 -1
- package/dist/render/api.js +42 -17
- package/dist/render/api.js.map +1 -1
- package/dist/render/internal.cjs.map +1 -1
- package/dist/render/internal.d.ts +3 -1
- package/dist/render/internal.d.ts.map +1 -1
- package/dist/render/internal.js.map +1 -1
- package/dist/render/util/label.cjs +9 -4
- package/dist/render/util/label.cjs.map +1 -1
- package/dist/render/util/label.d.ts.map +1 -1
- package/dist/render/util/label.js +9 -4
- package/dist/render/util/label.js.map +1 -1
- package/package.json +5 -5
- package/src/bconfig/lambdas.ts +35 -4
- package/src/bconfig/v3.ts +12 -2
- package/src/block_api_v3.ts +49 -0
- package/src/block_migrations.ts +173 -0
- package/src/block_model.ts +440 -0
- package/src/block_storage.test.ts +258 -0
- package/src/block_storage.ts +365 -0
- package/src/block_storage_vm.ts +349 -0
- package/src/builder.ts +24 -59
- package/src/components/PlDataTable.ts +2 -1
- package/src/index.ts +3 -0
- package/src/internal.ts +51 -0
- package/src/platforma.ts +31 -5
- package/src/render/api.ts +52 -21
- package/src/render/internal.ts +3 -1
- package/src/render/util/label.test.ts +25 -0
- package/src/render/util/label.ts +11 -5
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"block_model.js","sources":["../src/block_model.ts"],"sourcesContent":["import type {\n BlockRenderingMode,\n BlockSection,\n OutputWithStatus,\n PlRef,\n BlockCodeKnownFeatureFlags,\n BlockConfigContainer,\n} from '@milaboratories/pl-model-common';\nimport { getPlatformaInstance, isInUI, createAndRegisterRenderLambda, createRenderLambda } from './internal';\nimport type { DataModel } from './block_migrations';\nimport type { PlatformaV3 } from './platforma';\nimport type { InferRenderFunctionReturn, RenderFunction } from './render';\nimport { RenderCtx } from './render';\nimport { PlatformaSDKVersion } from './version';\n// Import storage VM integration (side-effect: registers internal callbacks)\nimport './block_storage_vm';\nimport type {\n ConfigRenderLambda,\n DeriveHref,\n ConfigRenderLambdaFlags,\n InferOutputsFromLambdas,\n} from './bconfig';\nimport {\n downgradeCfgOrLambda,\n isConfigLambda,\n} from './bconfig';\nimport type { PlatformaExtended } from './platforma';\n\ntype SectionsExpectedType = readonly BlockSection[];\n\ntype NoOb = Record<string, never>;\n\ninterface BlockModelV3Config<\n Args,\n OutputsCfg extends Record<string, ConfigRenderLambda>,\n Data,\n> {\n renderingMode: BlockRenderingMode;\n dataModel: DataModel<Data>;\n outputs: OutputsCfg;\n sections: ConfigRenderLambda;\n title: ConfigRenderLambda | undefined;\n subtitle: ConfigRenderLambda | undefined;\n tags: ConfigRenderLambda | undefined;\n enrichmentTargets: ConfigRenderLambda | undefined;\n featureFlags: BlockCodeKnownFeatureFlags;\n args: ConfigRenderLambda<Args> | undefined;\n prerunArgs: ConfigRenderLambda<unknown> | undefined;\n}\n\n/** Options for creating a BlockModelV3 */\nexport interface BlockModelV3Options<Data extends Record<string, unknown>> {\n /** The data model that defines initial data and migrations */\n dataModel: DataModel<Data>;\n /** Rendering mode for the block (default: 'Heavy') */\n renderingMode?: BlockRenderingMode;\n}\n\n/** Main entry point that each block should use in it's \"config\" module. Don't forget\n * to call {@link done()} at the end of configuration. Value returned by this builder must be\n * exported as constant with name \"platforma\" from the \"config\" module.\n * API version is 3 (for UI) and 2 (for model) */\nexport class BlockModelV3<\n Args,\n OutputsCfg extends Record<string, ConfigRenderLambda>,\n Data extends Record<string, unknown> = Record<string, unknown>,\n Href extends `/${string}` = '/',\n> {\n private constructor(\n private readonly config: BlockModelV3Config<Args, OutputsCfg, Data>,\n ) {}\n\n public static readonly INITIAL_BLOCK_FEATURE_FLAGS: BlockCodeKnownFeatureFlags = {\n supportsLazyState: true,\n requiresUIAPIVersion: 3,\n requiresModelAPIVersion: 2,\n };\n\n /**\n * Creates a new BlockModelV3 builder with the specified data model and options.\n *\n * @example\n * const dataModel = DataModel.create<BlockData>(() => ({ numbers: [], labels: [] }));\n * BlockModelV3.create({ dataModel })\n * .args((data) => ({ numbers: data.numbers }))\n * .sections(() => [{ type: 'link', href: '/', label: 'Main' }])\n * .done();\n *\n * @param options Configuration options including required data model\n */\n public static create<Data extends Record<string, unknown>>(\n options: BlockModelV3Options<Data>,\n ): BlockModelV3<NoOb, {}, Data> {\n const { dataModel, renderingMode = 'Heavy' } = options;\n\n // Register data model callbacks for VM use (initialData and upgrade)\n dataModel.registerCallbacks();\n\n return new BlockModelV3<NoOb, {}, Data>({\n renderingMode,\n dataModel,\n outputs: {},\n // Register default sections callback (returns empty array)\n sections: createAndRegisterRenderLambda({ handle: 'sections', lambda: () => [] }, true),\n title: undefined,\n subtitle: undefined,\n tags: undefined,\n enrichmentTargets: undefined,\n featureFlags: { ...BlockModelV3.INITIAL_BLOCK_FEATURE_FLAGS },\n args: undefined,\n prerunArgs: undefined,\n });\n }\n\n /**\n * Add output cell wrapped with additional status information to the configuration\n *\n * @param key output cell name, that can be later used to retrieve the rendered value\n * @param rf callback calculating output value using context, that allows to access\n * workflows outputs and interact with platforma drivers\n * @param flags additional flags that may alter lambda rendering procedure\n * */\n public output<const Key extends string, const RF extends RenderFunction<Args, Data>>(\n key: Key,\n rf: RF,\n flags: ConfigRenderLambdaFlags & { withStatus: true }\n ): BlockModelV3<\n Args,\n OutputsCfg & { [K in Key]: ConfigRenderLambda<InferRenderFunctionReturn<RF>> & { withStatus: true } },\n Data,\n Href\n >;\n /**\n * Add output cell to the configuration\n *\n * @param key output cell name, that can be later used to retrieve the rendered value\n * @param rf callback calculating output value using context, that allows to access\n * workflows outputs and interact with platforma drivers\n * @param flags additional flags that may alter lambda rendering procedure\n * */\n public output<const Key extends string, const RF extends RenderFunction<Args, Data>>(\n key: Key,\n rf: RF,\n flags?: ConfigRenderLambdaFlags\n ): BlockModelV3<\n Args,\n OutputsCfg & { [K in Key]: ConfigRenderLambda<InferRenderFunctionReturn<RF>> },\n Data,\n Href\n >;\n public output(\n key: string,\n cfgOrRf: RenderFunction<Args, Data>,\n flags: ConfigRenderLambdaFlags = {},\n ): BlockModelV3<Args, OutputsCfg, Data, Href> {\n return new BlockModelV3({\n ...this.config,\n outputs: {\n ...this.config.outputs,\n [key]: createAndRegisterRenderLambda({ handle: `output#${key}`, lambda: () => cfgOrRf(new RenderCtx()), ...flags }),\n },\n });\n }\n\n /** Shortcut for {@link output} with retentive flag set to true. */\n public retentiveOutput<const Key extends string, const RF extends RenderFunction<Args, Data>>(\n key: Key,\n rf: RF,\n ): BlockModelV3<\n Args,\n OutputsCfg & { [K in Key]: ConfigRenderLambda<InferRenderFunctionReturn<RF>> },\n Data,\n Href\n > {\n return this.output(key, rf, { retentive: true });\n }\n\n /** Shortcut for {@link output} with withStatus flag set to true. */\n public outputWithStatus<const Key extends string, const RF extends RenderFunction<Args, Data>>(\n key: Key,\n rf: RF,\n ) {\n return this.output(key, rf, { withStatus: true });\n }\n\n /**\n * Sets a function to derive block args from data.\n * This is called during setData to compute the args that will be used for block execution.\n *\n * @example\n * .args<BlockArgs>((data) => ({ numbers: data.numbers }))\n *\n * @example\n * .args<BlockArgs>((data) => {\n * if (data.numbers.length === 0) throw new Error('Numbers required'); // block not ready\n * return { numbers: data.numbers };\n * })\n */\n public args<Args>(lambda: (data: Data) => Args): BlockModelV3<Args, OutputsCfg, Data, Href> {\n return new BlockModelV3<Args, OutputsCfg, Data, Href>({\n ...this.config,\n args: createAndRegisterRenderLambda<Args>({ handle: 'args', lambda }),\n });\n }\n\n /**\n * Sets a function to derive pre-run args from data (optional).\n * This is called during setData to compute the args that will be used for staging/pre-run phase.\n *\n * If not defined, defaults to using the args() function result.\n * If defined, uses its return value for the staging / prerun phase.\n *\n * The staging / prerun phase runs only if currentPrerunArgs differs from the executed\n * version of prerunArgs (same comparison logic as currentArgs vs prodArgs).\n *\n * @example\n * .prerunArgs((data) => ({ numbers: data.numbers }))\n *\n * @example\n * .prerunArgs((data) => {\n * // Return undefined to skip staging for this block\n * if (!data.isReady) return undefined;\n * return { numbers: data.numbers };\n * })\n */\n public prerunArgs(fn: (data: Data) => unknown): BlockModelV3<Args, OutputsCfg, Data, Href> {\n return new BlockModelV3<Args, OutputsCfg, Data, Href>({\n ...this.config,\n prerunArgs: createAndRegisterRenderLambda({ handle: 'prerunArgs', lambda: fn }),\n });\n }\n\n /** Sets the lambda to generate list of sections in the left block overviews panel. */\n public sections<\n const Ret extends SectionsExpectedType,\n const RF extends RenderFunction<Args, Data, Ret>,\n >(rf: RF): BlockModelV3<Args, OutputsCfg, Data, DeriveHref<ReturnType<RF>>> {\n return new BlockModelV3<Args, OutputsCfg, Data, DeriveHref<ReturnType<RF>>>({\n ...this.config,\n // Replace the default sections callback with the user-provided one\n sections: createAndRegisterRenderLambda({ handle: 'sections', lambda: () => rf(new RenderCtx()) }, true),\n });\n }\n\n /** Sets a rendering function to derive block title, shown for the block in the left blocks-overview panel. */\n public title(\n rf: RenderFunction<Args, Data, string>,\n ): BlockModelV3<Args, OutputsCfg, Data, Href> {\n return new BlockModelV3<Args, OutputsCfg, Data, Href>({\n ...this.config,\n title: createAndRegisterRenderLambda({ handle: 'title', lambda: () => rf(new RenderCtx()) }),\n });\n }\n\n public subtitle(\n rf: RenderFunction<Args, Data, string>,\n ): BlockModelV3<Args, OutputsCfg, Data, Href> {\n return new BlockModelV3<Args, OutputsCfg, Data, Href>({\n ...this.config,\n subtitle: createAndRegisterRenderLambda({ handle: 'subtitle', lambda: () => rf(new RenderCtx()) }),\n });\n }\n\n public tags(\n rf: RenderFunction<Args, Data, string[]>,\n ): BlockModelV3<Args, OutputsCfg, Data, Href> {\n return new BlockModelV3<Args, OutputsCfg, Data, Href>({\n ...this.config,\n tags: createAndRegisterRenderLambda({ handle: 'tags', lambda: () => rf(new RenderCtx()) }),\n });\n }\n\n /** Sets or overrides feature flags for the block. */\n public withFeatureFlags(flags: Partial<BlockCodeKnownFeatureFlags>): BlockModelV3<Args, OutputsCfg, Data, Href> {\n return new BlockModelV3<Args, OutputsCfg, Data, Href>({\n ...this.config,\n featureFlags: { ...this.config.featureFlags, ...flags },\n });\n }\n\n /**\n * Defines how to derive list of upstream references this block is meant to enrich with its exports from block args.\n * Influences dependency graph construction.\n */\n public enriches(\n lambda: (args: Args) => PlRef[],\n ): BlockModelV3<Args, OutputsCfg, Data, Href> {\n return new BlockModelV3<Args, OutputsCfg, Data, Href>({\n ...this.config,\n enrichmentTargets: createAndRegisterRenderLambda({ handle: 'enrichmentTargets', lambda: lambda }),\n });\n }\n\n /** Renders all provided block settings into a pre-configured platforma API\n * instance, that can be used in frontend to interact with block data, and\n * other features provided by the platforma to the block. */\n public done(): PlatformaExtended<PlatformaV3<\n Args,\n InferOutputsFromLambdas<OutputsCfg>,\n Data,\n Href\n >> {\n return this.withFeatureFlags({\n ...this.config.featureFlags,\n })._done();\n }\n\n public _done(): PlatformaExtended<PlatformaV3<\n Args,\n InferOutputsFromLambdas<OutputsCfg>,\n Data,\n Href\n >> {\n if (this.config.args === undefined) throw new Error('Args rendering function not set.');\n\n const apiVersion = 3;\n\n const migrationCount = this.config.dataModel.migrationCount;\n\n const blockConfig: BlockConfigContainer = {\n v4: {\n configVersion: 4,\n modelAPIVersion: 2,\n sdkVersion: PlatformaSDKVersion,\n renderingMode: this.config.renderingMode,\n args: this.config.args,\n prerunArgs: this.config.prerunArgs,\n // Reference to __pl_data_initial callback registered by DataModel\n initialData: createRenderLambda({ handle: '__pl_data_initial' }),\n sections: this.config.sections,\n title: this.config.title,\n outputs: this.config.outputs,\n enrichmentTargets: this.config.enrichmentTargets,\n featureFlags: this.config.featureFlags,\n // Generate migration descriptors (indices for metadata)\n migrations: migrationCount > 0\n ? Array.from({ length: migrationCount }, (_, i) => ({ index: i }))\n : undefined,\n },\n\n // fields below are added to allow previous desktop versions read generated configs\n sdkVersion: PlatformaSDKVersion,\n renderingMode: this.config.renderingMode,\n sections: this.config.sections,\n outputs: Object.fromEntries(\n Object.entries(this.config.outputs).map(([key, value]) => [key, downgradeCfgOrLambda(value)]),\n ),\n };\n\n globalThis.platformaApiVersion = apiVersion;\n\n if (!isInUI())\n // we are in the configuration rendering routine, not in actual UI\n return { config: blockConfig } as any;\n // normal operation inside the UI\n else return {\n ...getPlatformaInstance({ sdkVersion: PlatformaSDKVersion, apiVersion }),\n blockModelInfo: {\n outputs: Object.fromEntries(\n Object.entries(this.config.outputs)\n .map(([key, value]) => [key, {\n withStatus: Boolean(isConfigLambda(value) && value.withStatus),\n }]),\n ),\n },\n } as any;\n }\n}\n\n// Type tests for BlockModelV3\n\nexport type Expect<T extends true> = T;\n\nexport type Equal<X, Y> =\n (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? true : false;\n\nexport type Merge<A, B> = {\n [K in keyof A | keyof B]: K extends keyof B ? B[K] : K extends keyof A ? A[K] : never;\n};\n\n// Helper types for testing\ntype _TestArgs = { inputFile: string; threshold: number };\ntype _TestData = { selectedTab: string };\ntype _TestOutputs = { result: ConfigRenderLambda<string>; count: ConfigRenderLambda<number> };\n\n// Test: Merge type works correctly\ntype _MergeTest1 = Expect<Equal<Merge<{ a: 1 }, { b: 2 }>, { a: 1; b: 2 }>>;\ntype _MergeTest2 = Expect<Equal<Merge<{ a: 1 }, { a: 2 }>, { a: 2 }>>;\ntype _MergeTest3 = Expect<Equal<Merge<{ a: 1; b: 1 }, { b: 2; c: 3 }>, { a: 1; b: 2; c: 3 }>>;\n\n// Test: create() returns a BlockModelV3 instance\n// Note: Due to function overloads, ReturnType uses the last overload signature.\n// We verify the structure is correct using a simpler assignability test.\ntype _CreateResult = ReturnType<typeof BlockModelV3.create>;\ntype _CreateIsBlockModelV3 = _CreateResult extends BlockModelV3<infer _A, infer _O, infer _S> ? true : false;\ntype _CreateTest = Expect<_CreateIsBlockModelV3>;\n\n// Test: BlockModelV3Config interface structure\ntype _ConfigTest = Expect<Equal<\n BlockModelV3Config<_TestArgs, _TestOutputs, _TestData>,\n {\n renderingMode: BlockRenderingMode;\n args: ConfigRenderLambda<_TestArgs> | undefined;\n prerunArgs: ConfigRenderLambda<unknown> | undefined;\n dataModel: DataModel<_TestData>;\n outputs: _TestOutputs;\n sections: ConfigRenderLambda;\n title: ConfigRenderLambda | undefined;\n subtitle: ConfigRenderLambda | undefined;\n tags: ConfigRenderLambda | undefined;\n enrichmentTargets: ConfigRenderLambda | undefined;\n featureFlags: BlockCodeKnownFeatureFlags;\n }\n>>;\n\n// Test: Default Href is '/'\ntype _HrefDefaultTest = BlockModelV3<_TestArgs, {}, _TestData> extends BlockModelV3<_TestArgs, {}, _TestData, '/'> ? true : false;\ntype _VerifyHrefDefault = Expect<_HrefDefaultTest>;\n\n// Test: Custom Href can be specified\ntype _CustomHref = '/settings' | '/main';\ntype _HrefCustomBuilder = BlockModelV3<_TestArgs, {}, _TestData, _CustomHref>;\ntype _HrefCustomTest = _HrefCustomBuilder extends BlockModelV3<_TestArgs, {}, _TestData, _CustomHref> ? true : false;\ntype _VerifyHrefCustom = Expect<_HrefCustomTest>;\n\n// Test: Output type accumulation with & intersection\ntype _OutputsAccumulation = { a: ConfigRenderLambda<string> } & { b: ConfigRenderLambda<number> };\ntype _VerifyOutputsHaveKeys = Expect<Equal<keyof _OutputsAccumulation, 'a' | 'b'>>;\n\n// Test: Builder with all type parameters specified compiles\ntype _FullBuilder = BlockModelV3<_TestArgs, _TestOutputs, _TestData, '/main'>;\ntype _FullBuilderTest = _FullBuilder extends BlockModelV3<_TestArgs, _TestOutputs, _TestData, '/main'> ? true : false;\ntype _VerifyFullBuilder = Expect<_FullBuilderTest>;\n\n// Test: InferOutputsFromLambdas maps outputs correctly\ntype _InferOutputsTest = InferOutputsFromLambdas<{ myOutput: ConfigRenderLambda<number> }>;\ntype _VerifyInferOutputs = Expect<Equal<\n _InferOutputsTest,\n { myOutput: OutputWithStatus<number> & { __unwrap: true } }\n>>;\n"],"names":[],"mappings":";;;;;;;;;;AA0DA;;;AAGiD;MACpC,YAAY,CAAA;AAOJ,IAAA,MAAA;AADnB,IAAA,WAAA,CACmB,MAAkD,EAAA;QAAlD,IAAA,CAAA,MAAM,GAAN,MAAM;IACtB;IAEI,OAAgB,2BAA2B,GAA+B;AAC/E,QAAA,iBAAiB,EAAE,IAAI;AACvB,QAAA,oBAAoB,EAAE,CAAC;AACvB,QAAA,uBAAuB,EAAE,CAAC;KAC3B;AAED;;;;;;;;;;;AAWG;IACI,OAAO,MAAM,CAClB,OAAkC,EAAA;QAElC,MAAM,EAAE,SAAS,EAAE,aAAa,GAAG,OAAO,EAAE,GAAG,OAAO;;QAGtD,SAAS,CAAC,iBAAiB,EAAE;QAE7B,OAAO,IAAI,YAAY,CAAiB;YACtC,aAAa;YACb,SAAS;AACT,YAAA,OAAO,EAAE,EAAE;;AAEX,YAAA,QAAQ,EAAE,6BAA6B,CAAC,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,IAAI,CAAC;AACvF,YAAA,KAAK,EAAE,SAAS;AAChB,YAAA,QAAQ,EAAE,SAAS;AACnB,YAAA,IAAI,EAAE,SAAS;AACf,YAAA,iBAAiB,EAAE,SAAS;AAC5B,YAAA,YAAY,EAAE,EAAE,GAAG,YAAY,CAAC,2BAA2B,EAAE;AAC7D,YAAA,IAAI,EAAE,SAAS;AACf,YAAA,UAAU,EAAE,SAAS;AACtB,SAAA,CAAC;IACJ;AAsCO,IAAA,MAAM,CACX,GAAW,EACX,OAAmC,EACnC,QAAiC,EAAE,EAAA;QAEnC,OAAO,IAAI,YAAY,CAAC;YACtB,GAAG,IAAI,CAAC,MAAM;AACd,YAAA,OAAO,EAAE;AAC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AC,EAAE,CAAC,MAAM,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC;AACjE,sBAAE,SAAS;AACd,aAAA;;AAGD,YAAA,UAAU,EAAE,mBAAmB;AAC/B,YAAA,aAAa,EAAE,IAAI,CAAC,MAAM,CAAC,aAAa;AACxC,YAAA,QAAQ,EAAE,IAAI,CAAC,MAAM,CAAC,QAAQ;AAC9B,YAAA,OAAO,EAAE,MAAM,CAAC,WAAW,CACzB,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,KAAK,CAAC,GAAG,EAAE,oBAAoB,CAAC,KAAK,CAAC,CAAC,CAAC,CAC9F;SACF;AAED,QAAA,UAAU,CAAC,mBAAmB,GAAG,UAAU;QAE3C,IAAI,CAAC,MAAM,EAAE;;AAEX,YAAA,OAAO,EAAE,MAAM,EAAE,WAAW,EAAS;;;YAElC,OAAO;gBACV,GAAG,oBAAoB,CAAC,EAAE,UAAU,EAAE,mBAAmB,EAAE,UAAU,EAAE,CAAC;AACxE,gBAAA,cAAc,EAAE;AACd,oBAAA,OAAO,EAAE,MAAM,CAAC,WAAW,CACzB,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO;AAC/B,yBAAA,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,KAAK,CAAC,GAAG,EAAE;4BAC3B,UAAU,EAAE,OAAO,CAAC,cAAc,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,UAAU,CAAC;AAC/D,yBAAA,CAAC,CAAC,CACN;AACF,iBAAA;aACK;IACV;;;;;"}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* BlockStorage - Typed storage abstraction for block persistent data.
|
|
5
|
+
*
|
|
6
|
+
* This module provides:
|
|
7
|
+
* - A typed structure for block storage with versioning and plugin support
|
|
8
|
+
* - Utility functions for manipulating storage
|
|
9
|
+
* - Handler interfaces for model-level customization
|
|
10
|
+
*
|
|
11
|
+
* @module block_storage
|
|
12
|
+
*/
|
|
13
|
+
// =============================================================================
|
|
14
|
+
// Core Types
|
|
15
|
+
// =============================================================================
|
|
16
|
+
/**
|
|
17
|
+
* Discriminator key for BlockStorage format detection.
|
|
18
|
+
* This unique hash-based key identifies data as BlockStorage vs legacy formats.
|
|
19
|
+
*/
|
|
20
|
+
const BLOCK_STORAGE_KEY = '__pl_a7f3e2b9__';
|
|
21
|
+
/**
|
|
22
|
+
* Current BlockStorage schema version.
|
|
23
|
+
* Increment this when the storage structure itself changes (not block state migrations).
|
|
24
|
+
*/
|
|
25
|
+
const BLOCK_STORAGE_SCHEMA_VERSION = 'v1';
|
|
26
|
+
/**
|
|
27
|
+
* Type guard to check if a value is a valid BlockStorage object.
|
|
28
|
+
* Checks for the discriminator key and valid schema version.
|
|
29
|
+
*/
|
|
30
|
+
function isBlockStorage(value) {
|
|
31
|
+
if (value === null || typeof value !== 'object')
|
|
32
|
+
return false;
|
|
33
|
+
const obj = value;
|
|
34
|
+
const schemaVersion = obj[BLOCK_STORAGE_KEY];
|
|
35
|
+
// Currently only 'v1' is valid, but this allows future versions
|
|
36
|
+
return schemaVersion === 'v1'; // Add more versions as schema evolves
|
|
37
|
+
}
|
|
38
|
+
// =============================================================================
|
|
39
|
+
// Factory Functions
|
|
40
|
+
// =============================================================================
|
|
41
|
+
/**
|
|
42
|
+
* Creates a BlockStorage with the given initial data
|
|
43
|
+
*
|
|
44
|
+
* @param initialData - The initial data value (defaults to empty object)
|
|
45
|
+
* @param version - The initial data version (defaults to 1)
|
|
46
|
+
* @returns A new BlockStorage instance with discriminator key
|
|
47
|
+
*/
|
|
48
|
+
function createBlockStorage(initialData = {}, version = 1) {
|
|
49
|
+
return {
|
|
50
|
+
[BLOCK_STORAGE_KEY]: BLOCK_STORAGE_SCHEMA_VERSION,
|
|
51
|
+
__dataVersion: version,
|
|
52
|
+
__data: initialData,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Normalizes raw storage data to BlockStorage format.
|
|
57
|
+
* If the input is already a BlockStorage, returns it as-is.
|
|
58
|
+
* If the input is legacy format (raw state), wraps it in BlockStorage structure.
|
|
59
|
+
*
|
|
60
|
+
* @param raw - Raw storage data (may be legacy format or BlockStorage)
|
|
61
|
+
* @returns Normalized BlockStorage
|
|
62
|
+
*/
|
|
63
|
+
function normalizeBlockStorage(raw) {
|
|
64
|
+
if (isBlockStorage(raw)) {
|
|
65
|
+
return raw;
|
|
66
|
+
}
|
|
67
|
+
// Legacy format: raw is the state directly
|
|
68
|
+
return createBlockStorage(raw, 1);
|
|
69
|
+
}
|
|
70
|
+
// =============================================================================
|
|
71
|
+
// Data Access & Update Functions
|
|
72
|
+
// =============================================================================
|
|
73
|
+
/**
|
|
74
|
+
* Gets the data from BlockStorage
|
|
75
|
+
*
|
|
76
|
+
* @param storage - The BlockStorage instance
|
|
77
|
+
* @returns The data value
|
|
78
|
+
*/
|
|
79
|
+
function getStorageData(storage) {
|
|
80
|
+
return storage.__data;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Derives data from raw block storage.
|
|
84
|
+
* This function is meant to be called from sdk/ui-vue to extract
|
|
85
|
+
* user-facing data from the raw storage returned by the middle layer.
|
|
86
|
+
*
|
|
87
|
+
* The middle layer returns raw storage (opaque to it), and the UI
|
|
88
|
+
* uses this function to derive the actual data value.
|
|
89
|
+
*
|
|
90
|
+
* @param rawStorage - Raw storage data from middle layer (may be any format)
|
|
91
|
+
* @returns The extracted data value, or undefined if storage is undefined/null
|
|
92
|
+
*/
|
|
93
|
+
function deriveDataFromStorage(rawStorage) {
|
|
94
|
+
// Normalize to BlockStorage format (handles legacy formats too)
|
|
95
|
+
const storage = normalizeBlockStorage(rawStorage);
|
|
96
|
+
return getStorageData(storage);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Updates the data in BlockStorage (immutable)
|
|
100
|
+
*
|
|
101
|
+
* @param storage - The current BlockStorage
|
|
102
|
+
* @param payload - The update payload with operation and value
|
|
103
|
+
* @returns A new BlockStorage with updated data
|
|
104
|
+
*/
|
|
105
|
+
function updateStorageData(storage, payload) {
|
|
106
|
+
switch (payload.operation) {
|
|
107
|
+
case 'update-data':
|
|
108
|
+
return { ...storage, __data: payload.value };
|
|
109
|
+
default:
|
|
110
|
+
throw new Error(`Unknown storage operation: ${payload.operation}`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Gets the data version from BlockStorage
|
|
115
|
+
*
|
|
116
|
+
* @param storage - The BlockStorage instance
|
|
117
|
+
* @returns The data version number
|
|
118
|
+
*/
|
|
119
|
+
function getStorageDataVersion(storage) {
|
|
120
|
+
return storage.__dataVersion;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Updates the data version in BlockStorage (immutable)
|
|
124
|
+
*
|
|
125
|
+
* @param storage - The current BlockStorage
|
|
126
|
+
* @param version - The new version number
|
|
127
|
+
* @returns A new BlockStorage with updated version
|
|
128
|
+
*/
|
|
129
|
+
function updateStorageDataVersion(storage, version) {
|
|
130
|
+
return { ...storage, __dataVersion: version };
|
|
131
|
+
}
|
|
132
|
+
// =============================================================================
|
|
133
|
+
// Plugin Data Functions
|
|
134
|
+
// =============================================================================
|
|
135
|
+
/**
|
|
136
|
+
* Gets plugin-specific data from BlockStorage
|
|
137
|
+
*
|
|
138
|
+
* @param storage - The BlockStorage instance
|
|
139
|
+
* @param pluginName - The plugin name (without `@plugin/` prefix)
|
|
140
|
+
* @returns The plugin data or undefined if not set
|
|
141
|
+
*/
|
|
142
|
+
function getPluginData(storage, pluginName) {
|
|
143
|
+
const key = `@plugin/${pluginName}`;
|
|
144
|
+
return storage[key];
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Sets plugin-specific data in BlockStorage (immutable)
|
|
148
|
+
*
|
|
149
|
+
* @param storage - The current BlockStorage
|
|
150
|
+
* @param pluginName - The plugin name (without `@plugin/` prefix)
|
|
151
|
+
* @param data - The plugin data to store
|
|
152
|
+
* @returns A new BlockStorage with updated plugin data
|
|
153
|
+
*/
|
|
154
|
+
function setPluginData(storage, pluginName, data) {
|
|
155
|
+
const key = `@plugin/${pluginName}`;
|
|
156
|
+
return { ...storage, [key]: data };
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Removes plugin-specific data from BlockStorage (immutable)
|
|
160
|
+
*
|
|
161
|
+
* @param storage - The current BlockStorage
|
|
162
|
+
* @param pluginName - The plugin name (without `@plugin/` prefix)
|
|
163
|
+
* @returns A new BlockStorage with the plugin data removed
|
|
164
|
+
*/
|
|
165
|
+
function removePluginData(storage, pluginName) {
|
|
166
|
+
const key = `@plugin/${pluginName}`;
|
|
167
|
+
const { [key]: _, ...rest } = storage;
|
|
168
|
+
return rest;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Gets all plugin names that have data stored
|
|
172
|
+
*
|
|
173
|
+
* @param storage - The BlockStorage instance
|
|
174
|
+
* @returns Array of plugin names (without `@plugin/` prefix)
|
|
175
|
+
*/
|
|
176
|
+
function getPluginNames(storage) {
|
|
177
|
+
return Object.keys(storage)
|
|
178
|
+
.filter((key) => key.startsWith('@plugin/'))
|
|
179
|
+
.map((key) => key.slice('@plugin/'.length));
|
|
180
|
+
}
|
|
181
|
+
// =============================================================================
|
|
182
|
+
// Generic Storage Access
|
|
183
|
+
// =============================================================================
|
|
184
|
+
/**
|
|
185
|
+
* Gets a value from BlockStorage by key
|
|
186
|
+
*
|
|
187
|
+
* @param storage - The BlockStorage instance
|
|
188
|
+
* @param key - The key to retrieve
|
|
189
|
+
* @returns The value at the given key
|
|
190
|
+
*/
|
|
191
|
+
function getFromStorage(storage, key) {
|
|
192
|
+
return storage[key];
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Updates a value in BlockStorage by key (immutable)
|
|
196
|
+
*
|
|
197
|
+
* @param storage - The current BlockStorage
|
|
198
|
+
* @param key - The key to update
|
|
199
|
+
* @param value - The new value
|
|
200
|
+
* @returns A new BlockStorage with the updated value
|
|
201
|
+
*/
|
|
202
|
+
function updateStorage(storage, key, value) {
|
|
203
|
+
return { ...storage, [key]: value };
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Default implementations of storage handlers
|
|
207
|
+
*/
|
|
208
|
+
const defaultBlockStorageHandlers = {
|
|
209
|
+
transformStateForStorage: (storage, newState) => updateStorageData(storage, { operation: 'update-data', value: newState }),
|
|
210
|
+
deriveStateForArgs: (storage) => getStorageData(storage),
|
|
211
|
+
migrateStorage: (storage, _fromVersion, toVersion) => updateStorageDataVersion(storage, toVersion),
|
|
212
|
+
};
|
|
213
|
+
/**
|
|
214
|
+
* Merges custom handlers with defaults
|
|
215
|
+
*
|
|
216
|
+
* @param customHandlers - Custom handlers to merge
|
|
217
|
+
* @returns Complete handlers with defaults for missing functions
|
|
218
|
+
*/
|
|
219
|
+
function mergeBlockStorageHandlers(customHandlers) {
|
|
220
|
+
return {
|
|
221
|
+
...defaultBlockStorageHandlers,
|
|
222
|
+
...customHandlers,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
exports.BLOCK_STORAGE_KEY = BLOCK_STORAGE_KEY;
|
|
227
|
+
exports.BLOCK_STORAGE_SCHEMA_VERSION = BLOCK_STORAGE_SCHEMA_VERSION;
|
|
228
|
+
exports.createBlockStorage = createBlockStorage;
|
|
229
|
+
exports.defaultBlockStorageHandlers = defaultBlockStorageHandlers;
|
|
230
|
+
exports.deriveDataFromStorage = deriveDataFromStorage;
|
|
231
|
+
exports.getFromStorage = getFromStorage;
|
|
232
|
+
exports.getPluginData = getPluginData;
|
|
233
|
+
exports.getPluginNames = getPluginNames;
|
|
234
|
+
exports.getStorageData = getStorageData;
|
|
235
|
+
exports.getStorageDataVersion = getStorageDataVersion;
|
|
236
|
+
exports.isBlockStorage = isBlockStorage;
|
|
237
|
+
exports.mergeBlockStorageHandlers = mergeBlockStorageHandlers;
|
|
238
|
+
exports.normalizeBlockStorage = normalizeBlockStorage;
|
|
239
|
+
exports.removePluginData = removePluginData;
|
|
240
|
+
exports.setPluginData = setPluginData;
|
|
241
|
+
exports.updateStorage = updateStorage;
|
|
242
|
+
exports.updateStorageData = updateStorageData;
|
|
243
|
+
exports.updateStorageDataVersion = updateStorageDataVersion;
|
|
244
|
+
//# sourceMappingURL=block_storage.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"block_storage.cjs","sources":["../src/block_storage.ts"],"sourcesContent":["/**\n * BlockStorage - Typed storage abstraction for block persistent data.\n *\n * This module provides:\n * - A typed structure for block storage with versioning and plugin support\n * - Utility functions for manipulating storage\n * - Handler interfaces for model-level customization\n *\n * @module block_storage\n */\n\n// =============================================================================\n// Core Types\n// =============================================================================\n\n/**\n * Discriminator key for BlockStorage format detection.\n * This unique hash-based key identifies data as BlockStorage vs legacy formats.\n */\nexport const BLOCK_STORAGE_KEY = '__pl_a7f3e2b9__';\n\n/**\n * Current BlockStorage schema version.\n * Increment this when the storage structure itself changes (not block state migrations).\n */\nexport const BLOCK_STORAGE_SCHEMA_VERSION = 'v1';\n\n/**\n * Type for valid schema versions\n */\nexport type BlockStorageSchemaVersion = 'v1'; // Add 'v2', 'v3', etc. as schema evolves\n\n/**\n * Plugin key type - keys starting with `@plugin/` are reserved for plugin data\n */\nexport type PluginKey = `@plugin/${string}`;\n\n/**\n * Core BlockStorage type that holds:\n * - __pl_a7f3e2b9__: Schema version (discriminator key identifies BlockStorage format)\n * - __dataVersion: Version number for block data migrations\n * - __data: The block's user-facing data (state)\n * - @plugin/*: Optional plugin-specific data\n */\nexport type BlockStorage<TState = unknown> = {\n /** Schema version - the key itself is the discriminator */\n readonly [BLOCK_STORAGE_KEY]: BlockStorageSchemaVersion;\n /** Version of the block data, used for migrations */\n __dataVersion: number;\n /** The block's user-facing data (state) */\n __data: TState;\n} & {\n /** Plugin-specific data, keyed by `@plugin/<pluginName>` */\n [K in PluginKey]?: unknown;\n};\n\n/**\n * Type guard to check if a value is a valid BlockStorage object.\n * Checks for the discriminator key and valid schema version.\n */\nexport function isBlockStorage(value: unknown): value is BlockStorage {\n if (value === null || typeof value !== 'object') return false;\n const obj = value as Record<string, unknown>;\n const schemaVersion = obj[BLOCK_STORAGE_KEY];\n // Currently only 'v1' is valid, but this allows future versions\n return schemaVersion === 'v1'; // Add more versions as schema evolves\n}\n\n// =============================================================================\n// Factory Functions\n// =============================================================================\n\n/**\n * Creates a BlockStorage with the given initial data\n *\n * @param initialData - The initial data value (defaults to empty object)\n * @param version - The initial data version (defaults to 1)\n * @returns A new BlockStorage instance with discriminator key\n */\nexport function createBlockStorage<TState = unknown>(\n initialData: TState = {} as TState,\n version: number = 1,\n): BlockStorage<TState> {\n return {\n [BLOCK_STORAGE_KEY]: BLOCK_STORAGE_SCHEMA_VERSION,\n __dataVersion: version,\n __data: initialData,\n };\n}\n\n/**\n * Normalizes raw storage data to BlockStorage format.\n * If the input is already a BlockStorage, returns it as-is.\n * If the input is legacy format (raw state), wraps it in BlockStorage structure.\n *\n * @param raw - Raw storage data (may be legacy format or BlockStorage)\n * @returns Normalized BlockStorage\n */\nexport function normalizeBlockStorage<TState = unknown>(raw: unknown): BlockStorage<TState> {\n if (isBlockStorage(raw)) {\n return raw as BlockStorage<TState>;\n }\n // Legacy format: raw is the state directly\n return createBlockStorage(raw as TState, 1);\n}\n\n// =============================================================================\n// Data Access & Update Functions\n// =============================================================================\n\n/**\n * Gets the data from BlockStorage\n *\n * @param storage - The BlockStorage instance\n * @returns The data value\n */\nexport function getStorageData<TState>(storage: BlockStorage<TState>): TState {\n return storage.__data;\n}\n\n/**\n * Derives data from raw block storage.\n * This function is meant to be called from sdk/ui-vue to extract\n * user-facing data from the raw storage returned by the middle layer.\n *\n * The middle layer returns raw storage (opaque to it), and the UI\n * uses this function to derive the actual data value.\n *\n * @param rawStorage - Raw storage data from middle layer (may be any format)\n * @returns The extracted data value, or undefined if storage is undefined/null\n */\nexport function deriveDataFromStorage<TData = unknown>(\n rawStorage: unknown,\n): TData {\n // Normalize to BlockStorage format (handles legacy formats too)\n const storage = normalizeBlockStorage<TData>(rawStorage);\n return getStorageData(storage);\n}\n\n/** Payload for storage mutation operations. SDK defines specific operations. */\nexport type MutateStoragePayload<T = unknown> = { operation: 'update-data'; value: T };\n\n/**\n * Updates the data in BlockStorage (immutable)\n *\n * @param storage - The current BlockStorage\n * @param payload - The update payload with operation and value\n * @returns A new BlockStorage with updated data\n */\nexport function updateStorageData<TValue = unknown>(\n storage: BlockStorage<TValue>,\n payload: MutateStoragePayload<TValue>,\n): BlockStorage<TValue> {\n switch (payload.operation) {\n case 'update-data':\n return { ...storage, __data: payload.value };\n default:\n throw new Error(`Unknown storage operation: ${(payload as { operation: string }).operation}`);\n }\n}\n\n/**\n * Gets the data version from BlockStorage\n *\n * @param storage - The BlockStorage instance\n * @returns The data version number\n */\nexport function getStorageDataVersion(storage: BlockStorage): number {\n return storage.__dataVersion;\n}\n\n/**\n * Updates the data version in BlockStorage (immutable)\n *\n * @param storage - The current BlockStorage\n * @param version - The new version number\n * @returns A new BlockStorage with updated version\n */\nexport function updateStorageDataVersion<TState>(\n storage: BlockStorage<TState>,\n version: number,\n): BlockStorage<TState> {\n return { ...storage, __dataVersion: version };\n}\n\n// =============================================================================\n// Plugin Data Functions\n// =============================================================================\n\n/**\n * Gets plugin-specific data from BlockStorage\n *\n * @param storage - The BlockStorage instance\n * @param pluginName - The plugin name (without `@plugin/` prefix)\n * @returns The plugin data or undefined if not set\n */\nexport function getPluginData<TData = unknown>(\n storage: BlockStorage,\n pluginName: string,\n): TData | undefined {\n const key: PluginKey = `@plugin/${pluginName}`;\n return storage[key] as TData | undefined;\n}\n\n/**\n * Sets plugin-specific data in BlockStorage (immutable)\n *\n * @param storage - The current BlockStorage\n * @param pluginName - The plugin name (without `@plugin/` prefix)\n * @param data - The plugin data to store\n * @returns A new BlockStorage with updated plugin data\n */\nexport function setPluginData<TState>(\n storage: BlockStorage<TState>,\n pluginName: string,\n data: unknown,\n): BlockStorage<TState> {\n const key: PluginKey = `@plugin/${pluginName}`;\n return { ...storage, [key]: data };\n}\n\n/**\n * Removes plugin-specific data from BlockStorage (immutable)\n *\n * @param storage - The current BlockStorage\n * @param pluginName - The plugin name (without `@plugin/` prefix)\n * @returns A new BlockStorage with the plugin data removed\n */\nexport function removePluginData<TState>(\n storage: BlockStorage<TState>,\n pluginName: string,\n): BlockStorage<TState> {\n const key: PluginKey = `@plugin/${pluginName}`;\n const { [key]: _, ...rest } = storage;\n return rest as BlockStorage<TState>;\n}\n\n/**\n * Gets all plugin names that have data stored\n *\n * @param storage - The BlockStorage instance\n * @returns Array of plugin names (without `@plugin/` prefix)\n */\nexport function getPluginNames(storage: BlockStorage): string[] {\n return Object.keys(storage)\n .filter((key): key is PluginKey => key.startsWith('@plugin/'))\n .map((key) => key.slice('@plugin/'.length));\n}\n\n// =============================================================================\n// Generic Storage Access\n// =============================================================================\n\n/**\n * Gets a value from BlockStorage by key\n *\n * @param storage - The BlockStorage instance\n * @param key - The key to retrieve\n * @returns The value at the given key\n */\nexport function getFromStorage<\n TState,\n K extends keyof BlockStorage<TState>,\n>(storage: BlockStorage<TState>, key: K): BlockStorage<TState>[K] {\n return storage[key];\n}\n\n/**\n * Updates a value in BlockStorage by key (immutable)\n *\n * @param storage - The current BlockStorage\n * @param key - The key to update\n * @param value - The new value\n * @returns A new BlockStorage with the updated value\n */\nexport function updateStorage<\n TState,\n K extends keyof BlockStorage<TState>,\n>(\n storage: BlockStorage<TState>,\n key: K,\n value: BlockStorage<TState>[K],\n): BlockStorage<TState> {\n return { ...storage, [key]: value };\n}\n\n// =============================================================================\n// Storage Handlers (for Phase 2 - Model-Level Customization)\n// =============================================================================\n\n/**\n * Interface for model-configurable storage operations.\n * These handlers allow block models to customize how storage is managed.\n */\nexport interface BlockStorageHandlers<TState = unknown> {\n /**\n * Called when setState is invoked - transforms the new state before storing.\n * Default behavior: replaces the state directly.\n *\n * @param currentStorage - The current BlockStorage\n * @param newState - The new state being set\n * @returns The updated BlockStorage\n */\n transformStateForStorage?: (\n currentStorage: BlockStorage<TState>,\n newState: TState,\n ) => BlockStorage<TState>;\n\n /**\n * Called when reading state for args derivation.\n * Default behavior: returns the state directly.\n *\n * @param storage - The current BlockStorage\n * @returns The state to use for args derivation\n */\n deriveStateForArgs?: (storage: BlockStorage<TState>) => TState;\n\n /**\n * Called during storage schema migration.\n * Default behavior: updates stateVersion only.\n *\n * @param oldStorage - The storage before migration\n * @param fromVersion - The version migrating from\n * @param toVersion - The version migrating to\n * @returns The migrated BlockStorage\n */\n migrateStorage?: (\n oldStorage: BlockStorage<TState>,\n fromVersion: number,\n toVersion: number,\n ) => BlockStorage<TState>;\n}\n\n/**\n * Default implementations of storage handlers\n */\nexport const defaultBlockStorageHandlers: Required<BlockStorageHandlers<unknown>> = {\n transformStateForStorage: <TState>(\n storage: BlockStorage<TState>,\n newState: TState,\n ): BlockStorage<TState> => updateStorageData(storage, { operation: 'update-data', value: newState }),\n\n deriveStateForArgs: <TState>(storage: BlockStorage<TState>): TState => getStorageData(storage),\n\n migrateStorage: <TState>(\n storage: BlockStorage<TState>,\n _fromVersion: number,\n toVersion: number,\n ): BlockStorage<TState> => updateStorageDataVersion(storage, toVersion),\n};\n\n/**\n * Merges custom handlers with defaults\n *\n * @param customHandlers - Custom handlers to merge\n * @returns Complete handlers with defaults for missing functions\n */\nexport function mergeBlockStorageHandlers<TState>(\n customHandlers?: BlockStorageHandlers<TState>,\n): Required<BlockStorageHandlers<TState>> {\n return {\n ...defaultBlockStorageHandlers,\n ...customHandlers,\n } as Required<BlockStorageHandlers<TState>>;\n}\n"],"names":[],"mappings":";;AAAA;;;;;;;;;AASG;AAEH;AACA;AACA;AAEA;;;AAGG;AACI,MAAM,iBAAiB,GAAG;AAEjC;;;AAGG;A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C,CAAC;AAC/C;AAEA;AACA;AACA;AAEA;;;;;;AAMG;AACG,SAAU,cAAc,CAG5B,OAA6B,EAAE,GAAM,EAAA;AACrC,IAAA,OAAO,OAAO,CAAC,GAAG,CAAC;AACrB;AAEA;;;;;;;AAOG;SACa,aAAa,CAI3B,OAA6B,EAC7B,GAAM,EACN,KAA8B,EAAA;IAE9B,OAAO,EAAE,GAAG,OAAO,EAAE,CAAC,GAAG,GAAG,KAAK,EAAE;AACrC;AAiDA;;AAEG;AACI,MAAM,2BAA2B,GAA4C;IAClF,wBAAwB,EAAE,CACxB,OAA6B,EAC7B,QAAgB,KACS,iBAAiB,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,aAAa,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC;IAEpG,kBAAkB,EAAE,CAAS,OAA6B,KAAa,cAAc,CAAC,OAAO,CAAC;AAE9F,IAAA,cAAc,EAAE,CACd,OAA6B,EAC7B,YAAoB,EACpB,SAAiB,KACQ,wBAAwB,CAAC,OAAO,EAAE,SAAS,CAAC;;AAGzE;;;;;AAKG;AACG,SAAU,yBAAyB,CACvC,cAA6C,EAAA;IAE7C,OAAO;AACL,QAAA,GAAG,2BAA2B;AAC9B,QAAA,GAAG,cAAc;KACwB;AAC7C;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BlockStorage - Typed storage abstraction for block persistent data.
|
|
3
|
+
*
|
|
4
|
+
* This module provides:
|
|
5
|
+
* - A typed structure for block storage with versioning and plugin support
|
|
6
|
+
* - Utility functions for manipulating storage
|
|
7
|
+
* - Handler interfaces for model-level customization
|
|
8
|
+
*
|
|
9
|
+
* @module block_storage
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Discriminator key for BlockStorage format detection.
|
|
13
|
+
* This unique hash-based key identifies data as BlockStorage vs legacy formats.
|
|
14
|
+
*/
|
|
15
|
+
export declare const BLOCK_STORAGE_KEY = "__pl_a7f3e2b9__";
|
|
16
|
+
/**
|
|
17
|
+
* Current BlockStorage schema version.
|
|
18
|
+
* Increment this when the storage structure itself changes (not block state migrations).
|
|
19
|
+
*/
|
|
20
|
+
export declare const BLOCK_STORAGE_SCHEMA_VERSION = "v1";
|
|
21
|
+
/**
|
|
22
|
+
* Type for valid schema versions
|
|
23
|
+
*/
|
|
24
|
+
export type BlockStorageSchemaVersion = 'v1';
|
|
25
|
+
/**
|
|
26
|
+
* Plugin key type - keys starting with `@plugin/` are reserved for plugin data
|
|
27
|
+
*/
|
|
28
|
+
export type PluginKey = `@plugin/${string}`;
|
|
29
|
+
/**
|
|
30
|
+
* Core BlockStorage type that holds:
|
|
31
|
+
* - __pl_a7f3e2b9__: Schema version (discriminator key identifies BlockStorage format)
|
|
32
|
+
* - __dataVersion: Version number for block data migrations
|
|
33
|
+
* - __data: The block's user-facing data (state)
|
|
34
|
+
* - @plugin/*: Optional plugin-specific data
|
|
35
|
+
*/
|
|
36
|
+
export type BlockStorage<TState = unknown> = {
|
|
37
|
+
/** Schema version - the key itself is the discriminator */
|
|
38
|
+
readonly [BLOCK_STORAGE_KEY]: BlockStorageSchemaVersion;
|
|
39
|
+
/** Version of the block data, used for migrations */
|
|
40
|
+
__dataVersion: number;
|
|
41
|
+
/** The block's user-facing data (state) */
|
|
42
|
+
__data: TState;
|
|
43
|
+
} & {
|
|
44
|
+
[K in PluginKey]?: unknown;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Type guard to check if a value is a valid BlockStorage object.
|
|
48
|
+
* Checks for the discriminator key and valid schema version.
|
|
49
|
+
*/
|
|
50
|
+
export declare function isBlockStorage(value: unknown): value is BlockStorage;
|
|
51
|
+
/**
|
|
52
|
+
* Creates a BlockStorage with the given initial data
|
|
53
|
+
*
|
|
54
|
+
* @param initialData - The initial data value (defaults to empty object)
|
|
55
|
+
* @param version - The initial data version (defaults to 1)
|
|
56
|
+
* @returns A new BlockStorage instance with discriminator key
|
|
57
|
+
*/
|
|
58
|
+
export declare function createBlockStorage<TState = unknown>(initialData?: TState, version?: number): BlockStorage<TState>;
|
|
59
|
+
/**
|
|
60
|
+
* Normalizes raw storage data to BlockStorage format.
|
|
61
|
+
* If the input is already a BlockStorage, returns it as-is.
|
|
62
|
+
* If the input is legacy format (raw state), wraps it in BlockStorage structure.
|
|
63
|
+
*
|
|
64
|
+
* @param raw - Raw storage data (may be legacy format or BlockStorage)
|
|
65
|
+
* @returns Normalized BlockStorage
|
|
66
|
+
*/
|
|
67
|
+
export declare function normalizeBlockStorage<TState = unknown>(raw: unknown): BlockStorage<TState>;
|
|
68
|
+
/**
|
|
69
|
+
* Gets the data from BlockStorage
|
|
70
|
+
*
|
|
71
|
+
* @param storage - The BlockStorage instance
|
|
72
|
+
* @returns The data value
|
|
73
|
+
*/
|
|
74
|
+
export declare function getStorageData<TState>(storage: BlockStorage<TState>): TState;
|
|
75
|
+
/**
|
|
76
|
+
* Derives data from raw block storage.
|
|
77
|
+
* This function is meant to be called from sdk/ui-vue to extract
|
|
78
|
+
* user-facing data from the raw storage returned by the middle layer.
|
|
79
|
+
*
|
|
80
|
+
* The middle layer returns raw storage (opaque to it), and the UI
|
|
81
|
+
* uses this function to derive the actual data value.
|
|
82
|
+
*
|
|
83
|
+
* @param rawStorage - Raw storage data from middle layer (may be any format)
|
|
84
|
+
* @returns The extracted data value, or undefined if storage is undefined/null
|
|
85
|
+
*/
|
|
86
|
+
export declare function deriveDataFromStorage<TData = unknown>(rawStorage: unknown): TData;
|
|
87
|
+
/** Payload for storage mutation operations. SDK defines specific operations. */
|
|
88
|
+
export type MutateStoragePayload<T = unknown> = {
|
|
89
|
+
operation: 'update-data';
|
|
90
|
+
value: T;
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Updates the data in BlockStorage (immutable)
|
|
94
|
+
*
|
|
95
|
+
* @param storage - The current BlockStorage
|
|
96
|
+
* @param payload - The update payload with operation and value
|
|
97
|
+
* @returns A new BlockStorage with updated data
|
|
98
|
+
*/
|
|
99
|
+
export declare function updateStorageData<TValue = unknown>(storage: BlockStorage<TValue>, payload: MutateStoragePayload<TValue>): BlockStorage<TValue>;
|
|
100
|
+
/**
|
|
101
|
+
* Gets the data version from BlockStorage
|
|
102
|
+
*
|
|
103
|
+
* @param storage - The BlockStorage instance
|
|
104
|
+
* @returns The data version number
|
|
105
|
+
*/
|
|
106
|
+
export declare function getStorageDataVersion(storage: BlockStorage): number;
|
|
107
|
+
/**
|
|
108
|
+
* Updates the data version in BlockStorage (immutable)
|
|
109
|
+
*
|
|
110
|
+
* @param storage - The current BlockStorage
|
|
111
|
+
* @param version - The new version number
|
|
112
|
+
* @returns A new BlockStorage with updated version
|
|
113
|
+
*/
|
|
114
|
+
export declare function updateStorageDataVersion<TState>(storage: BlockStorage<TState>, version: number): BlockStorage<TState>;
|
|
115
|
+
/**
|
|
116
|
+
* Gets plugin-specific data from BlockStorage
|
|
117
|
+
*
|
|
118
|
+
* @param storage - The BlockStorage instance
|
|
119
|
+
* @param pluginName - The plugin name (without `@plugin/` prefix)
|
|
120
|
+
* @returns The plugin data or undefined if not set
|
|
121
|
+
*/
|
|
122
|
+
export declare function getPluginData<TData = unknown>(storage: BlockStorage, pluginName: string): TData | undefined;
|
|
123
|
+
/**
|
|
124
|
+
* Sets plugin-specific data in BlockStorage (immutable)
|
|
125
|
+
*
|
|
126
|
+
* @param storage - The current BlockStorage
|
|
127
|
+
* @param pluginName - The plugin name (without `@plugin/` prefix)
|
|
128
|
+
* @param data - The plugin data to store
|
|
129
|
+
* @returns A new BlockStorage with updated plugin data
|
|
130
|
+
*/
|
|
131
|
+
export declare function setPluginData<TState>(storage: BlockStorage<TState>, pluginName: string, data: unknown): BlockStorage<TState>;
|
|
132
|
+
/**
|
|
133
|
+
* Removes plugin-specific data from BlockStorage (immutable)
|
|
134
|
+
*
|
|
135
|
+
* @param storage - The current BlockStorage
|
|
136
|
+
* @param pluginName - The plugin name (without `@plugin/` prefix)
|
|
137
|
+
* @returns A new BlockStorage with the plugin data removed
|
|
138
|
+
*/
|
|
139
|
+
export declare function removePluginData<TState>(storage: BlockStorage<TState>, pluginName: string): BlockStorage<TState>;
|
|
140
|
+
/**
|
|
141
|
+
* Gets all plugin names that have data stored
|
|
142
|
+
*
|
|
143
|
+
* @param storage - The BlockStorage instance
|
|
144
|
+
* @returns Array of plugin names (without `@plugin/` prefix)
|
|
145
|
+
*/
|
|
146
|
+
export declare function getPluginNames(storage: BlockStorage): string[];
|
|
147
|
+
/**
|
|
148
|
+
* Gets a value from BlockStorage by key
|
|
149
|
+
*
|
|
150
|
+
* @param storage - The BlockStorage instance
|
|
151
|
+
* @param key - The key to retrieve
|
|
152
|
+
* @returns The value at the given key
|
|
153
|
+
*/
|
|
154
|
+
export declare function getFromStorage<TState, K extends keyof BlockStorage<TState>>(storage: BlockStorage<TState>, key: K): BlockStorage<TState>[K];
|
|
155
|
+
/**
|
|
156
|
+
* Updates a value in BlockStorage by key (immutable)
|
|
157
|
+
*
|
|
158
|
+
* @param storage - The current BlockStorage
|
|
159
|
+
* @param key - The key to update
|
|
160
|
+
* @param value - The new value
|
|
161
|
+
* @returns A new BlockStorage with the updated value
|
|
162
|
+
*/
|
|
163
|
+
export declare function updateStorage<TState, K extends keyof BlockStorage<TState>>(storage: BlockStorage<TState>, key: K, value: BlockStorage<TState>[K]): BlockStorage<TState>;
|
|
164
|
+
/**
|
|
165
|
+
* Interface for model-configurable storage operations.
|
|
166
|
+
* These handlers allow block models to customize how storage is managed.
|
|
167
|
+
*/
|
|
168
|
+
export interface BlockStorageHandlers<TState = unknown> {
|
|
169
|
+
/**
|
|
170
|
+
* Called when setState is invoked - transforms the new state before storing.
|
|
171
|
+
* Default behavior: replaces the state directly.
|
|
172
|
+
*
|
|
173
|
+
* @param currentStorage - The current BlockStorage
|
|
174
|
+
* @param newState - The new state being set
|
|
175
|
+
* @returns The updated BlockStorage
|
|
176
|
+
*/
|
|
177
|
+
transformStateForStorage?: (currentStorage: BlockStorage<TState>, newState: TState) => BlockStorage<TState>;
|
|
178
|
+
/**
|
|
179
|
+
* Called when reading state for args derivation.
|
|
180
|
+
* Default behavior: returns the state directly.
|
|
181
|
+
*
|
|
182
|
+
* @param storage - The current BlockStorage
|
|
183
|
+
* @returns The state to use for args derivation
|
|
184
|
+
*/
|
|
185
|
+
deriveStateForArgs?: (storage: BlockStorage<TState>) => TState;
|
|
186
|
+
/**
|
|
187
|
+
* Called during storage schema migration.
|
|
188
|
+
* Default behavior: updates stateVersion only.
|
|
189
|
+
*
|
|
190
|
+
* @param oldStorage - The storage before migration
|
|
191
|
+
* @param fromVersion - The version migrating from
|
|
192
|
+
* @param toVersion - The version migrating to
|
|
193
|
+
* @returns The migrated BlockStorage
|
|
194
|
+
*/
|
|
195
|
+
migrateStorage?: (oldStorage: BlockStorage<TState>, fromVersion: number, toVersion: number) => BlockStorage<TState>;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Default implementations of storage handlers
|
|
199
|
+
*/
|
|
200
|
+
export declare const defaultBlockStorageHandlers: Required<BlockStorageHandlers<unknown>>;
|
|
201
|
+
/**
|
|
202
|
+
* Merges custom handlers with defaults
|
|
203
|
+
*
|
|
204
|
+
* @param customHandlers - Custom handlers to merge
|
|
205
|
+
* @returns Complete handlers with defaults for missing functions
|
|
206
|
+
*/
|
|
207
|
+
export declare function mergeBlockStorageHandlers<TState>(customHandlers?: BlockStorageHandlers<TState>): Required<BlockStorageHandlers<TState>>;
|
|
208
|
+
//# sourceMappingURL=block_storage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"block_storage.d.ts","sourceRoot":"","sources":["../src/block_storage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAMH;;;GAGG;AACH,eAAO,MAAM,iBAAiB,oBAAoB,CAAC;AAEnD;;;GAGG;AACH,eAAO,MAAM,4BAA4B,OAAO,CAAC;AAEjD;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAA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