@media-quest/engine 0.0.14 → 0.0.15

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 (60) hide show
  1. package/package.json +1 -2
  2. package/src/Delement/DElement.dto.ts +5 -0
  3. package/src/Delement/DElement.ts +78 -253
  4. package/src/Delement/DImg.ts +31 -70
  5. package/src/Delement/DStyle.ts +156 -156
  6. package/src/Delement/DText.ts +9 -25
  7. package/src/Delement/Ddiv.ts +10 -23
  8. package/src/Delement/button-click-action.ts +11 -0
  9. package/src/Delement/element-factory.ts +40 -0
  10. package/src/engine/DCommand.ts +21 -0
  11. package/src/engine/SchemaDto.ts +24 -0
  12. package/src/engine/SchemaEngine.ts +48 -120
  13. package/src/{player → engine}/dplayer.spec.ts +20 -17
  14. package/src/{player → engine}/dplayer.ts +29 -16
  15. package/src/engine/history-que.spec.ts +59 -0
  16. package/src/engine/history-que.ts +39 -0
  17. package/src/{player → engine}/next-que.spec.ts +19 -8
  18. package/src/engine/next-que.ts +93 -0
  19. package/src/page/Page.ts +160 -0
  20. package/src/page/media-player.ts +117 -0
  21. package/src/page/page-component.ts +113 -0
  22. package/src/page/page-result.ts +11 -0
  23. package/src/page/task-manager.ts +203 -0
  24. package/src/page/task-state.ts +55 -0
  25. package/src/page/task.ts +87 -0
  26. package/src/public-api.ts +17 -13
  27. package/src/rules/__test__/rule-engine.spec.ts +1 -1
  28. package/src/utils/DUtil.ts +110 -103
  29. package/tsconfig.tsbuildinfo +1 -0
  30. package/dist/public-api.js +0 -2245
  31. package/dist/public-api.mjs +0 -2205
  32. package/src/Delement/AudioContainer.ts +0 -169
  33. package/src/Delement/DAuto-play.ts +0 -36
  34. package/src/Delement/VideoContainer.ts +0 -199
  35. package/src/commands/DCommand.ts +0 -63
  36. package/src/commands/DCommandBus.ts +0 -60
  37. package/src/dto/AnimationDto.ts +0 -4
  38. package/src/dto/DElement.dto.ts +0 -46
  39. package/src/dto/SchemaDto.ts +0 -65
  40. package/src/engine/DPage.ts +0 -60
  41. package/src/engine/element-factory.ts +0 -52
  42. package/src/event-handlers/DEventHandler.ts +0 -29
  43. package/src/events/DEvents.ts +0 -94
  44. package/src/events/event-bus.spec.ts +0 -21
  45. package/src/events/event-bus.ts +0 -81
  46. package/src/kladd/context-menu-manager.ts +0 -56
  47. package/src/player/history-que.spec.ts +0 -46
  48. package/src/player/history-que.ts +0 -38
  49. package/src/player/next-que.ts +0 -93
  50. package/src/services/DMedia-manager.spec.ts +0 -27
  51. package/src/services/DMedia-manager.ts +0 -179
  52. package/src/services/resource-provider.ts +0 -33
  53. package/src/services/sequence-manager.spec.ts +0 -168
  54. package/src/services/sequence-manager.ts +0 -133
  55. package/src/state/Dstate.spec.ts +0 -7
  56. package/src/state/Dstate.ts +0 -105
  57. package/src/state/boolean-property.ts +0 -69
  58. package/src/state/state-service.spec.ts +0 -307
  59. package/src/state/state-service.ts +0 -251
  60. package/src/state/state-testing-helpers.ts +0 -59
package/package.json CHANGED
@@ -1,13 +1,12 @@
1
1
  {
2
2
  "name": "@media-quest/engine",
3
- "version": "0.0.14",
3
+ "version": "0.0.15",
4
4
  "description": "Rendering engine for media-quest schemas.",
5
5
  "main": "src/public-api.ts",
6
6
  "license": "MIT",
7
7
  "scripts": {
8
8
  "clean": "rimraf dist",
9
9
  "build": "npm run clean && tsup src/public-api.ts --sourcemap inline --format cjs,esm --dts",
10
- "prepublishOnlyasdf": "npm run build",
11
10
  "tsc:check:engine": "tsc --watch --noEmit"
12
11
  }
13
12
  }
