@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/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, ctx, _opts) {
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._initializeComponentProps();
26
- this._contextManager = ctx.fork(ZComponentContext, this)[0];
27
- this._root = this._inflate(this._opts.data.root, {}, this._contextManager);
28
- this.root = this._root?.root;
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
- _inflate(nodeId, additionalConstructorProps, ctx) {
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 children = additionalConstructorProps['children'] ?? [];
45
- for (const child of node.children) {
46
- children.push([
47
- (cp, ctx) => {
48
- const comp = this._inflate(child.id, cp, ctx);
49
- return {
50
- ...comp,
51
- dispose: () => {
52
- this.entityByID.delete(nodeId);
53
- this._nodesById.delete(nodeId);
54
- if (comp && typeof comp.dispose === 'function')
55
- comp.dispose();
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
- let impl;
63
- const constructorProps = {
64
- ...(this._opts.data.entityConstructorProps?.[nodeId] ?? {}),
65
- ...(this._opts.constructorPropReplacement?.[nodeId] ?? {}),
66
- ...additionalConstructorProps,
67
- children,
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
- this._opts.onConstructingNode?.(nodeId);
70
- try {
71
- impl = construct(constructorProps, ctx);
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
- catch (err) {
74
- console.log('Warning - unable to construct node of type', node.type, err);
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._behaviorsToInitialize.push([nodeId, impl, ctx]);
78
- if (impl.root)
79
- this.idByRoot.set(impl.root, nodeId);
80
- this._nodesById.set(nodeId, impl);
81
- this.entityByID.set(nodeId, impl);
82
- if (node.scriptName) {
83
- const entriesForScriptName = this._opts.data.entitiesByScriptName?.[node.scriptName] ?? {};
84
- let moreThanOne = false;
85
- for (const entry in entriesForScriptName) {
86
- if (entry !== node.id) {
87
- moreThanOne = true;
88
- break;
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
- if (moreThanOne) {
92
- const current = this.nodes[node.scriptName] ?? {};
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._opts.importMapping[behavior.type]?.();
138
- if (!constructor)
139
- continue;
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
- let value = undefined;
180
- Object.defineProperty(this, prop, {
181
- get: () => value,
182
- set: v => {
183
- value = v;
184
- for (const [entityID, overriddenProps] of Object.entries(entry)) {
185
- const entity = this.entityByID.get(entityID);
186
- if (!entity)
187
- continue;
188
- for (const overriddenProp of Object.keys(overriddenProps)) {
189
- try {
190
- this._setEntityProp(entity, overriddenProp, v);
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zcomponent/core",
3
- "version": "0.0.1",
3
+ "version": "0.0.4",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/lib/props.d.ts DELETED
File without changes
package/lib/props.js DELETED
@@ -1,4 +0,0 @@
1
- // export function defineProperty<T extends string, V, DefType extends V>(prop: T, fn: (val: V) => void, def: DefType) : { [key in T]: V } {
2
- // const t = {};
3
- // return t as { [key in T]: V };
4
- // }