-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
remove.js
61 lines (53 loc) · 1.64 KB
/
remove.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
import { EJSON } from 'meteor/ejson'
import { CollectionHooks } from './collection-hooks'
const isEmpty = a => !Array.isArray(a) || !a.length
CollectionHooks.defineAdvice('remove', function (userId, _super, instance, aspects, getTransform, args, suppressAspects) {
const ctx = { context: this, _super, args }
const [selector, callback] = args
const async = typeof callback === 'function'
let docs
let abort
const prev = []
if (!suppressAspects) {
try {
if (!isEmpty(aspects.before) || !isEmpty(aspects.after)) {
docs = CollectionHooks.getDocs.call(this, instance, selector).fetch()
}
// copy originals for convenience for the 'after' pointcut
if (!isEmpty(aspects.after)) {
docs.forEach(doc => prev.push(EJSON.clone(doc)))
}
// before
aspects.before.forEach((o) => {
docs.forEach((doc) => {
const r = o.aspect.call({ transform: getTransform(doc), ...ctx }, userId, doc)
if (r === false) abort = true
})
})
if (abort) return 0
} catch (e) {
if (async) return callback.call(this, e)
throw e
}
}
function after (err) {
if (!suppressAspects) {
aspects.after.forEach((o) => {
prev.forEach((doc) => {
o.aspect.call({ transform: getTransform(doc), err, ...ctx }, userId, doc)
})
})
}
}
if (async) {
const wrappedCallback = function (err, ...args) {
after(err)
return callback.call(this, err, ...args)
}
return _super.call(this, selector, wrappedCallback)
} else {
const result = _super.call(this, selector, callback)
after()
return result
}
})