Welcome to mirror list, hosted at ThFree Co, Russian Federation.

event_hub_factory.js « helpers « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a9c301e3a939e68863126bde88ca970409fafc41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
 * An event hub with a Vue instance like API
 *
 * NOTE: There's an [issue open][4] to eventually remove this when some
 * coupling in our codebase has been fixed.
 *
 * NOTE: This is a derivative work from [mitt][1] v1.2.0 which is licensed by
 * [MIT License][2] © [Jason Miller][3]
 *
 * [1]: https://github.com/developit/mitt
 * [2]: https://opensource.org/licenses/MIT
 * [3]: https://jasonformat.com/
 * [4]: https://gitlab.com/gitlab-org/gitlab/-/issues/223864
 */
class EventHub {
  constructor() {
    this.$_all = new Map();
  }

  dispose() {
    this.$_all.clear();
  }

  /**
   * Register an event handler for the given type.
   *
   * @param {string|symbol} type Type of event to listen for
   * @param {Function} handler Function to call in response to given event
   */
  $on(type, handler) {
    const handlers = this.$_all.get(type);
    const added = handlers && handlers.push(handler);

    if (!added) {
      this.$_all.set(type, [handler]);
    }
  }

  /**
   * Remove an event handler or all handlers for the given type.
   *
   * @param {string|symbol} type Type of event to unregister `handler`
   * @param {Function} handler Handler function to remove
   */
  $off(type, handler) {
    const handlers = this.$_all.get(type) || [];

    const newHandlers = handler ? handlers.filter(x => x !== handler) : [];

    if (newHandlers.length) {
      this.$_all.set(type, newHandlers);
    } else {
      this.$_all.delete(type);
    }
  }

  /**
   * Add an event listener to type but only trigger it once
   *
   * @param {string|symbol} type Type of event to listen for
   * @param {Function} handler Handler function to call in response to event
   */
  $once(type, handler) {
    const wrapHandler = (...args) => {
      this.$off(type, wrapHandler);
      handler(...args);
    };
    this.$on(type, wrapHandler);
  }

  /**
   * Invoke all handlers for the given type.
   *
   * @param {string|symbol} type The event type to invoke
   * @param {Any} [evt] Any value passed to each handler
   */
  $emit(type, ...args) {
    const handlers = this.$_all.get(type) || [];

    handlers.forEach(handler => {
      handler(...args);
    });
  }
}

/**
 * Return a Vue like event hub
 *
 * - $on
 * - $off
 * - $once
 * - $emit
 *
 * Please note, this was once implemented with `mitt`, but since then has been reverted
 * because of some API issues. https://gitlab.com/gitlab-org/gitlab/-/merge_requests/35074
 *
 * We'd like to shy away from using a full fledged Vue instance from this in the future.
 */
export default () => {
  return new EventHub();
};