@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
@@ -0,0 +1,160 @@
1
+ import { DElement } from "../Delement/DElement";
2
+ import { TaskManager } from "./task-manager";
3
+ import { createDElement } from "../Delement/element-factory";
4
+ import { ScaleService } from "../engine/scale";
5
+ import { Task } from "./task";
6
+ import { PStyle } from "../Delement/DStyle";
7
+ import { ButtonClickAction } from "../Delement/button-click-action";
8
+ import { PageComponent, PageComponentDto } from "./page-component";
9
+ import { DElementDto } from "../Delement/DElement.dto";
10
+ import { Fact } from "../rules/fact";
11
+ import { DTimestamp } from "../common/DTimestamp";
12
+ import { PageResult } from "./page-result";
13
+ import { TaskState, TaskStateDiff } from "./task-state";
14
+
15
+ export interface VideoPlayerDto {
16
+ playUrl: string;
17
+ style?: PStyle;
18
+ }
19
+
20
+ export interface PageDto {
21
+ readonly id: string;
22
+ readonly tags: string[];
23
+ staticElements: Array<DElementDto>;
24
+ background: string;
25
+ components: Array<PageComponentDto>;
26
+ videoPlayer?: VideoPlayerDto;
27
+ initialTasks: Array<Task>;
28
+ }
29
+
30
+ export const PageDto = {
31
+ createDummy: (id: number): PageDto => {
32
+ return {
33
+ id: "id" + id,
34
+ tags: [],
35
+ staticElements: [],
36
+ background: "white",
37
+ // videoList: [],
38
+ components: [
39
+ {
40
+ el: {
41
+ _tag: "div",
42
+ style: { x: 10, y: 0, w: 40, h: 20, backgroundColor: "red" },
43
+ children: [],
44
+ innerText: "Next btn " + id,
45
+ },
46
+ },
47
+ ],
48
+
49
+ initialTasks: [],
50
+ };
51
+ },
52
+ };
53
+
54
+ export class Page {
55
+ private readonly TAG = "[ DPage ]: ";
56
+ private staticElements: DElement<HTMLElement>[] = [];
57
+ private components: PageComponent[] = [];
58
+ private pageEntered: DTimestamp = DTimestamp.now();
59
+ private previousState: TaskState | false = false;
60
+
61
+ constructor(
62
+ private readonly dto: PageDto,
63
+ private readonly taskManager: TaskManager,
64
+ private readonly scaleService: ScaleService,
65
+ private readonly onCompleted: (result: PageResult) => void,
66
+ ) {
67
+ this.components = dto.components.map((el) => {
68
+ const component = new PageComponent(el, scaleService);
69
+ component.onClick = (actions) => {
70
+ this.handleButtonAction(actions);
71
+ };
72
+ this.components.push(component);
73
+
74
+ return component;
75
+ });
76
+ dto.staticElements.forEach((el) => {
77
+ const element = createDElement(el, scaleService);
78
+ this.staticElements.push(element);
79
+ });
80
+
81
+ if (dto.videoPlayer) {
82
+ this.taskManager.loadVideo(dto.videoPlayer.playUrl);
83
+ if (dto.videoPlayer.style) {
84
+ this.taskManager.setVideoStyles(dto.videoPlayer.style);
85
+ }
86
+ }
87
+
88
+ if (dto.initialTasks.length) {
89
+ this.taskManager.autoPlaySequence(dto.initialTasks);
90
+ }
91
+ }
92
+
93
+ private createPageResult(facts: Fact[]): PageResult {
94
+ const pageExited = DTimestamp.now();
95
+ const pageTime = DTimestamp.diff(this.pageEntered, pageExited);
96
+ return {
97
+ pageId: this.dto.id,
98
+ pageEntered: this.pageEntered,
99
+ pageExited,
100
+ pageTime,
101
+ collectedFacts: facts,
102
+ };
103
+ }
104
+ private handleButtonAction(a: ButtonClickAction) {
105
+ switch (a.kind) {
106
+ case "next-page":
107
+ const nextPageResult = this.createPageResult([]);
108
+ this.onCompleted(nextPageResult);
109
+ break;
110
+ case "play-video":
111
+ this.taskManager.execute(a.task);
112
+ break;
113
+ case "play-audio":
114
+ this.taskManager.execute(a.task);
115
+ break;
116
+ case "pause-video":
117
+ this.taskManager.pauseVideo();
118
+ break;
119
+ case "pause-audio":
120
+ this.taskManager.pauseAudio();
121
+ break;
122
+ case "submit-fact":
123
+ const submitFactResult = this.createPageResult([a.fact]);
124
+ this.onCompleted(submitFactResult);
125
+ break;
126
+ case "submit-form":
127
+ // TODO IMPLEMENT Collection of form-data // LOOP OVER ALL INPUTS
128
+ const submitFormResult = this.createPageResult([]);
129
+ this.onCompleted(submitFormResult);
130
+ break;
131
+ default:
132
+ const _exhaustiveCheck: never = a;
133
+ console.log(_exhaustiveCheck);
134
+ }
135
+ }
136
+
137
+ appendYourself(parent: HTMLElement) {
138
+ this.staticElements.forEach((el) => {
139
+ el.appendYourself(parent);
140
+ });
141
+
142
+ this.components.forEach((comp) => {
143
+ comp.appendToParent(parent);
144
+ });
145
+ }
146
+
147
+ destroy() {
148
+ // console.log("DESTROY PAGE ");
149
+ this.taskManager.clear();
150
+ }
151
+
152
+ tick() {
153
+ const prev = this.previousState;
154
+ const curr = this.taskManager.getState();
155
+ const diff = TaskState.getDiff(curr, prev);
156
+ this.components.forEach((comp) => {
157
+ comp.updateState(diff);
158
+ });
159
+ }
160
+ }
@@ -0,0 +1,117 @@
1
+ import { DStyle, PStyle } from "../Delement/DStyle";
2
+
3
+ type MediaPlayerEvent =
4
+ | { kind: "video-ended"; url: string }
5
+ | { kind: "audio-ended"; url: string }
6
+ | { kind: "error"; message: string };
7
+
8
+ export interface IMediaPlayer {
9
+ playVideo(url: string, loop: boolean): void;
10
+ playAudio(url: string, loop: boolean): void;
11
+ pauseVideo(): void;
12
+ pauseAudio(): void;
13
+ clear(): void;
14
+ hideVideo(): void;
15
+ loadVideo(url: string): void;
16
+ appendToParent(el: { appendChild: (el: HTMLElement) => void }): void;
17
+ }
18
+
19
+ export const createMediaPlayer = (onEvent: (mediaPlayerEvent: MediaPlayerEvent) => void): IMediaPlayer => {
20
+ const videoElement = document.createElement("video");
21
+ const audioElement = document.createElement("audio");
22
+ const videoStyles: PStyle = { h: 40, w: 80, borderStyle: "solid", borderWidth: { _unit: "px", value: 5 } };
23
+
24
+ DStyle.normalize(videoElement);
25
+ DStyle.applyStyles(videoElement, videoStyles, 1);
26
+ videoElement.onended = () => {
27
+ console.log("VIDEO ENDED");
28
+ };
29
+ videoElement.onerror = () => {
30
+ console.log("VIDEO ERROR WHY ?? ");
31
+ };
32
+ audioElement.onended = () => {
33
+ console.log("AUDIO ENDED");
34
+ };
35
+
36
+ const playVideo = (url: string, loop = false) => {
37
+ if (videoElement.src !== url) {
38
+ videoElement.src = url;
39
+ }
40
+
41
+ try {
42
+ videoElement.play();
43
+ } catch (e) {
44
+ console.log("VIDEO PLAY ERROR");
45
+ console.log(e);
46
+ }
47
+ };
48
+
49
+ const playAudio = (url: string, loop = false) => {
50
+ audioElement.loop = loop;
51
+ if (audioElement.src !== url) {
52
+ audioElement.src = url;
53
+ }
54
+ // audioElement.play();
55
+ try {
56
+ audioElement.play();
57
+ } catch (e) {
58
+ console.log("AUDIO PLAY ERROR");
59
+ console.log(e);
60
+ }
61
+ };
62
+
63
+ const showVideo = () => {
64
+ videoElement.style.visibility = "visible";
65
+ };
66
+
67
+ const pauseVideo = () => {
68
+ try {
69
+ videoElement.pause();
70
+ } catch (e) {
71
+ console.log("VIDEO PAUSE ERROR");
72
+ console.log(e);
73
+ }
74
+ };
75
+
76
+ const loadVideo = (url: string) => {
77
+ if (videoElement.src !== url) {
78
+ videoElement.src = url;
79
+ }
80
+ };
81
+ const pauseAudio = () => {
82
+ try {
83
+ audioElement.pause();
84
+ } catch (e) {
85
+ console.log("AUDIO PAUSE ERROR");
86
+ console.log(e);
87
+ }
88
+ };
89
+ const appendToParent = (el: { appendChild: (el: HTMLElement) => void }) => {
90
+ el.appendChild(videoElement);
91
+ el.appendChild(audioElement);
92
+ };
93
+ const clear = () => {
94
+ pauseVideo();
95
+ pauseAudio();
96
+ videoElement.src = "";
97
+ audioElement.src = "";
98
+ };
99
+ const hideVideo = () => {
100
+ videoElement.style.visibility = "hidden";
101
+ };
102
+
103
+ return {
104
+ playVideo,
105
+ playAudio,
106
+ pauseVideo,
107
+ loadVideo,
108
+ clear,
109
+ pauseAudio,
110
+ hideVideo,
111
+ appendToParent,
112
+ // mediaLayer,
113
+ // videoElement,
114
+ // audioElement,
115
+ // videoStyles,
116
+ };
117
+ };
@@ -0,0 +1,113 @@
1
+ import { PStyle } from "../Delement/DStyle";
2
+ import { ScaleService } from "../engine/scale";
3
+ import { DElement } from "../Delement/DElement";
4
+ import { createDElement } from "../Delement/element-factory";
5
+ import { ButtonClickAction } from "../Delement/button-click-action";
6
+ import { DElementDto } from "../Delement/DElement.dto";
7
+ import { TaskState, TaskStateDiff } from "./task-state";
8
+
9
+ export interface PageComponentDto {
10
+ readonly onClick?: ButtonClickAction;
11
+ readonly el: DElementDto;
12
+ readonly whenVideoPlay?: PStyle;
13
+ readonly whenVideoPaused?: PStyle;
14
+ readonly whenAudioPlaying?: PStyle;
15
+ readonly whenAudioPaused?: PStyle;
16
+ readonly whenAudioBlocked?: PStyle;
17
+ readonly whenVideoBlocked?: PStyle;
18
+ readonly whenAudioUnblocked?: PStyle;
19
+ readonly whenVideoUnblocked?: PStyle;
20
+ readonly whenResponseBlocked?: PStyle;
21
+ readonly whenResponseUnblocked?: PStyle;
22
+ readonly whenFormInputBlocked?: PStyle;
23
+ readonly whenFormInputUnblocked?: PStyle;
24
+ }
25
+
26
+ const isFalse = (bool?: boolean): bool is false => bool === false;
27
+ const isTrue = (bool?: boolean): bool is true => bool === true;
28
+
29
+ export class PageComponent {
30
+ private readonly TAG = "[ PageComponent ]: ";
31
+ private el: DElement<HTMLElement>;
32
+ private prevState: TaskState | false = false;
33
+
34
+ constructor(
35
+ readonly dto: PageComponentDto,
36
+ readonly scale: ScaleService,
37
+ ) {
38
+ this.el = createDElement(dto.el, scale);
39
+ this.el.onclick = () => {
40
+ if (dto.onClick) {
41
+ this.onClick(dto.onClick);
42
+ } else {
43
+ console.warn(this.TAG + "onClick not implemented");
44
+ }
45
+ };
46
+ }
47
+
48
+ onClick(action: ButtonClickAction) {
49
+ console.warn(this.TAG + "onclick not implemented");
50
+ }
51
+
52
+ updateState(state: TaskStateDiff) {
53
+ this.handleStateChanges(state);
54
+ }
55
+
56
+ setState(state: TaskState) {
57
+ const prev = this.prevState;
58
+ const diff = TaskState.getDiff(state, prev);
59
+ this.prevState = state;
60
+ if (Object.keys(diff).length > 0) {
61
+ this.handleStateChanges(diff);
62
+ }
63
+ }
64
+
65
+ private handleStateChanges(diff: TaskStateDiff) {
66
+ const {
67
+ videoIsPlaying,
68
+ audioIsPlaying,
69
+ blockAudio,
70
+ blockVideo,
71
+ blockResponseButton,
72
+ blockFormInput,
73
+ isGifMode,
74
+ } = diff;
75
+ const {
76
+ whenAudioPaused,
77
+ whenVideoPaused,
78
+ whenAudioPlaying,
79
+ whenFormInputBlocked,
80
+ whenResponseBlocked,
81
+ whenVideoPlay,
82
+ whenAudioBlocked,
83
+ whenVideoBlocked,
84
+ whenAudioUnblocked,
85
+ whenVideoUnblocked,
86
+ whenResponseUnblocked,
87
+ whenFormInputUnblocked,
88
+ } = this.dto;
89
+ if (isTrue(audioIsPlaying) && whenAudioPlaying) {
90
+ this.el.setStyle(whenAudioPlaying);
91
+ }
92
+ if (isFalse(audioIsPlaying) && whenAudioPaused) {
93
+ this.el.setStyle(whenAudioPaused);
94
+ }
95
+ if (isTrue(videoIsPlaying) && whenVideoPlay) {
96
+ this.el.setStyle(whenVideoPlay);
97
+ }
98
+ if (isFalse(videoIsPlaying) && whenVideoPaused) {
99
+ this.el.setStyle(whenVideoPaused);
100
+ }
101
+ // if (isTrue(blockAudio) && whenAudioBlocked) {
102
+ // this.el.setStyle(whenAudioBlocked);
103
+ // }
104
+ // if (isTrue(blockVideo) && whenVideoBlocked) {
105
+ // this.el.setStyle(whenVideoBlocked);
106
+ // }
107
+ }
108
+
109
+ appendToParent(parent: { append: (el: HTMLElement) => void }) {
110
+ // parent.appendChild(this.el.);
111
+ this.el.appendYourself(parent);
112
+ }
113
+ }
@@ -0,0 +1,11 @@
1
+ import { DTimestamp } from "../common/DTimestamp";
2
+ import { Fact } from "../rules/fact";
3
+
4
+ export interface PageResult {
5
+ readonly pageId: string;
6
+ readonly pagePrefix?: string;
7
+ readonly pageEntered: DTimestamp;
8
+ readonly pageExited: DTimestamp;
9
+ readonly pageTime: DTimestamp.Diff;
10
+ readonly collectedFacts: Fact[];
11
+ }
@@ -0,0 +1,203 @@
1
+ import { DTimestamp } from "../common/DTimestamp";
2
+ import { Task } from "./task";
3
+ import { DStyle, PStyle } from "../Delement/DStyle";
4
+ import { ScaleService } from "../engine/scale";
5
+ import { TaskState } from "./task-state";
6
+
7
+ export class TaskManager {
8
+ private readonly TAG = "[TaskManager]: ";
9
+ private readonly videoElement = document.createElement("video");
10
+ private readonly audioElement = document.createElement("audio");
11
+ private readonly showConsoleLogs = false;
12
+ private videoStyles: PStyle = {
13
+ h: 40,
14
+ w: 80,
15
+ y: 60,
16
+ x: 10,
17
+ };
18
+ private runningTask: { task: Task; startedAt: DTimestamp } | false = false;
19
+ private taskList: Array<Task> = [];
20
+ private delayRef: number | false = false;
21
+
22
+ clear() {
23
+ if (this.showConsoleLogs) console.log(this.TAG + "CLEAR");
24
+ if (typeof this.delayRef === "number") {
25
+ window.clearTimeout(this.delayRef);
26
+ }
27
+
28
+ this.pauseVideo();
29
+ this.pauseAudio();
30
+ this.videoElement.src = "";
31
+ this.audioElement.src = "";
32
+ this.taskList = [];
33
+ this.runningTask = false;
34
+ this.hideVideo();
35
+ }
36
+
37
+ getState(): TaskState {
38
+ const c = this.runningTask;
39
+ const isGifMode = this.videoElement.loop;
40
+ const audioIsPlaying = c && c.task.kind === "play-audio-task" && !this.audioElement.paused;
41
+ const videoIsPlaying = c && c.task.kind === "play-video-task" && !this.videoElement.paused;
42
+ const blockResponseButton = c && c.task.blockResponseButton;
43
+ const blockAudio = c && c.task.blockAudio;
44
+ const blockVideo = c && c.task.blockVideo;
45
+ const blockFormInput = c && c.task.blockFormInput;
46
+ return {
47
+ audioIsPlaying,
48
+ isGifMode,
49
+ videoIsPlaying,
50
+ blockFormInput,
51
+ blockResponseButton,
52
+ blockAudio,
53
+ blockVideo,
54
+ };
55
+ }
56
+
57
+ constructor(
58
+ private readonly mediaLayer: HTMLDivElement,
59
+ private readonly scale: ScaleService,
60
+ private readonly onError: (error: string) => void,
61
+ ) {
62
+ this.hideVideo();
63
+ this.mediaLayer.appendChild(this.videoElement);
64
+ this.mediaLayer.appendChild(this.audioElement);
65
+ DStyle.normalize(this.videoElement);
66
+ DStyle.applyStyles(this.videoElement, this.videoStyles, this.scale.scale);
67
+
68
+ this.videoElement.onended = () => {
69
+ const next = this.getNextTask();
70
+ if (next) {
71
+ this.execute(next);
72
+ } else {
73
+ this.runningTask = false;
74
+ }
75
+ };
76
+ this.videoElement.onerror = (e) => {
77
+ if (e instanceof Event) {
78
+ console.log(e.target);
79
+ console.log("IS EVENT");
80
+ }
81
+ console.log("VIDEO ERROR WHY ?? ");
82
+ onError("Error playing video." + this.videoElement.src);
83
+ };
84
+ this.audioElement.onended = () => {
85
+ const next = this.getNextTask();
86
+ if (next) {
87
+ this.execute(next);
88
+ } else {
89
+ this.runningTask = false;
90
+ }
91
+ };
92
+ }
93
+
94
+ execute(task: Task): boolean {
95
+ // console.log("EXECUTE TASK" + task.kind);
96
+ const curr = this.runningTask;
97
+
98
+ // Check if we should remove the current task.
99
+ if (curr && Task.shallRemoveCurrent(task) && Task.notEq(curr.task, task)) {
100
+ this.pauseAudio();
101
+ this.pauseVideo();
102
+ this.runningTask = false;
103
+ }
104
+
105
+ if (!curr) {
106
+ this.runningTask = { startedAt: DTimestamp.now(), task: task };
107
+ } else if (Task.notEq(curr.task, task)) {
108
+ this.runningTask = { startedAt: DTimestamp.now(), task: task };
109
+ }
110
+
111
+ if (task.priority === "replace-all" || task.priority === "replace-queue") {
112
+ this.taskList = [];
113
+ }
114
+
115
+ // STARTING PLAY VIDEO
116
+ if (task.kind === "play-video-task") {
117
+ this.showVideo();
118
+ // this.pauseAudio()
119
+ this.loadVideo(task.url);
120
+ if (task.loop) {
121
+ this.videoElement.loop = true;
122
+ } else {
123
+ this.videoElement.loop = false;
124
+ }
125
+
126
+ try {
127
+ this.videoElement.play();
128
+ } catch (e) {
129
+ console.error(e);
130
+ }
131
+ }
132
+
133
+ // STARTING PLAY AUDIO
134
+ if (task.kind === "play-audio-task") {
135
+ if (!this.videoElement.loop) {
136
+ this.pauseVideo();
137
+ }
138
+ if (task.url !== this.audioElement.src) {
139
+ this.audioElement.src = task.url;
140
+ }
141
+ this.audioElement.play();
142
+ }
143
+ return true;
144
+ }
145
+
146
+ setVideoStyles(styles: PStyle) {
147
+ this.videoStyles = styles;
148
+ DStyle.applyStyles(this.videoElement, this.videoStyles, this.scale.scale);
149
+ }
150
+
151
+ autoPlaySequence(tasks: Task[]) {
152
+ this.taskList = [...tasks];
153
+ const next = this.getNextTask();
154
+ if (next) {
155
+ this.execute(next);
156
+ }
157
+ }
158
+
159
+ loadVideo(url: string) {
160
+ // console.log("LOAD VIDEO " + !!url + " ");
161
+ if (this.videoElement.src !== url) {
162
+ this.videoElement.src = url;
163
+ }
164
+ this.showVideo();
165
+ }
166
+
167
+ private showVideo() {
168
+ this.videoElement.style.display = "block";
169
+ }
170
+
171
+ private hideVideo() {
172
+ this.videoElement.style.display = "none";
173
+ }
174
+
175
+ pauseVideo() {
176
+ try {
177
+ if (!this.videoElement.loop) {
178
+ this.videoElement.pause();
179
+ }
180
+ } catch (e) {
181
+ console.log(e);
182
+ this.onError("Error pausing video.");
183
+ }
184
+ }
185
+
186
+ pauseAudio() {
187
+ try {
188
+ if (!this.audioElement.loop) {
189
+ this.audioElement.pause();
190
+ }
191
+ } catch (e) {
192
+ console.log(e);
193
+ this.onError("Error pausing audio.");
194
+ }
195
+ }
196
+
197
+ private getNextTask(): Task | false {
198
+ // console.log("Getting next task.");
199
+ // console.log(this.taskList);
200
+ const first = this.taskList.shift();
201
+ return first ?? false;
202
+ }
203
+ }
@@ -0,0 +1,55 @@
1
+ export type TaskState = {
2
+ audioIsPlaying: boolean;
3
+ isGifMode: boolean;
4
+ videoIsPlaying: boolean;
5
+ blockFormInput: boolean;
6
+ blockResponseButton: boolean;
7
+ blockAudio: boolean;
8
+ blockVideo: boolean;
9
+ };
10
+
11
+ export type TaskStateDiff = Partial<TaskState> & { __diffed__: true };
12
+ export const TaskState = {
13
+ eq: (a: TaskState, b: TaskState) => {
14
+ return (
15
+ a.audioIsPlaying === b.audioIsPlaying &&
16
+ a.isGifMode === b.isGifMode &&
17
+ a.videoIsPlaying === b.videoIsPlaying &&
18
+ a.blockFormInput === b.blockFormInput &&
19
+ a.blockResponseButton === b.blockResponseButton &&
20
+ a.blockAudio === b.blockAudio &&
21
+ a.blockVideo === b.blockVideo
22
+ );
23
+ },
24
+
25
+ getDiff: (curr: TaskState, prev: TaskState | false): TaskStateDiff => {
26
+ if (prev === false) {
27
+ return curr as TaskStateDiff;
28
+ }
29
+
30
+ const diff = {} as TaskStateDiff;
31
+
32
+ if (curr.audioIsPlaying !== prev.audioIsPlaying) {
33
+ diff.audioIsPlaying = curr.audioIsPlaying;
34
+ }
35
+ if (curr.isGifMode !== prev.isGifMode) {
36
+ diff.isGifMode = curr.isGifMode;
37
+ }
38
+ if (curr.videoIsPlaying !== prev.videoIsPlaying) {
39
+ diff.videoIsPlaying = curr.videoIsPlaying;
40
+ }
41
+ if (curr.blockFormInput !== prev.blockFormInput) {
42
+ diff.blockFormInput = curr.blockFormInput;
43
+ }
44
+ if (curr.blockResponseButton !== prev.blockResponseButton) {
45
+ diff.blockResponseButton = curr.blockResponseButton;
46
+ }
47
+ if (curr.blockAudio !== prev.blockAudio) {
48
+ diff.blockAudio = curr.blockAudio;
49
+ }
50
+ if (curr.blockVideo !== prev.blockVideo) {
51
+ diff.blockVideo = curr.blockVideo;
52
+ }
53
+ return diff;
54
+ },
55
+ };