@zcomponent/core 0.0.8 → 0.0.9

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.
Files changed (47) hide show
  1. package/lib/actionbehavior.d.ts +6 -11
  2. package/lib/actionbehavior.js +11 -19
  3. package/lib/animation.d.ts +9 -9
  4. package/lib/behavior.d.ts +23 -12
  5. package/lib/behavior.js +27 -3
  6. package/lib/behaviors/PlaySound.d.ts +2 -1
  7. package/lib/behaviors/PlaySound.js +2 -2
  8. package/lib/component.d.ts +26 -17
  9. package/lib/component.js +45 -20
  10. package/lib/components/Children.d.ts +8 -0
  11. package/lib/components/Children.js +11 -0
  12. package/lib/components/DefaultLoader.d.ts +2 -2
  13. package/lib/components/DefaultLoader.js +19 -19
  14. package/lib/components/Gamepad.d.ts +68 -0
  15. package/lib/components/Gamepad.js +131 -0
  16. package/lib/components/LongLoad.d.ts +3 -3
  17. package/lib/components/LongLoad.js +2 -2
  18. package/lib/context.d.ts +39 -23
  19. package/lib/context.js +135 -46
  20. package/lib/contexts/analyticscontext.d.ts +3 -4
  21. package/lib/contexts/analyticscontext.js +10 -10
  22. package/lib/contexts/canvascontext.d.ts +12 -9
  23. package/lib/contexts/canvascontext.js +56 -51
  24. package/lib/contexts/cookieconsentcontext.d.ts +9 -10
  25. package/lib/contexts/cookieconsentcontext.js +19 -23
  26. package/lib/contexts/environmentcontext.d.ts +4 -5
  27. package/lib/contexts/environmentcontext.js +8 -7
  28. package/lib/contexts/gamepadcontext.d.ts +40 -0
  29. package/lib/contexts/gamepadcontext.js +99 -0
  30. package/lib/contexts/loadcontext.d.ts +15 -10
  31. package/lib/contexts/loadcontext.js +48 -51
  32. package/lib/contexts/tagcontext.d.ts +9 -10
  33. package/lib/contexts/tagcontext.js +27 -35
  34. package/lib/contexts/usereventcontext.d.ts +5 -5
  35. package/lib/contexts/usereventcontext.js +14 -19
  36. package/lib/index.d.ts +1 -0
  37. package/lib/index.js +1 -0
  38. package/lib/interfaces.d.ts +9 -9
  39. package/lib/observable.d.ts +52 -8
  40. package/lib/observable.js +37 -0
  41. package/lib/selectors.js +4 -4
  42. package/lib/types.d.ts +3 -3
  43. package/lib/zcomponent.d.ts +13 -12
  44. package/lib/zcomponent.js +31 -38
  45. package/lib/zcomponentconstruction.d.ts +3 -0
  46. package/lib/zcomponentconstruction.js +7 -0
  47. package/package.json +2 -2
