@hkdigital/lib-sveltekit 0.1.43 → 0.1.44

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.
@@ -26,11 +26,15 @@ export class PresenterState {
26
26
  /** @type {string} */
27
27
  currentSlideName: string;
28
28
  /** @type {string} */
29
+ nextSlideName: string;
30
+ /** @type {string} */
29
31
  pendingSlideName: string;
30
32
  /** @type {boolean} */
31
33
  configured: boolean;
32
- /** @type {Map<Symbol, () => void>} */
33
- onUpdateListeners: Map<Symbol, () => void>;
34
+ /** @type {Map<Symbol, ( params: ListenerParams ) => void>} */
35
+ onBeforeListeners: Map<Symbol, (params: ListenerParams) => void>;
36
+ /** @type {Map<Symbol, ( params: ListenerParams ) => void>} */
37
+ onShowListeners: Map<Symbol, (params: ListenerParams) => void>;
34
38
  /**
35
39
  * Configure the presentation
36
40
  *
@@ -64,25 +68,6 @@ export class PresenterState {
64
68
  export type Slide = import("./typedef").Slide;
65
69
  export type Transition = import("./typedef").Transition;
66
70
  export type Layer = import("./typedef").Layer;
67
- export type LoadController = {
68
- /**
69
- * - Function to call when loading is complete
70
- */
71
- loaded: () => void;
72
- /**
73
- * - Function to return to the previous slide
74
- */
75
- cancel: () => void;
76
- };
77
- export type PresenterRef = {
78
- /**
79
- * - Navigate to a slide by name
80
- */
81
- gotoSlide: (name: string) => void;
82
- /**
83
- * - Get the current slide name
84
- */
85
- getCurrentSlideName: () => string;
86
- onUpdate: (callback: any) => Function;
87
- };
71
+ export type PresenterRef = import("./typedef").Layer;
72
+ export type LoadController = import("./typedef").Layer;
88
73
  import { HkPromise } from '../../classes/promise/index.js';
@@ -6,6 +6,8 @@ import { untrack } from 'svelte';
6
6
 
7
7
  import { HkPromise } from '../../classes/promise/index.js';
8
8
 
9
+ import { STAGE_BEFORE, STAGE_SHOW } from './constants.js';
10
+
9
11
  /**
10
12
  * @typedef {import("./typedef").Slide} Slide
11
13
  */
@@ -19,16 +21,11 @@ import { HkPromise } from '../../classes/promise/index.js';
19
21
  */
20
22
 
21
23
  /**
22
- * @typedef {Object} LoadController
23
- * @property {() => void} loaded - Function to call when loading is complete
24
- * @property {() => void} cancel - Function to return to the previous slide
24
+ * @typedef {import("./typedef").Layer} PresenterRef
25
25
  */
26
26
 
27
27
  /**
28
- * @typedef {Object} PresenterRef
29
- * @property {(name: string) => void} gotoSlide - Navigate to a slide by name
30
- * @property {() => string} getCurrentSlideName - Get the current slide name
31
- * @property {(callback)=>Function} onUpdate
28
+ * @typedef {import("./typedef").Layer} LoadController
32
29
  */
33
30
 
34
31
  const Z_BACK = 0;
@@ -89,14 +86,20 @@ export class PresenterState {
89
86
  return currentSlide?.name || '';
90
87
  });
91
88
 
89
+ /** @type {string} */
90
+ nextSlideName;
91
+
92
92
  /** @type {string} */
93
93
  pendingSlideName;
94
94
 
95
95
  /** @type {boolean} */
96
96
  configured = false;
97
97
 
98
- /** @type {Map<Symbol, () => void>} */
99
- onUpdateListeners = new Map();
98
+ /** @type {Map<Symbol, ( params: ListenerParams ) => void>} */
99
+ onBeforeListeners = new Map();
100
+
101
+ /** @type {Map<Symbol, ( params: ListenerParams ) => void>} */
102
+ onShowListeners = new Map();
100
103
 
