-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
259 lines (230 loc) · 4.43 KB
/
index.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
* Dependency
*/
var emitter = require('zeroin')
/**
* Door factory.
*
* Examples:
*
* door()
* door('lock1')
* door('lock1 lock2')
* door(['lock1', 'lock2'])
* door(core => {
* core.unlock('lock1')
* }, 'lock1')
*
*
* @param {String|Array?}
* @return {Object}
* @api public
*/
module.exports = function (fn, names) {
if (typeof fn === 'function') {
var door = doors(names)
var promise = door.promise()
fn(door)
return promise
}
return doors(fn)
}
/**
* Door constructor.
*
* Examples:
*
* door()
* door('lock1')
* door('lock1 lock2')
* door(['lock1', 'lock2'])
*
*
* @param {String|Array?}
* @return {Object}
* @api public
*/
function doors(arg) {
var door = emitter({})
/**
* List of locks that are locked.
* @type {Array}
*/
var locks = []
/**
* Check if door is locked (true) or unlocked (false).
* Check if lock is locked or not.
*
* Examples:
*
* knock()
* knock('lock1')
* knock('lock1 lock2')
* knock(['lock1', 'lock2'])
*
* @param {String?} names
* @return {Boolean}
* @api public
*/
door.knock = function (names) {
if (names) {
return split(names).reduce(function (a, b) {
return !~locks.indexOf(b) && a
}, true)
}
return locks.length < 1
}
/**
* Promise factory.
* Resolve promise when open and reject when close.
* Resolve promise when lock and reject when unlock.
*
* Examples:
*
* promise()
* promise('lock1')
* promise('lock2 lock1')
* promise(['lock1', 'lock2'])
*
* @param {String|Array?}
* @return {Promise}
* @api public
*/
door.promise = function (names) {
if (names) {
return Promise.all(split(names).map(function (name) {
return new Promise(function (resolve, reject) {
door.once('lock ' + name, reject)
door.once('unlock ' + name, resolve)
})
}))
}
return new Promise(function (resolve, reject) {
door.once('open', resolve)
door.once('close', reject)
})
}
/**
* Add lock.
*
* @param {String} name
* @api private
*/
door.add = function (name) {
if (!~locks.indexOf(name)) {
var open = locks.length < 1
locks.push(name)
door.emit('lock ' + name)
door.emit('lock', name)
if (open && locks.length > 0) door.emit('close')
}
}
/**
* Remove lock.
*
* @param {String} name
* @api private
*/
door.remove = function (name) {
var index = locks.indexOf(name)
if (index > -1) {
var close = locks.length > 0
locks.splice(index, 1)
door.emit('unlock ' + name)
door.emit('unlock', name)
if (close && locks.length < 1) door.emit('open')
}
}
/**
* Lock one or multiple locks.
*
* Examples:
*
* lock('lock1')
* lock('lock1 lock2')
* lock(['lock1', 'lock2'])
*
* @param {String?} names
* @api public
*/
door.lock = function (names, promise) {
var add = map(names, door.add)
switch (type(promise)) {
case 'promise':
promise.then(add)
break
default:
add()
}
}
/**
* Unlock one or multiple locks.
*
* Examples:
*
* unlock('lock1')
* unlock('lock1 lock2')
* unlock(['lock1', 'lock2'])
*
* @param {String?} names
* @param {Promise?} promise
* @api public
*/
door.unlock = function (names, promise) {
var remove = map(names, door.remove)
switch (type(promise)) {
case 'promise':
promise.then(remove)
break
default:
remove()
}
}
/**
* Add locks on construction.
*/
if (arg) {
split(arg).map(function (name) {
door.add(name)
})
}
return door
}
/**
* Split string into array of words without spaces.
*
* @param {String|Array} names
* @return {Array}
* @api private
*/
function split (names) {
return names instanceof Array
? names
: names.trim().split(/\s+/g)
}
/**
* Map function with names input.
*
* @param {String|Array} names
* @param {Function} fn
* @return {Function}
* @api private
*/
function map (names, fn) {
return function () {
split(names).map(fn)
}
}
/**
* Return object type.
*
* @param {Object} obj
* @return {String}
* @api private
*/
function type (obj) {
if (obj) {
if (typeof obj.then === 'function') return 'promise'
else if (typeof obj.on === 'function' && typeof obj.emit === 'function') return 'emitter'
}
}