-
Notifications
You must be signed in to change notification settings - Fork 164
/
dispatcher.js
162 lines (134 loc) · 4.81 KB
/
dispatcher.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
var _clone = require("lodash/lang/clone"),
_mapValues = require("lodash/object/mapValues"),
_forOwn = require("lodash/object/forOwn"),
_intersection = require("lodash/array/intersection"),
_keys = require("lodash/object/keys"),
_map = require("lodash/collection/map"),
_each = require("lodash/collection/forEach"),
_size = require("lodash/collection/size"),
_findKey = require("lodash/object/findKey"),
_uniq = require("lodash/array/uniq");
var defaultDispatchInterceptor = function(action, dispatch) {
dispatch(action);
};
var Dispatcher = function(stores) {
this.stores = {};
this.currentDispatch = null;
this.currentActionType = null;
this.waitingToDispatch = [];
this.dispatchInterceptor = defaultDispatchInterceptor;
this._boundDispatch = this._dispatch.bind(this);
for (var key in stores) {
if (stores.hasOwnProperty(key)) {
this.addStore(key, stores[key]);
}
}
};
Dispatcher.prototype.addStore = function(name, store) {
store.dispatcher = this;
this.stores[name] = store;
};
Dispatcher.prototype.dispatch = function(action) {
this.dispatchInterceptor(action, this._boundDispatch);
};
Dispatcher.prototype._dispatch = function(action) {
if (!action || !action.type) {
throw new Error("Can only dispatch actions with a 'type' property");
}
if (this.currentDispatch) {
var complaint = "Cannot dispatch an action ('" + action.type + "') while another action ('" +
this.currentActionType + "') is being dispatched";
throw new Error(complaint);
}
this.waitingToDispatch = _clone(this.stores);
this.currentActionType = action.type;
this.currentDispatch = _mapValues(this.stores, function() {
return { resolved: false, waitingOn: [], waitCallback: null };
});
try {
this.doDispatchLoop(action);
} finally {
this.currentActionType = null;
this.currentDispatch = null;
}
};
Dispatcher.prototype.doDispatchLoop = function(action) {
var dispatch, canBeDispatchedTo, wasHandled = false,
removeFromDispatchQueue = [], dispatchedThisLoop = [];
_forOwn(this.waitingToDispatch, function(value, key) {
dispatch = this.currentDispatch[key];
canBeDispatchedTo = !dispatch.waitingOn.length ||
!_intersection(dispatch.waitingOn, _keys(this.waitingToDispatch)).length;
if (canBeDispatchedTo) {
if (dispatch.waitCallback) {
var stores = _map(dispatch.waitingOn, function(key) {
return this.stores[key];
}, this);
var fn = dispatch.waitCallback;
dispatch.waitCallback = null;
dispatch.waitingOn = [];
dispatch.resolved = true;
fn.apply(null, stores);
wasHandled = true;
} else {
dispatch.resolved = true;
var handled = this.stores[key].__handleAction__(action);
if (handled) {
wasHandled = true;
}
}
dispatchedThisLoop.push(key);
if (this.currentDispatch[key].resolved) {
removeFromDispatchQueue.push(key);
}
}
}, this);
if (_keys(this.waitingToDispatch).length && !dispatchedThisLoop.length) {
var storesWithCircularWaits = _keys(this.waitingToDispatch).join(", ");
throw new Error("Indirect circular wait detected among: " + storesWithCircularWaits);
}
_each(removeFromDispatchQueue, function(key) {
delete this.waitingToDispatch[key];
}, this);
if (_size(this.waitingToDispatch)) {
this.doDispatchLoop(action);
}
if (!wasHandled && console && console.warn) {
console.warn("An action of type " + action.type + " was dispatched, but no store handled it");
}
};
Dispatcher.prototype.waitForStores = function(store, stores, fn) {
if (!this.currentDispatch) {
throw new Error("Cannot wait unless an action is being dispatched");
}
var waitingStoreName = _findKey(this.stores, function(val) {
return val === store;
});
if (stores.indexOf(waitingStoreName) > -1) {
throw new Error("A store cannot wait on itself");
}
var dispatch = this.currentDispatch[waitingStoreName];
if (dispatch.waitingOn.length) {
throw new Error(waitingStoreName + " already waiting on stores");
}
_each(stores, function(storeName) {
var storeDispatch = this.currentDispatch[storeName];
if (!this.stores[storeName]) {
throw new Error("Cannot wait for non-existent store " + storeName);
}
if (storeDispatch.waitingOn.indexOf(waitingStoreName) > -1) {
throw new Error("Circular wait detected between " + waitingStoreName + " and " + storeName);
}
}, this);
dispatch.resolved = false;
dispatch.waitingOn = _uniq(dispatch.waitingOn.concat(stores));
dispatch.waitCallback = fn;
};
Dispatcher.prototype.setDispatchInterceptor = function(fn) {
if (fn) {
this.dispatchInterceptor = fn;
} else {
this.dispatchInterceptor = defaultDispatchInterceptor;
}
};
module.exports = Dispatcher;