-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.js
41 lines (34 loc) · 911 Bytes
/
base.js
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
const debug = require('debug')
const EventEmitter = require('eventemitter3')
module.exports = class Base extends EventEmitter {
constructor(options = {}) {
super()
this.options = options
this.id = options.id
this._ready = new Promise(resolve => this.once('ready', resolve))
this._debug = debug(`microverse/${this.label}`)
this.debug('instantiated')
if (options.bindReady === undefined) this.bindReady()
}
debug(...args) {
this._debug(...args)
}
ready() {
return this._ready
}
async bindReady(dependency = () => Promise.resolve()) {
await dependency()
this.handleReady()
this.emit('ready')
}
handleReady() {
this.debug('ready')
this.bindEventListeners()
}
bindEventListeners() {}
get label() {
const name = this.options.name || this.constructor.name
if (this.id) return `${name}{id:${this.id}}`
return name
}
}