-
Notifications
You must be signed in to change notification settings - Fork 0
/
go.js
54 lines (46 loc) · 1.3 KB
/
go.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
42
43
44
45
46
47
48
49
50
51
52
53
54
function use (plugin, options = {}) {
if (this instanceof Go) {
if (matchPlugin(plugin, this)) return this
if (plugin && typeof plugin.install === 'function') {
plugin.install(this, options)
this._plugins.push(plugin.install)
} else if (typeof plugin === 'function') {
plugin(this, options)
this._plugins.push(plugin)
} else {
throw new ReferenceError('\'plugin\' must be a function or an object with install method')
}
return this
} else {
throw new ReferenceError('use() should be called on instance of Go')
}
}
function isUsed (plugin) {
if (this instanceof Go) {
if (plugin && typeof plugin.install === 'function') {
plugin = plugin.install
}
return matchPlugin(plugin, this)
} else {
throw new ReferenceError('isUsed() should be called on instance of Go')
}
}
function matchPlugin (plugin, instance) {
for (let i = instance._plugins.length; i--;) {
if (instance._plugins[i] === plugin) return true
}
return false
}
let shouldCreateRealInstance = false
function Go () {
if (!shouldCreateRealInstance) {
shouldCreateRealInstance = true
const go = new Go()
shouldCreateRealInstance = false
return go
}
this._plugins = []
}
Go.prototype.use = use
Go.prototype.isUsed = isUsed
module.exports = Go