@lionweb/core 0.7.0-beta.18 → 0.7.0-beta.19

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/src/writing.ts ADDED
@@ -0,0 +1,79 @@
1
+ import { LionWebId, LionWebKey } from "@lionweb/json"
2
+
3
+ import { Classifier, EnumerationLiteral, Feature, Link } from "./m3/types.js"
4
+ import { Node } from "./types.js"
5
+
6
+
7
+ /**
8
+ * An interface that's used to parametrize generic deserialization of serialization chunks to
9
+ * (in-memory) nodes of the given type (parameter).
10
+ * Implementations of these interfaces {w|c}ould be:
11
+ * - specific to LionCore (so to match m3/types.ts)
12
+ * - generic to deserialize into {@link DynamicNode dynamic nodes}
13
+ */
14
+ export interface Writer<NT extends Node, PNT extends Node = NT> {
15
+
16
+ /**
17
+ * @return An instance of the given concept, also given its parent (or {@link undefined} for root nodes),
18
+ * its ID and the values of the node's properties ("settings").
19
+ * (The latter may be required as arguments for the constructor of a class, whose instances represent nodes.)
20
+ */
21
+ nodeFor: (parent: PNT | undefined, classifier: Classifier, id: LionWebId, propertySettings: { [propertyKey: LionWebKey]: unknown }) => NT
22
+ // TODO this prohibits multiple properties with the same key but different language => use a variant of LionWebJsonProperty[] with the value already deserialized
23
+
24
+ /**
25
+ * Sets the *single* given value of the indicated {@link Feature} on the given node.
26
+ * This means adding it in case the feature is multi-valued, meaning it is a {@link Link} with {@code multiple = true}.
27
+ */
28
+ setFeatureValue: (node: NT, feature: Feature, value: unknown) => void
29
+ // TODO split to setPropertyValue, &c.?
30
+
31
+ /**
32
+ * @return The runtime encoding of the given {@link EnumerationLiteral}.
33
+ */
34
+ encodingOf: (literal: EnumerationLiteral) => unknown
35
+
36
+ }
37
+
38
+ /**
39
+ * Alias for {@link Writer}, kept for backward compatibility, and to be deprecated and removed later.
40
+ */
41
+ export interface InstantiationFacade<NT extends Node, PNT extends Node = NT> extends Writer<NT, PNT> {}
42
+
43
+
44
+ /**
45
+ * Type def. for functions that update features’ values on a settings object.
46
+ */
47
+ export type SettingsUpdater = (settings: Record<string, unknown>, feature: Feature, value: unknown) => void
48
+
49
+ /**
50
+ * @return a {@link SettingsUpdater} that uses the given “meta key” – which is a property/key on the {@link Feature} type –
51
+ * to look up what key to look a feature’s value up on a settings object.
52
+ * <em>Note:</em> for internal use only — use with some care!
53
+ */
54
+ const settingsUpdater = (metaKey: keyof Feature): SettingsUpdater =>
55
+ (settings: Record<string, unknown>, feature: Feature, value: unknown): void => {
56
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
57
+ const key = (feature as any)[metaKey] as string
58
+ if (feature instanceof Link && feature.multiple) {
59
+ if (!Array.isArray(settings[key])) {
60
+ settings[key] = []
61
+ }
62
+ (settings[key] as unknown[]).push(value)
63
+ } else {
64
+ settings[key] = value
65
+ }
66
+ }
67
+
68
+ /**
69
+ * Updates the value of the given {@link Feature feature} on the given "settings" object
70
+ * (either a {@link Node node} or a sub object of it), using the feature's *name*.
71
+ */
72
+ export const updateSettingsNameBased: SettingsUpdater = settingsUpdater("name")
73
+
74
+ /**
75
+ * Updates the value of the given {@link Feature feature} on the given "settings" object
76
+ * (either a {@link Node node} or a sub object of it), using the feature's *key*.
77
+ */
78
+ export const updateSettingsKeyBased = settingsUpdater("key")
79
+