-
Notifications
You must be signed in to change notification settings - Fork 164
/
test.js
283 lines (211 loc) · 8.32 KB
/
test.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Should create a "mission statement" to focus specifically on what to change.
//
// Would be nice to list patterns that are currently hard/impossible that we want
// to explicitly support.
// NOTES:
// * Better abstraction around actions and the `flux` object being passed around.
// See for example http://stackoverflow.com/questions/27745113/pass-an-instance-of-flux-fluxxor-from-actions-hash-to-a-proxy-object
// See also https://github.com/BinaryMuse/fluxxor/issues/119#issuecomment-103612974
//
// Main Package
// ============================================================================
var Fluxxor = {
Dispatcher: require("fluxxor-dispatcher"),
Store: require("fluxxor-store")
};
module.exports = Fluxxor;
// Dispatcher
// ============================================================================
var Dispatcher = require("fluxxor-dispatcher");
var dispatcher = new Dispatcher();
// TODO: options...
// Behaviors can be Dispatcher.Behaviors.WARN, Dispatcher.Behaviors.THROW, Dispatcher.Behaviors.IGNORE
// custom behaviors? Should be a function that takes a message and does something.
// * unhandledAction - if no store callback returns `true`
// * cascadingDispatch - if detects cascading dispatches
// * circularDependency - if detects circular waitFor dependencies
// getting a store ID for `waitFor` - unique per registration per dispatcher
storeId = dispatcher.register(myStore.callback);
// assigning a manual store ID
// in this example, storeId === myStoreId
// throws if store ID is already taken
storeId = dispatcher.register(myStore.callback, myStoreId);
dispatcher.unregister(storeId);
// TODO: can the dispatcher be a registry for stores as well?
// if so, how to get them in there, now to identify them?
// Perhaps an object like Fluxxor.Flux should be responsible
// for being a registry of objects? Perhaps another lib like
// react-simple-di? How do actions fit in? How about if they're
// not being used?
// TODO: give the dispatch a wrapper implementation, just like
// wrapping `this.dispatch` in action creators. Allows for alternative
// dispatcher implementations when not using action creators.
// See fluxxor #100
// TODO: give dispatcher a way to specify which extra props to
// pass to the store callback. Useful for (when using Fluxxor.Flux)
// providing the `flux` instance, can also provide the `dispatcher`
// instance by default, or allow the user to specify any number of
// extra objects (e.g. router?)
class MyCustomStore {
callback(action, dispatcher) { // TODO: consider first-class dispatch object?? or is that too complex
console.log("My action:", action);
}
}
var store = new MyCustomStore();
store.storeId = dispatcher.register(store.callback);
var store = {
callback(action, dispatcher) {
console.log("My action:", action);
}
};
store.storeId = dispatcher.register(store.callback);
// TODO: need to be able to add/remove stores at any time
// TODO: should stores be required to return/respond to something to
// indicate they "handled" the event? (e.g. to keep errors when nothing
// handles an action)
// Stores
// ============================================================================
var binder = new BindingSystem();
matcher = binder.addMatch(match, result); // defaults to search === match
matcher.setComparator(comparator); // comparator(search, match) === true
matcher = binder.addMatch(match, result, comparator); // comparator(search, match) === true
binder.find(search); // returns array of results, in order of addition
class FluxxorStore {
constructor() {
this.__actionBinder = new BindingSystem();
this.__actionBinder.setDefaultComparator(this.getActionType);
}
bindActions() {
var args = Array.prototype.slice.call(arguments);
var match, handler, matchers = [];
for (var i = 0; i < args.length; i += 2) {
match = args[i];
handler = args[i + 1];
matchers.push(this.__actionBinder.addMatch(match, handler));
}
return { using: (extractFn) => {
var comparator = (needle, test) => exactOrArrayMatch(extractFn(needle), test) === true;
matchers.forEach((matcher) => matcher.setComparator(comparator));
}};
}
getActionType(action) {
return action.type;
}
callback(action, dispatcher) {
var handlers = this.__actionBinder.find(action);
handlers.forEach((handler) => handler(action.payload, action.type, action, dispatcher));
if (handlers.length > 0) {
return true;
}
}
}
var Store = require("fluxxor-store");
// TODO: What about automatic splitting of payload and type? What if that's not the
// shape that's being dispatched to us?
// Should Fluxxor.Store assume a minimum {type, payload} shape?
// Handler signature would be handleAction(payload, type, action, dispatcher)
class MyStore extends Store {
constructor(options) {
super(options);
// this.getActionType provided by Fluxxor, returns the action type
// TODO: maybe make this static? Store.getActionType()
this.bindActions(
SOME_ACTION_TYPE, someActionHandler
).using(this.getActionType);
// getActionType is the default
this.bindActions(
SOME_ACTION_TYPE, someActionHandler
);
// Any non-array value can be === checked,
// arrays have each element === checked
// TODO: should consider objects, others?
this.bindActions(
[SOME_ACTION_SOURCE, SOME_ACTION_TYPE], someActionHandler
).using(this.getActionSourceAndType);
// Or maybe allow a secondary checker, by default iterates into
// arrays and objects.
this.bindActions(
{type: SOME_ACTION_TYPE, source: SOME_ACTION_SOURCE}, someActionHandler
).using(this.getActionDescriptor, _.deepEquals);
}
getActionSourceAndType(action) {
return [action.source, action.type];
}
getActionDescriptor(action) {
return {type: action.type, source: action.source};
}
}
// if not using es6 classes/extend
var MyStore = Store.create({
initialize: function(options) {
this.bindActions(...).using(...);
}
});
// Actions
// ============================================================================
// TODO: It would be nice if a fully realized actionSet could have its dispatch
// method overwritten for testing purposes, e.g. replace it with a function
// that sets some vars to be asserted against.
//
// OR, it could be replaced with a function that calls a mock.
// This should be an overarching theme of the new APIs--each should be able to
// be tested, and potentially be useful, in isolation.
//
// TODO: needs to have `addAction` etc. API
var ActionSet = require("fluxxor-actionset");
var actionSet = ActionSet({
namespace: {
action(param1, param2, param3) {
this.dispatch(ACTION_TYPE, {some: payload});
}
}
});
actionSet.setDispatchFunction((dispatcher, type, source, payload) => {
dispatcher.dispatch({type: type, source: source, payload: payload});
});
// bind to special object that gives access to actions hash and also dispatcher?
actionSet.setDispatchFunction((type, source, payload) => {
var action = {type, source, payload};
this.emit("dispatch", action);
var dispatch = this.dispatcher.dispatch(action);
this.emit("dispatched", dispatch);
//
// OR
//
var action = {type, source, payload};
var dispatch = this.dispatcher.prepareDispatch(action);
this.emit("dispatch", dispatch);
this.dispatcher.dispatch(dispatch);
this.emit("dispatched", dispatch);
});
// also allow to getDispatchFunction so that it can be wrapped
// dispatch function can be made portable using:
var dispatchFunction = (type, source, payload) => {
var dispatch = this.dispatcher.prepareDispatch(...);
}
actionSet.setDispatchFunction(dispatchFunction);
// when used....
dispatcherFunction.call({dispatcher: dispatcher}, [...]);
// OOOORRRR
// maybe dispatchFunction should just take dispatcher as the first
// argument to make it more portable
actionSet.setDispatchFunction((dispatcher) => {
return (type, source, payload) => {
dispatcher.dispatch({type, source, payload});
};
});
var actions = actionSet.dispatchTo(dispatcher);
// TODO: will using arrow functions make dynamic binding
// solutions untenable?
/// TODO
//
//
//
//
//
// It would be nice to visualize data flow,
// e.g. see which stores have registered with
// which actions, see what constants are defined,
// see what action handlers are avail.
// Can we do this easily built-in?
// Or does it need to be a custom store impl?