This repository has been archived by the owner on May 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
188 lines (175 loc) · 4.85 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
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
var slice = [].slice, hasProp = {}.hasOwnProperty;
export function global(fn) {
fn._$$global = true;
return fn;
};
export var Adapters = {
SimpleImmutable: function(SimpleImmutable) {
return {
construct: function(rootValue) {
return SimpleImmutable(rootValue);
},
getRootValueForViews: function(collection) {
return collection.get();
},
updateAt: function(collection, path, func, funcThis, args) {
return collection.at(path, function(v) {
return func.call.apply(func, [funcThis, v].concat(slice.call(args)));
});
}
};
}
};
var produced = undefined;
export function produce(v) {
return produced = v;
};
function bindReducer(scope, ssc, fn) {
if (fn._$$global) {
return function() {
var oldProduced, result;
oldProduced = produced;
produced = void 0;
ssc.mutate(fn, this, arguments);
result = produced;
produced = oldProduced;
return result;
};
} else {
return function() {
var oldProduced, result;
oldProduced = produced;
produced = void 0;
ssc.updateAt(scope, fn, this, arguments);
result = produced;
produced = oldProduced;
return result;
};
}
};
function bindReducerTree(ssc, reducerTree, scope) {
var k, newScope, r, v;
if (scope == null) {
scope = [];
}
switch (typeof reducerTree) {
case "function":
reducerTree._$$name = scope.join(".");
return bindReducer(scope.slice(0, scope.length - 1), ssc, reducerTree);
case "object":
r = {};
for (k in reducerTree) {
if (!hasProp.call(reducerTree, k)) continue;
v = reducerTree[k];
if (k !== "initial") {
newScope = scope.slice();
newScope.push(k);
r[k] = bindReducerTree(ssc, v, newScope);
}
}
return r;
default:
console.log(reducerTree);
throw new Error("Invalid reducer tree node");
}
};
function extractInitialState(root, scope) {
var k, newScope, r, v;
if (scope == null) {
scope = [];
}
switch (typeof root) {
case "object":
if (root.initial) {
return root.initial;
}
r = {};
for (k in root) {
if (!hasProp.call(root, k)) continue;
v = root[k];
newScope = scope.slice();
newScope.push(k);
r[k] = extractInitialState(v, newScope);
}
return r;
case "function":
break;
default:
console.log("state." + scope.join(".") + " was", root);
throw new Error("Invalid tree node.");
}
};
function SimpleStateContainer(collection, adapter) {
var listeners;
collection = adapter.construct(collection);
listeners = [];
return {
subscribe: function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return listeners.push.apply(listeners, args);
},
get: function() {
return collection;
},
mutate: function(func, funcThis, args) {
var fn, i, len;
collection = func.call.apply(func, [funcThis, collection].concat(slice.call(args)));
for (i = 0, len = listeners.length; i < len; i++) {
fn = listeners[i];
fn(adapter.getRootValueForViews(collection), func, args);
}
return void 0;
},
updateAt: function(path, func, funcThis, args) {
var fn, i, len;
collection = adapter.updateAt(collection, path, func, funcThis, args);
for (i = 0, len = listeners.length; i < len; i++) {
fn = listeners[i];
fn(adapter.getRootValueForViews(collection), func, args);
}
return void 0;
}
};
};
export function consoleLogger($, _, fn, params) {
var previewParams = params.length && params.length > 1
? [params[0], "(...)"]
: slice.call(params);
console.groupCollapsed.apply(console, ["$." + fn._$$name].concat(previewParams));
console.log.apply(console, ["$." + fn._$$name].concat(["("]).concat(slice.call(params)).concat([")"]));
// console.log(fn);
console.log("State:", _);
console.trace();
console.groupEnd();
};
export function createStore(reducerTree, adapter) {
var $, initialState, ssc, subscribe;
if (!reducerTree) {
throw new Error("no reducer tree passed to crateStore");
}
if (!adapter) {
throw new Error("adapter passed to crateStore");
}
initialState = extractInitialState(reducerTree);
ssc = SimpleStateContainer(initialState, adapter);
$ = bindReducerTree(ssc, reducerTree, []);
subscribe = function() {
var fns;
fns = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return ssc.subscribe(function(_, fnName, params) {
var fn, i, len, results;
results = [];
for (i = 0, len = fns.length; i < len; i++) {
fn = fns[i];
results.push(fn($, _, fnName, params));
}
return results;
});
};
return {
_: ssc.get().get(),
$: $,
subscribe: subscribe
};
};