-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch-channel-store-methods.js
76 lines (57 loc) · 1.58 KB
/
patch-channel-store-methods.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
const { ReflectApply } = require('./primordials.js');
module.exports = function (unpatched) {
const channels = new WeakSet();
const dc_channel = unpatched.channel;
const dc = { ...unpatched };
dc.channel = function() {
const ch = dc_channel.apply(this, arguments);
if (channels.has(ch)) return ch;
ch._stores = new Map();
ch.bindStore = function(store, transform) {
// const replacing = this._stores.has(store);
// if (!replacing) channels.incRef(this.name);
this._stores.set(store, transform);
};
ch.unbindStore = function(store) {
if (!this._stores.has(store)) {
return false;
}
this._stores.delete(store);
// channels.decRef(this.name);
// maybeMarkInactive(this);
return true;
};
ch.runStores = function(data, fn, thisArg, ...args) {
let run = () => {
this.publish(data);
return ReflectApply(fn, thisArg, args);
};
for (const entry of this._stores.entries()) {
const store = entry[0];
const transform = entry[1];
run = wrapStoreRun(store, data, run, transform);
}
return run();
};
return ch;
};
return dc;
};
function wrapStoreRun(store, data, next, transform = defaultTransform) {
return () => {
let context;
try {
context = transform(data);
} catch (err) {
process.nextTick(() => {
// triggerUncaughtException(err, false);
throw err;
});
return next();
}
return store.run(context, next);
};
}
function defaultTransform(data) {
return data;
}