@zcomponent/core 1.29.0 → 2.0.0-alpha.2
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/README.md +1 -0
- package/css/zcomponent.css +121 -122
- package/index.js +0 -3
- package/lib/actionbehavior.d.ts +3 -3
- package/lib/actionbehavior.js +20 -19
- package/lib/animation/animation.d.ts +13 -6
- package/lib/animation/animation.js +56 -43
- package/lib/animation/bezier.d.ts +6 -0
- package/lib/animation/bezier.js +8 -2
- package/lib/animation/clips/clip.d.ts +13 -1
- package/lib/animation/clips/clip.js +34 -14
- package/lib/animation/index.d.ts +4 -4
- package/lib/animation/index.js +4 -4
- package/lib/animation/layer.d.ts +9 -0
- package/lib/animation/layer.js +28 -12
- package/lib/animation/layerclip.d.ts +21 -0
- package/lib/animation/layerclip.js +82 -45
- package/lib/animation/tracks/cliptrack.d.ts +18 -0
- package/lib/animation/tracks/cliptrack.js +27 -6
- package/lib/animation/tracks/functiontrack.d.ts +9 -0
- package/lib/animation/tracks/functiontrack.js +16 -3
- package/lib/animation/tracks/propertytrack.d.ts +20 -1
- package/lib/animation/tracks/propertytrack.js +48 -11
- package/lib/animation/tracks/streamtrack.d.ts +18 -0
- package/lib/animation/tracks/streamtrack.js +32 -6
- package/lib/animation/tracks/track.d.ts +8 -2
- package/lib/animation/tracks/track.js +3 -2
- package/lib/behavior.js +13 -10
- package/lib/behaviors/ActivateState.d.ts +22 -24
- package/lib/behaviors/ActivateState.js +145 -35
- package/lib/behaviors/CallFunction.d.ts +2 -4
- package/lib/behaviors/CallFunction.js +69 -11
- package/lib/behaviors/ChangeRootScene.d.ts +35 -0
- package/lib/behaviors/ChangeRootScene.js +123 -0
- package/lib/behaviors/ConsoleLog.d.ts +6 -5
- package/lib/behaviors/ConsoleLog.js +95 -12
- package/lib/behaviors/DownloadSnapshot.d.ts +7 -12
- package/lib/behaviors/DownloadSnapshot.js +70 -19
- package/lib/behaviors/EmitComponentPropEvent.d.ts +1 -3
- package/lib/behaviors/EmitComponentPropEvent.js +80 -12
- package/lib/behaviors/LaunchURL.d.ts +5 -5
- package/lib/behaviors/LaunchURL.js +92 -10
- package/lib/behaviors/LogAnalyticsEvent.d.ts +10 -4
- package/lib/behaviors/LogAnalyticsEvent.js +88 -7
- package/lib/behaviors/PauseLayerClip.d.ts +7 -9
- package/lib/behaviors/PauseLayerClip.js +89 -21
- package/lib/behaviors/PlayLayerClip.d.ts +35 -32
- package/lib/behaviors/PlayLayerClip.js +202 -48
- package/lib/behaviors/PlaySound.d.ts +19 -25
- package/lib/behaviors/PlaySound.js +122 -28
- package/lib/behaviors/SetLayerOff.d.ts +7 -9
- package/lib/behaviors/SetLayerOff.js +88 -21
- package/lib/behaviors/ShareSnapshot.d.ts +5 -9
- package/lib/behaviors/ShareSnapshot.js +70 -17
- package/lib/behaviors/ShowTextAlert.d.ts +16 -29
- package/lib/behaviors/ShowTextAlert.js +116 -48
- package/lib/behaviors/TakeSnapshot.d.ts +22 -26
- package/lib/behaviors/TakeSnapshot.js +131 -56
- package/lib/behaviors/ToggleLayerClips.d.ts +45 -62
- package/lib/behaviors/ToggleLayerClips.js +233 -64
- package/lib/behaviors/stream/PauseStream.d.ts +5 -16
- package/lib/behaviors/stream/PauseStream.js +94 -34
- package/lib/behaviors/stream/PlayStream.d.ts +14 -27
- package/lib/behaviors/stream/PlayStream.js +123 -34
- package/lib/behaviors/stream/SeekStream.d.ts +8 -18
- package/lib/behaviors/stream/SeekStream.js +97 -31
- package/lib/behaviors/stream/StopStream.d.ts +6 -15
- package/lib/behaviors/stream/StopStream.js +89 -27
- package/lib/component.d.ts +36 -13
- package/lib/component.js +237 -151
- package/lib/components/AnimatedLoader.d.ts +51 -0
- package/lib/components/AnimatedLoader.js +159 -0
- package/lib/components/Audio.d.ts +35 -37
- package/lib/components/Audio.js +264 -151
- package/lib/components/AudioLayerSettings.d.ts +13 -11
- package/lib/components/AudioLayerSettings.js +76 -18
- package/lib/components/Children.d.ts +2 -1
- package/lib/components/Children.js +66 -13
- package/lib/components/ConditionalConstruct.d.ts +44 -0
- package/lib/components/ConditionalConstruct.js +142 -0
- package/lib/components/DefaultCookieConsent.d.ts +53 -73
- package/lib/components/DefaultCookieConsent.js +382 -264
- package/lib/components/DefaultLoader.d.ts +40 -28
- package/lib/components/DefaultLoader.js +198 -46
- package/lib/components/Gamepad.d.ts +5 -26
- package/lib/components/Gamepad.js +201 -65
- package/lib/components/LoaderScope.d.ts +15 -0
- package/lib/components/LoaderScope.js +71 -0
- package/lib/components/LongLoad.d.ts +11 -15
- package/lib/components/LongLoad.js +89 -34
- package/lib/components/SnapshotUI.d.ts +19 -24
- package/lib/components/SnapshotUI.js +202 -105
- package/lib/context.d.ts +23 -5
- package/lib/context.js +12 -6
- package/lib/contexts/analyticscontext.d.ts +12 -2
- package/lib/contexts/analyticscontext.js +68 -10
- package/lib/contexts/audiocontextcontext.d.ts +1 -1
- package/lib/contexts/audiocontextcontext.js +9 -8
- package/lib/contexts/canvascontext.d.ts +32 -18
- package/lib/contexts/canvascontext.js +195 -120
- package/lib/contexts/cookieconsentcontext.d.ts +38 -28
- package/lib/contexts/cookieconsentcontext.js +35 -32
- package/lib/contexts/documentflagmanager.js +15 -11
- package/lib/contexts/environmentcontext.d.ts +22 -6
- package/lib/contexts/environmentcontext.js +135 -21
- package/lib/contexts/gamepadcontext.d.ts +29 -26
- package/lib/contexts/gamepadcontext.js +46 -46
- package/lib/contexts/gesturecontext.d.ts +31 -6
- package/lib/contexts/gesturecontext.js +110 -85
- package/lib/contexts/globaltagcontext.d.ts +1 -1
- package/lib/contexts/globaltagcontext.js +8 -11
- package/lib/contexts/loadcontext.d.ts +45 -8
- package/lib/contexts/loadcontext.js +232 -82
- package/lib/contexts/orientationcontext.d.ts +13 -15
- package/lib/contexts/orientationcontext.js +150 -67
- package/lib/contexts/rootcomponentscontext.d.ts +16 -0
- package/lib/contexts/rootcomponentscontext.js +65 -0
- package/lib/contexts/snapshotContext.d.ts +0 -3
- package/lib/contexts/snapshotContext.js +78 -23
- package/lib/contexts/tagcontext.d.ts +5 -5
- package/lib/contexts/tagcontext.js +16 -16
- package/lib/contexts/textalertcontext.d.ts +1 -5
- package/lib/contexts/textalertcontext.js +1 -5
- package/lib/contexts/usereventcontext.d.ts +1 -1
- package/lib/contexts/usereventcontext.js +12 -12
- package/lib/data/animation.d.ts +10 -2
- package/lib/data/change.d.ts +1 -1
- package/lib/data/change.js +7 -0
- package/lib/data/core.d.ts +9 -1
- package/lib/data/index.d.ts +1 -1
- package/lib/data/index.js +1 -1
- package/lib/decorators.d.ts +348 -0
- package/lib/decorators.js +239 -0
- package/lib/decoratorutils.d.ts +3 -0
- package/lib/decoratorutils.js +8 -0
- package/lib/emitter.d.ts +15 -2
- package/lib/emitter.js +15 -6
- package/lib/entity.d.ts +43 -25
- package/lib/entity.js +104 -71
- package/lib/event.d.ts +2 -1
- package/lib/event.js +1 -4
- package/lib/groups.d.ts +18 -0
- package/lib/groups.js +19 -0
- package/lib/icons.d.ts +3372 -0
- package/lib/icons.js +1 -0
- package/lib/index.d.ts +18 -13
- package/lib/index.js +17 -13
- package/lib/inflate.d.ts +7 -7
- package/lib/inflate.js +5 -5
- package/lib/observable.d.ts +9 -2
- package/lib/observable.js +5 -4
- package/lib/observe.d.ts +18 -0
- package/lib/observe.js +177 -0
- package/lib/profile.d.ts +9 -5
- package/lib/profile.js +27 -17
- package/lib/selectors.d.ts +1 -1
- package/lib/selectors.js +5 -4
- package/lib/types.d.ts +161 -19
- package/lib/types.js +22 -1
- package/lib/values/values.d.ts +7 -8
- package/lib/values/values.js +1 -2
- package/lib/values/valuesmutator.d.ts +4 -1
- package/lib/values/valuesmutator.js +10 -8
- package/lib/zcomponent.d.ts +21 -10
- package/lib/zcomponent.js +717 -540
- package/package.json +22 -5
- package/lib/behaviors/ChangeCursor.d.ts +0 -39
- package/lib/behaviors/ChangeCursor.js +0 -44
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { registerBehaviorRunAtDesignTime } from './behavior';
|
|
2
|
+
import { Event } from './event';
|
|
3
|
+
import { makeObservable } from './observe';
|
|
4
|
+
/**
|
|
5
|
+
* Creates an observable property that automatically triggers a callback when its value changes.
|
|
6
|
+
* This enables reactive programming patterns where property changes can update UI elements,
|
|
7
|
+
* trigger side effects, or synchronize state across components.
|
|
8
|
+
*
|
|
9
|
+
* @decorator
|
|
10
|
+
* @param withHandler - Optional callback function that executes when the property value changes.
|
|
11
|
+
* Receives the new value, the entity instance, and the property name.
|
|
12
|
+
* @param deep - Whether to observe nested object changes (default: true)
|
|
13
|
+
* @returns A property decorator that makes the property observable
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* // Simple observer that updates element visibility
|
|
18
|
+
* @zObserve((val, instance) => (instance.element.visible = val))
|
|
19
|
+
* public visible = true;
|
|
20
|
+
*
|
|
21
|
+
* // Observer that calls a method on change
|
|
22
|
+
* @zObserve((v, instance: Audio) => {
|
|
23
|
+
* instance._updateVolume();
|
|
24
|
+
* })
|
|
25
|
+
* public muted = false;
|
|
26
|
+
*
|
|
27
|
+
* // Observer without handler (just makes property observable)
|
|
28
|
+
* @zObserve()
|
|
29
|
+
* public position = { x: 0, y: 0, z: 0 };
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export function zObserve(withHandler, deep = true) {
|
|
33
|
+
return (value, context) => {
|
|
34
|
+
context.addInitializer(function () {
|
|
35
|
+
const evt = makeObservable(this, context.name, deep, true);
|
|
36
|
+
if (withHandler) {
|
|
37
|
+
const handler = withHandler.bind(this);
|
|
38
|
+
evt.addListener(handler);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
return function (initialValue) {
|
|
42
|
+
return initialValue;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
// export function zProp<T>(opts?: ZPropOptions): (val, context) => (initialValue: T) => T {
|
|
47
|
+
// return function (value: undefined, context) {
|
|
48
|
+
// return function (this: Entity, initialValue: T) {
|
|
49
|
+
// return initialValue;
|
|
50
|
+
// };
|
|
51
|
+
// };
|
|
52
|
+
// }
|
|
53
|
+
/**
|
|
54
|
+
* Exposes a property to the Mattercraft UI editor, allowing users to configure it visually.
|
|
55
|
+
* Supports various UI widget types, grouping, and value constraints.
|
|
56
|
+
*
|
|
57
|
+
* @decorator
|
|
58
|
+
* @param opts - Configuration options for how the property appears in the UI
|
|
59
|
+
* @returns A property decorator that exposes the property to the UI
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* // Simple UI property
|
|
64
|
+
* @zUI()
|
|
65
|
+
* public volume = 1;
|
|
66
|
+
*
|
|
67
|
+
* // UI property with group and priority
|
|
68
|
+
* @zUI({ group: 'Audio', priority: 20 })
|
|
69
|
+
* public muted = false;
|
|
70
|
+
*
|
|
71
|
+
* // UI property with specific widget type
|
|
72
|
+
* @zUI({ type: 'proportion' })
|
|
73
|
+
* public opacity = 0.8;
|
|
74
|
+
*
|
|
75
|
+
* // UI property with value constraints
|
|
76
|
+
* @zUI({ values: 'layerclipids' })
|
|
77
|
+
* public state?: string;
|
|
78
|
+
*
|
|
79
|
+
* // Combined with @zObserve for reactive updates
|
|
80
|
+
* @zUI({ group: 'Appearance', type: 'color-hex' })
|
|
81
|
+
* @zObserve((val, instance) => instance.updateColor(val))
|
|
82
|
+
* public color = '#FF0000';
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export function zUI(opts) {
|
|
86
|
+
return (value, context) => {
|
|
87
|
+
if (context.kind === 'method') {
|
|
88
|
+
return value;
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
return function (initialValue) {
|
|
92
|
+
return initialValue;
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Excludes a property from serialization and the Mattercraft UI.
|
|
99
|
+
* Use this for internal properties that shouldn't be exposed to users or persisted.
|
|
100
|
+
*
|
|
101
|
+
* @decorator
|
|
102
|
+
* @returns A property decorator that marks the property as ignored
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```typescript
|
|
106
|
+
* // Internal element reference
|
|
107
|
+
* @zIgnore()
|
|
108
|
+
* public element: HTMLElement;
|
|
109
|
+
*
|
|
110
|
+
* // Private cache that shouldn't be serialized
|
|
111
|
+
* @zIgnore()
|
|
112
|
+
* private _cache: Map<string, any>;
|
|
113
|
+
*
|
|
114
|
+
* // Temporary state
|
|
115
|
+
* @zIgnore()
|
|
116
|
+
* public isProcessing = false;
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
export function zIgnore() {
|
|
120
|
+
return (value, context) => { };
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Registers a class as a component in Mattercraft, making it available in the editor's UI.
|
|
124
|
+
* Components are the primary building blocks for creating interactive experiences.
|
|
125
|
+
*
|
|
126
|
+
* @decorator
|
|
127
|
+
* @param opts - Configuration options for the component
|
|
128
|
+
* @returns A class decorator that registers the component
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```typescript
|
|
132
|
+
* // Basic component registration
|
|
133
|
+
* @zComponent({ icon: 'deployed_code', group: 'Meshes' })
|
|
134
|
+
* export class Box extends Mesh {
|
|
135
|
+
* // Component implementation
|
|
136
|
+
* }
|
|
137
|
+
*
|
|
138
|
+
* // Component with tags and parent restrictions
|
|
139
|
+
* @zComponent({
|
|
140
|
+
* icon: 'music_note',
|
|
141
|
+
* group: 'Media',
|
|
142
|
+
* tags: ['core/audio'],
|
|
143
|
+
* })
|
|
144
|
+
* export class Audio extends Component {
|
|
145
|
+
* // Audio component implementation
|
|
146
|
+
* }
|
|
147
|
+
*
|
|
148
|
+
* // Component with default children
|
|
149
|
+
* @zComponent({
|
|
150
|
+
* icon: 'view_in_ar',
|
|
151
|
+
* group: '3D Objects',
|
|
152
|
+
* defaultChildren: [
|
|
153
|
+
* {
|
|
154
|
+
* label: 'Material',
|
|
155
|
+
* type: '@zcomponent/three/lib/components/materials/MeshStandardMaterial#MeshStandardMaterial',
|
|
156
|
+
* props: { color: '#FF0000' }
|
|
157
|
+
* }
|
|
158
|
+
* ]
|
|
159
|
+
* })
|
|
160
|
+
* export class CustomMesh extends Component {
|
|
161
|
+
* // Mesh with default material
|
|
162
|
+
* }
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
export function zComponent(opts) {
|
|
166
|
+
return (t) => t;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Registers a class as a behavior that can be attached to components.
|
|
170
|
+
* Behaviors add functionality to existing components without modifying their structure.
|
|
171
|
+
*
|
|
172
|
+
* @decorator
|
|
173
|
+
* @param opts - Configuration options for the behavior
|
|
174
|
+
* @returns A class decorator that registers the behavior
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```typescript
|
|
178
|
+
* // Basic behavior registration
|
|
179
|
+
* @zBehavior({ icon: 'directions_run', parents: ['three/Object3D/**'] })
|
|
180
|
+
* export class Animation extends Behavior {
|
|
181
|
+
* // Animation behavior implementation
|
|
182
|
+
* }
|
|
183
|
+
*
|
|
184
|
+
* // Behavior that runs in the editor
|
|
185
|
+
* @zBehavior({
|
|
186
|
+
* icon: 'visibility',
|
|
187
|
+
* group: 'Actions',
|
|
188
|
+
* runAtDesignTime: true,
|
|
189
|
+
* parents: ['three/Object3D/**']
|
|
190
|
+
* })
|
|
191
|
+
* export class PreviewMode extends Behavior {
|
|
192
|
+
* // Provides preview functionality in the editor
|
|
193
|
+
* }
|
|
194
|
+
*
|
|
195
|
+
* // Action behavior with specific parent restrictions
|
|
196
|
+
* @zBehavior({
|
|
197
|
+
* icon: 'play_circle',
|
|
198
|
+
* group: 'Animation Actions',
|
|
199
|
+
* parents: ['three/Object3D/GLTF'],
|
|
200
|
+
* tags: ['animation/control']
|
|
201
|
+
* })
|
|
202
|
+
* export class PlayAnimation extends ActionBehavior {
|
|
203
|
+
* // Plays animations on GLTF models
|
|
204
|
+
* }
|
|
205
|
+
* ```
|
|
206
|
+
*/
|
|
207
|
+
export function zBehavior(opts) {
|
|
208
|
+
return (t) => {
|
|
209
|
+
if (opts?.runAtDesignTime)
|
|
210
|
+
registerBehaviorRunAtDesignTime(t);
|
|
211
|
+
return t;
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
export function zRegister(propOrContextConstructorOrEvent, contextPropOrOpts, opts) {
|
|
215
|
+
return (originalMethod, context) => {
|
|
216
|
+
context.addInitializer(function () {
|
|
217
|
+
let evt;
|
|
218
|
+
let resolvedOpts;
|
|
219
|
+
if (typeof propOrContextConstructorOrEvent === 'string') {
|
|
220
|
+
evt = this[propOrContextConstructorOrEvent];
|
|
221
|
+
if (typeof contextPropOrOpts === 'object')
|
|
222
|
+
resolvedOpts = contextPropOrOpts;
|
|
223
|
+
}
|
|
224
|
+
else if (propOrContextConstructorOrEvent instanceof Event) {
|
|
225
|
+
evt = propOrContextConstructorOrEvent;
|
|
226
|
+
if (typeof contextPropOrOpts === 'object')
|
|
227
|
+
resolvedOpts = contextPropOrOpts;
|
|
228
|
+
}
|
|
229
|
+
else if (typeof contextPropOrOpts === 'string') {
|
|
230
|
+
const context = this.contextManager.get(propOrContextConstructorOrEvent);
|
|
231
|
+
evt = context[contextPropOrOpts];
|
|
232
|
+
resolvedOpts = opts;
|
|
233
|
+
}
|
|
234
|
+
if (evt)
|
|
235
|
+
this.register(evt, (...args) => this[context.name].call(this, ...args), resolvedOpts);
|
|
236
|
+
});
|
|
237
|
+
return originalMethod;
|
|
238
|
+
};
|
|
239
|
+
}
|
package/lib/emitter.d.ts
CHANGED
|
@@ -2,10 +2,23 @@
|
|
|
2
2
|
* The base class for classes that hold a set of functions to be called in response
|
|
3
3
|
* to an event or change in value, e.g. Observable and Event.
|
|
4
4
|
*/
|
|
5
|
-
export declare class Emitter<Args extends Array<any> = [
|
|
6
|
-
|
|
5
|
+
export declare class Emitter<Args extends Array<any> = [
|
|
6
|
+
]> {
|
|
7
|
+
/**
|
|
8
|
+
* @default []
|
|
9
|
+
*/
|
|
10
|
+
protected _funcs: [
|
|
11
|
+
fn: (...args: Args) => void,
|
|
12
|
+
priority: number
|
|
13
|
+
][];
|
|
14
|
+
/**
|
|
15
|
+
* @default false
|
|
16
|
+
*/
|
|
7
17
|
private _emitting;
|
|
8
18
|
private _toUnbind;
|
|
19
|
+
/**
|
|
20
|
+
* @default false
|
|
21
|
+
*/
|
|
9
22
|
private _needsSort;
|
|
10
23
|
private _next;
|
|
11
24
|
private _nextFn;
|
package/lib/emitter.js
CHANGED
|
@@ -3,12 +3,21 @@
|
|
|
3
3
|
* to an event or change in value, e.g. Observable and Event.
|
|
4
4
|
*/
|
|
5
5
|
export class Emitter {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
/**
|
|
7
|
+
* @default []
|
|
8
|
+
*/
|
|
9
|
+
_funcs = [];
|
|
10
|
+
/**
|
|
11
|
+
* @default false
|
|
12
|
+
*/
|
|
13
|
+
_emitting = false;
|
|
14
|
+
_toUnbind = new Set();
|
|
15
|
+
/**
|
|
16
|
+
* @default false
|
|
17
|
+
*/
|
|
18
|
+
_needsSort = false;
|
|
19
|
+
_next;
|
|
20
|
+
_nextFn;
|
|
12
21
|
/**
|
|
13
22
|
* Clears all listeners from the event.
|
|
14
23
|
*/
|
package/lib/entity.d.ts
CHANGED
|
@@ -2,16 +2,34 @@ import { ConstructorForComponent } from './component';
|
|
|
2
2
|
import { ContextManager } from './context';
|
|
3
3
|
import { Event } from './event';
|
|
4
4
|
import { Observable } from './observable';
|
|
5
|
+
import { ObserveOptions } from './observe';
|
|
5
6
|
import { ZComponent } from './zcomponent';
|
|
6
7
|
/**
|
|
7
8
|
* Base class for all components and behaviors.
|
|
8
9
|
*/
|
|
9
10
|
export declare class Entity {
|
|
10
11
|
readonly contextManager: ContextManager;
|
|
12
|
+
/**
|
|
13
|
+
* @default []
|
|
14
|
+
*/
|
|
11
15
|
private _registered;
|
|
16
|
+
/**
|
|
17
|
+
* @default []
|
|
18
|
+
*/
|
|
19
|
+
private _observed;
|
|
20
|
+
/**
|
|
21
|
+
* @default false
|
|
22
|
+
*/
|
|
12
23
|
private _handlersBound;
|
|
13
24
|
private _zcomponent;
|
|
25
|
+
/**
|
|
26
|
+
* @default false
|
|
27
|
+
*/
|
|
14
28
|
private _disposed;
|
|
29
|
+
/**
|
|
30
|
+
* @default true
|
|
31
|
+
*/
|
|
32
|
+
protected _enabledResolved: boolean;
|
|
15
33
|
/**
|
|
16
34
|
* An event that is fired as the last act of this entity being destroyed.
|
|
17
35
|
*/
|
|
@@ -24,34 +42,15 @@ export declare class Entity {
|
|
|
24
42
|
/**
|
|
25
43
|
* Gets the disposed status of the entity.
|
|
26
44
|
* @returns {boolean} The disposed status.
|
|
45
|
+
* @zignore
|
|
27
46
|
*/
|
|
28
47
|
get disposed(): boolean;
|
|
29
48
|
/**
|
|
30
|
-
* Sets the disposed status of the entity.
|
|
31
|
-
* @param {boolean} v - The new disposed status.
|
|
32
|
-
* @private
|
|
33
|
-
*/
|
|
34
|
-
private set disposed(value);
|
|
35
|
-
/**
|
|
36
|
-
* If `false`, this entity and its children will no longer participate in the experience.
|
|
37
|
-
*
|
|
38
|
-
* Note - to read this value, you may wish to use `enabledResolved` which will be `false` if
|
|
39
|
-
* this entity, or any of its parents, have `enabled` set to false.
|
|
40
|
-
*
|
|
41
|
-
* The precise implications of `enabled` being false will vary between entities,
|
|
42
|
-
* but in general disabled entities:
|
|
43
|
-
* - should not emit any events,
|
|
44
|
-
* - should not take any action, e.g. navigate the user to a different page,
|
|
45
|
-
* - should not perform any network communication,
|
|
46
|
-
* - should not make changes to other behaviors or components in the experience,
|
|
47
|
-
* - should minimise any runtime performance cost (e.g. detach from any frame handlers).
|
|
48
|
-
*
|
|
49
|
-
* @zprop
|
|
50
|
-
* @zdefault true
|
|
51
49
|
* @zgroup Behavior
|
|
52
|
-
* @zgrouppriority
|
|
50
|
+
* @zgrouppriority 5
|
|
51
|
+
* @default true
|
|
53
52
|
*/
|
|
54
|
-
|
|
53
|
+
enabled: boolean;
|
|
55
54
|
/**
|
|
56
55
|
* This will have value `false` if this entity, or any of its parents, have `enabled` set to `false`.
|
|
57
56
|
*
|
|
@@ -66,8 +65,9 @@ export declare class Entity {
|
|
|
66
65
|
* - should minimise any runtime performance cost.
|
|
67
66
|
*
|
|
68
67
|
* Disabled entities will typically remain visible (if they have a visible appearance).
|
|
68
|
+
* @zignore
|
|
69
69
|
*/
|
|
70
|
-
|
|
70
|
+
get enabledResolved(): boolean;
|
|
71
71
|
/**
|
|
72
72
|
* Get the instance of the ZComponent that constructed this entity.
|
|
73
73
|
*
|
|
@@ -77,6 +77,7 @@ export declare class Entity {
|
|
|
77
77
|
*
|
|
78
78
|
* @param type The ZComponent class that you are expecting to receive
|
|
79
79
|
* @returns The instance of the ZComponent that constructed this component or behavior
|
|
80
|
+
* @zignore
|
|
80
81
|
*/
|
|
81
82
|
getZComponentInstance<T extends ZComponent = ZComponent>(type?: ConstructorForComponent<T>): T;
|
|
82
83
|
/**
|
|
@@ -88,6 +89,7 @@ export declare class Entity {
|
|
|
88
89
|
*
|
|
89
90
|
* @param evt The Event or Observable to listen to
|
|
90
91
|
* @param fn A function that will be called when the event fires, or the Observable value changes
|
|
92
|
+
* @zignore
|
|
91
93
|
*/
|
|
92
94
|
register<Args extends Array<any>>(evt: Event<Args>, fn: (...args: Args) => void, priority?: number): any;
|
|
93
95
|
register<Args extends Array<any>>(evt: Event<Args>, fn: (...args: Args) => void, options?: RegisterOptions): any;
|
|
@@ -98,9 +100,18 @@ export declare class Entity {
|
|
|
98
100
|
*
|
|
99
101
|
* @param evt The Event or Observable
|
|
100
102
|
* @param fn The function that was passed in the call to `register`
|
|
103
|
+
* @zignore
|
|
101
104
|
*/
|
|
102
105
|
unregister<Args extends Array<any>>(evt: Event<Args>, fn: (...args: Args) => void): any;
|
|
103
106
|
unregister<Type>(observable: Observable<Type>, fn: (v: Type) => void): any;
|
|
107
|
+
/**
|
|
108
|
+
* @zignore
|
|
109
|
+
*/
|
|
110
|
+
protected observe<ObjectType extends object, PropNameType extends keyof ObjectType>(object: ObjectType, prop: PropNameType, fn: (v: ObjectType[PropNameType]) => void, options?: EntityObserveOptions): void;
|
|
111
|
+
/**
|
|
112
|
+
* @zignore
|
|
113
|
+
*/
|
|
114
|
+
protected unobserve<ObjectType extends object, PropNameType extends keyof ObjectType>(object: ObjectType, prop: PropNameType, fn: (v: ObjectType[PropNameType]) => void): void;
|
|
104
115
|
private _updateHandlers;
|
|
105
116
|
/**
|
|
106
117
|
* Destroy this entity, cleaning up any resources that it has created and
|
|
@@ -119,8 +130,9 @@ export declare class Entity {
|
|
|
119
130
|
* This ensures that the dispose implementation of the parent class runs.
|
|
120
131
|
*
|
|
121
132
|
* @returns The result of a call to super.dispose()
|
|
133
|
+
* @zignore
|
|
122
134
|
*/
|
|
123
|
-
dispose(): never;
|
|
135
|
+
dispose(): typeof Entity.callSuperDispose & never;
|
|
124
136
|
private static callSuperDispose;
|
|
125
137
|
}
|
|
126
138
|
/**
|
|
@@ -136,3 +148,9 @@ export interface RegisterOptions {
|
|
|
136
148
|
*/
|
|
137
149
|
bindWhenDisabled?: boolean;
|
|
138
150
|
}
|
|
151
|
+
export interface EntityObserveOptions extends ObserveOptions {
|
|
152
|
+
/**
|
|
153
|
+
* If `true`, the handler function will be bound to the underlying Event or Observable even if this entity is disabled.
|
|
154
|
+
*/
|
|
155
|
+
bindWhenDisabled?: boolean;
|
|
156
|
+
}
|
package/lib/entity.js
CHANGED
|
@@ -1,94 +1,78 @@
|
|
|
1
1
|
import { Event } from './event';
|
|
2
|
-
import {
|
|
2
|
+
import { observe, unobserve } from './observe';
|
|
3
3
|
import { getCurrentZComponentConstruction } from './zcomponentconstruction';
|
|
4
|
+
// import { zIgnore, zProp } from './decorators';
|
|
4
5
|
/**
|
|
5
6
|
* Base class for all components and behaviors.
|
|
6
7
|
*/
|
|
7
8
|
export class Entity {
|
|
9
|
+
contextManager;
|
|
10
|
+
/**
|
|
11
|
+
* @default []
|
|
12
|
+
*/
|
|
13
|
+
_registered = [];
|
|
14
|
+
/**
|
|
15
|
+
* @default []
|
|
16
|
+
*/
|
|
17
|
+
_observed = [];
|
|
18
|
+
/**
|
|
19
|
+
* @default false
|
|
20
|
+
*/
|
|
21
|
+
_handlersBound = false;
|
|
22
|
+
_zcomponent;
|
|
23
|
+
/**
|
|
24
|
+
* @default false
|
|
25
|
+
*/
|
|
26
|
+
_disposed = false;
|
|
27
|
+
/**
|
|
28
|
+
* @default true
|
|
29
|
+
*/
|
|
30
|
+
_enabledResolved = true;
|
|
31
|
+
/**
|
|
32
|
+
* An event that is fired as the last act of this entity being destroyed.
|
|
33
|
+
*/
|
|
34
|
+
onDispose = new Event();
|
|
8
35
|
/**
|
|
9
36
|
* Creates an instance of Entity.
|
|
10
37
|
* @param contextManager The current ContextManager
|
|
11
38
|
*/
|
|
12
39
|
constructor(contextManager) {
|
|
13
40
|
this.contextManager = contextManager;
|
|
14
|
-
this._registered = [];
|
|
15
|
-
this._handlersBound = false;
|
|
16
|
-
this._disposed = false;
|
|
17
|
-
/**
|
|
18
|
-
* An event that is fired as the last act of this entity being destroyed.
|
|
19
|
-
*/
|
|
20
|
-
this.onDispose = new Event();
|
|
21
|
-
/**
|
|
22
|
-
* If `false`, this entity and its children will no longer participate in the experience.
|
|
23
|
-
*
|
|
24
|
-
* Note - to read this value, you may wish to use `enabledResolved` which will be `false` if
|
|
25
|
-
* this entity, or any of its parents, have `enabled` set to false.
|
|
26
|
-
*
|
|
27
|
-
* The precise implications of `enabled` being false will vary between entities,
|
|
28
|
-
* but in general disabled entities:
|
|
29
|
-
* - should not emit any events,
|
|
30
|
-
* - should not take any action, e.g. navigate the user to a different page,
|
|
31
|
-
* - should not perform any network communication,
|
|
32
|
-
* - should not make changes to other behaviors or components in the experience,
|
|
33
|
-
* - should minimise any runtime performance cost (e.g. detach from any frame handlers).
|
|
34
|
-
*
|
|
35
|
-
* @zprop
|
|
36
|
-
* @zdefault true
|
|
37
|
-
* @zgroup Behavior
|
|
38
|
-
* @zgrouppriority 10
|
|
39
|
-
*/
|
|
40
|
-
this.enabled = new Observable(true);
|
|
41
|
-
/**
|
|
42
|
-
* This will have value `false` if this entity, or any of its parents, have `enabled` set to `false`.
|
|
43
|
-
*
|
|
44
|
-
* To change the `enabled` status of this entity, use the `enabled` property instead.
|
|
45
|
-
*
|
|
46
|
-
* The precise implications of `enabled` being false will vary between entities,
|
|
47
|
-
* but in general disabled entities:
|
|
48
|
-
* - should not emit any events,
|
|
49
|
-
* - should not take any action, e.g. navigate the user to a different page,
|
|
50
|
-
* - should not perform any network communication,
|
|
51
|
-
* - should not make changes to other behaviors or components in the experience,
|
|
52
|
-
* - should minimise any runtime performance cost.
|
|
53
|
-
*
|
|
54
|
-
* Disabled entities will typically remain visible (if they have a visible appearance).
|
|
55
|
-
*/
|
|
56
|
-
this.enabledResolved = new Observable(true);
|
|
57
|
-
this._updateHandlers = (enabled) => {
|
|
58
|
-
if (enabled && !this._handlersBound) {
|
|
59
|
-
for (const [e, fn, priority, bindWhenDisabled] of this._registered) {
|
|
60
|
-
if (bindWhenDisabled)
|
|
61
|
-
continue;
|
|
62
|
-
e.addListener(fn, priority);
|
|
63
|
-
}
|
|
64
|
-
this._handlersBound = true;
|
|
65
|
-
}
|
|
66
|
-
if (!enabled && this._handlersBound) {
|
|
67
|
-
for (const [e, fn, _, bindWhenDisabled] of this._registered) {
|
|
68
|
-
if (bindWhenDisabled)
|
|
69
|
-
continue;
|
|
70
|
-
e.removeListener(fn);
|
|
71
|
-
}
|
|
72
|
-
this._handlersBound = false;
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
41
|
this._zcomponent = getCurrentZComponentConstruction();
|
|
76
|
-
this
|
|
42
|
+
observe(this, 'enabledResolved', this._updateHandlers, { callImmediately: true });
|
|
77
43
|
}
|
|
78
44
|
/**
|
|
79
45
|
* Gets the disposed status of the entity.
|
|
80
46
|
* @returns {boolean} The disposed status.
|
|
47
|
+
* @zignore
|
|
81
48
|
*/
|
|
82
49
|
get disposed() {
|
|
83
50
|
return this._disposed;
|
|
84
51
|
}
|
|
85
52
|
/**
|
|
86
|
-
*
|
|
87
|
-
* @
|
|
88
|
-
* @
|
|
53
|
+
* @zgroup Behavior
|
|
54
|
+
* @zgrouppriority 5
|
|
55
|
+
* @default true
|
|
89
56
|
*/
|
|
90
|
-
|
|
91
|
-
|
|
57
|
+
enabled = true;
|
|
58
|
+
/**
|
|
59
|
+
* This will have value `false` if this entity, or any of its parents, have `enabled` set to `false`.
|
|
60
|
+
*
|
|
61
|
+
* To change the `enabled` status of this entity, use the `enabled` property instead.
|
|
62
|
+
*
|
|
63
|
+
* The precise implications of `enabled` being false will vary between entities,
|
|
64
|
+
* but in general disabled entities:
|
|
65
|
+
* - should not emit any events,
|
|
66
|
+
* - should not take any action, e.g. navigate the user to a different page,
|
|
67
|
+
* - should not perform any network communication,
|
|
68
|
+
* - should not make changes to other behaviors or components in the experience,
|
|
69
|
+
* - should minimise any runtime performance cost.
|
|
70
|
+
*
|
|
71
|
+
* Disabled entities will typically remain visible (if they have a visible appearance).
|
|
72
|
+
* @zignore
|
|
73
|
+
*/
|
|
74
|
+
get enabledResolved() {
|
|
75
|
+
return this._enabledResolved;
|
|
92
76
|
}
|
|
93
77
|
/**
|
|
94
78
|
* Get the instance of the ZComponent that constructed this entity.
|
|
@@ -99,6 +83,7 @@ export class Entity {
|
|
|
99
83
|
*
|
|
100
84
|
* @param type The ZComponent class that you are expecting to receive
|
|
101
85
|
* @returns The instance of the ZComponent that constructed this component or behavior
|
|
86
|
+
* @zignore
|
|
102
87
|
*/
|
|
103
88
|
getZComponentInstance(type) {
|
|
104
89
|
if (this._zcomponent === undefined)
|
|
@@ -125,6 +110,49 @@ export class Entity {
|
|
|
125
110
|
e.removeListener(fn);
|
|
126
111
|
this._registered = this._registered.filter(entry => entry[0] !== e || entry[1] !== fn);
|
|
127
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* @zignore
|
|
115
|
+
*/
|
|
116
|
+
observe(object, prop, fn, options) {
|
|
117
|
+
const opts = { ...options };
|
|
118
|
+
this._observed.push([object, prop, fn, opts]);
|
|
119
|
+
observe(object, prop, fn, opts);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* @zignore
|
|
123
|
+
*/
|
|
124
|
+
unobserve(object, prop, fn) {
|
|
125
|
+
unobserve(object, prop, fn);
|
|
126
|
+
this._observed = this._observed.filter(entry => entry[0] !== object || entry[1] !== prop || entry[2] !== fn);
|
|
127
|
+
}
|
|
128
|
+
_updateHandlers = (enabled) => {
|
|
129
|
+
if (enabled && !this._handlersBound) {
|
|
130
|
+
for (const [e, fn, priority, bindWhenDisabled] of this._registered) {
|
|
131
|
+
if (bindWhenDisabled)
|
|
132
|
+
continue;
|
|
133
|
+
e.addListener(fn, priority);
|
|
134
|
+
}
|
|
135
|
+
for (const [e, prop, fn, opts] of this._observed) {
|
|
136
|
+
if (opts?.bindWhenDisabled)
|
|
137
|
+
continue;
|
|
138
|
+
observe(e, prop, fn, { ...opts, callImmediately: false });
|
|
139
|
+
}
|
|
140
|
+
this._handlersBound = true;
|
|
141
|
+
}
|
|
142
|
+
if (!enabled && this._handlersBound) {
|
|
143
|
+
for (const [e, fn, _, bindWhenDisabled] of this._registered) {
|
|
144
|
+
if (bindWhenDisabled)
|
|
145
|
+
continue;
|
|
146
|
+
e.removeListener(fn);
|
|
147
|
+
}
|
|
148
|
+
for (const [e, prop, fn, opts] of this._observed) {
|
|
149
|
+
if (opts?.bindWhenDisabled)
|
|
150
|
+
continue;
|
|
151
|
+
unobserve(e, prop, fn);
|
|
152
|
+
}
|
|
153
|
+
this._handlersBound = false;
|
|
154
|
+
}
|
|
155
|
+
};
|
|
128
156
|
/**
|
|
129
157
|
* Destroy this entity, cleaning up any resources that it has created and
|
|
130
158
|
* handler functions or callbacks it has registered.
|
|
@@ -142,17 +170,22 @@ export class Entity {
|
|
|
142
170
|
* This ensures that the dispose implementation of the parent class runs.
|
|
143
171
|
*
|
|
144
172
|
* @returns The result of a call to super.dispose()
|
|
173
|
+
* @zignore
|
|
145
174
|
*/
|
|
146
175
|
dispose() {
|
|
147
|
-
this
|
|
176
|
+
unobserve(this, 'enabledResolved', this._updateHandlers);
|
|
148
177
|
for (const entry of this._registered) {
|
|
149
178
|
entry[0].removeListener(entry[1]);
|
|
150
179
|
}
|
|
180
|
+
for (const entry of this._observed) {
|
|
181
|
+
unobserve(entry[0], entry[1], entry[2]);
|
|
182
|
+
}
|
|
151
183
|
this._registered = [];
|
|
184
|
+
this._observed = [];
|
|
152
185
|
this.onDispose.emit();
|
|
153
186
|
this.onDispose.clearListeners();
|
|
154
|
-
this.
|
|
187
|
+
this._disposed = true;
|
|
155
188
|
return undefined;
|
|
156
189
|
}
|
|
190
|
+
static callSuperDispose = Symbol('Calling super.dispose() is mandatory');
|
|
157
191
|
}
|
|
158
|
-
Entity.callSuperDispose = Symbol('Calling super.dispose() is mandatory');
|
package/lib/event.d.ts
CHANGED
|
@@ -3,7 +3,8 @@ import { Observable } from './observable';
|
|
|
3
3
|
/**
|
|
4
4
|
* An event emitter class used to emit events and register listeners.
|
|
5
5
|
*/
|
|
6
|
-
export declare class Event<Args extends Array<any> = [
|
|
6
|
+
export declare class Event<Args extends Array<any> = [
|
|
7
|
+
]> extends Emitter<Args> {
|
|
7
8
|
hasListeners: Observable<boolean, never>;
|
|
8
9
|
/**
|
|
9
10
|
* Emit an event.
|