Skip to content

Commit

Permalink
BREAKING CHANGE: deduplicate when merging hooks re: Automattic/mongoo…
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Dec 22, 2017
1 parent adaaa00 commit d458573
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ Kareem.prototype.pre = function(name, isAsync, fn, error, unshift) {
}

this._pres[name] = get(this._pres, name, []);
var pres = this._pres[name];
const pres = this._pres[name];

if (isAsync) {
pres.numAsync = pres.numAsync || 0;
Expand Down Expand Up @@ -362,12 +362,21 @@ Kareem.prototype.clone = function() {

Kareem.prototype.merge = function(other) {
var ret = this.clone();

for (let key of Object.keys(other._pres)) {
ret._pres[key] = get(ret._pres, key, []).concat(other._pres[key].slice());
ret._pres[key].numAsync += other._pres[key].numAsync;
const sourcePres = get(ret._pres, key, []);
const deduplicated = other._pres[key].
// Deduplicate based on `fn`
filter(p => !sourcePres.map(({fn}) => fn).includes(p.fn));
ret._pres[key] = sourcePres.concat(deduplicated);
ret._pres[key].numAsync = get(ret._pres[key], 'numAsync', 0);
ret._pres[key].numAsync += deduplicated.filter(p => p.isAsync).length;
}
for (let key of Object.keys(other._posts)) {
ret._posts[key] = get(ret._posts, key, []).concat(other._posts[key].slice());
const sourcePosts = get(ret._posts, key, []);
const deduplicated = other._posts[key].
filter(p => !sourcePosts.includes(p));
ret._posts[key] = sourcePosts.concat(deduplicated);
}

return ret;
Expand Down

0 comments on commit d458573

Please sign in to comment.