source: trip-planner-front/node_modules/karma/lib/events.js@ 76712b2

Last change on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.6 KB
Line 
1'use strict'
2
3const EventEmitter = require('events').EventEmitter
4const helper = require('./helper')
5
6function bufferEvents (emitter, eventsToBuffer) {
7 const listeners = []
8 const eventsToReply = []
9
10 function genericListener () {
11 eventsToReply.push(Array.from(arguments))
12 }
13
14 eventsToBuffer.forEach((eventName) => {
15 const listener = genericListener.bind(null, eventName)
16 listeners.push(listener)
17 emitter.on(eventName, listener)
18 })
19
20 return function () {
21 listeners.forEach((listener, i) => {
22 emitter.removeListener(eventsToBuffer[i], listener)
23 })
24
25 eventsToReply.forEach((args) => {
26 EventEmitter.prototype.emit.apply(emitter, args)
27 })
28
29 listeners.length = 0
30 eventsToReply.length = 0
31 }
32}
33
34class KarmaEventEmitter extends EventEmitter {
35 bind (object) {
36 for (const method in object) {
37 if (method.startsWith('on') && helper.isFunction(object[method])) {
38 this.on(helper.camelToSnake(method.substr(2)), function () {
39 // We do not use an arrow function here, to supply the caller as this.
40 object[method].apply(object, Array.from(arguments).concat(this))
41 })
42 }
43 }
44 }
45
46 emitAsync (name) {
47 // TODO(vojta): allow passing args
48 // TODO(vojta): ignore/throw if listener call done() multiple times
49 let pending = this.listeners(name).length
50 const deferred = helper.defer()
51
52 this.emit(name, () => {
53 if (!--pending) {
54 deferred.resolve()
55 }
56 })
57
58 if (!pending) {
59 deferred.resolve()
60 }
61
62 return deferred.promise
63 }
64}
65
66exports.EventEmitter = KarmaEventEmitter
67exports.bufferEvents = bufferEvents
Note: See TracBrowser for help on using the repository browser.