-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
160 lines (136 loc) · 3.71 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
// @ts-check
'use strict'
const debug = require('debug')('agg-watcher')
exports.aggregate = aggregate
function aggregate(emitter, callback, setup) {
if (!isEventEmitter(emitter)) {
throw new TypeError(
`First parameter expected to be an EventEmitter instance. Got ${emitter}`,
)
}
if (!isCallback(callback)) {
throw new TypeError(
`Second parameter expected to be a function. Got ${callback}`,
)
}
if (setup && !isCallback(setup)) {
throw new TypeError(
`Third parameter expected to be a function. Got ${setup}`,
)
}
const unlinked = new Map()
const changed = new Map()
const added = new Map()
const hasValues = () => unlinked.size || changed.size || added.size
const popCache = () => {
const cache = {
unlinked: Array.from(unlinked.values()),
changed: Array.from(changed.values()),
added: Array.from(added.values()),
}
unlinked.clear()
changed.clear()
added.clear()
return cache
}
function execute() {
if (hasValues()) {
debug('executing action callback')
return callback(popCache())
} else {
debug('cache empty - execution skipped')
}
}
let isExecuting = false
const scheduleExecute = () => {
debug('requested scheduling execution')
if (isExecuting || !hasValues()) {
return debug('execution schedule skipped')
}
isExecuting = true
Promise.resolve()
.then(execute)
.catch((err) => {
debug('action callback failed')
emitter.emit('error', err)
})
.finally(() => {
isExecuting = false
scheduleExecute()
})
debug('execution scheduled')
}
if (setup) {
isExecuting = true
Promise.resolve()
.then(() => {
debug('executing setup callback')
return setup()
})
.catch((err) => {
debug('setup callback failed')
emitter.emit('error', err)
})
.finally(() => {
isExecuting = false
scheduleExecute()
})
}
const aggUnlink = createUnlinkAggregator({ unlinked, changed, added })
emitter.on('unlink', function onUnlink(path, maybeStat) {
debug('unlink', path, maybeStat)
aggUnlink(path, maybeStat ? [path, maybeStat] : [path])
scheduleExecute()
})
const aggChange = createChangeAggregator({ unlinked, changed, added })
emitter.on('change', function onChange(path, maybeStat) {
debug('change', path, maybeStat)
aggChange(path, maybeStat ? [path, maybeStat] : [path])
scheduleExecute()
})
const aggAdd = createAddAggregator({ unlinked, changed, added })
emitter.on('add', function onAdd(path, maybeStat) {
debug('add', path, maybeStat)
aggAdd(path, maybeStat ? [path, maybeStat] : [path])
scheduleExecute()
})
return emitter
}
function createUnlinkAggregator({ unlinked, changed, added }) {
return function aggregateUnlink(key, args) {
if (added.has(key)) {
// was added, now is deleted => noop
added.delete(key)
} else {
changed.delete(key)
unlinked.set(key, args)
}
}
}
function createChangeAggregator({ unlinked, changed, added }) {
return function aggregateChange(key, args) {
if (!added.has(key)) {
changed.set(key, args)
}
}
}
function createAddAggregator({ unlinked, changed, added }) {
return function aggregateAdd(key, args) {
if (unlinked.has(key)) {
unlinked.delete(key)
changed.set(key, args)
} else {
added.set(key, args)
}
}
}
function isEventEmitter(maybeEmitter) {
return (
typeof maybeEmitter === 'object' &&
typeof maybeEmitter.on === 'function' &&
typeof maybeEmitter.emit === 'function'
)
}
function isCallback(maybeCallback) {
return typeof maybeCallback === 'function'
}