@zcomponent/core 0.0.1 → 0.0.4
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/lib/actionbehavior.d.ts +10 -13
- package/lib/actionbehavior.js +17 -27
- package/lib/behavior.d.ts +22 -25
- package/lib/behavior.js +33 -5
- package/lib/behaviors/LaunchURL.d.ts +18 -7
- package/lib/behaviors/LaunchURL.js +14 -9
- package/lib/behaviors/LogAnalyticsEvent.d.ts +11 -5
- package/lib/behaviors/LogAnalyticsEvent.js +12 -7
- package/lib/behaviors/PlaySound.d.ts +15 -3
- package/lib/behaviors/PlaySound.js +35 -15
- package/lib/component.d.ts +28 -20
- package/lib/component.js +55 -3
- package/lib/components/DefaultLoader.d.ts +23 -16
- package/lib/components/DefaultLoader.js +63 -94
- package/lib/contexts/canvascontext.d.ts +1 -1
- package/lib/contexts/cookieconsentcontext.d.ts +5 -5
- package/lib/contexts/loadcontext.d.ts +4 -4
- package/lib/event.d.ts +1 -1
- package/lib/observable.d.ts +9 -6
- package/lib/observable.js +5 -2
- package/lib/selectors.js +10 -10
- package/lib/types.d.ts +2 -1
- package/lib/types.js +6 -1
- package/lib/zcomponent.d.ts +10 -12
- package/lib/zcomponent.js +135 -116
- package/package.json +1 -1
- package/lib/props.d.ts +0 -0
- package/lib/props.js +0 -4
package/lib/zcomponent.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { shouldBehaviorRunAtDesignTime } from './behavior';
|
|
2
|
+
import { Component } from './component';
|
|
2
3
|
import { defineContextType } from './context';
|
|
3
4
|
import { isDesignTime } from './contexts/environmentcontext';
|
|
4
5
|
import { Observable } from './observable';
|
|
@@ -11,8 +12,10 @@ export function useZComponentInstance(ctx, comp) {
|
|
|
11
12
|
export function useConstructed(ctx) {
|
|
12
13
|
return ctx.getOrThrow(ZComponentContext)?.zcomponent.constructed;
|
|
13
14
|
}
|
|
14
|
-
export class ZComponent {
|
|
15
|
-
constructor(constructorProps,
|
|
15
|
+
export class ZComponent extends Component {
|
|
16
|
+
constructor(constructorProps, contextManager, _opts) {
|
|
17
|
+
contextManager = contextManager.fork(ZComponentContext, undefined)[0];
|
|
18
|
+
super(constructorProps, contextManager, { dontConstructChildren: true });
|
|
16
19
|
this._opts = _opts;
|
|
17
20
|
this.idByRoot = new Map();
|
|
18
21
|
this.nodes = {};
|
|
@@ -21,16 +24,20 @@ export class ZComponent {
|
|
|
21
24
|
this.isConstructed = false;
|
|
22
25
|
this._nodesById = new Map();
|
|
23
26
|
this._behaviorsToInitialize = [];
|
|
27
|
+
contextManager.getOrThrow(ZComponentContext).zcomponent = this;
|
|
24
28
|
this.id = this._opts.data.id;
|
|
25
|
-
this.
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
+
const rootConstructor = this._constructorForNode(this._opts.data.root);
|
|
30
|
+
if (rootConstructor) {
|
|
31
|
+
this._constructChildren([
|
|
32
|
+
[rootConstructor, {}]
|
|
33
|
+
]);
|
|
34
|
+
}
|
|
29
35
|
this._inflateBehaviors();
|
|
36
|
+
this._initializeComponentProps();
|
|
30
37
|
this.isConstructed = true;
|
|
31
38
|
this._constructedResolve();
|
|
32
39
|
}
|
|
33
|
-
|
|
40
|
+
_constructorForNode(nodeId) {
|
|
34
41
|
const node = this._opts.data.nodes[nodeId];
|
|
35
42
|
if (!node) {
|
|
36
43
|
console.log('Warning - unable to find node with ID', nodeId);
|
|
@@ -41,80 +48,118 @@ export class ZComponent {
|
|
|
41
48
|
console.log('Warning - unable to find constructor for type', node.type);
|
|
42
49
|
return;
|
|
43
50
|
}
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
(
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
51
|
+
const that = this;
|
|
52
|
+
return class extends construct {
|
|
53
|
+
constructor(constructorProps, contextManager) {
|
|
54
|
+
that._opts.onConstructingNode?.(nodeId);
|
|
55
|
+
const children = constructorProps.children ?? [];
|
|
56
|
+
for (const child of node?.children ?? []) {
|
|
57
|
+
const childInstance = that._constructorForNode(child.id);
|
|
58
|
+
if (childInstance)
|
|
59
|
+
children.push([childInstance, {}]);
|
|
60
|
+
}
|
|
61
|
+
constructorProps = {
|
|
62
|
+
...constructorProps,
|
|
63
|
+
...(that._opts.data.entityConstructorProps?.[nodeId] ?? {}),
|
|
64
|
+
...(that._opts.constructorPropReplacement?.[nodeId] ?? {}),
|
|
65
|
+
children,
|
|
66
|
+
};
|
|
67
|
+
super(constructorProps, contextManager);
|
|
68
|
+
that.entityByID.set(nodeId, this);
|
|
69
|
+
that._nodesById.set(nodeId, this);
|
|
70
|
+
if (nodeId === that._opts.data.root) {
|
|
71
|
+
that._rootComponent = this;
|
|
72
|
+
that.root = this.root;
|
|
73
|
+
}
|
|
74
|
+
if (this.root)
|
|
75
|
+
that.idByRoot.set(this.root, nodeId);
|
|
76
|
+
if (node?.scriptName) {
|
|
77
|
+
const entriesForScriptName = that._opts.data.entitiesByScriptName?.[node.scriptName] ?? {};
|
|
78
|
+
let moreThanOne = false;
|
|
79
|
+
for (const entry in entriesForScriptName) {
|
|
80
|
+
if (entry !== node.id) {
|
|
81
|
+
moreThanOne = true;
|
|
82
|
+
break;
|
|
56
83
|
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
84
|
+
}
|
|
85
|
+
if (moreThanOne) {
|
|
86
|
+
const current = that.nodes[node.scriptName] ?? {};
|
|
87
|
+
that.nodes[node.scriptName] = current;
|
|
88
|
+
current[node.id] = this;
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
that.nodes[node.scriptName] = this;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
that._behaviorsToInitialize.push([nodeId, this, contextManager]);
|
|
95
|
+
const props = {
|
|
96
|
+
...(that._opts.data.entityProps?.[nodeId] ?? {}),
|
|
97
|
+
...(that._opts.propReplacement?.[nodeId] ?? {}),
|
|
98
|
+
// TOOD add forwarded props
|
|
99
|
+
};
|
|
100
|
+
for (const [key, val] of Object.entries(props)) {
|
|
101
|
+
try {
|
|
102
|
+
that._setEntityProp(this, key, val);
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
console.log('Warning - unable to set entity prop', key, val);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
that._opts.onConstructingNode?.(undefined);
|
|
109
|
+
if (that.isConstructed)
|
|
110
|
+
that._inflateBehaviors();
|
|
111
|
+
}
|
|
112
|
+
dispose() {
|
|
113
|
+
that.entityByID.delete(nodeId);
|
|
114
|
+
that._nodesById.delete(nodeId);
|
|
115
|
+
if (this.root)
|
|
116
|
+
that.idByRoot.delete(this.root);
|
|
117
|
+
return super.dispose();
|
|
118
|
+
}
|
|
68
119
|
};
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
120
|
+
}
|
|
121
|
+
_constructorForBehavior(behaviorId) {
|
|
122
|
+
const node = this._opts.data.behaviors[behaviorId];
|
|
123
|
+
if (!node) {
|
|
124
|
+
console.log('Warning - unable to find behavior with ID', behaviorId);
|
|
125
|
+
return;
|
|
72
126
|
}
|
|
73
|
-
|
|
74
|
-
|
|
127
|
+
const construct = this._opts.importMapping[node.type]?.();
|
|
128
|
+
if (!construct) {
|
|
129
|
+
console.log('Warning - unable to find constructor for type', node.type);
|
|
75
130
|
return;
|
|
76
131
|
}
|
|
77
|
-
this.
|
|
78
|
-
if (
|
|
79
|
-
|
|
80
|
-
this
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
132
|
+
const designTime = isDesignTime(this.contextManager);
|
|
133
|
+
if (designTime && !shouldBehaviorRunAtDesignTime(construct))
|
|
134
|
+
return;
|
|
135
|
+
const that = this;
|
|
136
|
+
return class extends construct {
|
|
137
|
+
constructor(constructorProps, contextManager) {
|
|
138
|
+
constructorProps = {
|
|
139
|
+
...constructorProps,
|
|
140
|
+
...(that._opts.data.entityConstructorProps?.[behaviorId] ?? {}),
|
|
141
|
+
...(that._opts.constructorPropReplacement?.[behaviorId] ?? {}),
|
|
142
|
+
};
|
|
143
|
+
super(constructorProps, contextManager);
|
|
144
|
+
that.entityByID.set(behaviorId, this);
|
|
145
|
+
const props = {
|
|
146
|
+
...(that._opts.data.entityProps?.[behaviorId] ?? {}),
|
|
147
|
+
...(that._opts.propReplacement?.[behaviorId] ?? {}),
|
|
148
|
+
// TOOD add forwarded props
|
|
149
|
+
};
|
|
150
|
+
for (const [key, val] of Object.entries(props)) {
|
|
151
|
+
try {
|
|
152
|
+
that._setEntityProp(this, key, val);
|
|
153
|
+
}
|
|
154
|
+
catch (err) {
|
|
155
|
+
console.log('Warning - unable to set entity prop', key, val);
|
|
156
|
+
}
|
|
89
157
|
}
|
|
90
158
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
this.nodes[node.scriptName] = current;
|
|
94
|
-
current[node.id] = impl;
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
this.nodes[node.scriptName] = impl;
|
|
159
|
+
dispose() {
|
|
160
|
+
return super.dispose();
|
|
98
161
|
}
|
|
99
|
-
}
|
|
100
|
-
if (!impl)
|
|
101
|
-
return impl;
|
|
102
|
-
const props = {
|
|
103
|
-
...(this._opts.data.entityProps?.[nodeId] ?? {}),
|
|
104
|
-
...(this._opts.propReplacement?.[nodeId] ?? {}),
|
|
105
162
|
};
|
|
106
|
-
for (const [key, val] of Object.entries(props)) {
|
|
107
|
-
try {
|
|
108
|
-
this._setEntityProp(impl, key, val);
|
|
109
|
-
}
|
|
110
|
-
catch (err) {
|
|
111
|
-
console.log('Warning - unable to set node prop', key, val);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
this._opts.onConstructingNode?.(undefined);
|
|
115
|
-
if (this.isConstructed)
|
|
116
|
-
this._inflateBehaviors();
|
|
117
|
-
return impl;
|
|
118
163
|
}
|
|
119
164
|
_inflateBehaviors() {
|
|
120
165
|
for (const [nodeID, impl, ctx] of this._behaviorsToInitialize) {
|
|
@@ -124,7 +169,6 @@ export class ZComponent {
|
|
|
124
169
|
}
|
|
125
170
|
_wrapBehaviors(nodeID, impl, ctx) {
|
|
126
171
|
const behaviors = this._opts.data.behaviorsByNode?.[nodeID] ?? [];
|
|
127
|
-
const designTime = isDesignTime(this._contextManager);
|
|
128
172
|
for (const behaviorID of behaviors) {
|
|
129
173
|
const behavior = this._opts.data.behaviors?.[behaviorID];
|
|
130
174
|
if (!behavior)
|
|
@@ -134,29 +178,10 @@ export class ZComponent {
|
|
|
134
178
|
...(this._opts.constructorPropReplacement?.[behaviorID] ?? {}),
|
|
135
179
|
instance: impl
|
|
136
180
|
};
|
|
137
|
-
const constructor = this.
|
|
138
|
-
if (
|
|
139
|
-
|
|
140
|
-
if (designTime && !shouldBehaviorRunAtDesignTime(constructor))
|
|
141
|
-
continue;
|
|
142
|
-
const res = constructor(constructorProps, ctx);
|
|
143
|
-
if (!res)
|
|
144
|
-
continue;
|
|
145
|
-
this.entityByID.set(behaviorID, res);
|
|
146
|
-
const props = {
|
|
147
|
-
...(this._opts.data.entityProps?.[behaviorID] ?? {}),
|
|
148
|
-
...(this._opts.propReplacement?.[behaviorID] ?? {}),
|
|
149
|
-
};
|
|
150
|
-
for (const [key, val] of Object.entries(props)) {
|
|
151
|
-
try {
|
|
152
|
-
this._setEntityProp(res, key, val);
|
|
153
|
-
}
|
|
154
|
-
catch (err) {
|
|
155
|
-
console.log('Warning - unable to set behavior prop', key, val);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
181
|
+
const constructor = this._constructorForBehavior(behaviorID);
|
|
182
|
+
if (constructor)
|
|
183
|
+
new constructor(constructorProps, ctx);
|
|
158
184
|
}
|
|
159
|
-
return impl;
|
|
160
185
|
}
|
|
161
186
|
notifyPropsChanged(entries) {
|
|
162
187
|
for (const [entityID, propSet] of entries.entries()) {
|
|
@@ -176,25 +201,22 @@ export class ZComponent {
|
|
|
176
201
|
_initializeComponentProps() {
|
|
177
202
|
const componentProps = this._opts.data.propEntityOverrides ?? {};
|
|
178
203
|
for (const [prop, entry] of Object.entries(componentProps)) {
|
|
179
|
-
|
|
180
|
-
Object.
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
catch (err) {
|
|
193
|
-
console.log('Warning - unable to set prop', overriddenProp, v);
|
|
194
|
-
}
|
|
204
|
+
const propInfo = this._opts.data.props[prop];
|
|
205
|
+
const entries = Object.entries(entry).map(e => [e[0], Object.keys(e[1])]);
|
|
206
|
+
this[prop] = new Observable(propInfo?.default, v => {
|
|
207
|
+
for (const e of entries) {
|
|
208
|
+
const entity = this.entityByID.get(e[0]);
|
|
209
|
+
if (!entity)
|
|
210
|
+
continue;
|
|
211
|
+
for (const overriddenProp of e[1]) {
|
|
212
|
+
try {
|
|
213
|
+
this._setEntityProp(entity, overriddenProp, v);
|
|
214
|
+
}
|
|
215
|
+
catch (err) {
|
|
216
|
+
console.log('Warning - unable to set prop', overriddenProp, v);
|
|
195
217
|
}
|
|
196
218
|
}
|
|
197
|
-
}
|
|
219
|
+
}
|
|
198
220
|
});
|
|
199
221
|
}
|
|
200
222
|
}
|
|
@@ -207,10 +229,6 @@ export class ZComponent {
|
|
|
207
229
|
_getNodeById(id) {
|
|
208
230
|
return this._nodesById.get(id);
|
|
209
231
|
}
|
|
210
|
-
set _parent(v) {
|
|
211
|
-
if (this._root)
|
|
212
|
-
this._root.parent = v;
|
|
213
|
-
}
|
|
214
232
|
dispose() {
|
|
215
233
|
for (const entity of this.entityByID.values()) {
|
|
216
234
|
if (entity && typeof entity.dispose === 'function') {
|
|
@@ -222,5 +240,6 @@ export class ZComponent {
|
|
|
222
240
|
}
|
|
223
241
|
}
|
|
224
242
|
}
|
|
243
|
+
return super.dispose();
|
|
225
244
|
}
|
|
226
245
|
}
|
package/package.json
CHANGED
package/lib/props.d.ts
DELETED
|
File without changes
|
package/lib/props.js
DELETED