101
104
  /**
102
105
  * Initialize the presenter state and set up reactivity
@@ -281,11 +284,19 @@ export class PresenterState {
281
284
  throw new Error('Not configured yet');
282
285
  }
283
286
 
287
+ if (slide.name === this.currentSlideName) {
288
+ throw new Error(`gotoSlide cannot transition to current slide`);
289
+ }
290
+
291
+ this.nextSlideName = slide.name;
292
+
284
293
  if (this.busy) {
285
294
  this.pendingSlideName = slide.name;
286
295
  return;
287
296
  }
288
297
 
298
+ this.#callOnBeforeListeners();
299
+
289
300
  this.slideLoadingPromise = null;
290
301
 
291
302
  // Get a presenter reference to pass to the slide
@@ -396,6 +407,7 @@ export class PresenterState {
396
407
  if (this.pendingSlideName) {
397
408
  const pendingName = this.pendingSlideName;
398
409
 
410
+ this.nextSlideName = pendingName;
399
411
  this.pendingSlideName = null;
400
412
 
401
413
  untrack(() => {
@@ -403,14 +415,26 @@ export class PresenterState {
403
415
  this.gotoSlide(pendingName);
404
416
  }
405
417
  });
418
+ } else {
419
+ this.nextSlideName = null;
406
420
  }
407
421
 
408
- this.#CallUpdateListeners();
422
+ this.#callOnShowListeners();
409
423
  }
410
424
 
411
- #CallUpdateListeners() {
412
- for (const fn of this.onUpdateListeners.values()) {
413
- fn();
425
+ #callOnBeforeListeners() {
426
+ let nextSlideName = this.nextSlideName;
427
+
428
+ for (const fn of this.onBeforeListeners.values()) {
429
+ fn({ stage: STAGE_BEFORE, slideName: nextSlideName });
430
+ }
431
+ }
432
+
433
+ #callOnShowListeners() {
434
+ let currentSlideName = this.currentSlideName;
435
+
436
+ for (const fn of this.onShowListeners.values()) {
437
+ fn({ stage: STAGE_SHOW, slideName: currentSlideName });
414
438
  }
415
439
  }
416
440
 
@@ -557,12 +581,20 @@ export class PresenterState {
557
581
  return {
558
582
  gotoSlide: (name) => this.gotoSlide(name),
559
583
  getCurrentSlideName: () => this.currentSlideName,
560
- onUpdate: (callback) => {
584
+ onBefore: (callback) => {
585
+ const key = Symbol();
586
+ this.onBeforeListeners.set(key, callback);
587
+
588
+ return () => {
589
+ this.onBeforeListeners.delete(key);
590
+ };
591
+ },
592
+ onAfter: (callback) => {
561
593
  const key = Symbol();
562
- this.onUpdateListeners.set(key, callback);
594
+ this.onShowListeners.set(key, callback);
563
595
 
564
596
  return () => {
565
- this.onUpdateListeners.delete(key);
597
+ this.onShowListeners.delete(key);
566
598
  };
567
599
  }
568
600
  };
@@ -1,3 +1,5 @@
1
1
  export const TRANSITION_CSS: "css";
2
2
  export const FADE_IN: "fade-in";
3
3
  export const FADE_OUT: "fade-out";
4
+ export const STAGE_BEFORE: "stage-before";
5
+ export const STAGE_SHOW: "stage-show";
@@ -2,3 +2,6 @@ export const TRANSITION_CSS = 'css';
2
2
 
3
3
  export const FADE_IN = 'fade-in';
4
4
  export const FADE_OUT = 'fade-out';
5
+
6
+ export const STAGE_BEFORE = 'stage-before';
7
+ export const STAGE_SHOW = 'stage-show';
@@ -25,6 +25,31 @@ export type FadeOutTransition = {
25
25
  duration?: number;
26
26
  timing?: string;
27
27
  };
28
+ export type ListenerParams = {
29
+ stage: string;
30
+ slideName: string;
31
+ };
32
+ export type LoadController = {
33
+ /**
34
+ * - Function to call when loading is complete
35
+ */
36
+ loaded: () => void;
37
+ /**
38
+ * - Function to return to the previous slide
39
+ */
40
+ cancel: () => void;
41
+ };
42
+ export type PresenterRef = {
43
+ /**
44
+ * - Navigate to a slide by name
45
+ */
46
+ gotoSlide: (name: string) => void;
47
+ /**
48
+ * - Get the current slide name
49
+ */
50
+ getCurrentSlideName: () => string;
51
+ onUpdate: (callback: any) => Function;
52
+ };
28
53
  export type Transition = CssTransition | FadeInTransition | FadeOutTransition;
29
54
  export type SlideData = {
30
55
  [attrs: string]: any;
@@ -32,6 +32,26 @@
32
32
  * }} FadeOutTransition
33
33
  */
34
34
 
35
+ /**
36
+ * @typedef {{
37
+ * stage: string,
38
+ * slideName: string
39
+ * }} ListenerParams
40
+ */
41
+
42
+ /**
43
+ * @typedef {Object} LoadController
44
+ * @property {() => void} loaded - Function to call when loading is complete
45
+ * @property {() => void} cancel - Function to return to the previous slide
46
+ */
47
+
48
+ /**
49
+ * @typedef {Object} PresenterRef
50
+ * @property {(name: string) => void} gotoSlide - Navigate to a slide by name
51
+ * @property {() => string} getCurrentSlideName - Get the current slide name
52
+ * @property {(callback)=>Function} onUpdate
53
+ */
54
+
35
55
  /**
36
56
  * @typedef {CssTransition|FadeInTransition|FadeOutTransition} Transition
37
57
  */
@@ -25,7 +25,7 @@ export function waitForRender(callback) {
25
25
  setTimeout(() => {
26
26
  callback();
27
27
  resolve();
28
- }, 50);
28
+ }, 20);
29
29
  });
30
30
  });
31
31
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hkdigital/lib-sveltekit",
3
- "version": "0.1.43",
3
+ "version": "0.1.44",
4
4
  "author": {
5
5
  "name": "HKdigital",
6
6
  "url": "https://hkdigital.nl"