summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/helpers/event_hub_factory.js
blob: 4d7f7550a944d5fcf5ebcc81522120a9aefde530 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import mitt from 'mitt';

export default () => {
  const emitter = mitt();

  emitter.once = (event, handler) => {
    const wrappedHandler = evt => {
      handler(evt);
      emitter.off(event, wrappedHandler);
    };
    emitter.on(event, wrappedHandler);
  };

  emitter.$on = emitter.on;
  emitter.$once = emitter.once;
  emitter.$off = emitter.off;
  emitter.$emit = emitter.emit;

  return emitter;
};