@arkitektio/bsx 4.3.0 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -18
- package/dist/bsx/analyze.d.ts +6 -5
- package/dist/bsx/analyze.d.ts.map +1 -1
- package/dist/bsx/analyze.js +12 -10
- package/dist/bsx/analyze.js.map +1 -1
- package/dist/bsx/declared.d.ts +111 -0
- package/dist/bsx/declared.d.ts.map +1 -0
- package/dist/bsx/declared.js +311 -0
- package/dist/bsx/declared.js.map +1 -0
- package/dist/bsx/index.d.ts +1 -0
- package/dist/bsx/index.d.ts.map +1 -1
- package/dist/bsx/index.js +1 -0
- package/dist/bsx/index.js.map +1 -1
- package/dist/bsx/infer.d.ts +40 -17
- package/dist/bsx/infer.d.ts.map +1 -1
- package/dist/bsx/infer.js +105 -49
- package/dist/bsx/infer.js.map +1 -1
- package/dist/bsx/language.d.ts +6 -2
- package/dist/bsx/language.d.ts.map +1 -1
- package/dist/bsx/language.js +65 -21
- package/dist/bsx/language.js.map +1 -1
- package/dist/bsx/parse.d.ts +19 -8
- package/dist/bsx/parse.d.ts.map +1 -1
- package/dist/bsx/parse.js +49 -59
- package/dist/bsx/parse.js.map +1 -1
- package/dist/bsx/print.d.ts.map +1 -1
- package/dist/bsx/print.js +25 -35
- package/dist/bsx/print.js.map +1 -1
- package/dist/bsx/references.d.ts +48 -54
- package/dist/bsx/references.d.ts.map +1 -1
- package/dist/bsx/references.js +359 -159
- package/dist/bsx/references.js.map +1 -1
- package/dist/catalogs/html.d.ts +3 -3
- package/dist/catalogs/html.d.ts.map +1 -1
- package/dist/catalogs/html.js +3 -3
- package/dist/catalogs/html.js.map +1 -1
- package/dist/catalogs/index.d.ts +172 -180
- package/dist/catalogs/index.d.ts.map +1 -1
- package/dist/catalogs/index.js +6 -2
- package/dist/catalogs/index.js.map +1 -1
- package/dist/catalogs/logo.d.ts +19 -33
- package/dist/catalogs/logo.d.ts.map +1 -1
- package/dist/catalogs/logo.js +4 -5
- package/dist/catalogs/logo.js.map +1 -1
- package/dist/catalogs/parts/data.d.ts +6 -6
- package/dist/catalogs/parts/feedback.d.ts +15 -15
- package/dist/catalogs/parts/forms.d.ts +24 -24
- package/dist/catalogs/parts/html.d.ts +3 -3
- package/dist/catalogs/parts/html.d.ts.map +1 -1
- package/dist/catalogs/parts/html.js +1 -1
- package/dist/catalogs/parts/html.js.map +1 -1
- package/dist/catalogs/parts/icons.d.ts +11 -11
- package/dist/catalogs/parts/icons.js +1 -1
- package/dist/catalogs/parts/index.d.ts +153 -165
- package/dist/catalogs/parts/index.d.ts.map +1 -1
- package/dist/catalogs/parts/layout.d.ts +11 -1
- package/dist/catalogs/parts/layout.d.ts.map +1 -1
- package/dist/catalogs/parts/layout.js +9 -0
- package/dist/catalogs/parts/layout.js.map +1 -1
- package/dist/catalogs/parts/logo.d.ts +17 -32
- package/dist/catalogs/parts/logo.d.ts.map +1 -1
- package/dist/catalogs/parts/logo.js +13 -15
- package/dist/catalogs/parts/logo.js.map +1 -1
- package/dist/catalogs/parts/navigation.d.ts +20 -20
- package/dist/catalogs/parts/overlays.d.ts +55 -55
- package/dist/catalogs/parts/run.d.ts +5 -13
- package/dist/catalogs/parts/run.d.ts.map +1 -1
- package/dist/catalogs/parts/run.js +1 -1
- package/dist/catalogs/parts/scene.d.ts +9 -17
- package/dist/catalogs/parts/scene.d.ts.map +1 -1
- package/dist/catalogs/parts/scene.js +1 -5
- package/dist/catalogs/parts/scene.js.map +1 -1
- package/dist/catalogs/parts/structure.js +2 -2
- package/dist/catalogs/parts/toast.js +1 -1
- package/dist/catalogs/parts/toast.js.map +1 -1
- package/dist/catalogs/parts/typography.d.ts +3 -3
- package/dist/catalogs/parts/typography.js +1 -1
- package/dist/catalogs/parts/typography.js.map +1 -1
- package/dist/catalogs/props.d.ts +4 -4
- package/dist/catalogs/scene.d.ts +8 -16
- package/dist/catalogs/scene.d.ts.map +1 -1
- package/dist/catalogs/scene.js +3 -3
- package/dist/catalogs/scene.js.map +1 -1
- package/dist/catalogs/shadcn.d.ts +58 -58
- package/dist/catalogs/shadcn.d.ts.map +1 -1
- package/dist/catalogs/shadcn.js +4 -4
- package/dist/catalogs/shadcn.js.map +1 -1
- package/dist/catalogs/structure.d.ts +1 -1
- package/dist/catalogs/structure.d.ts.map +1 -1
- package/dist/catalogs/structure.js +3 -3
- package/dist/catalogs/structure.js.map +1 -1
- package/dist/catalogs/ui.d.ts +83 -73
- package/dist/catalogs/ui.d.ts.map +1 -1
- package/dist/catalogs/ui.js +4 -3
- package/dist/catalogs/ui.js.map +1 -1
- package/dist/catalogs.d.ts +1 -1
- package/dist/catalogs.d.ts.map +1 -1
- package/dist/catalogs.js +2 -2
- package/dist/catalogs.js.map +1 -1
- package/dist/logo/actionLogoSource.d.ts +3 -3
- package/dist/logo/actionLogoSource.d.ts.map +1 -1
- package/dist/logo/actionLogoSource.js +4 -4
- package/dist/logo/actionLogoSource.js.map +1 -1
- package/dist/ports.js +1 -1
- package/dist/ports.js.map +1 -1
- package/dist/renderer/BlokRenderer.d.ts +10 -6
- package/dist/renderer/BlokRenderer.d.ts.map +1 -1
- package/dist/renderer/BlokRenderer.js +23 -9
- package/dist/renderer/BlokRenderer.js.map +1 -1
- package/dist/renderer/baseCatalog.d.ts +8 -6
- package/dist/renderer/baseCatalog.d.ts.map +1 -1
- package/dist/renderer/baseCatalog.js +7 -5
- package/dist/renderer/baseCatalog.js.map +1 -1
- package/dist/renderer/bloks/control.d.ts +20 -2
- package/dist/renderer/bloks/control.d.ts.map +1 -1
- package/dist/renderer/bloks/control.js +98 -15
- package/dist/renderer/bloks/control.js.map +1 -1
- package/dist/renderer/bloks/data.d.ts +6 -6
- package/dist/renderer/bloks/feedback.d.ts +15 -15
- package/dist/renderer/bloks/forms.d.ts +24 -24
- package/dist/renderer/bloks/html.d.ts +2 -2
- package/dist/renderer/bloks/html.js +2 -2
- package/dist/renderer/bloks/icons.d.ts +5 -5
- package/dist/renderer/bloks/index.d.ts +145 -144
- package/dist/renderer/bloks/index.d.ts.map +1 -1
- package/dist/renderer/bloks/index.js +1 -1
- package/dist/renderer/bloks/layout.d.ts +9 -1
- package/dist/renderer/bloks/layout.d.ts.map +1 -1
- package/dist/renderer/bloks/layout.js +15 -0
- package/dist/renderer/bloks/layout.js.map +1 -1
- package/dist/renderer/bloks/logo/index.d.ts +6 -13
- package/dist/renderer/bloks/logo/index.d.ts.map +1 -1
- package/dist/renderer/bloks/logo/index.js +3 -4
- package/dist/renderer/bloks/logo/index.js.map +1 -1
- package/dist/renderer/bloks/logo/logo.fixture.json +266 -250
- package/dist/renderer/bloks/navigation.d.ts +20 -20
- package/dist/renderer/bloks/overlays.d.ts +55 -55
- package/dist/renderer/bloks/run/components.d.ts +2 -6
- package/dist/renderer/bloks/run/components.d.ts.map +1 -1
- package/dist/renderer/bloks/run/components.js +1 -1
- package/dist/renderer/bloks/scene/index.d.ts +5 -9
- package/dist/renderer/bloks/scene/index.d.ts.map +1 -1
- package/dist/renderer/bloks/scene/interaction.d.ts.map +1 -1
- package/dist/renderer/bloks/scene/interaction.js +3 -2
- package/dist/renderer/bloks/scene/interaction.js.map +1 -1
- package/dist/renderer/bloks/shared/props.d.ts +2 -2
- package/dist/renderer/bloks/toast.d.ts +1 -1
- package/dist/renderer/bloks/typography.d.ts +3 -3
- package/dist/renderer/bloks/typography.js +1 -1
- package/dist/renderer/catalog.d.ts +4 -7
- package/dist/renderer/catalog.d.ts.map +1 -1
- package/dist/renderer/catalog.js +7 -31
- package/dist/renderer/catalog.js.map +1 -1
- package/dist/renderer/declarations.d.ts +40 -33
- package/dist/renderer/declarations.d.ts.map +1 -1
- package/dist/renderer/declarations.js +41 -56
- package/dist/renderer/declarations.js.map +1 -1
- package/dist/renderer/declare.d.ts +31 -19
- package/dist/renderer/declare.d.ts.map +1 -1
- package/dist/renderer/declare.js +44 -18
- package/dist/renderer/declare.js.map +1 -1
- package/dist/renderer/persist.d.ts +22 -13
- package/dist/renderer/persist.d.ts.map +1 -1
- package/dist/renderer/persist.js +40 -21
- package/dist/renderer/persist.js.map +1 -1
- package/dist/renderer/runtime/catalog.d.ts +27 -7
- package/dist/renderer/runtime/catalog.d.ts.map +1 -1
- package/dist/renderer/runtime/catalog.js +30 -3
- package/dist/renderer/runtime/catalog.js.map +1 -1
- package/dist/renderer/runtime/components.d.ts +10 -4
- package/dist/renderer/runtime/components.d.ts.map +1 -1
- package/dist/renderer/runtime/components.js +8 -5
- package/dist/renderer/runtime/components.js.map +1 -1
- package/dist/renderer/runtime/context.d.ts +5 -5
- package/dist/renderer/runtime/context.d.ts.map +1 -1
- package/dist/renderer/runtime/context.js +11 -5
- package/dist/renderer/runtime/context.js.map +1 -1
- package/dist/renderer/runtime/describe.d.ts +16 -6
- package/dist/renderer/runtime/describe.d.ts.map +1 -1
- package/dist/renderer/runtime/describe.js +5 -2
- package/dist/renderer/runtime/describe.js.map +1 -1
- package/dist/renderer/runtime/hooks.d.ts.map +1 -1
- package/dist/renderer/runtime/hooks.js +8 -4
- package/dist/renderer/runtime/hooks.js.map +1 -1
- package/dist/renderer/runtime/index.d.ts +2 -0
- package/dist/renderer/runtime/index.d.ts.map +1 -1
- package/dist/renderer/runtime/index.js +2 -0
- package/dist/renderer/runtime/index.js.map +1 -1
- package/dist/renderer/runtime/locals.d.ts +57 -0
- package/dist/renderer/runtime/locals.d.ts.map +1 -0
- package/dist/renderer/runtime/locals.js +87 -0
- package/dist/renderer/runtime/locals.js.map +1 -0
- package/dist/renderer/runtime/preflight.d.ts.map +1 -1
- package/dist/renderer/runtime/preflight.js +5 -2
- package/dist/renderer/runtime/preflight.js.map +1 -1
- package/dist/renderer/runtime/scope.d.ts +14 -0
- package/dist/renderer/runtime/scope.d.ts.map +1 -1
- package/dist/renderer/runtime/scope.js +25 -3
- package/dist/renderer/runtime/scope.js.map +1 -1
- package/dist/renderer/runtime/text.d.ts +28 -0
- package/dist/renderer/runtime/text.d.ts.map +1 -0
- package/dist/renderer/runtime/text.js +82 -0
- package/dist/renderer/runtime/text.js.map +1 -0
- package/dist/renderer/runtime/tree.d.ts.map +1 -1
- package/dist/renderer/runtime/tree.js +7 -1
- package/dist/renderer/runtime/tree.js.map +1 -1
- package/dist/renderer/runtime/types.d.ts +8 -3
- package/dist/renderer/runtime/types.d.ts.map +1 -1
- package/dist/runtime.d.ts +0 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +0 -1
- package/dist/runtime.js.map +1 -1
- package/dist/scene.d.ts +1 -1
- package/dist/scene.d.ts.map +1 -1
- package/dist/scene.js +2 -2
- package/dist/scene.js.map +1 -1
- package/dist/shadcn.d.ts +154 -153
- package/dist/shadcn.d.ts.map +1 -1
- package/dist/shadcn.js +7 -7
- package/dist/shadcn.js.map +1 -1
- package/dist/spec/bsx_v2.json +12324 -6934
- package/dist/spec/catalogs/base@4.json +13175 -0
- package/dist/spec/catalogs/base@4.json.js +13175 -0
- package/dist/spec/catalogs/orkestrator@1.json +489 -0
- package/dist/spec/catalogs/orkestrator@1.json.js +489 -0
- package/dist/spec/catalogs.d.ts +109 -46
- package/dist/spec/catalogs.d.ts.map +1 -1
- package/dist/spec/catalogs.js +243 -75
- package/dist/spec/catalogs.js.map +1 -1
- package/dist/spec/vocabulary.d.ts +44 -7
- package/dist/spec/vocabulary.d.ts.map +1 -1
- package/dist/spec/vocabulary.js +65 -7
- package/dist/spec/vocabulary.js.map +1 -1
- package/dist/spec/vocabulary.json +83 -43
- package/dist/spec/vocabulary.json.js +83 -43
- package/dist/vendor/core/ui/button-group.d.ts +1 -1
- package/dist/vendor/core/ui/button.d.ts +1 -1
- package/dist/vendor/core/ui/field.d.ts +1 -1
- package/package.json +1 -1
- package/src/bsx/analyze.ts +14 -12
- package/src/bsx/bsx.test.ts +4 -4
- package/src/bsx/declared.ts +346 -0
- package/src/bsx/index.ts +1 -0
- package/src/bsx/infer.test.ts +54 -52
- package/src/bsx/infer.ts +128 -71
- package/src/bsx/language.test.ts +37 -25
- package/src/bsx/language.ts +72 -25
- package/src/bsx/parse.ts +51 -64
- package/src/bsx/partial.test.ts +1 -1
- package/src/bsx/print.ts +27 -34
- package/src/bsx/references.test.ts +86 -83
- package/src/bsx/references.ts +391 -198
- package/src/catalogs/catalogs.test.ts +46 -14
- package/src/catalogs/html.ts +3 -3
- package/src/catalogs/index.ts +6 -2
- package/src/catalogs/logo.ts +4 -5
- package/src/catalogs/parts/html.ts +1 -1
- package/src/catalogs/parts/icons.ts +1 -1
- package/src/catalogs/parts/layout.ts +11 -0
- package/src/catalogs/parts/logo.ts +14 -18
- package/src/catalogs/parts/run.ts +1 -1
- package/src/catalogs/parts/scene.ts +1 -7
- package/src/catalogs/parts/structure.ts +2 -2
- package/src/catalogs/parts/toast.ts +1 -1
- package/src/catalogs/parts/typography.ts +1 -1
- package/src/catalogs/scene.ts +3 -3
- package/src/catalogs/shadcn.ts +4 -4
- package/src/catalogs/structure.ts +3 -3
- package/src/catalogs/ui.ts +4 -3
- package/src/catalogs.ts +9 -2
- package/src/logo/actionLogoSource.ts +4 -4
- package/src/ports.ts +1 -1
- package/src/renderer/BlokComponentHost.test.tsx +5 -5
- package/src/renderer/BlokRenderer.tsx +46 -14
- package/src/renderer/addBlokComponents.test.tsx +8 -22
- package/src/renderer/baseCatalog.ts +7 -5
- package/src/renderer/bloks/components.test.tsx +2 -2
- package/src/renderer/bloks/control.tsx +136 -17
- package/src/renderer/bloks/flow.test.tsx +1 -1
- package/src/renderer/bloks/html.tsx +2 -2
- package/src/renderer/bloks/index.ts +1 -1
- package/src/renderer/bloks/layout.tsx +28 -0
- package/src/renderer/bloks/logo/index.tsx +4 -5
- package/src/renderer/bloks/logo/logo.fixture.json +266 -250
- package/src/renderer/bloks/logo/logo.test.tsx +18 -7
- package/src/renderer/bloks/run/components.tsx +1 -1
- package/src/renderer/bloks/run/space.test.tsx +12 -4
- package/src/renderer/bloks/scene/interaction.tsx +11 -6
- package/src/renderer/bloks/scene/transform.test.ts +8 -7
- package/src/renderer/bloks/time.test.tsx +1 -1
- package/src/renderer/bloks/toast.tsx +1 -1
- package/src/renderer/bloks/typography.tsx +1 -1
- package/src/renderer/catalog.tsx +7 -31
- package/src/renderer/declarations.test.ts +38 -51
- package/src/renderer/declarations.ts +67 -88
- package/src/renderer/declare.test.ts +41 -0
- package/src/renderer/declare.ts +70 -32
- package/src/renderer/persist.test.ts +70 -49
- package/src/renderer/persist.ts +57 -34
- package/src/renderer/runtime/catalog.test.ts +2 -2
- package/src/renderer/runtime/catalog.ts +35 -10
- package/src/renderer/runtime/components.tsx +17 -8
- package/src/renderer/runtime/context.tsx +17 -10
- package/src/renderer/runtime/describe.ts +21 -8
- package/src/renderer/runtime/hooks.ts +6 -4
- package/src/renderer/runtime/index.ts +2 -0
- package/src/renderer/runtime/locals.tsx +105 -0
- package/src/renderer/runtime/preflight.ts +5 -2
- package/src/renderer/runtime/scope.tsx +31 -3
- package/src/renderer/runtime/text.test.tsx +65 -0
- package/src/renderer/runtime/text.ts +88 -0
- package/src/renderer/runtime/tree.tsx +16 -4
- package/src/renderer/runtime/types.ts +8 -3
- package/src/runtime.ts +0 -1
- package/src/scene.ts +2 -2
- package/src/shadcn.ts +7 -7
- package/src/spec/bsx_v2.json +12324 -6934
- package/src/spec/catalogs/base@4.json +13175 -0
- package/src/spec/catalogs/orkestrator@1.json +489 -0
- package/src/spec/catalogs.ts +259 -103
- package/src/spec/spec.test.ts +93 -24
- package/src/spec/vocabulary.json +83 -43
- package/src/spec/vocabulary.ts +102 -17
- package/dist/renderer/modules.d.ts +0 -4
- package/dist/renderer/modules.d.ts.map +0 -1
- package/dist/renderer/modules.js +0 -11
- package/dist/renderer/modules.js.map +0 -1
- package/dist/spec/catalogs/base@2.json +0 -355
- package/dist/spec/catalogs/base@2.json.js +0 -355
- package/dist/spec/catalogs/html@1.json +0 -308
- package/dist/spec/catalogs/html@1.json.js +0 -308
- package/dist/spec/catalogs/logo@1.json +0 -1687
- package/dist/spec/catalogs/logo@1.json.js +0 -1687
- package/dist/spec/catalogs/scene@1.json +0 -878
- package/dist/spec/catalogs/scene@1.json.js +0 -878
- package/dist/spec/catalogs/shadcn@1.json +0 -4002
- package/dist/spec/catalogs/shadcn@1.json.js +0 -4002
- package/dist/spec/catalogs/structure@1.json +0 -185
- package/dist/spec/catalogs/structure@1.json.js +0 -185
- package/dist/spec/catalogs/ui@1.json +0 -5349
- package/dist/spec/catalogs/ui@1.json.js +0 -5349
- package/src/renderer/modules.ts +0 -12
- package/src/spec/catalogs/base@2.json +0 -355
- package/src/spec/catalogs/html@1.json +0 -308
- package/src/spec/catalogs/logo@1.json +0 -1687
- package/src/spec/catalogs/scene@1.json +0 -878
- package/src/spec/catalogs/shadcn@1.json +0 -4002
- package/src/spec/catalogs/structure@1.json +0 -185
- package/src/spec/catalogs/ui@1.json +0 -5349
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {describe, expect, it} from 'vitest';
|
|
2
|
+
import * as z from 'zod';
|
|
3
|
+
import {threeScene} from '../scene';
|
|
4
|
+
import {shadcnExtras, shadcnHtml, shadcnLogo, shadcnUi} from '../shadcn';
|
|
5
|
+
import {hostedStructure} from '../ports/structure/index';
|
|
6
|
+
import {ORKESTRATOR_CATALOG_ID, parseSpecCatalog} from '../spec/catalogs';
|
|
7
|
+
import {addBlokComponents} from './catalog';
|
|
8
|
+
import {blokCatalogSpec, declareBlokUi, ownComponentsOf} from './declare';
|
|
9
|
+
import {BASE_CATALOG_ADDRESS, createBlokComponent, ownBlokCatalog} from './runtime';
|
|
10
|
+
|
|
11
|
+
const gauge = createBlokComponent({name: 'Gauge', service: 'lab', schema: z.object({value: z.number().optional().describe('What it reads.')}).describe('A gauge.')}, () => null);
|
|
12
|
+
|
|
13
|
+
describe('declareBlokUi', () => {
|
|
14
|
+
it('speaks the base catalog in the contexts it draws whole', () => {
|
|
15
|
+
const whole = declareBlokUi({name: 'whole', implements: [shadcnUi, shadcnHtml, shadcnLogo, shadcnExtras, threeScene, hostedStructure]});
|
|
16
|
+
expect(whole.contexts).toEqual(['PANEL', 'SCENE', 'LOGO']);
|
|
17
|
+
expect(whole.claims).toEqual(['base@4']);
|
|
18
|
+
const flat = declareBlokUi({name: 'flat', implements: [shadcnUi, shadcnHtml, shadcnLogo, shadcnExtras, hostedStructure]});
|
|
19
|
+
expect(flat.contexts).toEqual(['PANEL', 'LOGO']);
|
|
20
|
+
const logos = declareBlokUi({name: 'logos', implements: [shadcnLogo]});
|
|
21
|
+
expect([logos.contexts, logos.claims]).toEqual([['LOGO'], ['base@4']]);
|
|
22
|
+
expect(declareBlokUi({name: 'none', implements: []}).claims).toEqual([]);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('publishes what it draws beyond the base catalog as an app catalog', () => {
|
|
26
|
+
const ui = declareBlokUi({
|
|
27
|
+
name: 'lab',
|
|
28
|
+
implements: [shadcnUi, ownBlokCatalog({name: 'lab', version: 1, description: 'A lab.'}, [gauge])],
|
|
29
|
+
});
|
|
30
|
+
expect(ui.claims).toEqual(['lab@1']);
|
|
31
|
+
expect(ownComponentsOf(ui.catalog).map(component => component.name)).toEqual(['Gauge']);
|
|
32
|
+
const undo = addBlokComponents([createBlokComponent({name: 'Dial', schema: z.object({})}, () => null)], ui.catalog);
|
|
33
|
+
const spec = blokCatalogSpec({id: 'https://acme.example/catalogs/lab/v1', name: 'lab', version: 1, description: 'A lab.'}, ui.catalog);
|
|
34
|
+
undo();
|
|
35
|
+
expect([spec.id, spec.version, spec.minor, spec.extends]).toEqual(['https://acme.example/catalogs/lab/v1', 1, 0, BASE_CATALOG_ADDRESS]);
|
|
36
|
+
expect(spec.components.map(component => [component.name, component.service])).toEqual([['Dial', undefined], ['Gauge', 'lab']]);
|
|
37
|
+
const parsed = parseSpecCatalog(spec, 'lab');
|
|
38
|
+
expect(parsed.components.find(component => component.name === 'Gauge')?.service).toBe('lab');
|
|
39
|
+
expect(blokCatalogSpec({id: ORKESTRATOR_CATALOG_ID, name: 'orkestrator', version: 1, extends: BASE_CATALOG_ADDRESS, description: ''}, ui.catalog).extends).toBe(BASE_CATALOG_ADDRESS);
|
|
40
|
+
});
|
|
41
|
+
});
|
package/src/renderer/declare.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import {baseBlokCatalog} from './baseCatalog';
|
|
2
2
|
import {
|
|
3
|
+
BASE_CATALOG_ADDRESS,
|
|
4
|
+
BASE_CATALOG_REF,
|
|
3
5
|
catalogRef,
|
|
4
6
|
describeBlokComponent,
|
|
5
7
|
extendBlokCatalog,
|
|
@@ -8,28 +10,51 @@ import {
|
|
|
8
10
|
type BlokComponentDefinition,
|
|
9
11
|
type SpecCatalogJson,
|
|
10
12
|
} from './runtime';
|
|
13
|
+
import {baseCatalog} from '../spec/catalogs';
|
|
14
|
+
import type {BlokContextName} from '../spec/vocabulary';
|
|
11
15
|
|
|
12
16
|
/**
|
|
13
|
-
* A UI's catalog, declared:
|
|
17
|
+
* A UI's catalog, declared: what it draws, and nothing inferred.
|
|
14
18
|
*
|
|
15
|
-
* - `catalog` is what its renderers draw:
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
19
|
+
* - `catalog` is what its renderers draw: the language's own elements and every
|
|
20
|
+
* implementation.
|
|
21
|
+
* - `contexts` are the contexts in which it draws the base catalog whole
|
|
22
|
+
* (`HTML`, `SCENE`, `LOGO`): a blok written for the base catalog and drawn in
|
|
23
|
+
* one of them is known to render here.
|
|
24
|
+
* - `claims` are the catalogs it speaks: the base catalog when it draws it
|
|
25
|
+
* whole in at least one context, and each published catalog of its own.
|
|
26
|
+
* - Its own catalogs are part of `catalog`; what it publishes as its app
|
|
27
|
+
* catalog is `ownComponentsOf(catalog)`, which also counts what its modules
|
|
28
|
+
* add later (`addBlokComponents`).
|
|
24
29
|
*/
|
|
25
30
|
export type BlokUi = {
|
|
26
31
|
name: string;
|
|
27
32
|
description: string | null;
|
|
28
33
|
catalog: BlokCatalog;
|
|
29
34
|
implementations: readonly BlokCatalogImplementation[];
|
|
35
|
+
contexts: readonly BlokContextName[];
|
|
30
36
|
claims: readonly string[];
|
|
31
37
|
};
|
|
32
38
|
|
|
39
|
+
const CONTEXTS: readonly BlokContextName[] = ['PANEL', 'SCENE', 'LOGO'];
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The context a base component counts towards: its own, and for one that
|
|
43
|
+
* bridges a panel and another context (`Scene` opens a scene inside a panel),
|
|
44
|
+
* the other one: a renderer that draws no 3D still draws panels whole.
|
|
45
|
+
*/
|
|
46
|
+
const contextOf = (component: {context: BlokContextName; childrenContext: BlokContextName | null}): BlokContextName =>
|
|
47
|
+
component.context !== 'PANEL' ? component.context : (component.childrenContext ?? 'PANEL');
|
|
48
|
+
|
|
49
|
+
/** The contexts in which `catalog` draws every component of the base catalog. */
|
|
50
|
+
export const baseContextsOf = (catalog: BlokCatalog): BlokContextName[] => {
|
|
51
|
+
const components = baseCatalog().components;
|
|
52
|
+
return CONTEXTS.filter(context => {
|
|
53
|
+
const drawn = components.filter(component => !component.inheritsContext && contextOf(component) === context);
|
|
54
|
+
return drawn.length > 0 && drawn.every(component => catalog.components.has(component.name));
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
|
|
33
58
|
export const declareBlokUi = (declaration: {
|
|
34
59
|
name: string;
|
|
35
60
|
description?: string;
|
|
@@ -41,42 +66,55 @@ export const declareBlokUi = (declaration: {
|
|
|
41
66
|
if (twice.length > 0) {
|
|
42
67
|
throw new Error(`${declaration.name} implements ${[...new Set(twice)].join(', ')} twice`);
|
|
43
68
|
}
|
|
69
|
+
const catalog = extendBlokCatalog(
|
|
70
|
+
baseBlokCatalog,
|
|
71
|
+
declaration.name,
|
|
72
|
+
implementations.flatMap(implementation => implementation.components),
|
|
73
|
+
);
|
|
74
|
+
const contexts = baseContextsOf(catalog);
|
|
44
75
|
return {
|
|
45
76
|
name: declaration.name,
|
|
46
77
|
description: declaration.description ?? null,
|
|
47
|
-
catalog
|
|
48
|
-
baseBlokCatalog,
|
|
49
|
-
declaration.name,
|
|
50
|
-
implementations.flatMap(implementation => implementation.components),
|
|
51
|
-
),
|
|
78
|
+
catalog,
|
|
52
79
|
implementations,
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
.
|
|
56
|
-
|
|
80
|
+
contexts,
|
|
81
|
+
claims: [
|
|
82
|
+
...(contexts.length > 0 ? [BASE_CATALOG_REF] : []),
|
|
83
|
+
...implementations
|
|
84
|
+
.filter(implementation => implementation.contract.partOf === null && implementation.contract.version !== null && implementation.whole)
|
|
85
|
+
.map(implementation => catalogRef(implementation.contract))
|
|
86
|
+
.sort(),
|
|
87
|
+
],
|
|
57
88
|
};
|
|
58
89
|
};
|
|
59
90
|
|
|
60
91
|
/**
|
|
61
|
-
* What a UI
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
* and a global component a module drew over (it is the module's now).
|
|
92
|
+
* What a UI draws beyond the base catalog: every component of its catalog
|
|
93
|
+
* that is neither the language's own nor one of the base catalog's. Its own
|
|
94
|
+
* catalogs' components, and what its modules added since (`addBlokComponents`).
|
|
65
95
|
*/
|
|
66
|
-
export const
|
|
96
|
+
export const ownComponentsOf = (catalog: BlokCatalog): BlokComponentDefinition[] =>
|
|
67
97
|
[...catalog.components.values()].filter(
|
|
68
|
-
component => catalog.base?.components.get(component.name) !== component && component.origin
|
|
98
|
+
component => catalog.base?.components.get(component.name) !== component && component.origin !== BASE_CATALOG_REF,
|
|
69
99
|
);
|
|
70
100
|
|
|
71
101
|
/**
|
|
72
|
-
* A UI's
|
|
73
|
-
*
|
|
74
|
-
*
|
|
102
|
+
* A UI's app catalog as the spec writes one (`{id, name, version, minor,
|
|
103
|
+
* extends, components}`): what it publishes at `id`, registers with the
|
|
104
|
+
* server, and hands the language (`setAppBlokCatalog(parseSpecCatalog(...))`)
|
|
105
|
+
* so its own parser, checks and editor know the components only it draws.
|
|
75
106
|
*/
|
|
76
|
-
export const
|
|
77
|
-
name,
|
|
78
|
-
|
|
79
|
-
|
|
107
|
+
export const blokCatalogSpec = (
|
|
108
|
+
meta: {id: string; name: string; version: number; minor?: number; extends?: string; description: string},
|
|
109
|
+
catalog: BlokCatalog,
|
|
110
|
+
): SpecCatalogJson => ({
|
|
111
|
+
id: meta.id,
|
|
112
|
+
name: meta.name,
|
|
113
|
+
version: meta.version,
|
|
114
|
+
minor: meta.minor ?? 0,
|
|
115
|
+
extends: meta.extends ?? BASE_CATALOG_ADDRESS,
|
|
116
|
+
description: meta.description,
|
|
117
|
+
components: ownComponentsOf(catalog)
|
|
80
118
|
.map(component => describeBlokComponent(component.name, component))
|
|
81
119
|
.sort((left, right) => (left.name < right.name ? -1 : left.name > right.name ? 1 : 0)),
|
|
82
120
|
});
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
type KeptComponentState,
|
|
11
11
|
type KeptLocals,
|
|
12
12
|
} from "./persist";
|
|
13
|
-
import { createBlokRuntimeStore } from "./runtime";
|
|
13
|
+
import { createBlokRuntimeStore, getValueAtPath, localValuePath, runtimeIdOf } from "./runtime";
|
|
14
14
|
|
|
15
15
|
// No IndexedDB in node: the keyed store keeps what it saves in memory, as a window does.
|
|
16
16
|
const freshStore = () =>
|
|
@@ -32,13 +32,18 @@ const freshComponentStore = () =>
|
|
|
32
32
|
});
|
|
33
33
|
|
|
34
34
|
const SCOPE = { hub: "h1", user: "u1", materializedBlok: "7" };
|
|
35
|
-
/** The blok's locals: ports plus persist
|
|
35
|
+
/** The blok's locals as the tree resolves them: ports plus persist, each by its `<local>` node. */
|
|
36
|
+
const ITERATIONS = "Blok[0]/local[0]";
|
|
37
|
+
const POSE = "Blok[0]/local[1]";
|
|
38
|
+
const LABEL = "Blok[0]/local[2]";
|
|
39
|
+
/** The local inside a foreach: one value per item. */
|
|
40
|
+
const OPEN = "Blok[0]/foreach[0]/local[0]";
|
|
36
41
|
const LOCALS: BlokLocal[] = [
|
|
37
|
-
{ key: "iterations", kind: "INT", default: 15, persist: true },
|
|
38
|
-
{ key: "pose", kind: "LIST", default: [1, 0, 0, 0], persist: true },
|
|
39
|
-
{ key: "label", kind: "STRING", default: "x" },
|
|
42
|
+
{ node: ITERATIONS, key: "iterations", kind: "INT", default: 15, persist: true },
|
|
43
|
+
{ node: POSE, key: "pose", kind: "LIST", default: [1, 0, 0, 0], persist: true },
|
|
44
|
+
{ node: LABEL, key: "label", kind: "STRING", default: "x" },
|
|
45
|
+
{ node: OPEN, key: "open", kind: "BOOL", default: false, persist: true },
|
|
40
46
|
];
|
|
41
|
-
const DEFAULTS = { iterations: 15, pose: [1, 0, 0, 0], label: "x" };
|
|
42
47
|
|
|
43
48
|
beforeEach(() => {
|
|
44
49
|
vi.useFakeTimers();
|
|
@@ -53,105 +58,121 @@ afterEach(() => {
|
|
|
53
58
|
const open = (store: ReturnType<typeof freshStore>, locals = LOCALS) =>
|
|
54
59
|
openBlokPersistence({ scope: SCOPE, locals, store, componentStore: freshComponentStore(), delay: 100 });
|
|
55
60
|
|
|
56
|
-
/** A runtime as a materialized blok seeds it: the
|
|
57
|
-
const runtimeFor = (initial: KeptLocals,
|
|
58
|
-
createBlokRuntimeStore({ initialDataModel: DEFAULTS, keptValues: initial, keptRoots: keys });
|
|
61
|
+
/** A runtime as a materialized blok seeds it: the kept values at their locals' paths. */
|
|
62
|
+
const runtimeFor = (initial: KeptLocals, locals: ReadonlySet<string>) => createBlokRuntimeStore({ keptValues: initial, keptLocals: locals });
|
|
59
63
|
|
|
60
|
-
|
|
64
|
+
/** What the runtime holds for the local of `runtimeId` (undefined: nothing written, it reads its default). */
|
|
65
|
+
const valueOf = (runtime: ReturnType<typeof runtimeFor>, runtimeId: string) => getValueAtPath(runtime.getState().dataModel, localValuePath(runtimeId));
|
|
66
|
+
const write = (runtime: ReturnType<typeof runtimeFor>, runtimeId: string, value: unknown, below = "") =>
|
|
67
|
+
runtime.getState().setRuntimeValue(`${localValuePath(runtimeId)}${below}`, value);
|
|
68
|
+
|
|
69
|
+
describe("kept blok locals: <local persist>", () => {
|
|
61
70
|
it("keeps only the locals declared with persist", async () => {
|
|
62
71
|
const persistence = await open(freshStore());
|
|
63
|
-
expect([...persistence.
|
|
72
|
+
expect([...persistence.locals].sort()).toEqual([OPEN, ITERATIONS, POSE].sort());
|
|
64
73
|
});
|
|
65
74
|
|
|
66
|
-
it("seeds what was kept before the first render, and nothing for a first opening", async () => {
|
|
75
|
+
it("seeds what was kept before the first render, by runtime id, and nothing for a first opening", async () => {
|
|
67
76
|
const store = freshStore();
|
|
68
77
|
const first = await open(store);
|
|
69
78
|
expect(first.initial).toEqual({});
|
|
70
79
|
|
|
71
|
-
await store.save(keptLocalsId(SCOPE), "locals", {
|
|
80
|
+
await store.save(keptLocalsId(SCOPE), "locals", { [ITERATIONS]: 30, [runtimeIdOf(OPEN, ["7"])]: true });
|
|
72
81
|
const again = await open(store);
|
|
73
|
-
expect(again.initial).toEqual({
|
|
74
|
-
const runtime = runtimeFor(again.initial, again.
|
|
75
|
-
expect((runtime
|
|
82
|
+
expect(again.initial).toEqual({ [ITERATIONS]: 30, [`${OPEN}@7`]: true });
|
|
83
|
+
const runtime = runtimeFor(again.initial, again.locals);
|
|
84
|
+
expect(valueOf(runtime, ITERATIONS)).toBe(30);
|
|
85
|
+
expect(valueOf(runtime, `${OPEN}@7`)).toBe(true);
|
|
86
|
+
expect(valueOf(runtime, `${OPEN}@8`)).toBeUndefined();
|
|
76
87
|
});
|
|
77
88
|
|
|
78
|
-
it("writes a change of a kept local once, after the last change settles", async () => {
|
|
89
|
+
it("writes a change of a kept local once, after the last change settles, one value per foreach item", async () => {
|
|
79
90
|
const store = freshStore();
|
|
80
91
|
const save = vi.spyOn(store, "save");
|
|
81
92
|
const persistence = await open(store);
|
|
82
|
-
const runtime = runtimeFor(persistence.initial, persistence.
|
|
93
|
+
const runtime = runtimeFor(persistence.initial, persistence.locals);
|
|
83
94
|
const detach = persistence.attach(runtime);
|
|
84
95
|
|
|
85
|
-
runtime
|
|
86
|
-
runtime
|
|
87
|
-
runtime
|
|
96
|
+
write(runtime, POSE, [2, 0, 0, 0]);
|
|
97
|
+
write(runtime, POSE, 3, "/0");
|
|
98
|
+
write(runtime, LABEL, "not kept");
|
|
99
|
+
write(runtime, `${OPEN}@a`, true);
|
|
88
100
|
expect(save).not.toHaveBeenCalled();
|
|
89
101
|
await vi.advanceTimersByTimeAsync(100);
|
|
90
102
|
expect(save).toHaveBeenCalledTimes(1);
|
|
91
|
-
expect(await store.load(keptLocalsId(SCOPE), "locals")).toEqual({
|
|
103
|
+
expect(await store.load(keptLocalsId(SCOPE), "locals")).toEqual({ [POSE]: [3, 0, 0, 0], [`${OPEN}@a`]: true });
|
|
92
104
|
detach();
|
|
93
105
|
});
|
|
94
106
|
|
|
95
107
|
it("writes what is pending when the blok closes, and nothing back at the default", async () => {
|
|
96
108
|
const store = freshStore();
|
|
97
109
|
const persistence = await open(store);
|
|
98
|
-
const runtime = runtimeFor(persistence.initial, persistence.
|
|
110
|
+
const runtime = runtimeFor(persistence.initial, persistence.locals);
|
|
99
111
|
const detach = persistence.attach(runtime);
|
|
100
|
-
runtime
|
|
112
|
+
write(runtime, ITERATIONS, 4);
|
|
113
|
+
write(runtime, `${OPEN}@a`, false);
|
|
101
114
|
detach();
|
|
102
115
|
await vi.runAllTimersAsync();
|
|
103
|
-
expect(await store.load(keptLocalsId(SCOPE), "locals")).toEqual({
|
|
116
|
+
expect(await store.load(keptLocalsId(SCOPE), "locals")).toEqual({ [ITERATIONS]: 4 });
|
|
104
117
|
});
|
|
105
118
|
|
|
106
119
|
it("keeps the kept locals across a reset of the runtime values", async () => {
|
|
107
120
|
const persistence = await open(freshStore());
|
|
108
|
-
const runtime = runtimeFor(persistence.initial, persistence.
|
|
109
|
-
runtime
|
|
110
|
-
runtime
|
|
121
|
+
const runtime = runtimeFor(persistence.initial, persistence.locals);
|
|
122
|
+
write(runtime, POSE, [5, 0, 0, 0]);
|
|
123
|
+
write(runtime, `${OPEN}@a`, true);
|
|
124
|
+
write(runtime, LABEL, "edited");
|
|
111
125
|
runtime.getState().resetRuntimeValues();
|
|
112
|
-
|
|
113
|
-
expect(
|
|
114
|
-
expect(
|
|
126
|
+
expect(valueOf(runtime, POSE)).toEqual([5, 0, 0, 0]);
|
|
127
|
+
expect(valueOf(runtime, `${OPEN}@a`)).toBe(true);
|
|
128
|
+
expect(valueOf(runtime, LABEL)).toBeUndefined();
|
|
115
129
|
});
|
|
116
130
|
|
|
117
131
|
it("forgets every kept local and shows the defaults again", async () => {
|
|
118
132
|
const store = freshStore();
|
|
119
|
-
await store.save(keptLocalsId(SCOPE), "locals", {
|
|
133
|
+
await store.save(keptLocalsId(SCOPE), "locals", { [POSE]: [0, 1, 0, 0], [ITERATIONS]: 3 });
|
|
120
134
|
const persistence = await open(store);
|
|
121
|
-
const runtime = runtimeFor(persistence.initial, persistence.
|
|
135
|
+
const runtime = runtimeFor(persistence.initial, persistence.locals);
|
|
122
136
|
persistence.attach(runtime);
|
|
123
137
|
await persistence.resetLocals();
|
|
124
|
-
expect((runtime
|
|
138
|
+
expect(valueOf(runtime, POSE)).toBeUndefined();
|
|
125
139
|
expect(await store.load(keptLocalsId(SCOPE), "locals")).toBeNull();
|
|
126
140
|
});
|
|
127
141
|
|
|
128
142
|
it("drops what no longer persists or no longer fits its port", async () => {
|
|
129
143
|
const store = freshStore();
|
|
130
|
-
await store.save(keptLocalsId(SCOPE), "locals", {
|
|
144
|
+
await store.save(keptLocalsId(SCOPE), "locals", { [POSE]: { x: 1 }, [ITERATIONS]: 2.5, [LABEL]: "kept?", "Blok[0]/local[9]": true, [`${OPEN}@a`]: 3 });
|
|
145
|
+
expect((await open(store)).initial).toEqual({});
|
|
146
|
+
await store.save(keptLocalsId(SCOPE), "locals", { [ITERATIONS]: 2 });
|
|
147
|
+
expect((await open(store)).initial).toEqual({ [ITERATIONS]: 2 });
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("drops what a blok kept by top-level key, before locals were kept by runtime id", async () => {
|
|
151
|
+
const store = freshStore();
|
|
152
|
+
await store.save(keptLocalsId(SCOPE), "locals", { iterations: 30 });
|
|
131
153
|
expect((await open(store)).initial).toEqual({});
|
|
132
|
-
await store.save(keptLocalsId(SCOPE), "locals", { iterations: 2 });
|
|
133
|
-
expect((await open(store)).initial).toEqual({ iterations: 2 });
|
|
134
154
|
});
|
|
135
155
|
|
|
136
156
|
it("keeps nothing for another user or hub", async () => {
|
|
137
157
|
const store = freshStore();
|
|
138
|
-
await store.save(keptLocalsId(SCOPE), "locals", {
|
|
158
|
+
await store.save(keptLocalsId(SCOPE), "locals", { [ITERATIONS]: 2 });
|
|
139
159
|
const other = await openBlokPersistence({ scope: { ...SCOPE, user: "u2" }, locals: LOCALS, store, componentStore: freshComponentStore() });
|
|
140
160
|
expect(other.initial).toEqual({});
|
|
141
161
|
});
|
|
142
162
|
|
|
143
163
|
it("checks a kept value against the local's port", () => {
|
|
144
|
-
|
|
145
|
-
expect(fitsLocal(3
|
|
146
|
-
expect(fitsLocal(3.5, { key: "n", kind: "
|
|
147
|
-
expect(fitsLocal(
|
|
148
|
-
expect(fitsLocal(
|
|
149
|
-
expect(fitsLocal(
|
|
150
|
-
expect(fitsLocal(
|
|
151
|
-
expect(fitsLocal(
|
|
152
|
-
expect(fitsLocal("
|
|
153
|
-
expect(fitsLocal("
|
|
154
|
-
expect(fitsLocal(
|
|
164
|
+
const local = (port: Omit<BlokLocal, "node">): BlokLocal => ({ node: "n", ...port });
|
|
165
|
+
expect(fitsLocal(3, local({ key: "n", kind: "INT" }))).toBe(true);
|
|
166
|
+
expect(fitsLocal(3.5, local({ key: "n", kind: "INT" }))).toBe(false);
|
|
167
|
+
expect(fitsLocal(3.5, local({ key: "n", kind: "FLOAT" }))).toBe(true);
|
|
168
|
+
expect(fitsLocal(true, local({ key: "n", kind: "FLOAT" }))).toBe(false);
|
|
169
|
+
expect(fitsLocal("a", local({ key: "s", kind: "STRING" }))).toBe(true);
|
|
170
|
+
expect(fitsLocal({ object: "4", __identifier: "@mikro/image" }, local({ key: "img", kind: "STRUCTURE", identifier: "@mikro/image" }))).toBe(true);
|
|
171
|
+
expect(fitsLocal([1], local({ key: "l", kind: "LIST" }))).toBe(true);
|
|
172
|
+
expect(fitsLocal("x", local({ key: "l", kind: "LIST" }))).toBe(false);
|
|
173
|
+
expect(fitsLocal("fast", local({ key: "m", kind: "ENUM", choices: [{ value: "fast" }] }))).toBe(true);
|
|
174
|
+
expect(fitsLocal("slow", local({ key: "m", kind: "ENUM", choices: [{ value: "fast" }] }))).toBe(false);
|
|
175
|
+
expect(fitsLocal(null, local({ key: "n", kind: "INT" }))).toBe(true);
|
|
155
176
|
});
|
|
156
177
|
});
|
|
157
178
|
|
package/src/renderer/persist.ts
CHANGED
|
@@ -3,13 +3,16 @@ import type {KeyedStore, KeyedStoreOptions} from '../cache/keyedStore';
|
|
|
3
3
|
import {blokPlatform} from '../platform';
|
|
4
4
|
import {valueFitsPort} from '../spec/locals';
|
|
5
5
|
import {isExplicitId} from '../spec/vocabulary';
|
|
6
|
-
import
|
|
6
|
+
import {nodeOfRuntimeId} from '../bsx/declared';
|
|
7
|
+
import {LOCALS_ROOT, localNodeOfPath, runtimeIdOfPath, type BlokRuntimeStore} from './runtime';
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* What a blok keeps between openings, on this device only (blok language v2):
|
|
10
11
|
*
|
|
11
|
-
* - **form inputs**: a local declared
|
|
12
|
-
*
|
|
12
|
+
* - **form inputs**: a local declared `<local name="x" default={...} persist />`
|
|
13
|
+
* remembers the viewer's last input, by its runtime id: the `<local>`'s node
|
|
14
|
+
* id, plus `@<item key>` per enclosing foreach (one value per item). An
|
|
15
|
+
* explicit `id=` on the `<local>` keeps it across edits of the tree;
|
|
13
16
|
* - **component state**: a component that offers it as its own BOOL prop
|
|
14
17
|
* (`<Scene id="main" persist>`, `<Transform id="arm-pose" persist>`) keeps
|
|
15
18
|
* its interaction state (the camera, the pose) by its id. The language
|
|
@@ -17,10 +20,10 @@ import type {BlokRuntimeStore} from './runtime';
|
|
|
17
20
|
*
|
|
18
21
|
* Nothing reaches a server, so keeping cannot become a channel between
|
|
19
22
|
* clients. Values live in IndexedDB keyed by (hub, user, materialized blok)
|
|
20
|
-
* for the locals
|
|
21
|
-
* component, and are written a
|
|
22
|
-
*
|
|
23
|
-
* dropped when the blok opens.
|
|
23
|
+
* for the locals (one record, values by runtime id) and (hub, user,
|
|
24
|
+
* materialized blok, component id) for each component, and are written a
|
|
25
|
+
* moment after the last change. A local the blok no longer persists, or a
|
|
26
|
+
* value that no longer fits its port, is dropped when the blok opens.
|
|
24
27
|
*/
|
|
25
28
|
|
|
26
29
|
/** A choice a local's port offers (`ChoiceInput`). */
|
|
@@ -44,10 +47,14 @@ export type BlokLocalPort = {
|
|
|
44
47
|
description?: string | null;
|
|
45
48
|
};
|
|
46
49
|
|
|
47
|
-
/**
|
|
48
|
-
|
|
50
|
+
/**
|
|
51
|
+
* A `<local>` (vocabulary `locals`): an arg port plus `persist`, remembering
|
|
52
|
+
* the last input on this device, and `node`, the id of the `<local>` node
|
|
53
|
+
* that declares it.
|
|
54
|
+
*/
|
|
55
|
+
export type BlokLocal = BlokLocalPort & {node: string; persist?: boolean | null};
|
|
49
56
|
|
|
50
|
-
/** What is kept for one materialization's locals: values by local
|
|
57
|
+
/** What is kept for one materialization's locals: values by the local's runtime id (`runtimeIdOf`). */
|
|
51
58
|
export type KeptLocals = Record<string, unknown>;
|
|
52
59
|
|
|
53
60
|
/** Whose a kept record is: the hub and user it was set under, and the materialization. */
|
|
@@ -68,10 +75,11 @@ const lazyKeyedStore = <T,>(options: KeyedStoreOptions<T>): KeyedStore<T> => {
|
|
|
68
75
|
const isKeptLocals = (value: unknown): value is KeptLocals =>
|
|
69
76
|
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
70
77
|
|
|
78
|
+
/** Version 2: kept values by runtime id (version 1 kept them by top-level key, and is dropped). */
|
|
71
79
|
export const keptLocalsStore: KeyedStore<KeptLocals> = lazyKeyedStore<KeptLocals>({
|
|
72
80
|
db: 'orkestrator-blok-locals',
|
|
73
81
|
store: 'locals',
|
|
74
|
-
version:
|
|
82
|
+
version: 2,
|
|
75
83
|
isValue: isKeptLocals,
|
|
76
84
|
label: 'kept blok local',
|
|
77
85
|
});
|
|
@@ -107,11 +115,14 @@ export const fitsLocal = (kept: unknown, local: BlokLocal): boolean => {
|
|
|
107
115
|
return valueFitsPort(kept, local);
|
|
108
116
|
};
|
|
109
117
|
|
|
118
|
+
/** What of `record` still stands: the values of persisted locals (by their node) that still fit their port. */
|
|
110
119
|
export const keptValuesOf = (record: KeptLocals | null, locals: readonly BlokLocal[]): {values: KeptLocals; dropped: boolean} => {
|
|
111
120
|
const values: KeptLocals = {};
|
|
112
121
|
if (!record) return {values, dropped: false};
|
|
113
|
-
|
|
114
|
-
|
|
122
|
+
const persisted = new Map(locals.filter(local => local.persist).map(local => [local.node, local] as const));
|
|
123
|
+
for (const [runtimeId, value] of Object.entries(record)) {
|
|
124
|
+
const local = persisted.get(nodeOfRuntimeId(runtimeId));
|
|
125
|
+
if (local && fitsLocal(value, local)) values[runtimeId] = value;
|
|
115
126
|
}
|
|
116
127
|
return {values, dropped: Object.keys(values).length !== Object.keys(record).length};
|
|
117
128
|
};
|
|
@@ -123,9 +134,9 @@ const same = (a: unknown, b: unknown): boolean => JSON.stringify(a ?? null) ===
|
|
|
123
134
|
* seed the renderer with, and what writes changes back.
|
|
124
135
|
*/
|
|
125
136
|
export type BlokPersistence = {
|
|
126
|
-
/** The
|
|
127
|
-
|
|
128
|
-
/** The kept
|
|
137
|
+
/** The `<local>` node ids kept on this device (those declared with `persist`): `BlokRenderer`'s `keptLocals`. */
|
|
138
|
+
locals: ReadonlySet<string>;
|
|
139
|
+
/** The kept values to seed the runtime with, before the first render, by runtime id: `BlokRenderer`'s `keptValues`. */
|
|
129
140
|
initial: KeptLocals;
|
|
130
141
|
/** Follows `store`: a change of a kept local is written after `delay`. Returns the detach, which flushes. */
|
|
131
142
|
attach: (store: BlokRuntimeStore) => () => void;
|
|
@@ -173,9 +184,21 @@ export const componentStateStore = (
|
|
|
173
184
|
};
|
|
174
185
|
};
|
|
175
186
|
|
|
187
|
+
/** The values of the kept locals in `dataModel`, by runtime id: every item's, in a foreach. */
|
|
188
|
+
const keptIn = (dataModel: unknown, locals: ReadonlySet<string>): Map<string, unknown> => {
|
|
189
|
+
const found = new Map<string, unknown>();
|
|
190
|
+
const values = (dataModel as Record<string, unknown> | null)?.[LOCALS_ROOT];
|
|
191
|
+
if (typeof values !== 'object' || values === null) return found;
|
|
192
|
+
for (const [segment, value] of Object.entries(values as Record<string, unknown>)) {
|
|
193
|
+
const runtimeId = runtimeIdOfPath(`${LOCALS_ROOT}/${segment}`);
|
|
194
|
+
if (runtimeId !== null && locals.has(nodeOfRuntimeId(runtimeId))) found.set(runtimeId, value);
|
|
195
|
+
}
|
|
196
|
+
return found;
|
|
197
|
+
};
|
|
198
|
+
|
|
176
199
|
export const openBlokPersistence = async (options: {
|
|
177
200
|
scope: BlokPersistScope;
|
|
178
|
-
/** The blok's
|
|
201
|
+
/** The blok's resolved locals (by node); those with `persist` are kept. */
|
|
179
202
|
locals: readonly BlokLocal[];
|
|
180
203
|
store?: KeyedStore<KeptLocals>;
|
|
181
204
|
componentStore?: KeyedStore<KeptComponentState>;
|
|
@@ -186,9 +209,9 @@ export const openBlokPersistence = async (options: {
|
|
|
186
209
|
const delay = options.delay ?? 400;
|
|
187
210
|
const id = keptLocalsId(options.scope);
|
|
188
211
|
const persisted = options.locals.filter(local => local.persist);
|
|
189
|
-
const
|
|
190
|
-
const defaults = new Map(options.locals.map(local => [local.
|
|
191
|
-
const record =
|
|
212
|
+
const locals = new Set(persisted.map(local => local.node));
|
|
213
|
+
const defaults = new Map(options.locals.map(local => [local.node, local.default ?? null] as const));
|
|
214
|
+
const record = locals.size > 0 ? await store.load(id, LOCALS_RECORD) : null;
|
|
192
215
|
const {values: initial, dropped} = keptValuesOf(record, persisted);
|
|
193
216
|
if (dropped) {
|
|
194
217
|
await (Object.keys(initial).length > 0 ? store.save(id, LOCALS_RECORD, initial) : store.forget(id));
|
|
@@ -210,27 +233,27 @@ export const openBlokPersistence = async (options: {
|
|
|
210
233
|
};
|
|
211
234
|
|
|
212
235
|
return {
|
|
213
|
-
|
|
236
|
+
locals,
|
|
214
237
|
initial,
|
|
215
238
|
attach: target => {
|
|
216
239
|
runtime = target;
|
|
217
|
-
|
|
218
|
-
const seen = new Map([...keys].map(key => [key, valueOf(key)] as const));
|
|
240
|
+
let seen = keptIn(target.getState().dataModel, locals);
|
|
219
241
|
const unsubscribe = target.subscribe(state => {
|
|
242
|
+
const now = keptIn(state.dataModel, locals);
|
|
220
243
|
let changed = false;
|
|
221
|
-
for (const
|
|
222
|
-
const value = (
|
|
223
|
-
if (same(value, seen.get(
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
delete kept[key];
|
|
244
|
+
for (const runtimeId of new Set([...seen.keys(), ...now.keys()])) {
|
|
245
|
+
const value = now.get(runtimeId);
|
|
246
|
+
if (same(value, seen.get(runtimeId))) continue;
|
|
247
|
+
// Back at the default (or cleared) is nothing to keep.
|
|
248
|
+
if (value === undefined || same(value, defaults.get(nodeOfRuntimeId(runtimeId)))) {
|
|
249
|
+
if (!(runtimeId in kept)) continue;
|
|
250
|
+
delete kept[runtimeId];
|
|
229
251
|
} else {
|
|
230
|
-
kept[
|
|
252
|
+
kept[runtimeId] = value;
|
|
231
253
|
}
|
|
232
254
|
changed = true;
|
|
233
255
|
}
|
|
256
|
+
seen = now;
|
|
234
257
|
if (changed) schedule();
|
|
235
258
|
});
|
|
236
259
|
return () => {
|
|
@@ -244,8 +267,8 @@ export const openBlokPersistence = async (options: {
|
|
|
244
267
|
const state = runtime?.getState();
|
|
245
268
|
if (state) {
|
|
246
269
|
for (const path of Object.keys(state.runtimePathValues)) {
|
|
247
|
-
const
|
|
248
|
-
if (
|
|
270
|
+
const node = localNodeOfPath(path);
|
|
271
|
+
if (node !== null && locals.has(node)) state.clearRuntimeValue(path);
|
|
249
272
|
}
|
|
250
273
|
}
|
|
251
274
|
await write();
|
|
@@ -65,7 +65,7 @@ describe('describeBlokContract', () => {
|
|
|
65
65
|
{
|
|
66
66
|
name: 'Knob',
|
|
67
67
|
description: 'A knob.',
|
|
68
|
-
context: '
|
|
68
|
+
context: 'PANEL',
|
|
69
69
|
children_context: null,
|
|
70
70
|
inherits_context: false,
|
|
71
71
|
accepts_children: false,
|
|
@@ -87,7 +87,7 @@ describe('describeBlokContract', () => {
|
|
|
87
87
|
{
|
|
88
88
|
name: 'Label',
|
|
89
89
|
description: 'A label.',
|
|
90
|
-
context: '
|
|
90
|
+
context: 'PANEL',
|
|
91
91
|
children_context: null,
|
|
92
92
|
inherits_context: false,
|
|
93
93
|
accepts_children: false,
|