@@ -0,0 +1,5 @@
1
+ import { DImgDto } from "../Delement/DImg";
2
+ import { DTextDto } from "../Delement/DText";
3
+ import { DDivDto } from "../Delement/Ddiv";
4
+
5
+ export type DElementDto = DTextDto | DImgDto | DDivDto;
@@ -1,263 +1,88 @@
1
- import { DStyle } from "./DStyle";
2
- import { DElementBaseDto } from "../dto/DElement.dto";
3
- import { DCommand, ElementCommand } from "../commands/DCommand";
4
- import { DCommandBus } from "../commands/DCommandBus";
5
- import { DEventHandler } from "../event-handlers/DEventHandler";
6
- import { DUtil } from "../utils/DUtil";
7
- import { EventBus } from "../events/event-bus";
8
- import { DTimestamp } from "../common/DTimestamp";
9
- import { AnimationDto } from "../dto/AnimationDto";
1
+ import { DStyle, PStyle } from "./DStyle";
10
2
  import { ScaleService } from "../engine/scale";
11
- import { DState } from "../state/Dstate";
12
3
 
13
- export abstract class DElement<T extends HTMLElement> {
14
- protected readonly el: T;
15
- private clickHandlerIsEnabled = true;
16
- readonly id: string;
17
- private isAnimatingSelf = false;
18
- protected currStyle: Partial<DStyle> = {
19
- fontSize: { _unit: "px", value: 100 },
20
- fontWeight: 500,
21
- textColor: "black",
22
- opacity: 1,
23
- };
24
-
25
- // private readonly onQueryChangedHandlers =
26
- private readonly eventHandlers: DEventHandler.LookUp;
27
- protected readonly commandBus: DCommandBus;
28
- protected readonly eventBus: EventBus;
29
- protected scale: ScaleService;
30
- private readonly baseDto: DElementBaseDto;
31
- protected readonly subscriptions = new Set<() => void>();
32
-
33
- protected constructor(
34
- el: T,
35
- eventBus: EventBus,
36
- commandBus: DCommandBus,
37
- dto: DElementBaseDto,
38
- scale: ScaleService
39
- ) {
40
- this.el = el;
41
- this.id = dto.id;
42
- this.baseDto = dto;
43
- // this.el.style.position = "absolute";
44
- this.commandBus = commandBus;
45
- this.eventBus = eventBus;
46
- this.eventHandlers = DEventHandler.createLookUp(dto.eventHandlers);
47
- this.scale = scale;
48
- // CLICK
49
- this.onClickHandler = this.onClickHandler.bind(this);
50
- this.el.onclick = this.onClickHandler;
51
-
52
- // HOVER
53
- this.onMouseOver = this.onMouseOver.bind(this);
54
- this.el.onmouseover = this.onMouseOver;
55
-
56
- if (dto) {
57
- this.updateStyles(dto?.style);
58
- }
59
- const commandSubscription = this.commandBus.subscribe((c) => {
60
- if (c.kind === "ELEMENT_STYLE_COMMAND" && c.targetId === this.id) {
61
- this.setStyle(c.payload.changes);
62
- }
63
- if (c.kind === "ELEMENT_DISABLE_CLICK_COMMAND" && c.targetId === this.id) {
64
- this.clickHandlerIsEnabled = false;
65
- }
66
- if (c.kind === "ELEMENT_ENABLE_CLICK_COMMAND" && c.targetId === this.id) {
67
- this.clickHandlerIsEnabled = true;
68
- }
69
- if (c.kind === "ELEMENT_ANIMATE_COMMAND" && c.targetId === this.id) {
70
- console.log("TODO IMPLEMENT ANIMATION_HANDLER IN DElement.");
71
- }
72
- }, this.id);
73
- // TODO MEMORY LEAK
74
- const eventSubscription = this.eventBus.subscribe((event) => {
75
- const handlers = this.eventHandlers.get(event.kind) ?? [];
76
- // TODO Apply Conditions in WHEN.
77
- const commands = handlers.map((h) => h.thenExecute).flat(1);
78
- commands.forEach((command) => {
79
- this.commandBus.emit(command);
80
- // this.doAction(command);
81
- });
82
-
83
- if (event.kind === "HOST_SCALE_CHANGED_EVENT") {
84
- console.log("HANDLE THIS SCALE CHANGE!!");
85
- this.updateStyles({});
86
- }
87
-
88
- if (event.kind === "STATE_QUERY_RESULT_CHANGED_EVENT") {
89
- this.queryChangedHandler(event.data);
90
- }
91
- }, "ELEMENT: ");
92
- this.normalize();
93
- this.subscriptions.add(commandSubscription);
94
- this.subscriptions.add(eventSubscription);
95
- }
96
-
97
- abstract destroy(): void;
98
-
99
- // abstract setScale(scale: number): void;
100
-
101
- // /**
102
- // * The purpose of this function is to re-emit commands from event-handlers?
103
- // * @param command
104
- // * @private
105
- // */
106
- // private handleCommandsEmittedByPrivateEventHandlers(command: DCommand) {
107
- // console.log(command);
108
- // switch (command.kind) {
109
- // case "ELEMENT_STYLE_COMMAND":
110
- // if (command.targetId === this.id) {
111
- // this.updateStyles(command.payload.changes);
112
- // }
113
- // break;
114
- // case "ELEMENT_ANIMATE_COMMAND":
115
- // // TODO CHECK FOR TARGET ID
116
- // this.handleAnimateSelfAction(command.payload);
117
- // break;
118
- // case "ELEMENT_ENABLE_CLICK_COMMAND":
119
- // this.clickHandlerIsEnabled = true;
120
- // // this.updateStyles(command.payload.changes);
121
- // break;
122
- // case "ELEMENT_DISABLE_CLICK_COMMAND":
123
- // this.clickHandlerIsEnabled = false;
124
- // // this.handleAnimateSelfAction(command.payload);
125
- // break;
126
- // case "VIDEO_PLAY_COMMAND":
127
- // this.commandBus.emit(command);
128
- // break;
129
- // case "VIDEO_PAUSE_COMMAND":
130
- // this.commandBus.emit(command);
131
- // break;
132
- // case "ENGINE_LEAVE_PAGE_COMMAND":
133
- // this.commandBus.emit(command);
134
- // break;
135
- // case "VIDEO_JUMP_TO_COMMAND":
136
- // this.commandBus.emit(command);
137
- // break;
138
- // case "VIDEO_SET_VOLUME_COMMAND":
139
- // this.commandBus.emit(command);
140
- // break;
141
- // case "AUDIO_PLAY_COMMAND":
142
- // this.commandBus.emit(command);
143
- // break;
144
- // case "AUDIO_PAUSE_COMMAND":
145
- // this.commandBus.emit(command);
146
- // break;
147
- // case "AUDIO_SET_VOLUME_COMMAND":
148
- // this.commandBus.emit(command);
149
- // break;
150
- // case "PAGE_QUE_NEXT_PAGE_COMMAND":
151
- // this.commandBus.emit(command);
152
- // break;
153
- // case "PAGE_QUE_GO_TO_SEQUENCE_COMMAND":
154
- // this.commandBus.emit(command);
155
- // break;
156
- // case "PAGE_QUE_GO_TO_PAGE_COMMAND":
157
- // this.commandBus.emit(command);
158
- // break;
159
- // case "PAGE_QUE_EXCLUDE_BY_PAGE_ID_COMMAND":
160
- // this.commandBus.emit(command);
161
- // break;
162
- // case "PAGE_QUE_JUMP_TO_PAGE_COMMAND":
163
- // this.commandBus.emit(command);
164
- // break;
165
- // case "PAGE_QUE_EXCLUDE_BY_TAG_COMMAND":
166
- // this.commandBus.emit(command);
167
- // break;
168
- // case "STATE_MUTATE_COMMAND":
169
- // this.commandBus.emit(command);
170
- // break;
171
- // default:
172
- // DUtil.neverCheck(command);
173
- // }
174
- // }
175
-
176
- private queryChangedHandler(result: DState.StateQueryResult) {
177
- const maybeHandlers = this.baseDto.onStateChange;
178
- if (!maybeHandlers) {
179
- return;
180
- }
181
- const queryHandlers = maybeHandlers.filter((h) => h.queryName === result.queryName);
182
- queryHandlers.forEach((h) => {
183
- if (result.curr) {
184
- h.whenTrue.forEach((command) => {
185
- // this.doAction(command);
186
- this.commandBus.emit(command);
187
- });
188
- } else {
189
- h.whenFalse.forEach((command) => {
190
- // this.doAction(command);
191
- this.commandBus.emit(command);
192
- });
193
- }
194
- });
195
- // console.log(findByQuery, value);
196
- // const all = this.baseDto.stateQueryChange ?? [];
197
- }
198
-
199
- private onClickHandler(_: MouseEvent) {
200
- // console.log();
201
- if (!this.clickHandlerIsEnabled) {
202
- console.log(this.id + "Click disabled");
203
- return;
204
- }
205
- const clickAction = this.baseDto.onClick;
206
- // console.log("NATIVE CLICK ELEMENT: Handlers " + clickAction?.length);
207
- if (clickAction && clickAction.length > 0) {
208
- // console.log("TARGETID: " + clickAction[0].targetId + " " + this.id);
209
- this.eventBus.emit({
210
- kind: "USER_CLICKED_EVENT",
211
- producer: "DUser",
212
- producerId: this.id,
213
- data: { elementId: this.id },
214
- timestamp: DTimestamp.now(),
215
- });
216
- clickAction.forEach((command) => {
217
- this.commandBus.emit(command);
218
- });
219
- }
220
- }
221
-
222
- private onMouseOver(_: MouseEvent) {
223
- // console.log(ev);
224
- }
4
+ export interface DElementBaseDto {
5
+ readonly style: PStyle;
6
+ readonly onMouseEnter?: PStyle;
7
+ readonly onMouseLeave?: PStyle;
8
+ readonly onMouseDown?: PStyle;
9
+ readonly onMouseUp?: PStyle;
10
+ readonly innerText?: string;
11
+ }
225
12
 
226
- setStyle(style: Partial<DStyle>) {
227
- this.updateStyles(style);
13
+ export abstract class DElement<T extends HTMLElement> {
14
+ protected currStyle: Partial<DStyle> = {
15
+ fontSize: { _unit: "px", value: 100 },
16
+ fontWeight: 500,
17
+ textColor: "black",
18
+ opacity: 1,
19
+ };
20
+
21
+ protected constructor(
22
+ protected readonly el: T,
23
+ protected readonly dto: DElementBaseDto,
24
+ protected readonly scale: ScaleService,
25
+ ) {
26
+ if (dto.innerText) {
27
+ this.el.innerText = dto.innerText;
228
28
  }
229
-
230
- appendYourself(parent: HTMLElement) {
231
- parent.append(this.el);
232
- // console.log(parent);
29
+ this.setStyle = this.setStyle.bind(this);
30
+ this.normalize = this.normalize.bind(this);
31
+ this.appendYourself = this.appendYourself.bind(this);
32
+ this.updateStyles = this.updateStyles.bind(this);
33
+ const { onMouseEnter, onMouseLeave } = dto;
34
+
35
+ if (onMouseEnter) {
36
+ this.el.onmouseenter = () => {
37
+ this.setStyle(onMouseEnter);
38
+ };
233
39
  }
234
-
235
- private handleAnimateSelfAction(dto: AnimationDto) {
236
- this.isAnimatingSelf = true;
237
- const { keyframes, options } = dto;
238
-
239
- const animation = this.el.animate(keyframes, options.duration);
240
- animation.onfinish = (e: AnimationPlaybackEvent) => {
241
- console.log(e.type);
242
- this.isAnimatingSelf = false;
243
- };
244
- animation.onremove = () => {
245
- console.warn("ANIMATION REMOVED: " + animation.id);
246
- this.isAnimatingSelf = false;
247
- };
248
- animation.oncancel = () => {
249
- console.warn("ANIMATION CANCELED: " + animation.id);
250
- this.isAnimatingSelf = false;
251
- };
40
+ if (onMouseLeave) {
41
+ this.el.onmouseleave = () => {
42
+ this.setStyle(onMouseLeave);
43
+ };
252
44
  }
253
45
 
254
- private normalize() {
255
- DStyle.normalize(this.el);
256
- }
46
+ this.el.onclick = () => {
47
+ // if (onClick2) {
48
+ this.onclick();
49
+ // }
50
+ };
51
+ this.normalize();
257
52
 
258
- protected updateStyles(style: Partial<DStyle>) {
259
- this.currStyle = Object.assign(this.currStyle, style);
260
- DStyle.applyStyles(this.el, this.currStyle, this.scale.scale);
261
- window.getComputedStyle(this.el);
53
+ if (dto) {
54
+ this.updateStyles(dto?.style);
262
55
  }
56
+ }
57
+
58
+ /**
59
+ * This method is called when the element is clicked.
60
+ * This method shall be overridden by the pageClass.
61
+ * @param actions
62
+ */
63
+ onclick() {
64
+ // console.warn("onclick not implemented");
65
+ }
66
+
67
+ setStyle(style: PStyle) {
68
+ this.updateStyles(style);
69
+ }
70
+
71
+ appendYourself(parent: { append: (el: HTMLElement) => void }) {
72
+ parent.append(this.el);
73
+ // console.log(parent);
74
+ }
75
+
76
+ private normalize() {
77
+ this.el.style.padding = "0";
78
+ this.el.style.margin = "0";
79
+ this.el.style.position = "absolute";
80
+ this.el.style.boxSizing = "border-box";
81
+ }
82
+
83
+ protected updateStyles(style: Partial<DStyle>) {
84
+ this.currStyle = Object.assign(this.currStyle, style);
85
+ DStyle.applyStyles(this.el, this.currStyle, this.scale.scale);
86
+ window.getComputedStyle(this.el);
87
+ }
263
88
  }
@@ -1,78 +1,39 @@
1
- import { DElement } from "./DElement";
2
- import { DImgDto } from "../dto/DElement.dto";
3
- import { DCommandBus } from "../commands/DCommandBus";
4
- import { EventBus } from "../events/event-bus";
1
+ import { DElement, DElementBaseDto } from "./DElement";
5
2
  import { ScaleService } from "../engine/scale";
6
3
  import { DTimestamp } from "../common/DTimestamp";
7
4
 
8
- export class DImg extends DElement<HTMLImageElement> {
9
- private static IMAGE_COUNT = 0;
10
- private readonly imageCount: number;
11
- readonly TAG: string;
12
- readonly TIMING_TAG: string;
13
- private readonly loadStart: DTimestamp;
14
-
15
- constructor(
16
- private readonly dto: DImgDto,
17
- private readonly actionSubject: DCommandBus,
18
- readonly eventBus: EventBus,
19
- readonly scaleService: ScaleService
20
- ) {
21
- super(document.createElement("img"), eventBus, actionSubject, dto, scaleService);
22
- DImg.IMAGE_COUNT += 1;
23
- this.imageCount = DImg.IMAGE_COUNT;
24
- this.TAG = "[D_IMG " + DImg.IMAGE_COUNT + " ]: ";
25
- this.TIMING_TAG = "load-time (" + DImg.IMAGE_COUNT + ") ";
26
- this.el.loading = "eager";
27
- this.el.style.position = "absolute";
28
- this.setStyle(dto.style);
29
- this.loadStart = DTimestamp.now();
30
- // Bind Handlers
31
- this.onError = this.onError.bind(this);
32
- this.onLoad = this.onLoad.bind(this);
33
- // this.onClick = this.onClick.bind(this);
34
- this.el.onload = this.onLoad;
35
- this.el.onerror = this.onError;
36
- // this.el.onclick = this.onClick;
37
- this.el.src = dto.url;
38
- console.time(this.TIMING_TAG);
39
- }
40
-
41
- log(): void {}
5
+ export interface DImgDto extends DElementBaseDto {
6
+ readonly _tag: "img";
7
+ // readonly id: string;
8
+ readonly url: string;
9
+ }
42
10
 
43
- setScale(scale: number) {
44
- console.log(scale);
45
- this.setStyle(this.currStyle);
46
- }
47
- destroy() {
48
- this.subscriptions.forEach((unsubscribe) => {
49
- unsubscribe();
50
- });
51
- }
11
+ export class DImg extends DElement<HTMLImageElement> {
12
+ private static IMAGE_COUNT = 0;
13
+ private readonly imageCount: number;
14
+ readonly TAG: string;
15
+ readonly TIMING_TAG: string;
16
+ private readonly loadStart: DTimestamp;
52
17
 
53
- private onError(ev: Event | string) {
54
- if (ev instanceof Event) {
55
- console.log(this.TAG + " " + ev.type);
56
- } else {
57
- console.log(this.TAG + ev);
58
- }
59
- }
18
+ constructor(
19
+ protected readonly dto: DImgDto,
20
+ readonly scaleService: ScaleService,
21
+ ) {
22
+ super(document.createElement("img"), dto, scaleService);
23
+ DImg.IMAGE_COUNT += 1;
24
+ this.imageCount = DImg.IMAGE_COUNT;
25
+ this.TAG = "[D_IMG " + DImg.IMAGE_COUNT + " ]: ";
26
+ this.TIMING_TAG = "load-time (" + DImg.IMAGE_COUNT + ") ";
27
+ this.el.loading = "eager";
28
+ this.el.style.position = "absolute";
29
+ this.setStyle(dto.style);
60
30
 
61
- private onLoad(_: Event) {
62
- const loadTime = DTimestamp.diffNow(this.loadStart);
31
+ this.loadStart = DTimestamp.now();
32
+ this.el.onload = () => {};
33
+ this.el.onerror = () => {};
34
+ this.el.src = dto.url;
35
+ console.time(this.TIMING_TAG);
36
+ }
63
37
 
64
- this.eventBus.emit({
65
- kind: "IMAGE_LOADED_EVENT",
66
- producer: "DImage",
67
- producerId: this.id,
68
- timestamp: DTimestamp.now(),
69
- data: {
70
- loadTime,
71
- naturalHeight: this.el.naturalHeight,
72
- naturalWidth: this.el.naturalWidth,
73
- height: this.el.height,
74
- width: this.el.width,
75
- },
76
- });
77
- }
38
+ log(): void {}
78
39
  }