@@ -1,5 +1,37 @@
1
- declare type Resolved<T, M> = [T] extends [never] ? M : T;
2
- export declare class Observable<T = never, M extends any[] | [] | never = never> {
1
+ type Resolved<T, M> = [T] extends [never] ? M : T;
2
+ /**
3
+ * A class that holds a value and watches it for changes (deeply by default).
4
+ *
5
+ * An Observable can be constructed with a default value like this:
6
+ *
7
+ * ```ts
8
+ * const myVariable = new Observable(false);
9
+ * ```
10
+ *
11
+ * Observable will automatically infer the type from the default value. You may also specify the type using the first template parameter:
12
+ *
13
+ * ```ts
14
+ * const myVariable = new Observable<boolean|string>(false);
15
+ * ```
16
+ *
17
+ * The value of the Observable may be accessed and changed using the `value` parameter:
18
+ * ```ts
19
+ * console.log(myVariable.value);
20
+ *
21
+ * myVariable.value = 4;
22
+ * ```
23
+ *
24
+ * Setting `value` to `undefined` restores the value to the default specified in the constructor.
25
+ *
26
+ * A function can be registered to receive changes to the value (or any of its recursive descendent keys or elements):
27
+ * ```ts
28
+ * const myVariable = new Observable(false, newValue => console.log(newValue));
29
+ * ```
30
+ *
31
+ * @typeParam Type - The type of the contained value
32
+ * @typeParam TypeInternal - An internal type used to improve automatic type detection
33
+ */
34
+ export declare class Observable<Type = never, TypeInternal extends any[] | [] | never = never> {
3
35
  private _default;
4
36
  private _deep;
5
37
  private _proxies;
@@ -7,13 +39,25 @@ export declare class Observable<T = never, M extends any[] | [] | never = never>
7
39
  private _nextToken;
8
40
  private _handlersByToken;
9
41
  private _tokenByHandler;
10
- constructor(def: M, withHandler?: ((v: M) => void), deep?: boolean);
11
- constructor(def: T, withHandler?: ((v: T) => void), deep?: boolean);
12
- get value(): Resolved<T, M>;
42
+ /**
43
+ * Constructs a new Observable
44
+ *
45
+ * @param def - The default (and initial) value of the Observable.
46
+ * @param withHandler - A function that will be called when the value (or any of its recursive descendent keys or elements) is changed
47
+ * @param deep - Set to false to only track changes to the top level `value` itself (and not any of its descendent keys or elements)
48
+ */
49
+ constructor(def: TypeInternal, withHandler?: ((v: TypeInternal) => void), deep?: boolean);
50
+ constructor(def: Type, withHandler?: ((v: Type) => void), deep?: boolean);
51
+ /**
52
+ * Access the current value for this Observable. Changing this value (or, unless deep inspection is disabled, its recursive elements and keys) will trigger any registered handlers to be called with the new value.
53
+ *
54
+ * Setting `value` to `undefined` will restore the value to the default specified in the constructor.
55
+ */
56
+ get value(): Resolved<Type, TypeInternal>;
13
57
  private _wrap;
14
- set value(v: Resolved<T, M> | undefined);
15
- withValue(fn: (v: Resolved<T, M>) => void): number;
16
- removeWithValue(fnOrToken: number | ((v: Resolved<T, M>) => void)): void;
58
+ set value(v: Resolved<Type, TypeInternal> | undefined);
59
+ withValue(fn: (v: Resolved<Type, TypeInternal>) => void): number;
60
+ removeWithValue(fnOrToken: number | ((v: Resolved<Type, TypeInternal>) => void)): void;
17
61
  private _emitValue;
18
62
  }
19
63
  export {};
package/lib/observable.js CHANGED
@@ -1,3 +1,35 @@
1
+ /**
2
+ * A class that holds a value and watches it for changes (deeply by default).
3
+ *
4
+ * An Observable can be constructed with a default value like this:
5
+ *
6
+ * ```ts
7
+ * const myVariable = new Observable(false);
8
+ * ```
9
+ *
10
+ * Observable will automatically infer the type from the default value. You may also specify the type using the first template parameter:
11
+ *
12
+ * ```ts
13
+ * const myVariable = new Observable<boolean|string>(false);
14
+ * ```
15
+ *
16
+ * The value of the Observable may be accessed and changed using the `value` parameter:
17
+ * ```ts
18
+ * console.log(myVariable.value);
19
+ *
20
+ * myVariable.value = 4;
21
+ * ```
22
+ *
23
+ * Setting `value` to `undefined` restores the value to the default specified in the constructor.
24
+ *
25
+ * A function can be registered to receive changes to the value (or any of its recursive descendent keys or elements):
26
+ * ```ts
27
+ * const myVariable = new Observable(false, newValue => console.log(newValue));
28
+ * ```
29
+ *
30
+ * @typeParam Type - The type of the contained value
31
+ * @typeParam TypeInternal - An internal type used to improve automatic type detection
32
+ */
1
33
  export class Observable {
2
34
  constructor(_default, withHandler, _deep = true) {
3
35
  this._default = _default;
@@ -19,6 +51,11 @@ export class Observable {
19
51
  if (withHandler)
20
52
  this.withValue(withHandler);
21
53
  }
54
+ /**
55
+ * Access the current value for this Observable. Changing this value (or, unless deep inspection is disabled, its recursive elements and keys) will trigger any registered handlers to be called with the new value.
56
+ *
57
+ * Setting `value` to `undefined` will restore the value to the default specified in the constructor.
58
+ */
22
59
  get value() {
23
60
  return this._value.proxy;
24
61
  }
package/lib/selectors.js CHANGED
@@ -139,9 +139,12 @@ export const typeDefinitionForComponent = (nodes, props, scriptNames, url) => {
139
139
 
140
140
  ${importStrings.join('\n')}
141
141
 
142
+ /**
143
+ * @zcomponent
144
+ */
142
145
  declare class Comp extends ZComponent {
143
146
 
144
- constructor(constructorProps: {}, ctx: ContextManager);
147
+ constructor(contextManager: ContextManager, constructorProps: {});
145
148
 
146
149
  nodes: {
147
150
  ${Object.entries(scriptNames).map(entry => {
@@ -158,9 +161,6 @@ ${Object.entries(scriptNames).map(entry => {
158
161
  ${Object.values(props).map(typeOutputForProp).join('\n')}
159
162
  }
160
163
 
161
- /**
162
- * @zcomponent
163
- */
164
164
  export default Comp;
165
165
  `;
166
166
  };
package/lib/types.d.ts CHANGED
@@ -48,7 +48,7 @@ export interface FunctionType extends BaseType {
48
48
  export interface EventType extends BaseType {
49
49
  name: 'event';
50
50
  }
51
- export declare type Type = StringPrimitiveType | NumberPrimitiveType | BooleanPrimitiveType | ArrayType | TupleType | UnknownType | EnumType | UnionType | LiteralType | FunctionType | EventType;
51
+ export type Type = StringPrimitiveType | NumberPrimitiveType | BooleanPrimitiveType | ArrayType | TupleType | UnknownType | EnumType | UnionType | LiteralType | FunctionType | EventType;
52
52
  export declare enum TypeHint {
53
53
  "proportion" = "proportion",
54
54
  'color-norm-rgb' = "color-norm-rgb",
@@ -86,10 +86,10 @@ export interface ComponentInfo {
86
86
  allowedChildren: string[];
87
87
  allowedParents?: string[];
88
88
  }
89
- export declare type SourceFileTypeInfo = {
89
+ export type SourceFileTypeInfo = {
90
90
  [id: string]: ComponentInfo;
91
91
  };
92
- export declare type TypeInfoByFileName = {
92
+ export type TypeInfoByFileName = {
93
93
  [id: string]: SourceFileTypeInfo;
94
94
  };
95
95
  export declare function areTypesCompatible(a: Type, b: Type): boolean;
@@ -1,5 +1,5 @@
1
- import { Component, ComponentConstructor, ConstructorProps } from './component';
2
- import { ContextManager } from './context';
1
+ import { Component, ComponentChildren, ConstructorProps } from './component';
2
+ import { ContextManager, Context } from './context';
3
3
  import { ZComponentData } from './interfaces';
4
4
  export interface ZComponentOptions {
5
5
  data: ZComponentData;
@@ -18,16 +18,20 @@ export interface ZComponentOptions {
18
18
  };
19
19
  onConstructingNode?: (id: string | undefined) => void;
20
20
  }
21
- export interface ZComponentContext {
21
+ export interface ZComponentContextConstructorProps {
22
22
  zcomponent: ZComponent;
23
+ children: ComponentChildren;
23
24
  }
24
- export declare const ZComponentContext: import("./context").ContextTypeWithDefault<ZComponentContext, [zcomponent: ZComponent<any>]>;
25
- export declare function useZComponentInstance<T extends ZComponent>(ctx: ContextManager, comp?: ComponentConstructor<T>): T;
26
- export declare function useConstructed(ctx: ContextManager): Promise<void>;
27
- export declare class ZComponent<RootType = any> extends Component<ConstructorProps, RootType> {
25
+ export declare class ZComponentContext extends Context {
26
+ zcomponent: ZComponent;
27
+ children: ComponentChildren;
28
+ constructor(contextManager: ContextManager, props: ZComponentContextConstructorProps);
29
+ }
30
+ export declare class ZComponent<RootType = any> extends Component<RootType> {
31
+ constructorProps: ConstructorProps;
28
32
  private _opts;
29
33
  id: string;
30
- idByRoot: Map<any, string>;
34
+ idByElement: Map<any, string>;
31
35
  nodes: {
32
36
  [id: string]: Component | Component[];
33
37
  };
@@ -36,11 +40,8 @@ export declare class ZComponent<RootType = any> extends Component<ConstructorPro
36
40
  constructed: Promise<void>;
37
41
  isConstructed: boolean;
38
42
  private _nodesById;
39
- private _rootComponent;
40
43
  private _behaviorsToInitialize;
41
- constructor(constructorProps: {
42
- [id: string]: any;
43
- }, contextManager: ContextManager, _opts: ZComponentOptions);
44
+ constructor(contextManager: ContextManager, constructorProps: ConstructorProps, _opts: ZComponentOptions);
44
45
  private _constructorForNode;
45
46
  private _constructorForBehavior;
46
47
  private _inflateBehaviors;
package/lib/zcomponent.js CHANGED
@@ -1,36 +1,36 @@
1
1
  import { shouldBehaviorRunAtDesignTime } from './behavior';
2
2
  import { Component } from './component';
3
- import { defineContextType } from './context';
3
+ import { Context } from './context';
4
4
  import { isDesignTime } from './contexts/environmentcontext';
5
+ import { TagContext } from './contexts/tagcontext';
5
6
  import { Observable } from './observable';
6
- export const ZComponentContext = defineContextType((ctx, zcomponent) => {
7
- return { zcomponent };
8
- });
9
- export function useZComponentInstance(ctx, comp) {
10
- return ctx.getOrThrow(ZComponentContext)?.zcomponent;
11
- }
12
- export function useConstructed(ctx) {
13
- return ctx.getOrThrow(ZComponentContext)?.zcomponent.constructed;
7
+ import { setCurrentZComponentConstruction } from './zcomponentconstruction';
8
+ export class ZComponentContext extends Context {
9
+ constructor(contextManager, props) {
10
+ super(contextManager, props);
11
+ this.zcomponent = props.zcomponent;
12
+ this.children = props.children;
13
+ }
14
14
  }
15
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
+ constructor(contextManager, constructorProps, _opts) {
17
+ super(contextManager, { ...constructorProps, children: [] });
18
+ this.constructorProps = constructorProps;
19
19
  this._opts = _opts;
20
- this.idByRoot = new Map();
20
+ this.idByElement = new Map();
21
21
  this.nodes = {};
22
22
  this.entityByID = new Map();
23
23
  this.constructed = new Promise(resolve => this._constructedResolve = resolve);
24
24
  this.isConstructed = false;
25
25
  this._nodesById = new Map();
26
26
  this._behaviorsToInitialize = [];
27
- contextManager.getOrThrow(ZComponentContext).zcomponent = this;
27
+ const contextManagerForChildren = contextManager.forkMultiple([ZComponentContext, { zcomponent: this }], [TagContext, {}])[0];
28
28
  this.id = this._opts.data.id;
29
29
  const rootConstructor = this._constructorForNode(this._opts.data.root);
30
30
  if (rootConstructor) {
31
- this._constructChildren([
31
+ this.constructChildren([
32
32
  [rootConstructor, {}]
33
- ]);
33
+ ], contextManagerForChildren);
34
34
  }
35
35
  this._inflateBehaviors();
36
36
  this._initializeComponentProps();
@@ -50,7 +50,7 @@ export class ZComponent extends Component {
50
50
  }
51
51
  const that = this;
52
52
  return class extends construct {
53
- constructor(constructorProps, contextManager) {
53
+ constructor(contextManager, constructorProps) {
54
54
  that._opts.onConstructingNode?.(nodeId);
55
55
  const children = constructorProps.children ?? [];
56
56
  for (const child of node?.children ?? []) {
@@ -62,17 +62,15 @@ export class ZComponent extends Component {
62
62
  ...constructorProps,
63
63
  ...(that._opts.data.entityConstructorProps?.[nodeId] ?? {}),
64
64
  ...(that._opts.constructorPropReplacement?.[nodeId] ?? {}),
65
- children,
65
+ children
66
66
  };
67
- super(constructorProps, contextManager);
67
+ setCurrentZComponentConstruction(that);
68
+ super(contextManager, constructorProps);
68
69
  that.entityByID.set(nodeId, this);
69
70
  that._nodesById.set(nodeId, this);
70
- if (nodeId === that._opts.data.root) {
71
- that._rootComponent = this;
72
- that.root = this.root;
71
+ for (const element of this.elementsResolved) {
72
+ that.idByElement.set(element, nodeId);
73
73
  }
74
- if (this.root)
75
- that.idByRoot.set(this.root, nodeId);
76
74
  if (node?.scriptName) {
77
75
  const entriesForScriptName = that._opts.data.entitiesByScriptName?.[node.scriptName] ?? {};
78
76
  let moreThanOne = false;
@@ -91,7 +89,7 @@ export class ZComponent extends Component {
91
89
  that.nodes[node.scriptName] = this;
92
90
  }
93
91
  }
94
- that._behaviorsToInitialize.push([nodeId, this, contextManager]);
92
+ that._behaviorsToInitialize.push([nodeId, this, this.contextManager]);
95
93
  const props = {
96
94
  ...(that._opts.data.entityProps?.[nodeId] ?? {}),
97
95
  ...(that._opts.propReplacement?.[nodeId] ?? {}),
@@ -112,8 +110,9 @@ export class ZComponent extends Component {
112
110
  dispose() {
113
111
  that.entityByID.delete(nodeId);
114
112
  that._nodesById.delete(nodeId);
115
- if (this.root)
116
- that.idByRoot.delete(this.root);
113
+ for (const element of this.elementsResolved) {
114
+ that.idByElement.delete(element);
115
+ }
117
116
  return super.dispose();
118
117
  }
119
118
  };
@@ -134,13 +133,14 @@ export class ZComponent extends Component {
134
133
  return;
135
134
  const that = this;
136
135
  return class extends construct {
137
- constructor(constructorProps, contextManager) {
136
+ constructor(contextManager, instance, constructorProps) {
138
137
  constructorProps = {
139
138
  ...constructorProps,
140
139
  ...(that._opts.data.entityConstructorProps?.[behaviorId] ?? {}),
141
140
  ...(that._opts.constructorPropReplacement?.[behaviorId] ?? {}),
142
141
  };
143
- super(constructorProps, contextManager);
142
+ setCurrentZComponentConstruction(that);
143
+ super(contextManager, instance, constructorProps);
144
144
  that.entityByID.set(behaviorId, this);
145
145
  const props = {
146
146
  ...(that._opts.data.entityProps?.[behaviorId] ?? {}),
@@ -157,6 +157,7 @@ export class ZComponent extends Component {
157
157
  }
158
158
  }
159
159
  dispose() {
160
+ that.entityByID.delete(behaviorId);
160
161
  return super.dispose();
161
162
  }
162
163
  };
@@ -170,17 +171,9 @@ export class ZComponent extends Component {
170
171
  _wrapBehaviors(nodeID, impl, ctx) {
171
172
  const behaviors = this._opts.data.behaviorsByNode?.[nodeID] ?? [];
172
173
  for (const behaviorID of behaviors) {
173
- const behavior = this._opts.data.behaviors?.[behaviorID];
174
- if (!behavior)
175
- continue;
176
- const constructorProps = {
177
- ...(this._opts.data.entityConstructorProps?.[behaviorID] ?? {}),
178
- ...(this._opts.constructorPropReplacement?.[behaviorID] ?? {}),
179
- instance: impl
180
- };
181
174
  const constructor = this._constructorForBehavior(behaviorID);
182
175
  if (constructor)
183
- new constructor(constructorProps, ctx);
176
+ new constructor(ctx, impl);
184
177
  }
185
178
  }
186
179
  notifyPropsChanged(entries) {
@@ -0,0 +1,3 @@
1
+ import { ZComponent } from "./zcomponent";
2
+ export declare function getCurrentZComponentConstruction(): ZComponent<any> | undefined;
3
+ export declare function setCurrentZComponentConstruction(z: ZComponent | undefined): void;
@@ -0,0 +1,7 @@
1
+ let currentZComponentConstruction;
2
+ export function getCurrentZComponentConstruction() {
3
+ return currentZComponentConstruction;
4
+ }
5
+ export function setCurrentZComponentConstruction(z) {
6
+ currentZComponentConstruction = z;
7
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zcomponent/core",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -29,7 +29,7 @@
29
29
  "@playwright/test": "^1.25.0",
30
30
  "@types/ua-parser-js": "^0.7.36",
31
31
  "parcel": "^2.8.2",
32
- "typescript": "^4.6.3",
32
+ "typescript": "^4.9.4",
33
33
  "ua-parser-js": "^1.0.2"
34
34
  },
35
35
  "zexports": [