kamrify commited on
Commit
63b089b
·
1 Parent(s): 25b975e

Refactor hooks

Browse files
Files changed (5) hide show
  1. src/driver.ts +12 -9
  2. src/emitter.ts +15 -0
  3. src/events.ts +8 -0
  4. src/hooks.ts +0 -15
  5. src/stage.ts +2 -2
src/driver.ts CHANGED
@@ -3,7 +3,7 @@ import { destroyStage } from "./stage";
3
  import { destroyEvents, initEvents } from "./events";
4
  import { Config, configure, getConfig } from "./config";
5
  import { destroyHighlight, highlight } from "./highlight";
6
- import { destroyHooks, register } from "./hooks";
7
 
8
  import "./style.css";
9
 
@@ -17,6 +17,14 @@ let isInitialized = false;
17
  export function driver(options: Config = {}) {
18
  configure(options);
19
 
 
 
 
 
 
 
 
 
20
  function init() {
21
  // Avoid multiple initialization
22
  if (isInitialized) {
@@ -32,13 +40,8 @@ export function driver(options: Config = {}) {
32
  initEvents();
33
 
34
  // Register hooks
35
- register("overlayClick", () => {
36
- if (!getConfig("allowClose")) {
37
- return;
38
- }
39
-
40
- destroy();
41
- });
42
  }
43
 
44
  function destroy() {
@@ -51,7 +54,7 @@ export function driver(options: Config = {}) {
51
  destroyEvents();
52
  destroyHighlight();
53
  destroyStage();
54
- destroyHooks();
55
  }
56
 
57
  return {
 
3
  import { destroyEvents, initEvents } from "./events";
4
  import { Config, configure, getConfig } from "./config";
5
  import { destroyHighlight, highlight } from "./highlight";
6
+ import { destroyEmitter, listen } from "./emitter";
7
 
8
  import "./style.css";
9
 
 
17
  export function driver(options: Config = {}) {
18
  configure(options);
19
 
20
+ function handleClose() {
21
+ if (!getConfig("allowClose")) {
22
+ return;
23
+ }
24
+
25
+ destroy();
26
+ }
27
+
28
  function init() {
29
  // Avoid multiple initialization
30
  if (isInitialized) {
 
40
  initEvents();
41
 
42
  // Register hooks
43
+ listen("overlayClick", handleClose);
44
+ listen("escape", handleClose);
 
 
 
 
 
45
  }
46
 
47
  function destroy() {
 
54
  destroyEvents();
55
  destroyHighlight();
56
  destroyStage();
57
+ destroyEmitter();
58
  }
59
 
60
  return {
src/emitter.ts ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ type allowedEvents = "overlayClick" | "escape";
2
+
3
+ let registeredListeners: Partial<{ [key in allowedEvents]: () => void }> = {};
4
+
5
+ export function listen(hook: allowedEvents, callback: () => void) {
6
+ registeredListeners[hook] = callback;
7
+ }
8
+
9
+ export function emit(hook: allowedEvents) {
10
+ registeredListeners[hook]?.();
11
+ }
12
+
13
+ export function destroyEmitter() {
14
+ registeredListeners = {};
15
+ }
src/events.ts CHANGED
@@ -1,4 +1,5 @@
1
  import { refreshActiveHighlight } from "./highlight";
 
2
 
3
  let resizeTimeout: number;
4
 
@@ -10,6 +11,12 @@ function requireRefresh() {
10
  resizeTimeout = window.requestAnimationFrame(refreshActiveHighlight);
11
  }
12
 
 
 
 
 
 
 
13
  /**
14
  * Attaches click handler to the elements created by driver.js. It makes
15
  * sure to give the listener the first chance to handle the event, and
@@ -64,6 +71,7 @@ export function onDriverClick(
64
  }
65
 
66
  export function initEvents() {
 
67
  window.addEventListener("resize", requireRefresh);
68
  window.addEventListener("scroll", requireRefresh);
69
  }
 
1
  import { refreshActiveHighlight } from "./highlight";
2
+ import { emit } from "./emitter";
3
 
4
  let resizeTimeout: number;
5
 
 
11
  resizeTimeout = window.requestAnimationFrame(refreshActiveHighlight);
12
  }
13
 
14
+ function onKeyup(e: KeyboardEvent) {
15
+ if (e.key === "Escape") {
16
+ emit("escape");
17
+ }
18
+ }
19
+
20
  /**
21
  * Attaches click handler to the elements created by driver.js. It makes
22
  * sure to give the listener the first chance to handle the event, and
 
71
  }
72
 
73
  export function initEvents() {
74
+ window.addEventListener("keyup", onKeyup, false);
75
  window.addEventListener("resize", requireRefresh);
76
  window.addEventListener("scroll", requireRefresh);
77
  }
src/hooks.ts DELETED
@@ -1,15 +0,0 @@
1
- type allowedHooks = "overlayClick";
2
-
3
- let registeredHooks: Partial<{ [key in allowedHooks]: () => void }> = {};
4
-
5
- export function register(hook: allowedHooks, callback: () => void) {
6
- registeredHooks[hook] = callback;
7
- }
8
-
9
- export function trigger(hook: allowedHooks) {
10
- registeredHooks[hook]?.();
11
- }
12
-
13
- export function destroyHooks() {
14
- registeredHooks = {};
15
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/stage.ts CHANGED
@@ -1,6 +1,6 @@
1
  import { easeInOutQuad } from "./utils";
2
  import { onDriverClick } from "./events";
3
- import { trigger } from "./hooks";
4
  import { getConfig } from "./config";
5
 
6
  export type StageDefinition = {
@@ -108,7 +108,7 @@ function mountStage(stagePosition: StageDefinition) {
108
  return;
109
  }
110
 
111
- trigger("overlayClick");
112
  });
113
  }
114
 
 
1
  import { easeInOutQuad } from "./utils";
2
  import { onDriverClick } from "./events";
3
+ import { emit } from "./emitter";
4
  import { getConfig } from "./config";
5
 
6
  export type StageDefinition = {
 
108
  return;
109
  }
110
 
111
+ emit("overlayClick");
112
  });
113
  }
114