-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
112 lines (93 loc) · 2.54 KB
/
index.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
var Dispatcher = require('./lib/Dispatcher');
var FluxStore = require('./lib/Store');
var FluxActions = require('./lib/Action');
var FluxConstants = require('./lib/Constants');
var extend = require('lodash-node/modern/objects/assign');
var stores = [];
var actions = [];
var assignDataToStore = function(initialData, Store) {
if (Store.name && initialData) {
var state = initialData[Store.name];
if (state) {
Store.replaceState(state);
}
}
};
var safeStringify = function (obj) {
return JSON.stringify(obj).replace(/<\//g, '<\\\\/').replace(/<\!--/g, '<\\\\!--');
};
var Fluxy = function () {
this._dispatcher = new Dispatcher();
};
Fluxy.createStore = function (proto) {
var Store = FluxStore.extend(function () {
FluxStore.call(this);
}, proto);
var store = new Store();
stores.push(store);
return store;
};
Fluxy.createActions = function (proto) {
var Action = FluxActions.extend(proto);
var action = new Action();
actions.push(action);
return action;
};
Fluxy.createConstants = function (values) {
return FluxConstants(values);
};
Fluxy.start = function (initialData) {
var flux = new Fluxy();
stores.forEach(function (store) {
store.mount(flux);
assignDataToStore(initialData, store);
});
actions.forEach(function (action) {
action.mount(flux);
});
return flux;
};
Fluxy.bootstrap = function (key, context) {
var initialData;
if (!context && window) {
context = window;
}
if (context && context[key]) {
initialData = context[key];
}
Fluxy.start(initialData);
};
Fluxy.renderStateToString = function (serializer) {
var state = {};
serializer = serializer || safeStringify;
stores.forEach(function (store) {
if (store.name) {
state[store.name] = store.toJS(store.state);
}
});
return serializer(state);
};
Fluxy.reset = function () {
stores = [];
actions = [];
};
//What type of collections are being used?
//e.g. mori, immutable
Fluxy.setCollectionProxyType = function (type) {
var Proxy = require('./lib/collections/Proxy');
Fluxy.$ = Proxy._setProxyType(type);
Fluxy._Proxy = Proxy;
};
Fluxy.prototype = extend(Fluxy.prototype, {
//dispatcher delegation
registerAction: function () {
return this._dispatcher.registerAction.apply(this._dispatcher, arguments);
},
registerDeferedAction: function () {
return this._dispatcher.registerDeferedAction.apply(this._dispatcher, arguments);
},
dispatchAction: function () {
return this._dispatcher.dispatchAction.apply(this._dispatcher, arguments);
},
});
module.exports = Fluxy;