@jam-overture/loom 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +743 -0
- package/dist/catalogue.d.ts +70 -0
- package/dist/catalogue.js +88 -0
- package/dist/cli/args.d.ts +17 -0
- package/dist/cli/args.js +83 -0
- package/dist/cli/filesystem.d.ts +15 -0
- package/dist/cli/filesystem.js +31 -0
- package/dist/cli/index.d.ts +12 -0
- package/dist/cli/index.js +12 -0
- package/dist/cli/main.d.ts +3 -0
- package/dist/cli/main.js +30 -0
- package/dist/cli/namespace.d.ts +40 -0
- package/dist/cli/namespace.js +40 -0
- package/dist/cli/plan.d.ts +84 -0
- package/dist/cli/plan.js +113 -0
- package/dist/cli/run.d.ts +17 -0
- package/dist/cli/run.js +75 -0
- package/dist/cli/templates.d.ts +43 -0
- package/dist/cli/templates.js +134 -0
- package/dist/closed-set.d.ts +34 -0
- package/dist/closed-set.js +34 -0
- package/dist/data/adapter.d.ts +123 -0
- package/dist/data/adapter.js +91 -0
- package/dist/data/binding.d.ts +48 -0
- package/dist/data/binding.js +39 -0
- package/dist/data/catalogue.d.ts +26 -0
- package/dist/data/catalogue.js +6 -0
- package/dist/data/index.d.ts +14 -0
- package/dist/data/index.js +14 -0
- package/dist/data/plan.d.ts +58 -0
- package/dist/data/plan.js +0 -0
- package/dist/data/resolution.d.ts +91 -0
- package/dist/data/resolution.js +96 -0
- package/dist/data/resolve.d.ts +46 -0
- package/dist/data/resolve.js +37 -0
- package/dist/data/source.d.ts +40 -0
- package/dist/data/source.js +45 -0
- package/dist/deadline.d.ts +57 -0
- package/dist/deadline.js +78 -0
- package/dist/frame/catalogue.d.ts +24 -0
- package/dist/frame/catalogue.js +5 -0
- package/dist/frame/index.d.ts +12 -0
- package/dist/frame/index.js +12 -0
- package/dist/frame/origin.d.ts +87 -0
- package/dist/frame/origin.js +114 -0
- package/dist/frame/resolution.d.ts +85 -0
- package/dist/frame/resolution.js +79 -0
- package/dist/grammar.d.ts +26 -0
- package/dist/grammar.js +28 -0
- package/dist/ids.d.ts +57 -0
- package/dist/ids.js +104 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +26 -0
- package/dist/interactivity.d.ts +50 -0
- package/dist/interactivity.js +22 -0
- package/dist/interpretation/anthropic.d.ts +22 -0
- package/dist/interpretation/anthropic.js +76 -0
- package/dist/interpretation/client.d.ts +83 -0
- package/dist/interpretation/client.js +2 -0
- package/dist/interpretation/draft.d.ts +234 -0
- package/dist/interpretation/draft.js +84 -0
- package/dist/interpretation/index.d.ts +15 -0
- package/dist/interpretation/index.js +15 -0
- package/dist/interpretation/interpreter.d.ts +62 -0
- package/dist/interpretation/interpreter.js +151 -0
- package/dist/interpretation/materialize.d.ts +42 -0
- package/dist/interpretation/materialize.js +107 -0
- package/dist/interpretation/prompt.d.ts +229 -0
- package/dist/interpretation/prompt.js +285 -0
- package/dist/interpretation/render.d.ts +76 -0
- package/dist/interpretation/render.js +272 -0
- package/dist/interpretation/schema.d.ts +49 -0
- package/dist/interpretation/schema.js +131 -0
- package/dist/json.d.ts +35 -0
- package/dist/json.js +11 -0
- package/dist/paging.d.ts +60 -0
- package/dist/paging.js +49 -0
- package/dist/primitive-type.d.ts +32 -0
- package/dist/primitive-type.js +39 -0
- package/dist/render/addressing.d.ts +59 -0
- package/dist/render/addressing.js +60 -0
- package/dist/render/anchor.d.ts +105 -0
- package/dist/render/anchor.js +84 -0
- package/dist/render/behaviour-adjust.d.ts +62 -0
- package/dist/render/behaviour-adjust.js +78 -0
- package/dist/render/behaviour-copy.d.ts +53 -0
- package/dist/render/behaviour-copy.js +75 -0
- package/dist/render/behaviour-disclose.d.ts +74 -0
- package/dist/render/behaviour-disclose.js +77 -0
- package/dist/render/behaviour-present.d.ts +123 -0
- package/dist/render/behaviour-present.js +173 -0
- package/dist/render/behaviour.d.ts +243 -0
- package/dist/render/behaviour.js +284 -0
- package/dist/render/control.d.ts +81 -0
- package/dist/render/control.js +80 -0
- package/dist/render/decorative.d.ts +62 -0
- package/dist/render/decorative.js +2 -0
- package/dist/render/diagnostics.d.ts +260 -0
- package/dist/render/diagnostics.js +61 -0
- package/dist/render/disclosed.d.ts +37 -0
- package/dist/render/disclosed.js +37 -0
- package/dist/render/editable.d.ts +34 -0
- package/dist/render/editable.js +35 -0
- package/dist/render/frame.d.ts +42 -0
- package/dist/render/frame.js +31 -0
- package/dist/render/index.d.ts +35 -0
- package/dist/render/index.js +35 -0
- package/dist/render/inline-variables.d.ts +77 -0
- package/dist/render/inline-variables.js +220 -0
- package/dist/render/presented.d.ts +82 -0
- package/dist/render/presented.js +82 -0
- package/dist/render/primitive.d.ts +228 -0
- package/dist/render/primitive.js +36 -0
- package/dist/render/props.d.ts +48 -0
- package/dist/render/props.js +2 -0
- package/dist/render/reads.d.ts +73 -0
- package/dist/render/reads.js +45 -0
- package/dist/render/render.d.ts +164 -0
- package/dist/render/render.js +571 -0
- package/dist/render/request.d.ts +111 -0
- package/dist/render/request.js +60 -0
- package/dist/render/text.d.ts +91 -0
- package/dist/render/text.js +44 -0
- package/dist/render/theme.d.ts +61 -0
- package/dist/render/theme.js +52 -0
- package/dist/reserved-props.d.ts +35 -0
- package/dist/reserved-props.js +45 -0
- package/dist/result.d.ts +27 -0
- package/dist/result.js +30 -0
- package/dist/role.d.ts +71 -0
- package/dist/role.js +22 -0
- package/dist/runtime/analysis.d.ts +149 -0
- package/dist/runtime/analysis.js +187 -0
- package/dist/runtime/assessment.d.ts +35 -0
- package/dist/runtime/assessment.js +13 -0
- package/dist/runtime/disposition.d.ts +102 -0
- package/dist/runtime/disposition.js +59 -0
- package/dist/runtime/events.d.ts +187 -0
- package/dist/runtime/events.js +7 -0
- package/dist/runtime/gate.d.ts +43 -0
- package/dist/runtime/gate.js +169 -0
- package/dist/runtime/index.d.ts +27 -0
- package/dist/runtime/index.js +27 -0
- package/dist/runtime/intent.d.ts +60 -0
- package/dist/runtime/intent.js +36 -0
- package/dist/runtime/interpreter.d.ts +117 -0
- package/dist/runtime/interpreter.js +46 -0
- package/dist/runtime/inverse-interpreter.d.ts +90 -0
- package/dist/runtime/inverse-interpreter.js +56 -0
- package/dist/runtime/inverse.d.ts +87 -0
- package/dist/runtime/inverse.js +48 -0
- package/dist/runtime/narration.d.ts +24 -0
- package/dist/runtime/narration.js +36 -0
- package/dist/runtime/nesting.d.ts +31 -0
- package/dist/runtime/nesting.js +46 -0
- package/dist/runtime/pipeline.d.ts +124 -0
- package/dist/runtime/pipeline.js +178 -0
- package/dist/runtime/policy-fingerprint.d.ts +29 -0
- package/dist/runtime/policy-fingerprint.js +119 -0
- package/dist/runtime/policy-source.d.ts +42 -0
- package/dist/runtime/policy-source.js +8 -0
- package/dist/runtime/policy.d.ts +156 -0
- package/dist/runtime/policy.js +107 -0
- package/dist/runtime/proposal.d.ts +423 -0
- package/dist/runtime/proposal.js +109 -0
- package/dist/runtime/redirection.d.ts +53 -0
- package/dist/runtime/redirection.js +38 -0
- package/dist/runtime/repointing.d.ts +71 -0
- package/dist/runtime/repointing.js +0 -0
- package/dist/runtime/reversibility.d.ts +36 -0
- package/dist/runtime/reversibility.js +39 -0
- package/dist/runtime/stake-level.d.ts +33 -0
- package/dist/runtime/stake-level.js +58 -0
- package/dist/runtime/stakes.d.ts +47 -0
- package/dist/runtime/stakes.js +305 -0
- package/dist/runtime/vocabulary.d.ts +122 -0
- package/dist/runtime/vocabulary.js +87 -0
- package/dist/sdk/audit.d.ts +195 -0
- package/dist/sdk/audit.js +128 -0
- package/dist/sdk/catalogue.d.ts +37 -0
- package/dist/sdk/catalogue.js +16 -0
- package/dist/sdk/conformance.d.ts +342 -0
- package/dist/sdk/conformance.js +494 -0
- package/dist/sdk/copy.d.ts +105 -0
- package/dist/sdk/copy.js +90 -0
- package/dist/sdk/definition.d.ts +242 -0
- package/dist/sdk/definition.js +64 -0
- package/dist/sdk/index.d.ts +19 -0
- package/dist/sdk/index.js +19 -0
- package/dist/sdk/interactivity.d.ts +29 -0
- package/dist/sdk/interactivity.js +34 -0
- package/dist/sdk/pairings.d.ts +124 -0
- package/dist/sdk/pairings.js +81 -0
- package/dist/sdk/registry.d.ts +162 -0
- package/dist/sdk/registry.js +329 -0
- package/dist/sdk/selection.d.ts +46 -0
- package/dist/sdk/selection.js +24 -0
- package/dist/sdk/text.d.ts +100 -0
- package/dist/sdk/text.js +105 -0
- package/dist/sdk/vocabulary.d.ts +39 -0
- package/dist/sdk/vocabulary.js +36 -0
- package/dist/signals/broadcast.d.ts +134 -0
- package/dist/signals/broadcast.js +451 -0
- package/dist/signals/collect.d.ts +184 -0
- package/dist/signals/collect.js +196 -0
- package/dist/signals/deliver.d.ts +64 -0
- package/dist/signals/deliver.js +85 -0
- package/dist/signals/fold.d.ts +112 -0
- package/dist/signals/fold.js +76 -0
- package/dist/signals/index.d.ts +31 -0
- package/dist/signals/index.js +31 -0
- package/dist/signals/ingest.d.ts +51 -0
- package/dist/signals/ingest.js +70 -0
- package/dist/signals/intake.d.ts +149 -0
- package/dist/signals/intake.js +154 -0
- package/dist/signals/journal.d.ts +94 -0
- package/dist/signals/journal.js +11 -0
- package/dist/signals/kinds.d.ts +22 -0
- package/dist/signals/kinds.js +22 -0
- package/dist/signals/ledger.d.ts +46 -0
- package/dist/signals/ledger.js +81 -0
- package/dist/signals/memory.d.ts +17 -0
- package/dist/signals/memory.js +99 -0
- package/dist/signals/migrate.d.ts +27 -0
- package/dist/signals/migrate.js +78 -0
- package/dist/signals/postgres.d.ts +7 -0
- package/dist/signals/postgres.js +295 -0
- package/dist/signals/rollup.d.ts +142 -0
- package/dist/signals/rollup.js +0 -0
- package/dist/signals/schema.d.ts +534 -0
- package/dist/signals/schema.js +92 -0
- package/dist/signals/signal.d.ts +381 -0
- package/dist/signals/signal.js +150 -0
- package/dist/signals/tally.d.ts +70 -0
- package/dist/signals/tally.js +2 -0
- package/dist/signals/view.d.ts +51 -0
- package/dist/signals/view.js +54 -0
- package/dist/store/attribution.d.ts +125 -0
- package/dist/store/attribution.js +116 -0
- package/dist/store/database.d.ts +14 -0
- package/dist/store/database.js +2 -0
- package/dist/store/driver.d.ts +29 -0
- package/dist/store/driver.js +39 -0
- package/dist/store/errors.d.ts +56 -0
- package/dist/store/errors.js +37 -0
- package/dist/store/index.d.ts +16 -0
- package/dist/store/index.js +16 -0
- package/dist/store/memory.d.ts +3 -0
- package/dist/store/memory.js +115 -0
- package/dist/store/migrate.d.ts +54 -0
- package/dist/store/migrate.js +122 -0
- package/dist/store/postgres-holds.d.ts +4 -0
- package/dist/store/postgres-holds.js +192 -0
- package/dist/store/postgres.d.ts +8 -0
- package/dist/store/postgres.js +262 -0
- package/dist/store/replay.d.ts +94 -0
- package/dist/store/replay.js +82 -0
- package/dist/store/revert.d.ts +116 -0
- package/dist/store/revert.js +243 -0
- package/dist/store/schema.d.ts +371 -0
- package/dist/store/schema.js +78 -0
- package/dist/store/source.d.ts +23 -0
- package/dist/store/source.js +37 -0
- package/dist/store/store.d.ts +231 -0
- package/dist/store/store.js +52 -0
- package/dist/store/undone.d.ts +18 -0
- package/dist/store/undone.js +89 -0
- package/dist/submit/catalogue.d.ts +20 -0
- package/dist/submit/catalogue.js +5 -0
- package/dist/submit/declaration.d.ts +39 -0
- package/dist/submit/declaration.js +30 -0
- package/dist/submit/endpoint.d.ts +131 -0
- package/dist/submit/endpoint.js +160 -0
- package/dist/submit/index.d.ts +13 -0
- package/dist/submit/index.js +13 -0
- package/dist/submit/plan.d.ts +49 -0
- package/dist/submit/plan.js +47 -0
- package/dist/submit/resolution.d.ts +61 -0
- package/dist/submit/resolution.js +64 -0
- package/dist/submit/resolve.d.ts +45 -0
- package/dist/submit/resolve.js +36 -0
- package/dist/telemetry/calibration.d.ts +132 -0
- package/dist/telemetry/calibration.js +151 -0
- package/dist/telemetry/episode.d.ts +140 -0
- package/dist/telemetry/episode.js +272 -0
- package/dist/telemetry/event.d.ts +2310 -0
- package/dist/telemetry/event.js +405 -0
- package/dist/telemetry/index.d.ts +15 -0
- package/dist/telemetry/index.js +15 -0
- package/dist/telemetry/journal.d.ts +116 -0
- package/dist/telemetry/journal.js +11 -0
- package/dist/telemetry/memory.d.ts +24 -0
- package/dist/telemetry/memory.js +75 -0
- package/dist/telemetry/migrate.d.ts +26 -0
- package/dist/telemetry/migrate.js +40 -0
- package/dist/telemetry/postgres.d.ts +6 -0
- package/dist/telemetry/postgres.js +139 -0
- package/dist/telemetry/retention.d.ts +131 -0
- package/dist/telemetry/retention.js +217 -0
- package/dist/telemetry/schema.d.ts +113 -0
- package/dist/telemetry/schema.js +33 -0
- package/dist/telemetry/sink.d.ts +42 -0
- package/dist/telemetry/sink.js +61 -0
- package/dist/testing/contracts.d.ts +34 -0
- package/dist/testing/contracts.js +34 -0
- package/dist/testing/definitions.d.ts +15 -0
- package/dist/testing/definitions.js +48 -0
- package/dist/testing/doubles.d.ts +94 -0
- package/dist/testing/doubles.js +141 -0
- package/dist/testing/episode-harness.d.ts +49 -0
- package/dist/testing/episode-harness.js +65 -0
- package/dist/testing/filesystem.d.ts +17 -0
- package/dist/testing/filesystem.js +17 -0
- package/dist/testing/fixtures.d.ts +57 -0
- package/dist/testing/fixtures.js +84 -0
- package/dist/testing/hold-contract.d.ts +19 -0
- package/dist/testing/hold-contract.js +374 -0
- package/dist/testing/index.d.ts +41 -0
- package/dist/testing/index.js +41 -0
- package/dist/testing/journal-contract.d.ts +17 -0
- package/dist/testing/journal-contract.js +353 -0
- package/dist/testing/model-replies.d.ts +34 -0
- package/dist/testing/model-replies.js +101 -0
- package/dist/testing/primitives.d.ts +7 -0
- package/dist/testing/primitives.js +29 -0
- package/dist/testing/reader-signal-contract.d.ts +38 -0
- package/dist/testing/reader-signal-contract.js +318 -0
- package/dist/testing/row-security.d.ts +17 -0
- package/dist/testing/row-security.js +21 -0
- package/dist/testing/store-contract.d.ts +25 -0
- package/dist/testing/store-contract.js +500 -0
- package/dist/theme/apply.d.ts +17 -0
- package/dist/theme/apply.js +60 -0
- package/dist/theme/contrast.d.ts +197 -0
- package/dist/theme/contrast.js +259 -0
- package/dist/theme/derive.d.ts +77 -0
- package/dist/theme/derive.js +167 -0
- package/dist/theme/faces.d.ts +73 -0
- package/dist/theme/faces.js +119 -0
- package/dist/theme/font-packs.d.ts +25 -0
- package/dist/theme/font-packs.js +257 -0
- package/dist/theme/index.d.ts +20 -0
- package/dist/theme/index.js +20 -0
- package/dist/theme/lab.d.ts +30 -0
- package/dist/theme/lab.js +46 -0
- package/dist/theme/library.d.ts +114 -0
- package/dist/theme/library.js +300 -0
- package/dist/theme/measure.d.ts +144 -0
- package/dist/theme/measure.js +145 -0
- package/dist/theme/palettes.d.ts +64 -0
- package/dist/theme/palettes.js +497 -0
- package/dist/theme/registry.d.ts +59 -0
- package/dist/theme/registry.js +60 -0
- package/dist/theme/separation.d.ts +177 -0
- package/dist/theme/separation.js +203 -0
- package/dist/theme/style-presets.d.ts +60 -0
- package/dist/theme/style-presets.js +124 -0
- package/dist/theme/theme.d.ts +253 -0
- package/dist/theme/theme.js +171 -0
- package/dist/tree/apply.d.ts +16 -0
- package/dist/tree/apply.js +104 -0
- package/dist/tree/builders.d.ts +19 -0
- package/dist/tree/builders.js +20 -0
- package/dist/tree/compare.d.ts +68 -0
- package/dist/tree/compare.js +75 -0
- package/dist/tree/configuration.d.ts +25 -0
- package/dist/tree/configuration.js +108 -0
- package/dist/tree/delta.d.ts +288 -0
- package/dist/tree/delta.js +88 -0
- package/dist/tree/errors.d.ts +69 -0
- package/dist/tree/errors.js +34 -0
- package/dist/tree/identity.d.ts +117 -0
- package/dist/tree/identity.js +145 -0
- package/dist/tree/index.d.ts +21 -0
- package/dist/tree/index.js +21 -0
- package/dist/tree/inverse.d.ts +24 -0
- package/dist/tree/inverse.js +112 -0
- package/dist/tree/mutation.d.ts +18 -0
- package/dist/tree/mutation.js +49 -0
- package/dist/tree/naming.d.ts +4 -0
- package/dist/tree/naming.js +35 -0
- package/dist/tree/navigation.d.ts +38 -0
- package/dist/tree/navigation.js +83 -0
- package/dist/tree/node.d.ts +103 -0
- package/dist/tree/node.js +46 -0
- package/dist/tree/outline.d.ts +28 -0
- package/dist/tree/outline.js +10 -0
- package/dist/tree/tree.d.ts +83 -0
- package/dist/tree/tree.js +62 -0
- package/dist/write/commit.d.ts +124 -0
- package/dist/write/commit.js +234 -0
- package/dist/write/held.d.ts +674 -0
- package/dist/write/held.js +185 -0
- package/dist/write/index.d.ts +11 -0
- package/dist/write/index.js +11 -0
- package/dist/write/liveness.d.ts +132 -0
- package/dist/write/liveness.js +91 -0
- package/dist/write/revert.d.ts +79 -0
- package/dist/write/revert.js +134 -0
- package/package.json +160 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import type { NodeId } from "../ids.js";
|
|
2
|
+
import { type LoomNode } from "./node.js";
|
|
3
|
+
import type { LoomTree } from "./tree.js";
|
|
4
|
+
/**
|
|
5
|
+
* Whether an id still names one thing.
|
|
6
|
+
*
|
|
7
|
+
* 0001 makes the id the address of a node for the life of the tree, and 0028
|
|
8
|
+
* leans on that when it joins two trees by id to say how they differ. Both read
|
|
9
|
+
* as facts and neither was checked. What is actually enforced is narrower: a
|
|
10
|
+
* delta may not insert an id the *live tree* already holds. An id that has been
|
|
11
|
+
* removed is not in the live tree, so nothing stops a later insert minting a
|
|
12
|
+
* fresh node onto it — and then `n_4` is a card at revision 3 and a paragraph at
|
|
13
|
+
* revision 9, and every reader that joins by id across the log is silently
|
|
14
|
+
* conflating two nodes.
|
|
15
|
+
*
|
|
16
|
+
* Reuse is not always wrong, which is what makes this worth doing carefully. An
|
|
17
|
+
* undo of a `remove` re-inserts the exact node that was removed (`inverse.ts`),
|
|
18
|
+
* and undo is a first-class operation (0032, 0035). A rule that forbade an id
|
|
19
|
+
* from ever returning would forbid taking a removal back. So the question is
|
|
20
|
+
* never "did this id return" but "did it return as the node that left".
|
|
21
|
+
*
|
|
22
|
+
* This module answers that by reading a tree's history. It stores nothing and
|
|
23
|
+
* changes nothing: it is handed each consecutive pair of states a fold already
|
|
24
|
+
* produces, and it reports. Enforcement across a log would need the tree to
|
|
25
|
+
* carry the ids it has retired, which is a schema change — see 0038.
|
|
26
|
+
*/
|
|
27
|
+
/**
|
|
28
|
+
* A node's shape, ids included, at one moment.
|
|
29
|
+
*
|
|
30
|
+
* Ids are part of the fingerprint on purpose. The question is whether the node
|
|
31
|
+
* that returned *is* the node that left, not whether it resembles it: a subtree
|
|
32
|
+
* rebuilt with fresh children under a retired parent id is a new node wearing an
|
|
33
|
+
* old address, and that is exactly the case worth reporting.
|
|
34
|
+
*
|
|
35
|
+
* Props are compared by serialisation, which is order-sensitive — the same
|
|
36
|
+
* choice `compareTrees` makes and for the same reason: a props bag is rewritten
|
|
37
|
+
* wholesale (0009), so two orderings came from two different writes.
|
|
38
|
+
*/
|
|
39
|
+
export declare const nodeFingerprint: (node: LoomNode) => string;
|
|
40
|
+
/** Who was living at an id, and what a reader would call them. */
|
|
41
|
+
export type Occupant = {
|
|
42
|
+
readonly label: string;
|
|
43
|
+
readonly fingerprint: string;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* One unbroken stretch of an id being present in the tree.
|
|
47
|
+
*
|
|
48
|
+
* A node that is configured while resident keeps one tenancy: it never left, so
|
|
49
|
+
* nothing about its identity is in doubt. `entered` is therefore the shape it
|
|
50
|
+
* arrived with and not necessarily its shape now, which is all the comparison
|
|
51
|
+
* needs — the pairing that matters is the shape it *left* with against the shape
|
|
52
|
+
* the next arrival brought.
|
|
53
|
+
*/
|
|
54
|
+
export type IdTenancy = {
|
|
55
|
+
readonly enteredAt: number;
|
|
56
|
+
readonly entered: Occupant;
|
|
57
|
+
/** null while the node is still in the tree. */
|
|
58
|
+
readonly leftAt: number | null;
|
|
59
|
+
readonly left: Occupant | null;
|
|
60
|
+
};
|
|
61
|
+
/** Every stretch every id has had, oldest first. */
|
|
62
|
+
export type IdHistory = ReadonlyMap<NodeId, readonly IdTenancy[]>;
|
|
63
|
+
/**
|
|
64
|
+
* The history of a tree nobody has changed yet: every id present in it arrived
|
|
65
|
+
* at its revision. Usually revision 0, but a caller auditing from a later known
|
|
66
|
+
* state gets an honest starting point rather than a fictional one.
|
|
67
|
+
*/
|
|
68
|
+
export declare const seedIdHistory: (seed: LoomTree) => IdHistory;
|
|
69
|
+
/**
|
|
70
|
+
* Extends a history by one revision, from the two states either side of it.
|
|
71
|
+
*
|
|
72
|
+
* Deliberately takes trees rather than the delta that produced them. Presence is
|
|
73
|
+
* the only thing identity turns on, and presence is a property of the states: a
|
|
74
|
+
* `move` cannot change it, a `configure` cannot change it, and reading it off
|
|
75
|
+
* the operations would mean re-deriving what "the tree afterwards" means in a
|
|
76
|
+
* second place. `applyDelta` is the one thing allowed to answer that.
|
|
77
|
+
*
|
|
78
|
+
* A whole subtree that leaves records a departure for every id in it, not only
|
|
79
|
+
* the one the `remove` named — every one of them is an address that could later
|
|
80
|
+
* be reused, and reporting only the root would miss it.
|
|
81
|
+
*/
|
|
82
|
+
export declare const trackIds: (history: IdHistory, before: LoomTree, after: LoomTree) => IdHistory;
|
|
83
|
+
/**
|
|
84
|
+
* An id that left the tree and came back.
|
|
85
|
+
*
|
|
86
|
+
* Two outcomes rather than one, because they mean opposite things to a reader.
|
|
87
|
+
* `restored` is a removal that was taken back and is the expected shape of an
|
|
88
|
+
* undo — reported because it is a real event in the tree's history, not because
|
|
89
|
+
* anything is wrong. `recycled` is one address naming two different nodes, which
|
|
90
|
+
* makes every id-joined reading of this tree's log ambiguous from `returnedAt`
|
|
91
|
+
* onwards.
|
|
92
|
+
*/
|
|
93
|
+
export type IdReturn = {
|
|
94
|
+
readonly code: "restored";
|
|
95
|
+
readonly nodeId: NodeId;
|
|
96
|
+
readonly label: string;
|
|
97
|
+
readonly leftAt: number;
|
|
98
|
+
readonly returnedAt: number;
|
|
99
|
+
} | {
|
|
100
|
+
readonly code: "recycled";
|
|
101
|
+
readonly nodeId: NodeId;
|
|
102
|
+
readonly leftAs: string;
|
|
103
|
+
readonly returnedAs: string;
|
|
104
|
+
readonly leftAt: number;
|
|
105
|
+
readonly returnedAt: number;
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Every return in a history, in the order they happened.
|
|
109
|
+
*
|
|
110
|
+
* Chronological rather than grouped by id, because a reader is asking what
|
|
111
|
+
* became of this tree and not what became of one address. Ties within a revision
|
|
112
|
+
* are broken by id so the reading is stable across runs.
|
|
113
|
+
*/
|
|
114
|
+
export declare const idReturnsIn: (history: IdHistory) => readonly IdReturn[];
|
|
115
|
+
/** The returns that make an id ambiguous, which is the subset worth acting on. */
|
|
116
|
+
export declare const recycledIds: (returns: readonly IdReturn[]) => readonly IdReturn[];
|
|
117
|
+
//# sourceMappingURL=identity.d.ts.map
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { assertNever } from "../result.js";
|
|
2
|
+
import { nodeLabel } from "./node.js";
|
|
3
|
+
import { walkTree } from "./navigation.js";
|
|
4
|
+
/**
|
|
5
|
+
* Whether an id still names one thing.
|
|
6
|
+
*
|
|
7
|
+
* 0001 makes the id the address of a node for the life of the tree, and 0028
|
|
8
|
+
* leans on that when it joins two trees by id to say how they differ. Both read
|
|
9
|
+
* as facts and neither was checked. What is actually enforced is narrower: a
|
|
10
|
+
* delta may not insert an id the *live tree* already holds. An id that has been
|
|
11
|
+
* removed is not in the live tree, so nothing stops a later insert minting a
|
|
12
|
+
* fresh node onto it — and then `n_4` is a card at revision 3 and a paragraph at
|
|
13
|
+
* revision 9, and every reader that joins by id across the log is silently
|
|
14
|
+
* conflating two nodes.
|
|
15
|
+
*
|
|
16
|
+
* Reuse is not always wrong, which is what makes this worth doing carefully. An
|
|
17
|
+
* undo of a `remove` re-inserts the exact node that was removed (`inverse.ts`),
|
|
18
|
+
* and undo is a first-class operation (0032, 0035). A rule that forbade an id
|
|
19
|
+
* from ever returning would forbid taking a removal back. So the question is
|
|
20
|
+
* never "did this id return" but "did it return as the node that left".
|
|
21
|
+
*
|
|
22
|
+
* This module answers that by reading a tree's history. It stores nothing and
|
|
23
|
+
* changes nothing: it is handed each consecutive pair of states a fold already
|
|
24
|
+
* produces, and it reports. Enforcement across a log would need the tree to
|
|
25
|
+
* carry the ids it has retired, which is a schema change — see 0038.
|
|
26
|
+
*/
|
|
27
|
+
/**
|
|
28
|
+
* A node's shape, ids included, at one moment.
|
|
29
|
+
*
|
|
30
|
+
* Ids are part of the fingerprint on purpose. The question is whether the node
|
|
31
|
+
* that returned *is* the node that left, not whether it resembles it: a subtree
|
|
32
|
+
* rebuilt with fresh children under a retired parent id is a new node wearing an
|
|
33
|
+
* old address, and that is exactly the case worth reporting.
|
|
34
|
+
*
|
|
35
|
+
* Props are compared by serialisation, which is order-sensitive — the same
|
|
36
|
+
* choice `compareTrees` makes and for the same reason: a props bag is rewritten
|
|
37
|
+
* wholesale (0009), so two orderings came from two different writes.
|
|
38
|
+
*/
|
|
39
|
+
export const nodeFingerprint = (node) => {
|
|
40
|
+
switch (node.kind) {
|
|
41
|
+
case "element":
|
|
42
|
+
return `element ${node.id} ${node.type} ${JSON.stringify(node.props)} [${node.children
|
|
43
|
+
.map(nodeFingerprint)
|
|
44
|
+
.join(" ")}]`;
|
|
45
|
+
case "slot":
|
|
46
|
+
return `slot ${node.id} ${node.name} [${node.children.map(nodeFingerprint).join(" ")}]`;
|
|
47
|
+
case "text":
|
|
48
|
+
return `text ${node.id} ${JSON.stringify(node.value)}`;
|
|
49
|
+
default:
|
|
50
|
+
return assertNever(node, "nodeFingerprint");
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
const occupantOf = (node) => ({
|
|
54
|
+
label: nodeLabel(node),
|
|
55
|
+
fingerprint: nodeFingerprint(node),
|
|
56
|
+
});
|
|
57
|
+
const occupantsOf = (tree) => new Map(Array.from(walkTree(tree.root), (node) => [node.id, occupantOf(node)]));
|
|
58
|
+
/**
|
|
59
|
+
* The history of a tree nobody has changed yet: every id present in it arrived
|
|
60
|
+
* at its revision. Usually revision 0, but a caller auditing from a later known
|
|
61
|
+
* state gets an honest starting point rather than a fictional one.
|
|
62
|
+
*/
|
|
63
|
+
export const seedIdHistory = (seed) => new Map(Array.from(occupantsOf(seed), ([nodeId, occupant]) => [
|
|
64
|
+
nodeId,
|
|
65
|
+
[{ enteredAt: seed.revision, entered: occupant, leftAt: null, left: null }],
|
|
66
|
+
]));
|
|
67
|
+
const closeTenancy = (tenancies, leftAt, left) => tenancies.map((tenancy, index) => index === tenancies.length - 1 && tenancy.leftAt === null
|
|
68
|
+
? { ...tenancy, leftAt, left }
|
|
69
|
+
: tenancy);
|
|
70
|
+
/**
|
|
71
|
+
* Extends a history by one revision, from the two states either side of it.
|
|
72
|
+
*
|
|
73
|
+
* Deliberately takes trees rather than the delta that produced them. Presence is
|
|
74
|
+
* the only thing identity turns on, and presence is a property of the states: a
|
|
75
|
+
* `move` cannot change it, a `configure` cannot change it, and reading it off
|
|
76
|
+
* the operations would mean re-deriving what "the tree afterwards" means in a
|
|
77
|
+
* second place. `applyDelta` is the one thing allowed to answer that.
|
|
78
|
+
*
|
|
79
|
+
* A whole subtree that leaves records a departure for every id in it, not only
|
|
80
|
+
* the one the `remove` named — every one of them is an address that could later
|
|
81
|
+
* be reused, and reporting only the root would miss it.
|
|
82
|
+
*/
|
|
83
|
+
export const trackIds = (history, before, after) => {
|
|
84
|
+
const wasThere = occupantsOf(before);
|
|
85
|
+
const isThere = occupantsOf(after);
|
|
86
|
+
const tracked = new Map(history);
|
|
87
|
+
for (const [nodeId, occupant] of wasThere) {
|
|
88
|
+
if (isThere.has(nodeId))
|
|
89
|
+
continue;
|
|
90
|
+
const tenancies = tracked.get(nodeId);
|
|
91
|
+
if (tenancies)
|
|
92
|
+
tracked.set(nodeId, closeTenancy(tenancies, after.revision, occupant));
|
|
93
|
+
}
|
|
94
|
+
for (const [nodeId, occupant] of isThere) {
|
|
95
|
+
if (wasThere.has(nodeId))
|
|
96
|
+
continue;
|
|
97
|
+
const arrival = {
|
|
98
|
+
enteredAt: after.revision,
|
|
99
|
+
entered: occupant,
|
|
100
|
+
leftAt: null,
|
|
101
|
+
left: null,
|
|
102
|
+
};
|
|
103
|
+
tracked.set(nodeId, [...(tracked.get(nodeId) ?? []), arrival]);
|
|
104
|
+
}
|
|
105
|
+
return tracked;
|
|
106
|
+
};
|
|
107
|
+
const returnBetween = (nodeId, previous, next) => {
|
|
108
|
+
if (previous.leftAt === null || previous.left === null)
|
|
109
|
+
return undefined;
|
|
110
|
+
return previous.left.fingerprint === next.entered.fingerprint
|
|
111
|
+
? {
|
|
112
|
+
code: "restored",
|
|
113
|
+
nodeId,
|
|
114
|
+
label: next.entered.label,
|
|
115
|
+
leftAt: previous.leftAt,
|
|
116
|
+
returnedAt: next.enteredAt,
|
|
117
|
+
}
|
|
118
|
+
: {
|
|
119
|
+
code: "recycled",
|
|
120
|
+
nodeId,
|
|
121
|
+
leftAs: previous.left.label,
|
|
122
|
+
returnedAs: next.entered.label,
|
|
123
|
+
leftAt: previous.leftAt,
|
|
124
|
+
returnedAt: next.enteredAt,
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* Every return in a history, in the order they happened.
|
|
129
|
+
*
|
|
130
|
+
* Chronological rather than grouped by id, because a reader is asking what
|
|
131
|
+
* became of this tree and not what became of one address. Ties within a revision
|
|
132
|
+
* are broken by id so the reading is stable across runs.
|
|
133
|
+
*/
|
|
134
|
+
export const idReturnsIn = (history) => Array.from(history)
|
|
135
|
+
.flatMap(([nodeId, tenancies]) => tenancies.flatMap((tenancy, index) => {
|
|
136
|
+
const previous = tenancies[index - 1];
|
|
137
|
+
if (previous === undefined)
|
|
138
|
+
return [];
|
|
139
|
+
const found = returnBetween(nodeId, previous, tenancy);
|
|
140
|
+
return found === undefined ? [] : [found];
|
|
141
|
+
}))
|
|
142
|
+
.sort((left, right) => left.returnedAt - right.returnedAt || left.nodeId.localeCompare(right.nodeId));
|
|
143
|
+
/** The returns that make an id ambiguous, which is the subset worth acting on. */
|
|
144
|
+
export const recycledIds = (returns) => returns.filter((found) => found.code === "recycled");
|
|
145
|
+
//# sourceMappingURL=identity.js.map
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The tree, and every operation that produces another one.
|
|
3
|
+
*
|
|
4
|
+
* Nothing here mutates: a delta applied to a tree returns a new tree or an error
|
|
5
|
+
* saying why it could not. This is the model everything else in Loom is about.
|
|
6
|
+
*/
|
|
7
|
+
export * from "./apply.js";
|
|
8
|
+
export * from "./builders.js";
|
|
9
|
+
export * from "./compare.js";
|
|
10
|
+
export * from "./configuration.js";
|
|
11
|
+
export * from "./delta.js";
|
|
12
|
+
export * from "./errors.js";
|
|
13
|
+
export * from "./identity.js";
|
|
14
|
+
export * from "./inverse.js";
|
|
15
|
+
export * from "./mutation.js";
|
|
16
|
+
export * from "./naming.js";
|
|
17
|
+
export * from "./navigation.js";
|
|
18
|
+
export * from "./node.js";
|
|
19
|
+
export * from "./outline.js";
|
|
20
|
+
export * from "./tree.js";
|
|
21
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The tree, and every operation that produces another one.
|
|
3
|
+
*
|
|
4
|
+
* Nothing here mutates: a delta applied to a tree returns a new tree or an error
|
|
5
|
+
* saying why it could not. This is the model everything else in Loom is about.
|
|
6
|
+
*/
|
|
7
|
+
export * from "./apply.js";
|
|
8
|
+
export * from "./builders.js";
|
|
9
|
+
export * from "./compare.js";
|
|
10
|
+
export * from "./configuration.js";
|
|
11
|
+
export * from "./delta.js";
|
|
12
|
+
export * from "./errors.js";
|
|
13
|
+
export * from "./identity.js";
|
|
14
|
+
export * from "./inverse.js";
|
|
15
|
+
export * from "./mutation.js";
|
|
16
|
+
export * from "./naming.js";
|
|
17
|
+
export * from "./navigation.js";
|
|
18
|
+
export * from "./node.js";
|
|
19
|
+
export * from "./outline.js";
|
|
20
|
+
export * from "./tree.js";
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { DeltaId } from "../ids.js";
|
|
2
|
+
import { type Result } from "../result.js";
|
|
3
|
+
import type { TreeDelta, TreeOperation } from "./delta.js";
|
|
4
|
+
import type { TreeError } from "./errors.js";
|
|
5
|
+
import { type LoomNode } from "./node.js";
|
|
6
|
+
import type { LoomTree } from "./tree.js";
|
|
7
|
+
export declare const invertOperation: (root: LoomNode, operation: TreeOperation) => Result<TreeOperation, TreeError>;
|
|
8
|
+
/**
|
|
9
|
+
* The operations that undo `delta`, without deciding which revision they apply
|
|
10
|
+
* to. Fails for the same reasons applying `delta` would fail, so a successful
|
|
11
|
+
* inversion also proves the original delta is applicable.
|
|
12
|
+
*
|
|
13
|
+
* Separate from `invertDelta` because an undo is not always applied where it
|
|
14
|
+
* was computed: reverting a revision from the log inverts against the tree that
|
|
15
|
+
* revision observed, then applies the result at head. Only the operations
|
|
16
|
+
* survive that journey — the id and the base revision belong to whichever delta
|
|
17
|
+
* eventually carries them.
|
|
18
|
+
*/
|
|
19
|
+
export declare const invertOperations: (tree: LoomTree, delta: TreeDelta) => Result<readonly TreeOperation[], TreeError>;
|
|
20
|
+
/**
|
|
21
|
+
* The delta that undoes `delta`, targeting the revision `delta` produces.
|
|
22
|
+
*/
|
|
23
|
+
export declare const invertDelta: (tree: LoomTree, delta: TreeDelta, inverseDeltaId: DeltaId) => Result<TreeDelta, TreeError>;
|
|
24
|
+
//# sourceMappingURL=inverse.d.ts.map
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { assertNever, err, mapResult, ok } from "../result.js";
|
|
2
|
+
import { applyOperation } from "./apply.js";
|
|
3
|
+
import { configurationOf } from "./configuration.js";
|
|
4
|
+
import { childrenOf } from "./node.js";
|
|
5
|
+
import { findNode, findParent } from "./navigation.js";
|
|
6
|
+
/**
|
|
7
|
+
* Inverse deltas.
|
|
8
|
+
*
|
|
9
|
+
* Every operation has an exact inverse, so undo is a delta rather than a
|
|
10
|
+
* snapshot. This is also what makes reversibility a computable property: the
|
|
11
|
+
* Gate does not guess whether a change can be taken back, it is handed the
|
|
12
|
+
* delta that takes it back.
|
|
13
|
+
*
|
|
14
|
+
* The inverse of an operation depends on the state that operation observed, so
|
|
15
|
+
* inversion walks the delta forward — inverting each operation against the tree
|
|
16
|
+
* as it stood at that point — and then reverses the resulting order.
|
|
17
|
+
*/
|
|
18
|
+
const indexOfChild = (parent, nodeId) => childrenOf(parent).findIndex((child) => child.id === nodeId);
|
|
19
|
+
const invertRemove = (root, nodeId) => {
|
|
20
|
+
const removed = findNode(root, nodeId);
|
|
21
|
+
if (!removed)
|
|
22
|
+
return err({ code: "node-not-found", nodeId });
|
|
23
|
+
const parent = findParent(root, nodeId);
|
|
24
|
+
if (!parent)
|
|
25
|
+
return err({ code: "root-not-detachable", nodeId });
|
|
26
|
+
return ok({ op: "insert", parentId: parent.id, index: indexOfChild(parent, nodeId), node: removed });
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* A move is detach-then-insert, so its inverse restores the original index in
|
|
30
|
+
* the original parent's child list minus the node — which is exactly the list
|
|
31
|
+
* the inverse move sees once it has detached the node from where it went.
|
|
32
|
+
*/
|
|
33
|
+
const invertMove = (root, nodeId) => {
|
|
34
|
+
const parent = findParent(root, nodeId);
|
|
35
|
+
if (!parent) {
|
|
36
|
+
return findNode(root, nodeId)
|
|
37
|
+
? err({ code: "root-not-detachable", nodeId })
|
|
38
|
+
: err({ code: "node-not-found", nodeId });
|
|
39
|
+
}
|
|
40
|
+
return ok({ op: "move", nodeId, parentId: parent.id, index: indexOfChild(parent, nodeId) });
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Restores whatever the touched keys held before: keys that existed are set
|
|
44
|
+
* back to their prior values, keys that did not are unset again.
|
|
45
|
+
*/
|
|
46
|
+
const invertConfigure = (root, nodeId, set, unset) => {
|
|
47
|
+
const node = findNode(root, nodeId);
|
|
48
|
+
if (!node)
|
|
49
|
+
return err({ code: "node-not-found", nodeId });
|
|
50
|
+
const previous = configurationOf(node);
|
|
51
|
+
const touchedKeys = new Set([...Object.keys(set), ...unset]);
|
|
52
|
+
const restored = {};
|
|
53
|
+
const cleared = [];
|
|
54
|
+
for (const key of touchedKeys) {
|
|
55
|
+
const priorValue = previous[key];
|
|
56
|
+
if (priorValue === undefined)
|
|
57
|
+
cleared.push(key);
|
|
58
|
+
else
|
|
59
|
+
restored[key] = priorValue;
|
|
60
|
+
}
|
|
61
|
+
return ok({ op: "configure", nodeId, set: restored, unset: cleared });
|
|
62
|
+
};
|
|
63
|
+
export const invertOperation = (root, operation) => {
|
|
64
|
+
switch (operation.op) {
|
|
65
|
+
case "insert":
|
|
66
|
+
return ok({ op: "remove", nodeId: operation.node.id });
|
|
67
|
+
case "remove":
|
|
68
|
+
return invertRemove(root, operation.nodeId);
|
|
69
|
+
case "move":
|
|
70
|
+
return invertMove(root, operation.nodeId);
|
|
71
|
+
case "configure":
|
|
72
|
+
return invertConfigure(root, operation.nodeId, operation.set, operation.unset);
|
|
73
|
+
default:
|
|
74
|
+
return assertNever(operation, "invertOperation");
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* The operations that undo `delta`, without deciding which revision they apply
|
|
79
|
+
* to. Fails for the same reasons applying `delta` would fail, so a successful
|
|
80
|
+
* inversion also proves the original delta is applicable.
|
|
81
|
+
*
|
|
82
|
+
* Separate from `invertDelta` because an undo is not always applied where it
|
|
83
|
+
* was computed: reverting a revision from the log inverts against the tree that
|
|
84
|
+
* revision observed, then applies the result at head. Only the operations
|
|
85
|
+
* survive that journey — the id and the base revision belong to whichever delta
|
|
86
|
+
* eventually carries them.
|
|
87
|
+
*/
|
|
88
|
+
export const invertOperations = (tree, delta) => {
|
|
89
|
+
let state = tree.root;
|
|
90
|
+
const inverted = [];
|
|
91
|
+
for (const operation of delta.operations) {
|
|
92
|
+
const inverse = invertOperation(state, operation);
|
|
93
|
+
if (!inverse.ok)
|
|
94
|
+
return inverse;
|
|
95
|
+
const advanced = applyOperation(state, operation);
|
|
96
|
+
if (!advanced.ok)
|
|
97
|
+
return advanced;
|
|
98
|
+
inverted.unshift(inverse.value);
|
|
99
|
+
state = advanced.value;
|
|
100
|
+
}
|
|
101
|
+
return ok(inverted);
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* The delta that undoes `delta`, targeting the revision `delta` produces.
|
|
105
|
+
*/
|
|
106
|
+
export const invertDelta = (tree, delta, inverseDeltaId) => mapResult(invertOperations(tree, delta), (operations) => ({
|
|
107
|
+
deltaId: inverseDeltaId,
|
|
108
|
+
treeId: tree.treeId,
|
|
109
|
+
baseRevision: tree.revision + 1,
|
|
110
|
+
operations,
|
|
111
|
+
}));
|
|
112
|
+
//# sourceMappingURL=inverse.js.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { NodeId } from "../ids.js";
|
|
2
|
+
import { type Result } from "../result.js";
|
|
3
|
+
import type { TreeError } from "./errors.js";
|
|
4
|
+
import { type LoomNode } from "./node.js";
|
|
5
|
+
/**
|
|
6
|
+
* Structural edits on an immutable tree. Each returns a new root; nodes off the
|
|
7
|
+
* edited path keep their identity, so a renderer can rely on reference equality
|
|
8
|
+
* to skip untouched subtrees.
|
|
9
|
+
*/
|
|
10
|
+
export type NodeTransform = (node: LoomNode) => Result<LoomNode, TreeError>;
|
|
11
|
+
export declare const replaceNode: (root: LoomNode, nodeId: NodeId, transform: NodeTransform) => Result<LoomNode, TreeError>;
|
|
12
|
+
export declare const insertChild: (root: LoomNode, parentId: NodeId, index: number, node: LoomNode) => Result<LoomNode, TreeError>;
|
|
13
|
+
export type DetachedTree = {
|
|
14
|
+
readonly root: LoomNode;
|
|
15
|
+
readonly detached: LoomNode;
|
|
16
|
+
};
|
|
17
|
+
export declare const detachNode: (root: LoomNode, nodeId: NodeId) => Result<DetachedTree, TreeError>;
|
|
18
|
+
//# sourceMappingURL=mutation.d.ts.map
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { err, mapResult, ok, reduceResult } from "../result.js";
|
|
2
|
+
import { containsNode, findNode, findParent } from "./navigation.js";
|
|
3
|
+
import { isContainerNode, withChildren, } from "./node.js";
|
|
4
|
+
const requireContainer = (node) => isContainerNode(node)
|
|
5
|
+
? ok(node)
|
|
6
|
+
: err({ code: "not-a-container", nodeId: node.id, nodeKind: node.kind });
|
|
7
|
+
export const replaceNode = (root, nodeId, transform) => {
|
|
8
|
+
if (!containsNode(root, nodeId))
|
|
9
|
+
return err({ code: "node-not-found", nodeId });
|
|
10
|
+
const rebuild = (node) => {
|
|
11
|
+
if (node.id === nodeId)
|
|
12
|
+
return transform(node);
|
|
13
|
+
if (!isContainerNode(node) || !containsNode(node, nodeId))
|
|
14
|
+
return ok(node);
|
|
15
|
+
const rebuiltChildren = reduceResult(node.children, [], (accumulated, child) => mapResult(rebuild(child), (next) => [...accumulated, next]));
|
|
16
|
+
return mapResult(rebuiltChildren, (children) => withChildren(node, children));
|
|
17
|
+
};
|
|
18
|
+
return rebuild(root);
|
|
19
|
+
};
|
|
20
|
+
export const insertChild = (root, parentId, index, node) => {
|
|
21
|
+
const parent = findNode(root, parentId);
|
|
22
|
+
if (!parent)
|
|
23
|
+
return err({ code: "node-not-found", nodeId: parentId });
|
|
24
|
+
const container = requireContainer(parent);
|
|
25
|
+
if (!container.ok)
|
|
26
|
+
return container;
|
|
27
|
+
const childCount = container.value.children.length;
|
|
28
|
+
if (!Number.isInteger(index) || index < 0 || index > childCount) {
|
|
29
|
+
return err({ code: "index-out-of-range", parentId, index, childCount });
|
|
30
|
+
}
|
|
31
|
+
return replaceNode(root, parentId, (current) => mapResult(requireContainer(current), (target) => withChildren(target, [
|
|
32
|
+
...target.children.slice(0, index),
|
|
33
|
+
node,
|
|
34
|
+
...target.children.slice(index),
|
|
35
|
+
])));
|
|
36
|
+
};
|
|
37
|
+
export const detachNode = (root, nodeId) => {
|
|
38
|
+
if (root.id === nodeId)
|
|
39
|
+
return err({ code: "root-not-detachable", nodeId });
|
|
40
|
+
const detached = findNode(root, nodeId);
|
|
41
|
+
if (!detached)
|
|
42
|
+
return err({ code: "node-not-found", nodeId });
|
|
43
|
+
const parent = findParent(root, nodeId);
|
|
44
|
+
if (!parent)
|
|
45
|
+
return err({ code: "node-not-found", nodeId });
|
|
46
|
+
const pruned = replaceNode(root, parent.id, (current) => mapResult(requireContainer(current), (target) => withChildren(target, target.children.filter((child) => child.id !== nodeId))));
|
|
47
|
+
return mapResult(pruned, (nextRoot) => ({ root: nextRoot, detached }));
|
|
48
|
+
};
|
|
49
|
+
//# sourceMappingURL=mutation.js.map
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { assertNever } from "../result.js";
|
|
2
|
+
import { collectNodeIds } from "./navigation.js";
|
|
3
|
+
/**
|
|
4
|
+
* Which nodes a set of operations names, read from the operations alone.
|
|
5
|
+
*
|
|
6
|
+
* `ChangeAnalysis` already answers a richer version of this question, but it
|
|
7
|
+
* needs the tree the delta applied to. Two deltas from different points in a
|
|
8
|
+
* log cannot both be analysed against one tree, so asking whether they overlap
|
|
9
|
+
* — which is what a revert has to know — needs an answer that depends on
|
|
10
|
+
* nothing but the operations themselves.
|
|
11
|
+
*
|
|
12
|
+
* It is deliberately a *names* relation rather than an *affects* one. A `remove`
|
|
13
|
+
* names one id and destroys a whole subtree; the subtree's ids are not in the
|
|
14
|
+
* operation, so they are not reported. That under-reports, and it is the safe
|
|
15
|
+
* direction: an operation that later depends on a node something else removed
|
|
16
|
+
* fails in `applyDelta` rather than passing quietly. Over-reporting is the
|
|
17
|
+
* direction that would be silent, so `insert` and `move` name their parent as
|
|
18
|
+
* well as their subject.
|
|
19
|
+
*/
|
|
20
|
+
const namedByOperation = (operation) => {
|
|
21
|
+
switch (operation.op) {
|
|
22
|
+
case "insert":
|
|
23
|
+
return [operation.parentId, ...collectNodeIds(operation.node)];
|
|
24
|
+
case "remove":
|
|
25
|
+
return [operation.nodeId];
|
|
26
|
+
case "move":
|
|
27
|
+
return [operation.nodeId, operation.parentId];
|
|
28
|
+
case "configure":
|
|
29
|
+
return [operation.nodeId];
|
|
30
|
+
default:
|
|
31
|
+
return assertNever(operation, "namedByOperation");
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
export const namedNodeIds = (operations) => Array.from(new Set(operations.flatMap(namedByOperation)));
|
|
35
|
+
//# sourceMappingURL=naming.js.map
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { NodeId } from "../ids.js";
|
|
2
|
+
import { type LoomNode } from "./node.js";
|
|
3
|
+
/**
|
|
4
|
+
* Read-only traversal. Every function here takes a root and derives a view;
|
|
5
|
+
* nothing caches, because a cache would be a second source of truth about a
|
|
6
|
+
* structure that changes on every accepted delta.
|
|
7
|
+
*/
|
|
8
|
+
export declare function walkTree(root: LoomNode): Generator<LoomNode>;
|
|
9
|
+
export declare const collectNodeIds: (root: LoomNode) => readonly NodeId[];
|
|
10
|
+
export declare const findNode: (root: LoomNode, nodeId: NodeId) => LoomNode | null;
|
|
11
|
+
export declare const containsNode: (root: LoomNode, nodeId: NodeId) => boolean;
|
|
12
|
+
export declare const findParent: (root: LoomNode, nodeId: NodeId) => LoomNode | null;
|
|
13
|
+
/**
|
|
14
|
+
* The chain of nodes from the root down to and including `nodeId`, or null when
|
|
15
|
+
* the node is absent. Paths are derived on demand and never persisted — the id
|
|
16
|
+
* is the stable address, the path is the current position.
|
|
17
|
+
*/
|
|
18
|
+
export declare const nodePath: (root: LoomNode, nodeId: NodeId) => readonly LoomNode[] | null;
|
|
19
|
+
/** The same chain as ids, for callers that only need the address. */
|
|
20
|
+
export declare const pathToNode: (root: LoomNode, nodeId: NodeId) => readonly NodeId[] | null;
|
|
21
|
+
export declare const isDescendantOf: (root: LoomNode, ancestorId: NodeId, nodeId: NodeId) => boolean;
|
|
22
|
+
/**
|
|
23
|
+
* What a node says: every text node under it, in tree order, concatenated.
|
|
24
|
+
*
|
|
25
|
+
* Adjacent text nodes join with nothing between them because that is how they
|
|
26
|
+
* render — two text children of one element are one run of text to a reader,
|
|
27
|
+
* and a separator here would put something on a reader's clipboard that is not
|
|
28
|
+
* on their screen.
|
|
29
|
+
*
|
|
30
|
+
* It reads the **tree**, never the rendered markup. A behaviour that acts on a
|
|
31
|
+
* node's text (`render/behaviour.ts`) is then right before the browser has laid
|
|
32
|
+
* anything out, and cannot pick up a label, a caption or a control that a
|
|
33
|
+
* primitive rendered beside the content rather than as it.
|
|
34
|
+
*/
|
|
35
|
+
export declare const textOf: (root: LoomNode) => string;
|
|
36
|
+
/** Ids that appear more than once anywhere under `root`. */
|
|
37
|
+
export declare const duplicateNodeIds: (root: LoomNode) => readonly NodeId[];
|
|
38
|
+
//# sourceMappingURL=navigation.d.ts.map
|