@media-quest/engine 0.0.26 → 0.0.27

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.
@@ -0,0 +1,510 @@
1
+ type Fact = Fact.Numeric | Fact.String;
2
+ declare namespace Fact {
3
+ interface Numeric {
4
+ readonly kind: "numeric-fact";
5
+ readonly value: number;
6
+ readonly label: string;
7
+ readonly referenceId: string;
8
+ readonly referenceLabel: string;
9
+ }
10
+ interface String {
11
+ readonly kind: "string-fact";
12
+ readonly label: string;
13
+ readonly value: string;
14
+ readonly referenceId: string;
15
+ readonly referenceLabel: string;
16
+ }
17
+ }
18
+
19
+ type DTimestamp = number & {
20
+ __timestamp__: true;
21
+ };
22
+ declare namespace DTimestamp {
23
+ const now: () => DTimestamp;
24
+ type Diff = number & {
25
+ __diff__: true;
26
+ };
27
+ const addMills: (t: DTimestamp, ms: number) => DTimestamp;
28
+ const diff: (t1: DTimestamp, t2: DTimestamp) => Diff;
29
+ const diffNow: (t: DTimestamp) => Diff;
30
+ }
31
+
32
+ interface _MqEvent<K extends string, P extends object> {
33
+ readonly kind: K;
34
+ readonly timestamp: DTimestamp;
35
+ readonly payload: P;
36
+ }
37
+ type MqEventEngineStart = _MqEvent<"engine-start", {
38
+ readonly schemaId: string;
39
+ readonly schemaPrefix: string;
40
+ }>;
41
+ interface MqEventPageEnter extends _MqEvent<"page-enter", {
42
+ readonly pageId: string;
43
+ readonly pagePrefix: string;
44
+ }> {
45
+ }
46
+ interface MqEventPageLeave extends _MqEvent<"page-leave", {
47
+ readonly pageId: string;
48
+ readonly pagePrefix: string;
49
+ }> {
50
+ }
51
+ type MqEventUserClicked = _MqEvent<"user-clicked", {
52
+ readonly pageId: string;
53
+ pagePrefix: string;
54
+ action: string;
55
+ descriptions: string;
56
+ }>;
57
+ type MqEvent = MqEventPageEnter | MqEventPageLeave | MqEventEngineStart | MqEventUserClicked;
58
+ declare const MqEvent: {
59
+ readonly engineStart: (schemaId: string, schemaPrefix: string) => MqEventEngineStart;
60
+ readonly pageEnter: (pageId: string, pagePrefix: string) => MqEventPageEnter;
61
+ readonly pageLeave: (pageId: string, pagePrefix: string) => MqEventPageLeave;
62
+ readonly userClicked: (data: {
63
+ pageId: string;
64
+ pagePrefix: string;
65
+ action: string;
66
+ descriptions: string;
67
+ }) => MqEventUserClicked;
68
+ };
69
+
70
+ interface SchemaResult {
71
+ readonly schemaId: string;
72
+ readonly pagesLeft: number;
73
+ readonly predefinedFacts: ReadonlyArray<Fact>;
74
+ readonly eventLog: ReadonlyArray<MqEvent>;
75
+ readonly answers: ReadonlyArray<Fact>;
76
+ }
77
+
78
+ type Condition = Condition.String | Condition.Numeric | Condition.Complex;
79
+ declare namespace Condition {
80
+ type StringOperator = "eq" | "not-eq" | "longer-then" | "shorter-then";
81
+ type NumericOperator = "eq" | "not-eq" | "greater-then" | "less-then" | "greater-then-inclusive" | "less-then-inclusive";
82
+ interface Numeric {
83
+ readonly referenceId: string;
84
+ readonly referenceLabel: string;
85
+ readonly valueLabel: string;
86
+ readonly kind: "numeric-condition";
87
+ readonly operator: NumericOperator;
88
+ readonly value: number;
89
+ }
90
+ interface String {
91
+ readonly referenceId: string;
92
+ readonly referenceLabel: string;
93
+ readonly valueLabel: string;
94
+ readonly kind: "string-condition";
95
+ readonly operator: StringOperator;
96
+ readonly value: string;
97
+ }
98
+ interface Complex {
99
+ readonly kind: "complex-condition";
100
+ readonly name: string;
101
+ readonly all: ReadonlyArray<Condition.Simple>;
102
+ readonly some: ReadonlyArray<Condition.Simple>;
103
+ }
104
+ type Simple = Condition.String | Condition.Numeric;
105
+ /**
106
+ * An empty condition will evaluate to false,
107
+ * @param condition: Condition.Any
108
+ * @param facts
109
+ */
110
+ const evaluate: (condition: Condition, facts: ReadonlyArray<Fact>) => boolean;
111
+ const isEmpty: (complex: Complex) => boolean;
112
+ const getAllSimpleConditions: (condition: Condition | Array<Condition>) => ReadonlyArray<Condition.Simple>;
113
+ }
114
+
115
+ interface Rule<OnSuccessAction, OnFailureAction> {
116
+ readonly id?: string;
117
+ readonly description: string;
118
+ readonly all: ReadonlyArray<Condition>;
119
+ readonly some: ReadonlyArray<Condition>;
120
+ readonly onSuccess: ReadonlyArray<OnSuccessAction>;
121
+ readonly onFailure: ReadonlyArray<OnFailureAction>;
122
+ }
123
+ declare namespace Rule {
124
+ /**
125
+ * Validates that the rule is valid.
126
+ * @param rule
127
+ */
128
+ const isEmpty: (rule: Rule<any, any>) => boolean;
129
+ const solve: (rule: Rule<any, any>, facts: ReadonlyArray<Fact>) => boolean;
130
+ }
131
+
132
+ type RuleActionPageQue = {
133
+ kind: "excludeByTag";
134
+ tagIds: string[];
135
+ } | {
136
+ kind: "excludeByPageId";
137
+ pageIds: Array<string>;
138
+ } | {
139
+ kind: "jumpToPage";
140
+ pageId: string;
141
+ };
142
+
143
+ type BaseTask = {
144
+ readonly blockAudio: boolean;
145
+ readonly blockVideo: boolean;
146
+ readonly blockResponseButton: boolean;
147
+ readonly blockFormInput: boolean;
148
+ readonly priority: "run-if-idle" | "follow-queue" | "replace-all" | "replace-current" | "replace-queue" | "prepend-to-queue" | "append-to-queue";
149
+ };
150
+ type DelayTask = BaseTask & {
151
+ readonly kind: "delay-task";
152
+ readonly duration: number;
153
+ };
154
+ type PlayAudioTask = BaseTask & {
155
+ readonly kind: "play-audio-task";
156
+ readonly url: string;
157
+ readonly audioId: string;
158
+ };
159
+ type PlayVideoTask = BaseTask & {
160
+ readonly kind: "play-video-task";
161
+ readonly url: string;
162
+ readonly videoId: string;
163
+ readonly loop?: boolean;
164
+ readonly startAt?: number;
165
+ readonly stopAt?: "end" | "start" | number;
166
+ readonly volume?: number;
167
+ };
168
+ declare const Task: {
169
+ eq: (a: Task, b: Task) => boolean;
170
+ is: (task: Task | undefined | false | null | {}) => task is Task;
171
+ notEq: (a: Task, b: Task) => boolean;
172
+ deleteTaskList: (task: Task) => boolean;
173
+ shallRemoveCurrent: (task: Task) => boolean;
174
+ };
175
+ type Task = PlayVideoTask | PlayAudioTask | DelayTask;
176
+
177
+ declare namespace DCss {
178
+ interface Px {
179
+ readonly _unit: "px";
180
+ readonly value: number;
181
+ }
182
+ type LengthString = `${number}px` | `${number}%`;
183
+ interface Percent {
184
+ readonly _unit: "percent";
185
+ readonly value: number;
186
+ }
187
+ type LengthUnit = Px | Percent;
188
+ /**
189
+ * Will scale to 3% of baseScale
190
+ * @param unit
191
+ * @param scale
192
+ */
193
+ const toString: (unit: Readonly<LengthUnit>, scale: number) => LengthString;
194
+ const isLengthUnit: (unit?: LengthUnit) => unit is LengthUnit;
195
+ }
196
+
197
+ type PStyle = Partial<DStyle>;
198
+ interface DStyle {
199
+ opacity: number;
200
+ backgroundColor: string;
201
+ visibility: "visible" | "hidden";
202
+ cursor: "pointer" | "help" | "copy" | "wait" | "not-allowed" | "context-menu" | "move" | "grabbing" | "grab" | "zoom-in" | "zoom-out" | "none" | "auto" | "default";
203
+ h: number;
204
+ w: number;
205
+ x: number;
206
+ y: number;
207
+ borderStyle: "solid" | "none" | "dotted" | "dashed";
208
+ borderRadius: DCss.Px | DCss.Percent;
209
+ borderWidth: DCss.Px;
210
+ borderColor: string;
211
+ margin: DCss.Px | DCss.Percent;
212
+ padding: DCss.Px | DCss.Percent;
213
+ transform: string;
214
+ translate: string;
215
+ fontSize: DCss.Px;
216
+ textColor: string;
217
+ fontWeight: 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
218
+ textAlign: "right" | "left" | "center";
219
+ letterSpacing: DCss.Px;
220
+ }
221
+ declare namespace DStyle {
222
+ const normalize: <T extends HTMLElement>(el: T) => T;
223
+ const applyStyles: <T extends HTMLElement>(el: T, style: Partial<DStyle>, scale: number) => T;
224
+ }
225
+
226
+ declare class ScaleService {
227
+ private readonly baseHeight;
228
+ private readonly baseWidth;
229
+ private containerHeight;
230
+ private containerWidth;
231
+ get scale(): number;
232
+ get pageHeight(): number;
233
+ get pageWidth(): number;
234
+ private _scale;
235
+ private readonly subscribers;
236
+ constructor(config: {
237
+ baseHeight: number;
238
+ baseWidth: number;
239
+ containerHeight: number;
240
+ containerWidth: number;
241
+ });
242
+ setContainerBounds(bounds: {
243
+ height: number;
244
+ width: number;
245
+ }): void;
246
+ private updateScale;
247
+ onChange(scaleChangeHandler: (scale: number) => void, subscriberId: string): () => void;
248
+ }
249
+
250
+ type TaskStateDiff = Partial<TaskState> & {
251
+ __diffed__: true;
252
+ };
253
+ type TaskState = {
254
+ audioIsPlaying: boolean;
255
+ isGifMode: boolean;
256
+ videoIsPlaying: boolean;
257
+ blockFormInput: boolean;
258
+ blockResponseButton: boolean;
259
+ blockAudio: boolean;
260
+ blockVideo: boolean;
261
+ };
262
+ declare const TaskState: {
263
+ eq: (a: TaskState, b: TaskState) => boolean;
264
+ getDiff: (curr: TaskState, prev: TaskState | false) => TaskStateDiff;
265
+ };
266
+
267
+ type ButtonClickAction = {
268
+ kind: "play-audio";
269
+ task: PlayAudioTask;
270
+ } | {
271
+ kind: "pause-audio";
272
+ } | {
273
+ kind: "play-video";
274
+ task: PlayVideoTask;
275
+ } | {
276
+ kind: "pause-video";
277
+ } | {
278
+ kind: "submit-fact";
279
+ fact: Fact;
280
+ } | {
281
+ kind: "next-page";
282
+ } | {
283
+ kind: "submit-form";
284
+ };
285
+ declare const ButtonClickAction: {
286
+ describe: (a: ButtonClickAction) => string;
287
+ };
288
+
289
+ interface DElementBaseDto {
290
+ readonly style: PStyle;
291
+ readonly onMouseEnter?: PStyle;
292
+ readonly onMouseLeave?: PStyle;
293
+ readonly onMouseDown?: PStyle;
294
+ readonly onMouseUp?: PStyle;
295
+ readonly innerText?: string;
296
+ }
297
+ declare abstract class DElement<T extends HTMLElement> {
298
+ protected readonly el: T;
299
+ protected readonly dto: DElementBaseDto;
300
+ protected readonly scale: ScaleService;
301
+ protected currStyle: Partial<DStyle>;
302
+ protected constructor(el: T, dto: DElementBaseDto, scale: ScaleService);
303
+ /**
304
+ * This method is called when the element is clicked.
305
+ * This method shall be overridden by the pageClass.
306
+ * @param actions
307
+ */
308
+ onclick(): void;
309
+ setStyle(style: PStyle): void;
310
+ appendYourself(parent: {
311
+ append: (el: HTMLElement) => void;
312
+ }): void;
313
+ private normalize;
314
+ protected updateStyles(style: Partial<DStyle>): void;
315
+ }
316
+
317
+ interface DImgDto extends DElementBaseDto {
318
+ readonly _tag: "img";
319
+ readonly url: string;
320
+ }
321
+ declare class DImg extends DElement<HTMLImageElement> {
322
+ protected readonly dto: DImgDto;
323
+ readonly scaleService: ScaleService;
324
+ private static IMAGE_COUNT;
325
+ private readonly imageCount;
326
+ readonly TAG: string;
327
+ readonly TIMING_TAG: string;
328
+ private readonly loadStart;
329
+ constructor(dto: DImgDto, scaleService: ScaleService);
330
+ log(): void;
331
+ }
332
+
333
+ interface DTextDto extends DElementBaseDto {
334
+ readonly _tag: "p";
335
+ readonly innerText: string;
336
+ }
337
+ declare class DText extends DElement<HTMLParagraphElement> {
338
+ constructor(dto: DTextDto, scale: ScaleService);
339
+ }
340
+
341
+ interface DDivDto extends DElementBaseDto {
342
+ readonly _tag: "div";
343
+ readonly children: Array<DTextDto | DImgDto>;
344
+ }
345
+ declare class DDiv extends DElement<HTMLDivElement> {
346
+ private readonly TAG;
347
+ protected readonly defaultStyle: {
348
+ x: number;
349
+ y: number;
350
+ };
351
+ private children;
352
+ constructor(dto: DDivDto, scale: ScaleService, children: Array<DText | DImg>);
353
+ }
354
+
355
+ type DElementDto = DTextDto | DImgDto | DDivDto;
356
+
357
+ interface PageComponentDto {
358
+ readonly onClick?: ButtonClickAction;
359
+ readonly el: DElementDto;
360
+ readonly whenVideoPlay?: PStyle;
361
+ readonly whenVideoPaused?: PStyle;
362
+ readonly whenAudioPlaying?: PStyle;
363
+ readonly whenAudioPaused?: PStyle;
364
+ readonly whenAudioBlocked?: PStyle;
365
+ readonly whenVideoBlocked?: PStyle;
366
+ readonly whenAudioUnblocked?: PStyle;
367
+ readonly whenVideoUnblocked?: PStyle;
368
+ readonly whenResponseBlocked?: PStyle;
369
+ readonly whenResponseUnblocked?: PStyle;
370
+ readonly whenFormInputBlocked?: PStyle;
371
+ readonly whenFormInputUnblocked?: PStyle;
372
+ }
373
+ declare class PageComponent {
374
+ readonly dto: PageComponentDto;
375
+ readonly scale: ScaleService;
376
+ private readonly TAG;
377
+ private el;
378
+ private prevState;
379
+ constructor(dto: PageComponentDto, scale: ScaleService);
380
+ onClick(action: ButtonClickAction): void;
381
+ updateState(state: TaskStateDiff): void;
382
+ setState(state: TaskState): void;
383
+ private handleStateChanges;
384
+ appendToParent(parent: {
385
+ append: (el: HTMLElement) => void;
386
+ }): void;
387
+ }
388
+
389
+ interface VideoPlayerDto {
390
+ playUrl: string;
391
+ style?: PStyle;
392
+ }
393
+ interface PageDto {
394
+ readonly id: string;
395
+ readonly prefix: string;
396
+ readonly tags: string[];
397
+ staticElements: Array<DElementDto>;
398
+ background: string;
399
+ components: Array<PageComponentDto>;
400
+ videoPlayer?: VideoPlayerDto;
401
+ initialTasks: Array<Task>;
402
+ }
403
+ declare const PageDto: {
404
+ createDummy: (id: number) => PageDto;
405
+ };
406
+
407
+ type PageQueRules = Rule<RuleActionPageQue, never>;
408
+ interface PageSequenceDto {
409
+ readonly id: string;
410
+ readonly rules: Array<PageQueRules>;
411
+ readonly pages: Array<PageDto>;
412
+ }
413
+ interface SchemaDto {
414
+ readonly id: string;
415
+ readonly baseHeight: number;
416
+ readonly baseWidth: number;
417
+ readonly backgroundColor: string;
418
+ readonly pages: Array<PageDto>;
419
+ readonly rules: Array<PageQueRules>;
420
+ readonly pageSequences?: Array<PageSequenceDto>;
421
+ readonly predefinedFacts?: ReadonlyArray<Fact>;
422
+ }
423
+
424
+ interface EngineLogger {
425
+ info(message: string): void;
426
+ error(message: string): void;
427
+ warn(message: string): void;
428
+ }
429
+ interface ISchemaEngine {
430
+ onProgress(handler: (result: SchemaResult) => void): void;
431
+ onFatalError(handler: (error: {
432
+ message: string;
433
+ }) => void): void;
434
+ setLogger(logger: EngineLogger): void;
435
+ }
436
+ declare class SchemaEngine implements ISchemaEngine {
437
+ private readonly height;
438
+ private readonly width;
439
+ private readonly schema;
440
+ private readonly TAG;
441
+ private readonly scale;
442
+ private readonly hostElement;
443
+ private readonly taskManager;
444
+ private logger;
445
+ private readonly uiLayer;
446
+ private readonly mediaLayer;
447
+ private player;
448
+ private currentPage;
449
+ private readonly tickerRef;
450
+ constructor(hostEl: HTMLDivElement, height: number, width: number, schema: SchemaDto);
451
+ private handlePageCompleted;
452
+ private styleSelf;
453
+ private nextPage;
454
+ destroy(): void;
455
+ private _onProgress;
456
+ onProgress(handler: (result: SchemaResult) => void): void;
457
+ private _onFatalError;
458
+ onFatalError(handler: (error: {
459
+ message: string;
460
+ }) => void): void;
461
+ setLogger(logger: EngineLogger): void;
462
+ }
463
+
464
+ interface SolveResult<S, F> {
465
+ matching: ReadonlyArray<Match<S, F>>;
466
+ errors: ReadonlyArray<RuleEngineError>;
467
+ }
468
+ interface Match<S, F> {
469
+ readonly matchingRuleId: string;
470
+ readonly ruleDescription: string;
471
+ readonly actionList: ReadonlyArray<S> | ReadonlyArray<F>;
472
+ }
473
+ interface RuleEngineError {
474
+ readonly kind?: string;
475
+ readonly message: string;
476
+ }
477
+ declare class RuleEngine<S, F> {
478
+ constructor();
479
+ solveAll(rules: Rule<S, F>[], facts: Fact[]): SolveResult<S, F>;
480
+ solve(rule: Rule<S, F>, facts: Fact[]): boolean;
481
+ }
482
+
483
+ declare namespace DUtil {
484
+ const randomString: (length: number) => string;
485
+ const randomObjectId: () => string;
486
+ const deleteProp: <Obj, Key extends keyof Obj>(obj: Obj, key: Key) => Omit<Obj, Key>;
487
+ const isInRange: (min: number, max: number) => (value: number) => boolean;
488
+ const isInfinity: (number: number) => boolean;
489
+ type NonEmptyArray<T> = [T, ...T[]];
490
+ const isNonEmptyArray: <T>(array: T[]) => array is NonEmptyArray<T>;
491
+ const neverCheck: (args: never) => void;
492
+ const isString: (str: unknown) => str is string;
493
+ const hasKey: <T extends string>(obj: unknown, key: T) => obj is Record<T, unknown>;
494
+ const isRecord: (obj: unknown) => obj is Record<string, unknown>;
495
+ const isBool: (obj?: boolean) => obj is boolean;
496
+ const isTrue: (bool?: boolean) => bool is true;
497
+ const isFalse: (bool?: boolean) => bool is false;
498
+ const isDefined: (obj: unknown) => boolean;
499
+ const hasKind: (obj: unknown) => obj is {
500
+ readonly kind: string;
501
+ };
502
+ const hasValue: (obj: unknown) => obj is {
503
+ value: unknown;
504
+ };
505
+ const isNumber: (value?: number) => value is number;
506
+ const maxFn: (upperLimit: number) => (value: number) => number;
507
+ const minFn: (lowerLimit: number) => (value: number) => number;
508
+ }
509
+
510
+ export { ButtonClickAction, Condition, DCss, DDiv, type DDivDto, DElement, type DElementBaseDto, type DElementDto, DImg, type DImgDto, DStyle, DText, type DTextDto, DUtil, type DelayTask, type EngineLogger, Fact, type ISchemaEngine, type Match, MqEvent, type MqEventEngineStart, type MqEventPageEnter, type MqEventPageLeave, type MqEventUserClicked, type PStyle, PageComponent, type PageComponentDto, PageDto, type PageQueRules, type PageSequenceDto, type PlayAudioTask, type PlayVideoTask, Rule, type RuleActionPageQue, RuleEngine, type RuleEngineError, type SchemaDto, SchemaEngine, type SchemaResult, type SolveResult, Task };