@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
@@ -0,0 +1,131 @@
1
+ import { Component } from "../component";
2
+ import { useOnBeforeRender } from "../contexts/canvascontext";
3
+ import { isEditTime, EnvironmentContext } from "../contexts/environmentcontext";
4
+ import { GamepadContext } from "../contexts/gamepadcontext";
5
+ import { Event } from "../event";
6
+ import { Observable } from "../observable";
7
+ /**
8
+ * @zcomponent
9
+ */
10
+ export class Gamepad extends Component {
11
+ constructor(contextManager, props) {
12
+ super(contextManager, props);
13
+ /** @zprop */
14
+ this.onButtonDown = new Event();
15
+ /** @zprop */
16
+ this.onButtonUp = new Event();
17
+ /** @zprop */
18
+ this.onButtonSouth = new Event();
19
+ /** @zprop */
20
+ this.onButtonWest = new Event();
21
+ /** @zprop */
22
+ this.onButtonNorth = new Event();
23
+ /** @zprop */
24
+ this.onButtonEast = new Event();
25
+ /** @zprop */
26
+ this.onButtonShoulderLeft = new Event();
27
+ /** @zprop */
28
+ this.onButtonShoulderRight = new Event();
29
+ /** @zprop */
30
+ this.onButtonTriggerLeft = new Event();
31
+ /** @zprop */
32
+ this.onButtonTriggerRight = new Event();
33
+ /** @zprop */
34
+ this.onButtonDLeft = new Event();
35
+ /** @zprop */
36
+ this.onButtonDRight = new Event();
37
+ /** @zprop */
38
+ this.onButtonDUp = new Event();
39
+ /** @zprop */
40
+ this.onButtonDDown = new Event();
41
+ /** @zprop */
42
+ this.onButtonSelect = new Event();
43
+ /** @zprop */
44
+ this.onButtonStart = new Event();
45
+ /** @zprop */
46
+ this.onButtonCenter = new Event();
47
+ /** @zprop */
48
+ this.onButtonStickLeft = new Event();
49
+ /** @zprop */
50
+ this.onButtonStickRight = new Event();
51
+ this._indexMapping = [
52
+ this.onButtonSouth,
53
+ this.onButtonEast,
54
+ this.onButtonWest,
55
+ this.onButtonNorth,
56
+ this.onButtonShoulderLeft,
57
+ this.onButtonShoulderRight,
58
+ this.onButtonTriggerLeft,
59
+ this.onButtonTriggerRight,
60
+ this.onButtonSelect,
61
+ this.onButtonStart,
62
+ this.onButtonStickLeft,
63
+ this.onButtonStickRight,
64
+ this.onButtonDUp,
65
+ this.onButtonDDown,
66
+ this.onButtonDLeft,
67
+ this.onButtonDRight,
68
+ this.onButtonCenter,
69
+ ];
70
+ /**
71
+ * @zprop
72
+ */
73
+ this.controllerIndex = new Observable(undefined);
74
+ /**
75
+ * @zprop
76
+ * @zdefault 0.1
77
+ */
78
+ this.analogPressedThreshold = new Observable(0.1);
79
+ this._downLastFrame = new Map();
80
+ this._updateRegistration = () => {
81
+ if (isEditTime(this.contextManager))
82
+ this.unregister(useOnBeforeRender(this.contextManager), this._update);
83
+ else
84
+ this.register(useOnBeforeRender(this.contextManager), this._update);
85
+ };
86
+ this._update = () => {
87
+ if (!this.enabledResolved.value)
88
+ return;
89
+ const downEvents = [];
90
+ const upEvents = [];
91
+ const context = this.contextManager.get(GamepadContext);
92
+ context.forGamepadsMatchingIndex(this.controllerIndex.value, gamepad => {
93
+ const downLastFrame = this._downLastFrame.get(gamepad.index) ?? new Set();
94
+ this._downLastFrame.set(gamepad.index, downLastFrame);
95
+ for (let i = 0; i < gamepad.buttons.length; i++) {
96
+ const button = gamepad.buttons[i];
97
+ const downThisFrame = button.pressed || button.value > this.analogPressedThreshold.value;
98
+ if (downThisFrame && !downLastFrame.has(i))
99
+ downEvents.push({
100
+ button: i,
101
+ gamepad: gamepad.index,
102
+ pressed: button.pressed,
103
+ touched: button.touched,
104
+ value: button.value
105
+ });
106
+ if (!downThisFrame && downLastFrame.has(i))
107
+ upEvents.push({
108
+ button: i,
109
+ gamepad: gamepad.index,
110
+ pressed: button.pressed,
111
+ touched: button.touched,
112
+ value: button.value
113
+ });
114
+ if (downThisFrame)
115
+ downLastFrame.add(i);
116
+ else
117
+ downLastFrame.delete(i);
118
+ }
119
+ });
120
+ for (const evt of downEvents) {
121
+ this.onButtonDown.emit(evt);
122
+ this._indexMapping[evt.button]?.emit(evt);
123
+ }
124
+ for (const evt of upEvents) {
125
+ this.onButtonUp.emit(evt);
126
+ }
127
+ };
128
+ const environment = this.contextManager.get(EnvironmentContext);
129
+ this.register(environment.editTime, this._updateRegistration);
130
+ }
131
+ }
@@ -7,7 +7,7 @@ export interface LongLoadConstructorProps {
7
7
  * @zgrouppriority 10
8
8
  * @zdefault 10
9
9
  */
10
- numberOfLoads: number;
10
+ numberOfLoads?: number;
11
11
  /**
12
12
  * The shortest time (in milliseconds) that a given load should take
13
13
  * @zprop
@@ -40,6 +40,6 @@ export interface LongLoadConstructorProps {
40
40
  * @zicon settings
41
41
  * @zgroup Advanced
42
42
  */
43
- export declare class LongLoad extends Component {
44
- constructor(props: LongLoadConstructorProps, contextManager: ContextManager);
43
+ export declare class LongLoad extends Component<any, LongLoadConstructorProps> {
44
+ constructor(contextManager: ContextManager, props: LongLoadConstructorProps);
45
45
  }
@@ -7,8 +7,8 @@ import { Component, isDevelopmentBuild, registerLoadable } from "..";
7
7
  * @zgroup Advanced
8
8
  */
9
9
  export class LongLoad extends Component {
10
- constructor(props, contextManager) {
11
- super(props, contextManager);
10
+ constructor(contextManager, props) {
11
+ super(contextManager, props);
12
12
  if (!props.runInProduction && !isDevelopmentBuild(contextManager))
13
13
  return;
14
14
  const minimumLoadTime = props.minimumLoadTime ?? 500;
package/lib/context.d.ts CHANGED
@@ -1,28 +1,44 @@
1
- export interface ContextType<T extends {}> {
2
- key: string;
3
- hasDefault: false;
1
+ export type ContextConstructor<ConstructorProps extends {
2
+ [id: string]: any;
3
+ } = {
4
+ [id: string]: any;
5
+ }> = new (contextManager: ContextManager, props: ConstructorProps) => Context<ConstructorProps>;
6
+ type RequiredKeys<T> = {
7
+ [K in keyof T]-?: {} extends Pick<T, K> ? never : K;
8
+ }[keyof T];
9
+ type ConstructorProps<T> = T extends {
10
+ new (contextManager: ContextManager, props: infer U): any;
11
+ } ? U : never;
12
+ type ConstructorWithOnlyOptional<T> = RequiredKeys<ConstructorProps<T>> extends never ? T : never;
13
+ type ConstructorWithProps<T extends ContextConstructor = ContextConstructor> = [T, ConstructorParameters<T>[1]];
14
+ type InstancesOfConstructorsWithProps<T extends Array<ConstructorWithProps>> = {
15
+ [K in keyof T]: InstanceType<T[K][0]>;
16
+ };
17
+ export declare class Context<ConstructorPropsType extends {
18
+ [id: string]: any;
19
+ } = {}> {
20
+ contextManager: ContextManager;
21
+ constructorProps: ConstructorPropsType;
22
+ disposed: boolean;
23
+ constructor(contextManager: ContextManager, constructorProps: ConstructorPropsType);
24
+ dispose(): never;
25
+ private static callSuperDispose;
4
26
  }
5
- export interface ContextTypeWithDefault<T extends {}, Args extends Array<any> = []> {
6
- key: string;
7
- hasDefault: true;
8
- def: (ctx: ContextManager, ...args: Args) => T;
9
- }
10
- export declare function defineContextType<T extends {}>(): ContextType<T>;
11
- export declare function defineContextType<T extends {}, Args extends Array<any> = []>(def: (ctx: ContextManager, ...args: Args) => T): ContextTypeWithDefault<T, Args>;
12
27
  export declare class ContextManager {
13
28
  private _byKey;
14
29
  private _root;
15
- constructor();
16
- static create(): ContextManager;
17
- static create<T extends {}>(type: ContextType<T>, props: T): ContextManager;
18
- static create<T extends {}, Args extends Array<any>>(type: ContextTypeWithDefault<T, Args>, ...args: Args): ContextManager;
19
- add<T extends {}>(type: ContextType<T>, obj: T): T;
20
- add<T extends {}, Args extends Array<any>>(type: ContextTypeWithDefault<T, Args>, ...args: Args): T;
21
- fork<T extends {}>(type: ContextType<T>, obj: T): [ContextManager, T];
22
- fork<T extends {}, Args extends Array<any>>(type: ContextTypeWithDefault<T, Args>, ...args: Args): [ContextManager, T];
23
- get<T extends {}>(type: ContextType<T>): T | undefined;
24
- get<T extends {}, Args extends Array<any>>(type: ContextTypeWithDefault<T, Args>, ...args: Args): T;
25
- getExisting<T extends {}, Args extends Array<any>>(type: ContextType<T> | ContextTypeWithDefault<T, Args>): T | undefined;
26
- getOrThrow<T extends {}, Args extends Array<any>>(type: ContextType<T> | ContextTypeWithDefault<T, Args>): T;
27
- delete<T extends {}, Args extends Array<any>>(type: ContextType<T> | ContextTypeWithDefault<T, Args>): void;
30
+ static create<T extends new (contextManager: ContextManager, props: {}) => any>(ctx: ConstructorWithOnlyOptional<T>): ContextManager;
31
+ static create<T extends new (contextManager: ContextManager, props: {}) => any>(ctx: T, props: ConstructorParameters<T>[1]): ContextManager;
32
+ get<T extends new (contextManager: ContextManager, props: {}) => any>(ctx: ConstructorWithOnlyOptional<T>): InstanceType<T>;
33
+ get<T extends new (contextManager: ContextManager, props: {}) => any>(ctx: T, props: ConstructorParameters<T>[1]): InstanceType<T>;
34
+ getExisting<T extends new (contextManager: ContextManager, props: {}) => any>(ctx: T): InstanceType<T> | undefined;
35
+ getOrThrow<T extends new (contextManager: ContextManager, props: {}) => any>(ctx: T): InstanceType<T>;
36
+ add<T extends new (contextManager: ContextManager, props: {}) => any>(ctx: ConstructorWithOnlyOptional<T>): InstanceType<T>;
37
+ add<T extends new (contextManager: ContextManager, props: {}) => any>(ctx: T, props: ConstructorParameters<T>[1]): InstanceType<T>;
38
+ fork<T extends new (contextManager: ContextManager, props: {}) => any>(ctx: ConstructorWithOnlyOptional<T>): [ContextManager, InstanceType<T>];
39
+ fork<T extends new (contextManager: ContextManager, props: {}) => any>(ctx: T, props: ConstructorParameters<T>[1]): [ContextManager, InstanceType<T>];
40
+ forkMultiple<T extends ConstructorWithProps[]>(...ctx: T): [ContextManager, InstancesOfConstructorsWithProps<T>];
41
+ delete(ctx: ContextConstructor): void;
42
+ private _keyForContext;
28
43
  }
44
+ export {};
package/lib/context.js CHANGED
@@ -1,65 +1,154 @@
1
1
  let _nextContextKey = 1;
2
- export function defineContextType(def) {
3
- _nextContextKey++;
4
- if (def) {
5
- return {
6
- key: _nextContextKey.toString(),
7
- hasDefault: def !== undefined,
8
- def,
9
- };
2
+ export class Context {
3
+ constructor(contextManager, constructorProps) {
4
+ this.contextManager = contextManager;
5
+ this.constructorProps = constructorProps;
6
+ this.disposed = false;
7
+ }
8
+ dispose() {
9
+ this.disposed = true;
10
+ return undefined;
10
11
  }
11
- else
12
- return {
13
- key: _nextContextKey.toString(),
14
- hasDefault: false
15
- };
16
12
  }
13
+ Context.callSuperDispose = Symbol('Calling super.dispose() is mandatory');
14
+ const keysByType = new Map();
17
15
  export class ContextManager {
18
16
  constructor() {
19
17
  this._byKey = {};
20
18
  this._root = this;
21
19
  }
22
- static create(type, ...args) {
23
- const ctx = new ContextManager();
24
- if (type) {
25
- if (type.hasDefault)
26
- ctx.add(type, ...args);
27
- else
28
- ctx.add(type, args[0]);
29
- }
30
- return ctx;
20
+ static create(ctx, props) {
21
+ const ret = new ContextManager();
22
+ ret.add(ctx, props ?? {});
23
+ return ret;
24
+ }
25
+ get(ctx, props) {
26
+ const key = this._keyForContext(ctx);
27
+ const entry = this._byKey[key];
28
+ if (!entry)
29
+ return this.add(ctx, props);
30
+ return entry ?? this.add(ctx, props);
31
+ }
32
+ getExisting(ctx) {
33
+ const key = this._keyForContext(ctx);
34
+ return this._byKey[key];
35
+ }
36
+ getOrThrow(ctx) {
37
+ const entry = this.getExisting(ctx);
38
+ if (!entry)
39
+ throw new Error("Required context not present");
40
+ return entry;
31
41
  }
32
- add(type, ...args) {
33
- const root = this._root;
34
- const t = type.hasDefault ? type.def(root, ...args) : args[0];
35
- root._byKey[type.key] = t;
36
- return t;
42
+ add(ctx, props) {
43
+ const key = this._keyForContext(ctx);
44
+ const ret = new ctx(this, props ?? {});
45
+ this._root._byKey[key] = ret;
46
+ return ret;
37
47
  }
38
- fork(type, ...args) {
48
+ fork(ctx, props) {
39
49
  const ret = new ContextManager();
40
50
  ret._root = this._root;
41
51
  ret._byKey = Object.create(this._byKey);
42
- const t = type.hasDefault ? type.def(ret, ...args) : args[0];
43
- ret._byKey[type.key] = t;
44
- return [ret, t];
52
+ const key = this._keyForContext(ctx);
53
+ const instance = new ctx(this, props ?? {});
54
+ this._byKey[key] = instance;
55
+ return [ret, instance];
45
56
  }
46
- get(type, ...args) {
47
- let entry = this._byKey[type.key];
48
- if (!entry && type.hasDefault) {
49
- entry = this.add(type, ...args);
57
+ forkMultiple(...ctx) {
58
+ const ret = new ContextManager();
59
+ ret._root = this._root;
60
+ ret._byKey = Object.create(this._byKey);
61
+ const instances = [];
62
+ for (const entry of ctx) {
63
+ const key = this._keyForContext(entry[0]);
64
+ const instance = new entry[0](ret, entry[1]);
65
+ ret._byKey[key] = instance;
66
+ instances.push(instance);
50
67
  }
51
- return entry;
68
+ return [ret, instances];
52
69
  }
53
- getExisting(type) {
54
- return this._byKey[type.key];
70
+ delete(ctx) {
71
+ const key = this._keyForContext(ctx);
72
+ const existing = this._byKey[key];
73
+ existing?.dispose();
74
+ delete this._byKey[key];
55
75
  }
56
- getOrThrow(type) {
57
- const res = this._byKey[type.key];
58
- if (!res)
59
- throw new Error("Required context not present");
60
- return res;
61
- }
62
- delete(type) {
63
- delete this._byKey[type.key];
76
+ _keyForContext(ctx) {
77
+ let key = keysByType.get(ctx);
78
+ if (key === undefined) {
79
+ key = _nextContextKey++;
80
+ keysByType.set(ctx, key);
81
+ }
82
+ return key;
64
83
  }
65
84
  }
85
+ // export function defineContextType<T extends {}>(): ContextType<T>;
86
+ // export function defineContextType<T extends {}, Args extends Array<any> = []>(def: (ctx: ContextManager, ...args: Args) => T): ContextTypeWithDefault<T, Args>;
87
+ // export function defineContextType<T extends {}, Args extends Array<any> = []>(def?: (ctx: ContextManager, ...args: Args) => T): ContextType<T> | ContextTypeWithDefault<T, Args> {
88
+ // _nextContextKey++;
89
+ // if (def) {
90
+ // return {
91
+ // key: _nextContextKey.toString(),
92
+ // hasDefault: def !== undefined,
93
+ // def,
94
+ // }
95
+ // } else return {
96
+ // key: _nextContextKey.toString(),
97
+ // hasDefault: false
98
+ // }
99
+ // }
100
+ // export class ContextManager {
101
+ // private _byKey: { [id: string]: unknown | undefined } = {};
102
+ // private _root: ContextManager = this;
103
+ // constructor() {
104
+ // }
105
+ // static create(): ContextManager;
106
+ // static create<T extends {}>(type: ContextType<T>, props: T): ContextManager;
107
+ // static create<T extends {}, Args extends Array<any>>(type: ContextTypeWithDefault<T, Args>, ...args: Args): ContextManager;
108
+ // static create<T extends {}, Args extends Array<any>>(type?: ContextType<T> | ContextTypeWithDefault<T, Args>, ...args: Args): ContextManager {
109
+ // const ctx = new ContextManager();
110
+ // if (type) {
111
+ // if (type.hasDefault) ctx.add(type, ...args);
112
+ // else ctx.add(type, args[0]);
113
+ // }
114
+ // return ctx;
115
+ // }
116
+ // add<T extends {}>(type: ContextType<T>, obj: T): T;
117
+ // add<T extends {}, Args extends Array<any>>(type: ContextTypeWithDefault<T, Args>, ...args: Args): T;
118
+ // add<T extends {}, Args extends Array<any>>(type: ContextType<T> | ContextTypeWithDefault<T, Args>, ...args: Args): T {
119
+ // const root = this._root;
120
+ // const t = type.hasDefault ? type.def(root, ...args) : args[0] as T;
121
+ // root._byKey[type.key] = t;
122
+ // return t;
123
+ // }
124
+ // fork<T extends {}>(type: ContextType<T>, obj: T): [ContextManager, T];
125
+ // fork<T extends {}, Args extends Array<any>>(type: ContextTypeWithDefault<T, Args>, ...args: Args): [ContextManager, T];
126
+ // fork<T extends {}, Args extends Array<any>>(type: ContextType<T> | ContextTypeWithDefault<T, Args>, ...args: Args): [ContextManager, T] {
127
+ // const ret = new ContextManager();
128
+ // ret._root = this._root;
129
+ // ret._byKey = Object.create(this._byKey);
130
+ // const t = type.hasDefault ? type.def(ret, ...args) : args[0] as T;
131
+ // ret._byKey[type.key] = t;
132
+ // return [ret, t];
133
+ // }
134
+ // get<T extends {}>(type: ContextType<T>): T | undefined;
135
+ // get<T extends {}, Args extends Array<any>>(type: ContextTypeWithDefault<T, Args>, ...args: Args): T;
136
+ // get<T extends {}, Args extends Array<any>>(type: ContextType<T> | ContextTypeWithDefault<T, Args>, ...args: Args): T | undefined {
137
+ // let entry = this._byKey[type.key] as T | undefined;
138
+ // if (!entry && type.hasDefault) {
139
+ // entry = this.add(type, ...args);
140
+ // }
141
+ // return entry;
142
+ // }
143
+ // getExisting<T extends {}, Args extends Array<any>>(type: ContextType<T> | ContextTypeWithDefault<T, Args>): T | undefined {
144
+ // return this._byKey[type.key] as T | undefined;
145
+ // }
146
+ // getOrThrow<T extends {}, Args extends Array<any>>(type: ContextType<T> | ContextTypeWithDefault<T, Args>): T {
147
+ // const res = this._byKey[type.key] as T | undefined;
148
+ // if (!res) throw new Error("Required context not present");
149
+ // return res;
150
+ // }
151
+ // delete<T extends {}, Args extends Array<any>>(type: ContextType<T> | ContextTypeWithDefault<T, Args>) {
152
+ // delete this._byKey[type.key];
153
+ // }
154
+ // }
@@ -1,9 +1,8 @@
1
- import { ContextManager } from '../context';
1
+ import { ContextManager, Context } from '../context';
2
2
  import { Event } from '../event';
3
- export interface AnalyticsContext {
3
+ export declare class AnalyticsContext extends Context {
4
4
  onEvent: Event<[string, any]>;
5
- logEvent: (name: string, params?: any) => void;
5
+ logEvent(name: string, params?: any): void;
6
6
  }
7
- export declare const AnalyticsContext: import("../context").ContextTypeWithDefault<AnalyticsContext, []>;
8
7
  export declare function logEvent(mgr: ContextManager, name: string, params?: any): void;
9
8
  export declare function useAnalyticsOnEvent(mgr: ContextManager): Event<[string, any]>;
@@ -1,14 +1,14 @@
1
- import { defineContextType } from '../context';
1
+ import { Context } from '../context';
2
2
  import { Event } from '../event';
3
- export const AnalyticsContext = defineContextType((ctx) => {
4
- const onEvent = new Event();
5
- return {
6
- onEvent,
7
- logEvent: (name, params) => {
8
- onEvent.emit(name, params);
9
- }
10
- };
11
- });
3
+ export class AnalyticsContext extends Context {
4
+ constructor() {
5
+ super(...arguments);
6
+ this.onEvent = new Event();
7
+ }
8
+ logEvent(name, params) {
9
+ this.onEvent.emit(name, params);
10
+ }
11
+ }
12
12
  export function logEvent(mgr, name, params) {
13
13
  return mgr.get(AnalyticsContext).logEvent(name, params);
14
14
  }
@@ -1,19 +1,22 @@
1
- import { ContextManager } from '../context';
1
+ import { ContextManager, Context } from '../context';
2
2
  import { Event } from '../event';
3
3
  import { Observable } from '../observable';
4
- export interface CanvasContext {
5
- canvas: HTMLCanvasElement;
4
+ export interface CanvasContextOptions {
5
+ canvas?: HTMLCanvasElement;
6
+ overlay?: HTMLDivElement;
7
+ }
8
+ export declare class CanvasContext extends Context {
9
+ constructorProps: CanvasContextOptions;
6
10
  onBeforeRender: Event<[number]>;
7
11
  onAfterRender: Event<[number]>;
8
- size: Observable<[number, number]>;
9
- dispose: () => void;
12
+ canvas: HTMLCanvasElement;
10
13
  overlayDefault: HTMLDivElement;
11
14
  overlay: Observable<HTMLDivElement>;
15
+ size: Observable<[number, number]>;
16
+ private _resizeObserver;
17
+ constructor(contextManager: ContextManager, constructorProps: CanvasContextOptions);
18
+ dispose(): never;
12
19
  }
13
- export interface CanvasContextOptions {
14
- overlay: HTMLDivElement;
15
- }
16
- export declare const CanvasContext: import("../context").ContextTypeWithDefault<CanvasContext, [canvas?: HTMLCanvasElement | undefined, options?: CanvasContextOptions | undefined]>;
17
20
  export declare enum HorizontalAlignment {
18
21
  'left' = "left",
19
22
  'center' = "center",
@@ -1,58 +1,63 @@
1
- import { defineContextType } from '../context';
1
+ import { Context } from '../context';
2
2
  import { Event } from '../event';
3
3
  import { Observable } from '../observable';
4
- export const CanvasContext = defineContextType((ctx, canvas, options) => {
5
- if (!canvas) {
6
- canvas = document.createElement("canvas");
7
- canvas.style.position = "fixed";
8
- canvas.style.top = "0px";
9
- canvas.style.left = "0px";
10
- canvas.style.width = "100%";
11
- canvas.style.height = "100%";
12
- canvas.style.touchAction = "none";
13
- document.body.append(canvas);
14
- }
15
- const overlayDefault = options?.overlay ?? document.createElement('div');
16
- overlayDefault.id = 'zcomponent-overlay';
17
- if (!options?.overlay) {
18
- document.body.append(overlayDefault);
19
- }
20
- const size = new Observable([canvas.clientWidth, canvas.clientHeight]);
21
- const resizeObserver = new ResizeObserver(entries => {
22
- for (const entry of entries) {
23
- if (entry.target !== canvas)
24
- continue;
25
- size.value = [entry.contentRect.width, entry.contentRect.height];
4
+ export class CanvasContext extends Context {
5
+ constructor(contextManager, constructorProps) {
6
+ super(contextManager, constructorProps);
7
+ this.constructorProps = constructorProps;
8
+ this.onBeforeRender = new Event();
9
+ this.onAfterRender = new Event();
10
+ this._resizeObserver = new ResizeObserver(entries => {
11
+ for (const entry of entries) {
12
+ if (entry.target !== this.canvas)
13
+ continue;
14
+ this.size.value = [entry.contentRect.width, entry.contentRect.height];
15
+ }
16
+ });
17
+ if (!constructorProps.canvas) {
18
+ this.canvas = document.createElement("canvas");
19
+ this.canvas.style.position = "fixed";
20
+ this.canvas.style.top = "0px";
21
+ this.canvas.style.left = "0px";
22
+ this.canvas.style.width = "100%";
23
+ this.canvas.style.height = "100%";
24
+ this.canvas.style.touchAction = "none";
25
+ document.body.append(this.canvas);
26
+ }
27
+ else {
28
+ this.canvas = constructorProps.canvas;
26
29
  }
27
- });
28
- resizeObserver.observe(canvas);
29
- const onBeforeRender = new Event();
30
- let lastRect;
31
- onBeforeRender.bindfn(() => {
32
- const rect = canvas.getBoundingClientRect();
33
- if (lastRect && lastRect.top === rect.top && lastRect.left === rect.left && lastRect.width === rect.width && lastRect.height === rect.height)
34
- return;
35
- overlayDefault.style.top = rect.top.toString() + 'px';
36
- overlayDefault.style.left = rect.left.toString() + 'px';
37
- overlayDefault.style.width = rect.width.toString() + 'px';
38
- overlayDefault.style.height = rect.height.toString() + 'px';
39
- lastRect = rect;
40
- });
41
- const onAfterRender = new Event();
42
- return {
43
- canvas,
44
- onBeforeRender,
45
- onAfterRender,
46
- size,
47
- overlayDefault,
48
- overlay: new Observable(overlayDefault, undefined, false),
49
- dispose: () => {
50
- onBeforeRender.clear();
51
- onAfterRender.clear();
52
- resizeObserver.disconnect();
30
+ this.overlayDefault = constructorProps.overlay ?? document.createElement('div');
31
+ this.overlayDefault.id = 'zcomponent-overlay';
32
+ if (!constructorProps.overlay) {
33
+ document.body.append(this.overlayDefault);
53
34
  }
54
- };
55
- });
35
+ this.overlay = new Observable(this.overlayDefault, undefined, false);
36
+ this.size = new Observable([this.canvas.clientWidth, this.canvas.clientHeight]);
37
+ this._resizeObserver.observe(this.canvas);
38
+ let lastRect;
39
+ this.onBeforeRender.bindfn(() => {
40
+ const rect = this.canvas.getBoundingClientRect();
41
+ if (lastRect && lastRect.top === rect.top && lastRect.left === rect.left && lastRect.width === rect.width && lastRect.height === rect.height)
42
+ return;
43
+ this.overlayDefault.style.top = rect.top.toString() + 'px';
44
+ this.overlayDefault.style.left = rect.left.toString() + 'px';
45
+ this.overlayDefault.style.width = rect.width.toString() + 'px';
46
+ this.overlayDefault.style.height = rect.height.toString() + 'px';
47
+ lastRect = rect;
48
+ });
49
+ }
50
+ dispose() {
51
+ this.onBeforeRender.clear();
52
+ this.onAfterRender.clear();
53
+ this._resizeObserver.disconnect();
54
+ if (!this.constructorProps.canvas)
55
+ this.canvas.remove();
56
+ if (!this.constructorProps.overlay)
57
+ this.overlayDefault.remove();
58
+ return super.dispose();
59
+ }
60
+ }
56
61
  export var HorizontalAlignment;
57
62
  (function (HorizontalAlignment) {
58
63
  HorizontalAlignment["left"] = "left";
@@ -1,4 +1,4 @@
1
- import { ContextManager } from '../context';
1
+ import { ContextManager, Context } from '../context';
2
2
  import { Observable } from '../observable';
3
3
  export declare enum Status {
4
4
  "unknown" = "unknown",
@@ -15,18 +15,17 @@ export interface Vendor {
15
15
  description: string;
16
16
  privacyPolicyUrl: string;
17
17
  }
18
- export interface CookieConsentContext {
19
- status: Observable<Status>;
20
- functionalCookies: Observable<ConsentStatus>;
21
- performanceCookies: Observable<ConsentStatus>;
22
- marketingCookies: Observable<ConsentStatus>;
18
+ export declare class CookieConsentContext extends Context {
19
+ status: Observable<Status, never>;
20
+ functionalCookies: Observable<ConsentStatus, never>;
21
+ performanceCookies: Observable<ConsentStatus, never>;
22
+ marketingCookies: Observable<ConsentStatus, never>;
23
+ showSettings: Observable<(() => void) | undefined, never>;
23
24
  performanceVendors: Vendor[];
24
25
  marketingVendors: Vendor[];
25
- registerPerformanceVendor: (v: Vendor) => void;
26
- registerMarketingVendor: (v: Vendor) => void;
27
- showSettings: Observable<(() => void) | undefined>;
26
+ registerPerformanceVendor(v: Vendor): void;
27
+ registerMarketingVendor(v: Vendor): void;
28
28
  }
29
- export declare const CookieConsentContext: import("../context").ContextTypeWithDefault<CookieConsentContext, []>;
30
29
  export declare function useCookieConsentFunctional(mgr: ContextManager): Observable<ConsentStatus, never>;
31
30
  export declare function useCookieConsentPerformance(mgr: ContextManager): Observable<ConsentStatus, never>;
32
31
  export declare function useCookieConsentMarketing(mgr: ContextManager): Observable<ConsentStatus, never>;