sygnal 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc +3 -0
- package/LICENSE +21 -0
- package/README.md +301 -0
- package/dist/collection.js +83 -0
- package/dist/component.js +1152 -0
- package/dist/extra/eventDriver.js +49 -0
- package/dist/extra/logDriver.js +14 -0
- package/dist/extra/processForm.js +81 -0
- package/dist/extra/run.js +38 -0
- package/dist/index.js +59 -0
- package/dist/switchable.js +159 -0
- package/dist/sygnal.min.js +8 -0
- package/package.json +44 -0
|
@@ -0,0 +1,1152 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.ABORT = void 0;
|
|
7
|
+
exports["default"] = component;
|
|
8
|
+
|
|
9
|
+
var _isolate = _interopRequireDefault(require("@cycle/isolate"));
|
|
10
|
+
|
|
11
|
+
var _collection = _interopRequireDefault(require("./collection.js"));
|
|
12
|
+
|
|
13
|
+
var _state2 = require("@cycle/state");
|
|
14
|
+
|
|
15
|
+
var _xstream = _interopRequireWildcard(require("xstream"));
|
|
16
|
+
|
|
17
|
+
var _delay = _interopRequireDefault(require("xstream/extra/delay.js"));
|
|
18
|
+
|
|
19
|
+
var _concat = _interopRequireDefault(require("xstream/extra/concat.js"));
|
|
20
|
+
|
|
21
|
+
var _debounce = _interopRequireDefault(require("xstream/extra/debounce.js"));
|
|
22
|
+
|
|
23
|
+
var _dropRepeats = _interopRequireDefault(require("xstream/extra/dropRepeats.js"));
|
|
24
|
+
|
|
25
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
26
|
+
|
|
27
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
28
|
+
|
|
29
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
30
|
+
|
|
31
|
+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
32
|
+
|
|
33
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
34
|
+
|
|
35
|
+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
36
|
+
|
|
37
|
+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
38
|
+
|
|
39
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
40
|
+
|
|
41
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
42
|
+
|
|
43
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
44
|
+
|
|
45
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
46
|
+
|
|
47
|
+
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
48
|
+
|
|
49
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
50
|
+
|
|
51
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
52
|
+
|
|
53
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
54
|
+
|
|
55
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
56
|
+
|
|
57
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
58
|
+
|
|
59
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
60
|
+
|
|
61
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
62
|
+
|
|
63
|
+
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
64
|
+
|
|
65
|
+
// import syntax has bugs for xstream in Node context
|
|
66
|
+
// this attempts to normalize to work in both Node and browser
|
|
67
|
+
// if (!xs.never && xs.default && xs.default.never) {
|
|
68
|
+
// xs.never = xs.default.never
|
|
69
|
+
// xs.merge = xs.default.merge
|
|
70
|
+
// xs.of = xs.default.of
|
|
71
|
+
// }
|
|
72
|
+
// const concat = (Concat && Concat.default) ? Concat.default : Concat
|
|
73
|
+
// const delay = (Delay && Delay.default) ? Delay.default : Delay
|
|
74
|
+
// const dropRepeats = (DropRepeats && DropRepeats.default) ? DropRepeats.default : DropRepeats
|
|
75
|
+
var ENVIRONMENT = typeof window != 'undefined' && window || process && process.env || {};
|
|
76
|
+
var REQUEST_SELECTOR_METHOD = 'request';
|
|
77
|
+
var BOOTSTRAP_ACTION = 'BOOTSTRAP';
|
|
78
|
+
var INITIALIZE_ACTION = 'INITIALIZE';
|
|
79
|
+
var HYDRATE_ACTION = 'HYDRATE';
|
|
80
|
+
var IS_ROOT_COMPONENT = true;
|
|
81
|
+
var ABORT = '~#~#~ABORT~#~#~';
|
|
82
|
+
exports.ABORT = ABORT;
|
|
83
|
+
|
|
84
|
+
function component(opts) {
|
|
85
|
+
var name = opts.name,
|
|
86
|
+
sources = opts.sources,
|
|
87
|
+
isolateOpts = opts.isolateOpts,
|
|
88
|
+
_opts$stateSourceName = opts.stateSourceName,
|
|
89
|
+
stateSourceName = _opts$stateSourceName === void 0 ? 'STATE' : _opts$stateSourceName;
|
|
90
|
+
|
|
91
|
+
if (sources && _typeof(sources) !== 'object') {
|
|
92
|
+
throw new Error('Sources must be a Cycle.js sources object:', name);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
var fixedIsolateOpts;
|
|
96
|
+
|
|
97
|
+
if (typeof isolateOpts == 'string') {
|
|
98
|
+
fixedIsolateOpts = _defineProperty({}, stateSourceName, isolateOpts);
|
|
99
|
+
} else {
|
|
100
|
+
if (isolateOpts === true) {
|
|
101
|
+
fixedIsolateOpts = {};
|
|
102
|
+
} else {
|
|
103
|
+
fixedIsolateOpts = isolateOpts;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
var currySources = typeof sources === 'undefined';
|
|
108
|
+
|
|
109
|
+
if (_typeof(fixedIsolateOpts) == 'object') {
|
|
110
|
+
var wrapped = function wrapped(sources) {
|
|
111
|
+
var fixedOpts = _objectSpread(_objectSpread({}, opts), {}, {
|
|
112
|
+
sources: sources
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
return new Component(fixedOpts).sinks;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
return currySources ? (0, _isolate["default"])(wrapped, fixedIsolateOpts) : (0, _isolate["default"])(wrapped, fixedIsolateOpts)(sources);
|
|
119
|
+
} else {
|
|
120
|
+
return currySources ? function (sources) {
|
|
121
|
+
return new Component(_objectSpread(_objectSpread({}, opts), {}, {
|
|
122
|
+
sources: sources
|
|
123
|
+
})).sinks;
|
|
124
|
+
} : new Component(opts).sinks;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
var Component = /*#__PURE__*/function () {
|
|
129
|
+
// [ PASSED PARAMETERS ]
|
|
130
|
+
// name
|
|
131
|
+
// sources
|
|
132
|
+
// intent
|
|
133
|
+
// request
|
|
134
|
+
// model
|
|
135
|
+
// response
|
|
136
|
+
// view
|
|
137
|
+
// children
|
|
138
|
+
// initialState
|
|
139
|
+
// calculated
|
|
140
|
+
// storeCalculatedInState
|
|
141
|
+
// DOMSourceName
|
|
142
|
+
// stateSourceName
|
|
143
|
+
// requestSourceName
|
|
144
|
+
// [ PRIVATE / CALCULATED VALUES ]
|
|
145
|
+
// sourceNames
|
|
146
|
+
// intent$
|
|
147
|
+
// action$
|
|
148
|
+
// model$
|
|
149
|
+
// response$
|
|
150
|
+
// sendResponse$
|
|
151
|
+
// children$
|
|
152
|
+
// vdom$
|
|
153
|
+
// subComponentSink$
|
|
154
|
+
// [ INSTANTIATED STREAM OPERATOR ]
|
|
155
|
+
// log
|
|
156
|
+
// [ OUTPUT ]
|
|
157
|
+
// sinks
|
|
158
|
+
function Component(_ref) {
|
|
159
|
+
var _this = this;
|
|
160
|
+
|
|
161
|
+
var _ref$name = _ref.name,
|
|
162
|
+
name = _ref$name === void 0 ? 'NO NAME' : _ref$name,
|
|
163
|
+
sources = _ref.sources,
|
|
164
|
+
intent = _ref.intent,
|
|
165
|
+
request = _ref.request,
|
|
166
|
+
model = _ref.model,
|
|
167
|
+
response = _ref.response,
|
|
168
|
+
view = _ref.view,
|
|
169
|
+
_ref$children = _ref.children,
|
|
170
|
+
children = _ref$children === void 0 ? {} : _ref$children,
|
|
171
|
+
_ref$components = _ref.components,
|
|
172
|
+
components = _ref$components === void 0 ? {} : _ref$components,
|
|
173
|
+
initialState = _ref.initialState,
|
|
174
|
+
calculated = _ref.calculated,
|
|
175
|
+
_ref$storeCalculatedI = _ref.storeCalculatedInState,
|
|
176
|
+
storeCalculatedInState = _ref$storeCalculatedI === void 0 ? true : _ref$storeCalculatedI,
|
|
177
|
+
_ref$DOMSourceName = _ref.DOMSourceName,
|
|
178
|
+
DOMSourceName = _ref$DOMSourceName === void 0 ? 'DOM' : _ref$DOMSourceName,
|
|
179
|
+
_ref$stateSourceName = _ref.stateSourceName,
|
|
180
|
+
stateSourceName = _ref$stateSourceName === void 0 ? 'STATE' : _ref$stateSourceName,
|
|
181
|
+
_ref$requestSourceNam = _ref.requestSourceName,
|
|
182
|
+
requestSourceName = _ref$requestSourceNam === void 0 ? 'HTTP' : _ref$requestSourceNam;
|
|
183
|
+
|
|
184
|
+
_classCallCheck(this, Component);
|
|
185
|
+
|
|
186
|
+
if (!sources || _typeof(sources) != 'object') throw new Error('Missing or invalid sources');
|
|
187
|
+
this.name = name;
|
|
188
|
+
this.sources = sources;
|
|
189
|
+
this.intent = intent;
|
|
190
|
+
this.request = request;
|
|
191
|
+
this.model = model;
|
|
192
|
+
this.response = response;
|
|
193
|
+
this.view = view;
|
|
194
|
+
this.children = children;
|
|
195
|
+
this.components = components;
|
|
196
|
+
this.initialState = initialState;
|
|
197
|
+
this.calculated = calculated;
|
|
198
|
+
this.storeCalculatedInState = storeCalculatedInState;
|
|
199
|
+
this.DOMSourceName = DOMSourceName;
|
|
200
|
+
this.stateSourceName = stateSourceName;
|
|
201
|
+
this.requestSourceName = requestSourceName;
|
|
202
|
+
this.sourceNames = Object.keys(sources);
|
|
203
|
+
this.isSubComponent = this.sourceNames.includes('props$');
|
|
204
|
+
var state$ = sources[stateSourceName] && sources[stateSourceName].stream;
|
|
205
|
+
|
|
206
|
+
if (state$) {
|
|
207
|
+
this.currentState = initialState || {};
|
|
208
|
+
this.sources[this.stateSourceName] = new _state2.StateSource(state$.map(function (val) {
|
|
209
|
+
_this.currentState = val;
|
|
210
|
+
return val;
|
|
211
|
+
}));
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (IS_ROOT_COMPONENT && typeof this.intent === 'undefined' && typeof this.model === 'undefined') {
|
|
215
|
+
this.initialState = initialState || true;
|
|
216
|
+
|
|
217
|
+
this.intent = function (_) {
|
|
218
|
+
return {
|
|
219
|
+
__NOOP_ACTION__: _xstream["default"].never()
|
|
220
|
+
};
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
this.model = {
|
|
224
|
+
__NOOP_ACTION__: function __NOOP_ACTION__(state) {
|
|
225
|
+
return state;
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
IS_ROOT_COMPONENT = false;
|
|
231
|
+
this.log = makeLog(name);
|
|
232
|
+
this.initIntent$();
|
|
233
|
+
this.initAction$();
|
|
234
|
+
this.initResponse$();
|
|
235
|
+
this.initState();
|
|
236
|
+
this.initModel$();
|
|
237
|
+
this.initSendResponse$();
|
|
238
|
+
this.initChildren$();
|
|
239
|
+
this.initSubComponentSink$();
|
|
240
|
+
this.initVdom$();
|
|
241
|
+
this.initSinks();
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
_createClass(Component, [{
|
|
245
|
+
key: "initIntent$",
|
|
246
|
+
value: function initIntent$() {
|
|
247
|
+
if (!this.intent) {
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (typeof this.intent != 'function') {
|
|
252
|
+
throw new Error('Intent must be a function');
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
this.intent$ = this.intent(this.sources);
|
|
256
|
+
|
|
257
|
+
if (!(this.intent$ instanceof _xstream.Stream) && _typeof(this.intent$) != 'object') {
|
|
258
|
+
throw new Error('Intent must return either an action$ stream or map of event streams');
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}, {
|
|
262
|
+
key: "initAction$",
|
|
263
|
+
value: function initAction$() {
|
|
264
|
+
var requestSource = this.sources && this.sources[this.requestSourceName] || null;
|
|
265
|
+
|
|
266
|
+
if (!this.intent$) {
|
|
267
|
+
this.action$ = _xstream["default"].never();
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
var runner;
|
|
272
|
+
|
|
273
|
+
if (this.intent$ instanceof _xstream.Stream) {
|
|
274
|
+
runner = this.intent$;
|
|
275
|
+
} else {
|
|
276
|
+
var mapped = Object.entries(this.intent$).map(function (_ref2) {
|
|
277
|
+
var _ref3 = _slicedToArray(_ref2, 2),
|
|
278
|
+
type = _ref3[0],
|
|
279
|
+
data$ = _ref3[1];
|
|
280
|
+
|
|
281
|
+
return data$.map(function (data) {
|
|
282
|
+
return {
|
|
283
|
+
type: type,
|
|
284
|
+
data: data
|
|
285
|
+
};
|
|
286
|
+
});
|
|
287
|
+
});
|
|
288
|
+
runner = _xstream["default"].merge.apply(_xstream["default"], [_xstream["default"].never()].concat(_toConsumableArray(mapped)));
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
var action$ = runner instanceof _xstream.Stream ? runner : runner.apply && runner(this.sources) || _xstream["default"].never();
|
|
292
|
+
var wrapped$ = (0, _concat["default"])(_xstream["default"].of({
|
|
293
|
+
type: BOOTSTRAP_ACTION
|
|
294
|
+
}), action$).compose((0, _delay["default"])(10));
|
|
295
|
+
var initialApiData;
|
|
296
|
+
|
|
297
|
+
if (requestSource && typeof requestSource.select == 'function') {
|
|
298
|
+
initialApiData = requestSource.select('initial').flatten();
|
|
299
|
+
} else {
|
|
300
|
+
initialApiData = _xstream["default"].never();
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
var hydrate$ = initialApiData.map(function (data) {
|
|
304
|
+
return {
|
|
305
|
+
type: HYDRATE_ACTION,
|
|
306
|
+
data: data
|
|
307
|
+
};
|
|
308
|
+
});
|
|
309
|
+
this.action$ = _xstream["default"].merge(wrapped$, hydrate$).compose(this.log(function (_ref4) {
|
|
310
|
+
var type = _ref4.type;
|
|
311
|
+
return "Action triggered: <".concat(type, ">");
|
|
312
|
+
}));
|
|
313
|
+
}
|
|
314
|
+
}, {
|
|
315
|
+
key: "initResponse$",
|
|
316
|
+
value: function initResponse$() {
|
|
317
|
+
var _this2 = this;
|
|
318
|
+
|
|
319
|
+
if (typeof this.request == 'undefined') {
|
|
320
|
+
return;
|
|
321
|
+
} else if (_typeof(this.request) != 'object') {
|
|
322
|
+
throw new Error('The request parameter must be an object');
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
var router$ = this.sources[this.requestSourceName];
|
|
326
|
+
var methods = Object.entries(this.request);
|
|
327
|
+
var wrapped = methods.reduce(function (acc, _ref5) {
|
|
328
|
+
var _ref6 = _slicedToArray(_ref5, 2),
|
|
329
|
+
method = _ref6[0],
|
|
330
|
+
routes = _ref6[1];
|
|
331
|
+
|
|
332
|
+
var _method = method.toLowerCase();
|
|
333
|
+
|
|
334
|
+
if (typeof router$[_method] != 'function') {
|
|
335
|
+
throw new Error('Invalid method in request object:', method);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
var entries = Object.entries(routes);
|
|
339
|
+
var mapped = entries.reduce(function (acc, _ref7) {
|
|
340
|
+
var _ref8 = _slicedToArray(_ref7, 2),
|
|
341
|
+
route = _ref8[0],
|
|
342
|
+
action = _ref8[1];
|
|
343
|
+
|
|
344
|
+
var routeString = "[".concat(_method.toUpperCase(), "]:").concat(route || 'none');
|
|
345
|
+
|
|
346
|
+
var actionType = _typeof(action);
|
|
347
|
+
|
|
348
|
+
if (actionType === 'undefined') {
|
|
349
|
+
throw new Error("Action for '".concat(route, "' route in request object not specified"));
|
|
350
|
+
} else if (actionType !== 'string' && actionType !== 'function') {
|
|
351
|
+
throw new Error("Invalid action for '".concat(route, "' route: expecting string or function"));
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
var actionString = actionType === 'function' ? '[ FUNCTION ]' : "< ".concat(action, " >");
|
|
355
|
+
console.log("[".concat(_this2.name, "] Adding ").concat(_this2.requestSourceName, " route:"), _method.toUpperCase(), "'".concat(route, "' <").concat(actionString, ">"));
|
|
356
|
+
|
|
357
|
+
var route$ = router$[_method](route).compose((0, _dropRepeats["default"])(function (a, b) {
|
|
358
|
+
return a.id == b.id;
|
|
359
|
+
})).map(function (req) {
|
|
360
|
+
if (!req || !req.id) {
|
|
361
|
+
throw new Error("No id found in request: ".concat(routeString));
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
try {
|
|
365
|
+
var _reqId = req.id;
|
|
366
|
+
var params = req.params;
|
|
367
|
+
var body = req.body;
|
|
368
|
+
var cookies = req.cookies;
|
|
369
|
+
var type = actionType === 'function' ? 'FUNCTION' : action;
|
|
370
|
+
var data = {
|
|
371
|
+
params: params,
|
|
372
|
+
body: body,
|
|
373
|
+
cookies: cookies,
|
|
374
|
+
req: req
|
|
375
|
+
};
|
|
376
|
+
var obj = {
|
|
377
|
+
type: type,
|
|
378
|
+
data: body,
|
|
379
|
+
req: req,
|
|
380
|
+
_reqId: _reqId,
|
|
381
|
+
_action: type
|
|
382
|
+
};
|
|
383
|
+
var timestamp = new Date().toISOString();
|
|
384
|
+
var ip = req.get ? req.get('host') : '0.0.0.0';
|
|
385
|
+
console.log("".concat(timestamp, " ").concat(ip, " ").concat(req.method, " ").concat(req.url));
|
|
386
|
+
|
|
387
|
+
if (ENVIRONMENT.DEBUG) {
|
|
388
|
+
_this2.action$.setDebugListener({
|
|
389
|
+
next: function next(_ref9) {
|
|
390
|
+
var type = _ref9.type;
|
|
391
|
+
return console.log("[".concat(_this2.name, "] Action from ").concat(_this2.requestSourceName, " request: <").concat(type, ">"));
|
|
392
|
+
}
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
if (actionType === 'function') {
|
|
397
|
+
var enhancedState = _this2.addCalculated(_this2.currentState);
|
|
398
|
+
|
|
399
|
+
var result = action(enhancedState, req);
|
|
400
|
+
return _xstream["default"].of(_objectSpread(_objectSpread({}, obj), {}, {
|
|
401
|
+
data: result
|
|
402
|
+
}));
|
|
403
|
+
} else {
|
|
404
|
+
_this2.action$.shamefullySendNext(obj);
|
|
405
|
+
|
|
406
|
+
var sourceEntries = Object.entries(_this2.sources);
|
|
407
|
+
var responses = sourceEntries.reduce(function (acc, _ref10) {
|
|
408
|
+
var _ref11 = _slicedToArray(_ref10, 2),
|
|
409
|
+
name = _ref11[0],
|
|
410
|
+
source = _ref11[1];
|
|
411
|
+
|
|
412
|
+
if (!source || typeof source[REQUEST_SELECTOR_METHOD] != 'function') return acc;
|
|
413
|
+
var selected$ = source[REQUEST_SELECTOR_METHOD](_reqId);
|
|
414
|
+
return [].concat(_toConsumableArray(acc), [selected$]);
|
|
415
|
+
}, []);
|
|
416
|
+
return _xstream["default"].merge.apply(_xstream["default"], _toConsumableArray(responses));
|
|
417
|
+
}
|
|
418
|
+
} catch (err) {
|
|
419
|
+
console.error(err);
|
|
420
|
+
}
|
|
421
|
+
}).flatten();
|
|
422
|
+
|
|
423
|
+
return [].concat(_toConsumableArray(acc), [route$]);
|
|
424
|
+
}, []);
|
|
425
|
+
|
|
426
|
+
var mapped$ = _xstream["default"].merge.apply(_xstream["default"], _toConsumableArray(mapped));
|
|
427
|
+
|
|
428
|
+
return [].concat(_toConsumableArray(acc), [mapped$]);
|
|
429
|
+
}, []);
|
|
430
|
+
this.response$ = _xstream["default"].merge.apply(_xstream["default"], _toConsumableArray(wrapped)).compose(this.log(function (res) {
|
|
431
|
+
if (res._action) return "[".concat(_this2.requestSourceName, "] response data received for Action: <").concat(res._action, ">");
|
|
432
|
+
return "[".concat(_this2.requestSourceName, "] response data received from FUNCTION");
|
|
433
|
+
}));
|
|
434
|
+
|
|
435
|
+
if (typeof this.response != 'undefined' && typeof this.response$ == 'undefined') {
|
|
436
|
+
throw new Error('Cannot have a response parameter without a request parameter');
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}, {
|
|
440
|
+
key: "initState",
|
|
441
|
+
value: function initState() {
|
|
442
|
+
var _this3 = this;
|
|
443
|
+
|
|
444
|
+
if (this.model != undefined) {
|
|
445
|
+
if (this.model[INITIALIZE_ACTION] === undefined) {
|
|
446
|
+
this.model[INITIALIZE_ACTION] = _defineProperty({}, this.stateSourceName, function (_, data) {
|
|
447
|
+
return _objectSpread({}, _this3.addCalculated(data));
|
|
448
|
+
});
|
|
449
|
+
} else {
|
|
450
|
+
Object.keys(this.model[INITIALIZE_ACTION]).forEach(function (name) {
|
|
451
|
+
if (name !== _this3.stateSourceName) {
|
|
452
|
+
console.warn("".concat(INITIALIZE_ACTION, " can only be used with the ").concat(_this3.stateSourceName, " source... disregarding ").concat(name));
|
|
453
|
+
delete _this3.model[INITIALIZE_ACTION][name];
|
|
454
|
+
}
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}, {
|
|
460
|
+
key: "initModel$",
|
|
461
|
+
value: function initModel$() {
|
|
462
|
+
var _this4 = this;
|
|
463
|
+
|
|
464
|
+
if (typeof this.model == 'undefined') {
|
|
465
|
+
this.model$ = this.sourceNames.reduce(function (a, s) {
|
|
466
|
+
a[s] = _xstream["default"].never();
|
|
467
|
+
return a;
|
|
468
|
+
}, {});
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
var initial = {
|
|
473
|
+
type: INITIALIZE_ACTION,
|
|
474
|
+
data: this.initialState
|
|
475
|
+
};
|
|
476
|
+
var shimmed$ = this.initialState ? (0, _concat["default"])(_xstream["default"].of(initial), this.action$).compose((0, _delay["default"])(0)) : this.action$;
|
|
477
|
+
var onState = this.makeOnAction(shimmed$, true, this.action$);
|
|
478
|
+
var onNormal = this.makeOnAction(this.action$, false, this.action$);
|
|
479
|
+
var modelEntries = Object.entries(this.model);
|
|
480
|
+
var reducers = {};
|
|
481
|
+
modelEntries.forEach(function (entry) {
|
|
482
|
+
var _entry = _slicedToArray(entry, 2),
|
|
483
|
+
action = _entry[0],
|
|
484
|
+
sinks = _entry[1];
|
|
485
|
+
|
|
486
|
+
if (typeof sinks === 'function') {
|
|
487
|
+
sinks = _defineProperty({}, _this4.stateSourceName, sinks);
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
if (_typeof(sinks) !== 'object') {
|
|
491
|
+
throw new Error("Entry for each action must be an object: ".concat(_this4.name, " ").concat(action));
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
var sinkEntries = Object.entries(sinks);
|
|
495
|
+
sinkEntries.forEach(function (entry) {
|
|
496
|
+
var _entry2 = _slicedToArray(entry, 2),
|
|
497
|
+
sink = _entry2[0],
|
|
498
|
+
reducer = _entry2[1];
|
|
499
|
+
|
|
500
|
+
var isStateSink = sink == _this4.stateSourceName;
|
|
501
|
+
var on = isStateSink ? onState : onNormal;
|
|
502
|
+
var onned = on(action, reducer);
|
|
503
|
+
var wrapped = onned.compose(_this4.log(function (data) {
|
|
504
|
+
if (isStateSink) {
|
|
505
|
+
return "State reducer added: <".concat(action, ">");
|
|
506
|
+
} else {
|
|
507
|
+
var extra = data && (data.type || data.command || data.name || data.key || Array.isArray(data) && 'Array' || data);
|
|
508
|
+
return "Data sent to [".concat(sink, "]: <").concat(action, "> ").concat(extra);
|
|
509
|
+
}
|
|
510
|
+
}));
|
|
511
|
+
|
|
512
|
+
if (Array.isArray(reducers[sink])) {
|
|
513
|
+
reducers[sink].push(wrapped);
|
|
514
|
+
} else {
|
|
515
|
+
reducers[sink] = [wrapped];
|
|
516
|
+
}
|
|
517
|
+
});
|
|
518
|
+
});
|
|
519
|
+
var model$ = Object.entries(reducers).reduce(function (acc, entry) {
|
|
520
|
+
var _entry3 = _slicedToArray(entry, 2),
|
|
521
|
+
sink = _entry3[0],
|
|
522
|
+
streams = _entry3[1];
|
|
523
|
+
|
|
524
|
+
acc[sink] = _xstream["default"].merge.apply(_xstream["default"], [_xstream["default"].never()].concat(_toConsumableArray(streams)));
|
|
525
|
+
return acc;
|
|
526
|
+
}, {});
|
|
527
|
+
this.model$ = model$;
|
|
528
|
+
}
|
|
529
|
+
}, {
|
|
530
|
+
key: "initSendResponse$",
|
|
531
|
+
value: function initSendResponse$() {
|
|
532
|
+
var _this5 = this;
|
|
533
|
+
|
|
534
|
+
var responseType = _typeof(this.response);
|
|
535
|
+
|
|
536
|
+
if (responseType != 'function' && responseType != 'undefined') {
|
|
537
|
+
throw new Error('The response parameter must be a function');
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
if (responseType == 'undefined') {
|
|
541
|
+
if (this.response$) {
|
|
542
|
+
this.response$.subscribe({
|
|
543
|
+
next: this.log(function (_ref12) {
|
|
544
|
+
var _reqId = _ref12._reqId,
|
|
545
|
+
_action = _ref12._action;
|
|
546
|
+
return "Unhandled response for request: ".concat(_action, " ").concat(_reqId);
|
|
547
|
+
})
|
|
548
|
+
});
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
this.sendResponse$ = _xstream["default"].never();
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
var selectable = {
|
|
556
|
+
select: function select(actions) {
|
|
557
|
+
if (typeof actions == 'undefined') return _this5.response$;
|
|
558
|
+
if (!Array.isArray(actions)) actions = [actions];
|
|
559
|
+
return _this5.response$.filter(function (_ref13) {
|
|
560
|
+
var _action = _ref13._action;
|
|
561
|
+
return actions.length > 0 ? _action === 'FUNCTION' || actions.includes(_action) : true;
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
var out = this.response(selectable);
|
|
566
|
+
|
|
567
|
+
if (_typeof(out) != 'object') {
|
|
568
|
+
throw new Error('The response function must return an object');
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
var entries = Object.entries(out);
|
|
572
|
+
var out$ = entries.reduce(function (acc, _ref14) {
|
|
573
|
+
var _ref15 = _slicedToArray(_ref14, 2),
|
|
574
|
+
command = _ref15[0],
|
|
575
|
+
response$ = _ref15[1];
|
|
576
|
+
|
|
577
|
+
var mapped$ = response$.map(function (_ref16) {
|
|
578
|
+
var _reqId = _ref16._reqId,
|
|
579
|
+
_action = _ref16._action,
|
|
580
|
+
data = _ref16.data;
|
|
581
|
+
|
|
582
|
+
if (!_reqId) {
|
|
583
|
+
throw new Error("No request id found for response for: ".concat(command));
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
return {
|
|
587
|
+
_reqId: _reqId,
|
|
588
|
+
_action: _action,
|
|
589
|
+
command: command,
|
|
590
|
+
data: data
|
|
591
|
+
};
|
|
592
|
+
});
|
|
593
|
+
return [].concat(_toConsumableArray(acc), [mapped$]);
|
|
594
|
+
}, []);
|
|
595
|
+
this.sendResponse$ = _xstream["default"].merge.apply(_xstream["default"], _toConsumableArray(out$)).compose(this.log(function (_ref17) {
|
|
596
|
+
var _reqId = _ref17._reqId,
|
|
597
|
+
_action = _ref17._action;
|
|
598
|
+
return "[".concat(_this5.requestSourceName, "] response sent for: <").concat(_action, ">");
|
|
599
|
+
}));
|
|
600
|
+
}
|
|
601
|
+
}, {
|
|
602
|
+
key: "initChildren$",
|
|
603
|
+
value: function initChildren$() {
|
|
604
|
+
var _this6 = this;
|
|
605
|
+
|
|
606
|
+
var initial = this.sourceNames.reduce(function (acc, name) {
|
|
607
|
+
if (name == _this6.DOMSourceName) {
|
|
608
|
+
acc[name] = {};
|
|
609
|
+
} else {
|
|
610
|
+
acc[name] = [];
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
return acc;
|
|
614
|
+
}, {});
|
|
615
|
+
this.children$ = Object.entries(this.children).reduce(function (acc, _ref18) {
|
|
616
|
+
var _ref19 = _slicedToArray(_ref18, 2),
|
|
617
|
+
childName = _ref19[0],
|
|
618
|
+
childFactory = _ref19[1];
|
|
619
|
+
|
|
620
|
+
var child$ = childFactory(_this6.sources);
|
|
621
|
+
|
|
622
|
+
_this6.sourceNames.forEach(function (source) {
|
|
623
|
+
if (source == _this6.DOMSourceName) {
|
|
624
|
+
acc[source][childName] = child$[source];
|
|
625
|
+
} else {
|
|
626
|
+
acc[source].push(child$[source]);
|
|
627
|
+
}
|
|
628
|
+
});
|
|
629
|
+
|
|
630
|
+
return acc;
|
|
631
|
+
}, initial);
|
|
632
|
+
}
|
|
633
|
+
}, {
|
|
634
|
+
key: "initSubComponentSink$",
|
|
635
|
+
value: function initSubComponentSink$() {
|
|
636
|
+
var _this7 = this;
|
|
637
|
+
|
|
638
|
+
var subComponentSink$ = _xstream["default"].create({
|
|
639
|
+
start: function start(listener) {
|
|
640
|
+
_this7.newSubComponentSinks = listener.next.bind(listener);
|
|
641
|
+
},
|
|
642
|
+
stop: function stop(_) {}
|
|
643
|
+
});
|
|
644
|
+
|
|
645
|
+
subComponentSink$.subscribe({
|
|
646
|
+
next: function next(_) {
|
|
647
|
+
return _;
|
|
648
|
+
}
|
|
649
|
+
});
|
|
650
|
+
this.subComponentSink$ = subComponentSink$.filter(function (sinks) {
|
|
651
|
+
return Object.keys(sinks).length > 0;
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
}, {
|
|
655
|
+
key: "initVdom$",
|
|
656
|
+
value: function initVdom$() {
|
|
657
|
+
var _this8 = this;
|
|
658
|
+
|
|
659
|
+
if (typeof this.view != 'function') {
|
|
660
|
+
this.vdom$ = _xstream["default"].of(null);
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
var state = this.sources[this.stateSourceName];
|
|
665
|
+
|
|
666
|
+
var renderParams = _objectSpread({}, this.children$[this.DOMSourceName]);
|
|
667
|
+
|
|
668
|
+
var enhancedState = state && state.isolateSource(state, {
|
|
669
|
+
get: function get(state) {
|
|
670
|
+
return _this8.addCalculated(state);
|
|
671
|
+
}
|
|
672
|
+
});
|
|
673
|
+
|
|
674
|
+
var stateStream = enhancedState && enhancedState.stream || _xstream["default"].never();
|
|
675
|
+
|
|
676
|
+
renderParams.state = stateStream;
|
|
677
|
+
renderParams[this.stateSourceName] = stateStream;
|
|
678
|
+
|
|
679
|
+
if (this.sources.props$) {
|
|
680
|
+
renderParams.props = this.sources.props$;
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
if (this.sources.children$) {
|
|
684
|
+
renderParams.children = this.sources.children$;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
var pulled = Object.entries(renderParams).reduce(function (acc, _ref20) {
|
|
688
|
+
var _ref21 = _slicedToArray(_ref20, 2),
|
|
689
|
+
name = _ref21[0],
|
|
690
|
+
stream = _ref21[1];
|
|
691
|
+
|
|
692
|
+
acc.names.push(name);
|
|
693
|
+
acc.streams.push(stream);
|
|
694
|
+
return acc;
|
|
695
|
+
}, {
|
|
696
|
+
names: [],
|
|
697
|
+
streams: []
|
|
698
|
+
});
|
|
699
|
+
|
|
700
|
+
var merged = _xstream["default"].combine.apply(_xstream["default"], _toConsumableArray(pulled.streams));
|
|
701
|
+
|
|
702
|
+
var throttled = merged.compose((0, _debounce["default"])(1)).map(function (arr) {
|
|
703
|
+
return pulled.names.reduce(function (acc, name, index) {
|
|
704
|
+
acc[name] = arr[index];
|
|
705
|
+
return acc;
|
|
706
|
+
}, {});
|
|
707
|
+
});
|
|
708
|
+
var componentNames = Object.keys(this.components);
|
|
709
|
+
this.vdom$ = throttled.fold(function (previousComponents, renderParams) {
|
|
710
|
+
var vDom = _this8.view(renderParams) || {
|
|
711
|
+
sel: 'div',
|
|
712
|
+
data: {},
|
|
713
|
+
children: []
|
|
714
|
+
};
|
|
715
|
+
var foundComponents = getComponents(vDom, componentNames);
|
|
716
|
+
var entries = Object.entries(foundComponents);
|
|
717
|
+
var rootEntry = {
|
|
718
|
+
'::ROOT::': vDom
|
|
719
|
+
};
|
|
720
|
+
if (entries.length === 0) return rootEntry;
|
|
721
|
+
var newComponents = entries.reduce(function (acc, _ref22) {
|
|
722
|
+
var _ref23 = _slicedToArray(_ref22, 2),
|
|
723
|
+
id = _ref23[0],
|
|
724
|
+
el = _ref23[1];
|
|
725
|
+
|
|
726
|
+
var componentName = el.sel;
|
|
727
|
+
var data = el.data;
|
|
728
|
+
var props = data.props || {};
|
|
729
|
+
var children = el.children || [];
|
|
730
|
+
var isCollection = data.isCollection || false;
|
|
731
|
+
var isSwitchable = data.isSwitchable || false;
|
|
732
|
+
|
|
733
|
+
if (previousComponents[id]) {
|
|
734
|
+
var entry = previousComponents[id];
|
|
735
|
+
acc[id] = entry;
|
|
736
|
+
entry.props$.shamefullySendNext(props);
|
|
737
|
+
entry.children$.shamefullySendNext(children);
|
|
738
|
+
return acc;
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
var factory = _this8.components[componentName];
|
|
742
|
+
|
|
743
|
+
var props$ = _xstream["default"].create().startWith(props);
|
|
744
|
+
|
|
745
|
+
var children$ = _xstream["default"].create().startWith(children);
|
|
746
|
+
|
|
747
|
+
var propState;
|
|
748
|
+
var sink$;
|
|
749
|
+
|
|
750
|
+
if (isCollection) {
|
|
751
|
+
var _objectSpread2;
|
|
752
|
+
|
|
753
|
+
propState = new _state2.StateSource(props$.map(function (val) {
|
|
754
|
+
return val.value;
|
|
755
|
+
}).remember());
|
|
756
|
+
|
|
757
|
+
var sources = _objectSpread(_objectSpread({}, _this8.sources), {}, (_objectSpread2 = {}, _defineProperty(_objectSpread2, _this8.stateSourceName, propState), _defineProperty(_objectSpread2, "props$", props$), _defineProperty(_objectSpread2, "children$", children$), _objectSpread2));
|
|
758
|
+
|
|
759
|
+
var _factory = _this8.components[data.component];
|
|
760
|
+
var lense = {
|
|
761
|
+
get: function get(state) {
|
|
762
|
+
return state;
|
|
763
|
+
},
|
|
764
|
+
set: function set(state) {
|
|
765
|
+
return state;
|
|
766
|
+
}
|
|
767
|
+
};
|
|
768
|
+
sink$ = (0, _collection["default"])(_factory, lense)(sources);
|
|
769
|
+
} else if (isSwitchable) {} else {
|
|
770
|
+
var _objectSpread3;
|
|
771
|
+
|
|
772
|
+
var _lense = function _lense(props) {
|
|
773
|
+
var state = props.state;
|
|
774
|
+
if (typeof state === 'undefined') return props;
|
|
775
|
+
if (_typeof(state) !== 'object') return state;
|
|
776
|
+
|
|
777
|
+
var copy = _objectSpread({}, props);
|
|
778
|
+
|
|
779
|
+
delete copy.state;
|
|
780
|
+
return _objectSpread(_objectSpread({}, copy), state);
|
|
781
|
+
};
|
|
782
|
+
|
|
783
|
+
propState = new _state2.StateSource(props$.map(_lense));
|
|
784
|
+
|
|
785
|
+
var _sources = _objectSpread(_objectSpread({}, _this8.sources), {}, (_objectSpread3 = {}, _defineProperty(_objectSpread3, _this8.stateSourceName, propState), _defineProperty(_objectSpread3, "props$", props$), _defineProperty(_objectSpread3, "children$", children$), _objectSpread3));
|
|
786
|
+
|
|
787
|
+
sink$ = factory(_sources);
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
var originalDOMSink = sink$[_this8.DOMSourceName];
|
|
791
|
+
sink$[_this8.DOMSourceName] = propState.stream.map(function (state) {
|
|
792
|
+
return originalDOMSink.compose((0, _debounce["default"])(2));
|
|
793
|
+
}).flatten();
|
|
794
|
+
acc[id] = {
|
|
795
|
+
sink$: sink$,
|
|
796
|
+
props$: props$,
|
|
797
|
+
children$: children$
|
|
798
|
+
};
|
|
799
|
+
return acc;
|
|
800
|
+
}, rootEntry);
|
|
801
|
+
return newComponents;
|
|
802
|
+
}, {}).map(function (components) {
|
|
803
|
+
var root = components['::ROOT::'];
|
|
804
|
+
var ids = [];
|
|
805
|
+
var entries = Object.entries(components).filter(function (_ref24) {
|
|
806
|
+
var _ref25 = _slicedToArray(_ref24, 1),
|
|
807
|
+
id = _ref25[0];
|
|
808
|
+
|
|
809
|
+
return id !== '::ROOT::';
|
|
810
|
+
});
|
|
811
|
+
if (entries.length === 0) return _xstream["default"].of(root);
|
|
812
|
+
var sinkArrays = entries.reduce(function (acc, _ref26) {
|
|
813
|
+
var _ref27 = _slicedToArray(_ref26, 2),
|
|
814
|
+
id = _ref27[0],
|
|
815
|
+
val = _ref27[1];
|
|
816
|
+
|
|
817
|
+
Object.entries(val.sink$).forEach(function (_ref28) {
|
|
818
|
+
var _ref29 = _slicedToArray(_ref28, 2),
|
|
819
|
+
name = _ref29[0],
|
|
820
|
+
stream = _ref29[1];
|
|
821
|
+
|
|
822
|
+
if (!acc[name]) acc[name] = [];
|
|
823
|
+
if (name !== _this8.DOMSourceName) acc[name].push(stream);
|
|
824
|
+
});
|
|
825
|
+
return acc;
|
|
826
|
+
}, {});
|
|
827
|
+
var sink$ = Object.entries(sinkArrays).reduce(function (acc, _ref30) {
|
|
828
|
+
var _ref31 = _slicedToArray(_ref30, 2),
|
|
829
|
+
sink = _ref31[0],
|
|
830
|
+
streams = _ref31[1];
|
|
831
|
+
|
|
832
|
+
acc[sink] = _xstream["default"].merge.apply(_xstream["default"], _toConsumableArray(streams));
|
|
833
|
+
return acc;
|
|
834
|
+
}, {});
|
|
835
|
+
|
|
836
|
+
_this8.newSubComponentSinks(sink$);
|
|
837
|
+
|
|
838
|
+
var vdom$ = entries.map(function (_ref32) {
|
|
839
|
+
var _ref33 = _slicedToArray(_ref32, 2),
|
|
840
|
+
id = _ref33[0],
|
|
841
|
+
val = _ref33[1];
|
|
842
|
+
|
|
843
|
+
ids.push(id);
|
|
844
|
+
return val.sink$[_this8.DOMSourceName];
|
|
845
|
+
});
|
|
846
|
+
if (vdom$.length === 0) return _xstream["default"].of(root);
|
|
847
|
+
return _xstream["default"].combine.apply(_xstream["default"], _toConsumableArray(vdom$)).map(function (vdoms) {
|
|
848
|
+
var withIds = vdoms.reduce(function (acc, vdom, index) {
|
|
849
|
+
acc[ids[index]] = vdom;
|
|
850
|
+
return acc;
|
|
851
|
+
}, {});
|
|
852
|
+
var injected = injectComponents(root, withIds, componentNames);
|
|
853
|
+
return injected;
|
|
854
|
+
});
|
|
855
|
+
}).flatten().filter(function (val) {
|
|
856
|
+
return !!val;
|
|
857
|
+
}).remember().compose(this.log('View Rendered'));
|
|
858
|
+
}
|
|
859
|
+
}, {
|
|
860
|
+
key: "initSinks",
|
|
861
|
+
value: function initSinks() {
|
|
862
|
+
var _this9 = this;
|
|
863
|
+
|
|
864
|
+
this.sinks = this.sourceNames.reduce(function (acc, name) {
|
|
865
|
+
if (name == _this9.DOMSourceName) return acc;
|
|
866
|
+
var subComponentSink$ = _this9.subComponentSink$ ? _this9.subComponentSink$.map(function (sinks) {
|
|
867
|
+
return sinks[name];
|
|
868
|
+
}).filter(function (sink) {
|
|
869
|
+
return !!sink;
|
|
870
|
+
}).flatten() : _xstream["default"].never();
|
|
871
|
+
|
|
872
|
+
if (name === _this9.stateSourceName) {
|
|
873
|
+
acc[name] = _xstream["default"].merge.apply(_xstream["default"], [_this9.model$[name] || _xstream["default"].never(), _this9.sources[_this9.stateSourceName].stream.filter(function (_) {
|
|
874
|
+
return false;
|
|
875
|
+
})].concat(_toConsumableArray(_this9.children$[name])));
|
|
876
|
+
} else {
|
|
877
|
+
acc[name] = _xstream["default"].merge.apply(_xstream["default"], [_this9.model$[name] || _xstream["default"].never(), subComponentSink$].concat(_toConsumableArray(_this9.children$[name])));
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
return acc;
|
|
881
|
+
}, {});
|
|
882
|
+
this.sinks[this.DOMSourceName] = this.vdom$;
|
|
883
|
+
this.sinks[this.requestSourceName] = _xstream["default"].merge(this.sendResponse$, this.sinks[this.requestSourceName]);
|
|
884
|
+
}
|
|
885
|
+
}, {
|
|
886
|
+
key: "makeOnAction",
|
|
887
|
+
value: function makeOnAction(action$) {
|
|
888
|
+
var _this10 = this;
|
|
889
|
+
|
|
890
|
+
var isStateSink = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
|
|
891
|
+
var rootAction$ = arguments.length > 2 ? arguments[2] : undefined;
|
|
892
|
+
rootAction$ = rootAction$ || action$;
|
|
893
|
+
return function (name, reducer) {
|
|
894
|
+
var filtered$ = action$.filter(function (_ref34) {
|
|
895
|
+
var type = _ref34.type;
|
|
896
|
+
return type == name;
|
|
897
|
+
});
|
|
898
|
+
var returnStream$;
|
|
899
|
+
|
|
900
|
+
if (typeof reducer === 'function') {
|
|
901
|
+
returnStream$ = filtered$.map(function (action) {
|
|
902
|
+
var next = function next(type, data) {
|
|
903
|
+
var _reqId = action._reqId || action.req && action.req.id;
|
|
904
|
+
|
|
905
|
+
var _data = _reqId ? _typeof(data) == 'object' ? _objectSpread(_objectSpread({}, data), {}, {
|
|
906
|
+
_reqId: _reqId,
|
|
907
|
+
_action: name
|
|
908
|
+
}) : {
|
|
909
|
+
data: data,
|
|
910
|
+
_reqId: _reqId,
|
|
911
|
+
_action: name
|
|
912
|
+
} : data; // put the "next" action request at the end of the event loop so the "current" action completes first
|
|
913
|
+
|
|
914
|
+
|
|
915
|
+
setTimeout(function () {
|
|
916
|
+
// push the "next" action request into the action$ stream
|
|
917
|
+
rootAction$.shamefullySendNext({
|
|
918
|
+
type: type,
|
|
919
|
+
data: _data
|
|
920
|
+
});
|
|
921
|
+
}, 10);
|
|
922
|
+
};
|
|
923
|
+
|
|
924
|
+
var data = action.data;
|
|
925
|
+
if (data && data.data && data._reqId) data = data.data;
|
|
926
|
+
|
|
927
|
+
if (isStateSink) {
|
|
928
|
+
return function (state) {
|
|
929
|
+
var _state = _this10.isSubComponent ? _this10.currentState : state;
|
|
930
|
+
|
|
931
|
+
var enhancedState = _this10.addCalculated(_state);
|
|
932
|
+
|
|
933
|
+
var newState = reducer(enhancedState, data, next, action.req);
|
|
934
|
+
if (newState == ABORT) return _state;
|
|
935
|
+
return _this10.cleanupCalculated(newState);
|
|
936
|
+
};
|
|
937
|
+
} else {
|
|
938
|
+
var enhancedState = _this10.addCalculated(_this10.currentState);
|
|
939
|
+
|
|
940
|
+
var reduced = reducer(enhancedState, data, next, action.req);
|
|
941
|
+
|
|
942
|
+
var type = _typeof(reduced);
|
|
943
|
+
|
|
944
|
+
var _reqId = action._reqId || action.req && action.req.id;
|
|
945
|
+
|
|
946
|
+
if (['string', 'number', 'boolean', 'function'].includes(type)) return reduced;
|
|
947
|
+
if (type == 'object') return _objectSpread(_objectSpread({}, reduced), {}, {
|
|
948
|
+
_reqId: _reqId,
|
|
949
|
+
_action: name
|
|
950
|
+
});
|
|
951
|
+
|
|
952
|
+
if (type == 'undefined') {
|
|
953
|
+
console.warn("'undefined' value sent to ".concat(name));
|
|
954
|
+
return reduced;
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
throw new Error("Invalid reducer type for ".concat(name, " ").concat(type));
|
|
958
|
+
}
|
|
959
|
+
}).filter(function (result) {
|
|
960
|
+
return result != ABORT;
|
|
961
|
+
});
|
|
962
|
+
} else if (reducer === undefined || reducer === true) {
|
|
963
|
+
returnStream$ = filtered$.map(function (_ref35) {
|
|
964
|
+
var data = _ref35.data;
|
|
965
|
+
return data;
|
|
966
|
+
});
|
|
967
|
+
} else {
|
|
968
|
+
var value = reducer;
|
|
969
|
+
returnStream$ = filtered$.mapTo(value);
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
return returnStream$;
|
|
973
|
+
};
|
|
974
|
+
}
|
|
975
|
+
}, {
|
|
976
|
+
key: "addCalculated",
|
|
977
|
+
value: function addCalculated(state) {
|
|
978
|
+
if (!this.calculated || _typeof(state) !== 'object') return state;
|
|
979
|
+
if (_typeof(this.calculated) !== 'object') throw new Error("'calculated' parameter must be an object mapping calculated state field named to functions");
|
|
980
|
+
var entries = Object.entries(this.calculated);
|
|
981
|
+
var calculated = entries.reduce(function (acc, _ref36) {
|
|
982
|
+
var _ref37 = _slicedToArray(_ref36, 2),
|
|
983
|
+
field = _ref37[0],
|
|
984
|
+
fn = _ref37[1];
|
|
985
|
+
|
|
986
|
+
if (typeof fn !== 'function') throw new Error("Missing or invalid calculator function for calculated field '".concat(field));
|
|
987
|
+
|
|
988
|
+
try {
|
|
989
|
+
acc[field] = fn(state);
|
|
990
|
+
} catch (e) {
|
|
991
|
+
console.warn("Calculated field '".concat(field, "' threw an error during calculation: ").concat(e.message));
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
return acc;
|
|
995
|
+
}, {});
|
|
996
|
+
return _objectSpread(_objectSpread({}, state), calculated);
|
|
997
|
+
}
|
|
998
|
+
}, {
|
|
999
|
+
key: "cleanupCalculated",
|
|
1000
|
+
value: function cleanupCalculated(state) {
|
|
1001
|
+
var _this11 = this;
|
|
1002
|
+
|
|
1003
|
+
if (this.storeCalculatedInState) return this.addCalculated(state);
|
|
1004
|
+
if (!this.calculated || !state || _typeof(state) !== 'object') return state;
|
|
1005
|
+
var keys = Object.keys(this.calculated);
|
|
1006
|
+
|
|
1007
|
+
var copy = _objectSpread({}, state);
|
|
1008
|
+
|
|
1009
|
+
keys.forEach(function (key) {
|
|
1010
|
+
if (_this11.initialState && typeof _this11.initialState[key] !== 'undefined') {
|
|
1011
|
+
copy[key] = _this11.initialState[key];
|
|
1012
|
+
} else {
|
|
1013
|
+
delete copy[key];
|
|
1014
|
+
}
|
|
1015
|
+
});
|
|
1016
|
+
return copy;
|
|
1017
|
+
}
|
|
1018
|
+
}]);
|
|
1019
|
+
|
|
1020
|
+
return Component;
|
|
1021
|
+
}();
|
|
1022
|
+
/**
|
|
1023
|
+
* factory to create a logging function meant to be used inside of an xstream .compose()
|
|
1024
|
+
*
|
|
1025
|
+
* @param {String} context name of the component or file to be prepended to any messages
|
|
1026
|
+
* @return {Function}
|
|
1027
|
+
*
|
|
1028
|
+
* returned function accepts either a `String` of `Function`
|
|
1029
|
+
* `String` values will be logged to `console` as is
|
|
1030
|
+
* `Function` values will be called with the current `stream` value and the result will be logged to `console`
|
|
1031
|
+
* all output will be prepended with the `context` (ex. "[CONTEXT] My output")
|
|
1032
|
+
* ONLY outputs if the global `DEBUG` variable is set to `true`
|
|
1033
|
+
*/
|
|
1034
|
+
|
|
1035
|
+
|
|
1036
|
+
function makeLog(context) {
|
|
1037
|
+
return function (msg) {
|
|
1038
|
+
var fixedMsg = typeof msg === 'function' ? msg : function (_) {
|
|
1039
|
+
return msg;
|
|
1040
|
+
};
|
|
1041
|
+
return function (stream) {
|
|
1042
|
+
return stream.debug(function (msg) {
|
|
1043
|
+
if (ENVIRONMENT.DEBUG == 'true' || ENVIRONMENT.DEBUG === true) {
|
|
1044
|
+
console.log("[".concat(context, "] ").concat(fixedMsg(msg)));
|
|
1045
|
+
}
|
|
1046
|
+
});
|
|
1047
|
+
};
|
|
1048
|
+
};
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
function getComponents(currentElement, componentNames) {
|
|
1052
|
+
var _currentElement$data;
|
|
1053
|
+
|
|
1054
|
+
var isNestedElement = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
1055
|
+
if (!currentElement) return {};
|
|
1056
|
+
if ((_currentElement$data = currentElement.data) !== null && _currentElement$data !== void 0 && _currentElement$data.componentsProcessed) return {};
|
|
1057
|
+
if (!isNestedElement) currentElement.data.componentsProcessed = true;
|
|
1058
|
+
var sel = currentElement.sel;
|
|
1059
|
+
var isCollection = sel && sel.toLowerCase() === 'collection';
|
|
1060
|
+
var isSwitchable = sel && sel.toLowerCase() === 'switchable';
|
|
1061
|
+
var isComponent = sel && ['collection', 'swtichable'].concat(_toConsumableArray(componentNames)).includes(currentElement.sel);
|
|
1062
|
+
var props = currentElement.data && currentElement.data.props || {};
|
|
1063
|
+
var children = currentElement.children || [];
|
|
1064
|
+
var found = {};
|
|
1065
|
+
|
|
1066
|
+
if (isComponent) {
|
|
1067
|
+
var id = getComponentIdFromElement(currentElement);
|
|
1068
|
+
|
|
1069
|
+
if (isCollection) {
|
|
1070
|
+
if (!props.component) throw new Error("Collection element missing required 'component' property");
|
|
1071
|
+
if (typeof props.component !== 'string') throw new Error("Invalid 'component' property of collection element: found ".concat(_typeof(props.component), " requires string"));
|
|
1072
|
+
if (!componentNames.includes(props.component)) throw new Error("Specified component for collection not found: ".concat(props.component));
|
|
1073
|
+
if (!props.value || !Array.isArray(props.value)) console.warn("No valid array found in the 'value' property of collection ".concat(props.component, ": no collection components will be created"));
|
|
1074
|
+
currentElement.data.isCollection = true;
|
|
1075
|
+
currentElement.data.component = props.component; // currentElement.data.componentArray = props.value
|
|
1076
|
+
} else if (isSwitchable) {
|
|
1077
|
+
if (!props.components) throw new Error("Switchable element missing required 'components' property");
|
|
1078
|
+
if (_typeof(props.components) !== 'object') throw new Error("Invalid 'components' property of switchable element: found ".concat(_typeof(props.components), " requires object mapping names to component factories"));
|
|
1079
|
+
var switchableComponents = Object.values(props.components);
|
|
1080
|
+
if (!switchableComponents.every(function (comp) {
|
|
1081
|
+
return typeof comp === 'function';
|
|
1082
|
+
})) throw new Error("One or more invalid component factories for switchable element is not a valid component factory");
|
|
1083
|
+
if (!props.current || typeof props.current !== 'string') throw new Error("Missing or invalid 'current' property for switchable element: found ".concat(_typeof(props.current), " requires string"));
|
|
1084
|
+
var switchableComponentNames = Object.keys(props.components);
|
|
1085
|
+
if (!switchableComponentNames.includes(props.current)) throw new Error("Component '".concat(props.current, "' not found in switchable element"));
|
|
1086
|
+
currentElement.data.isSwitchable = true;
|
|
1087
|
+
currentElement.data.components = props.components;
|
|
1088
|
+
currentElement.data.currentComponent = props.current;
|
|
1089
|
+
} else {}
|
|
1090
|
+
|
|
1091
|
+
found[id] = currentElement;
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
if (children.length > 0) {
|
|
1095
|
+
children.map(function (child) {
|
|
1096
|
+
return getComponents(child, componentNames, true);
|
|
1097
|
+
}).forEach(function (child) {
|
|
1098
|
+
Object.entries(child).forEach(function (_ref38) {
|
|
1099
|
+
var _ref39 = _slicedToArray(_ref38, 2),
|
|
1100
|
+
id = _ref39[0],
|
|
1101
|
+
el = _ref39[1];
|
|
1102
|
+
|
|
1103
|
+
return found[id] = el;
|
|
1104
|
+
});
|
|
1105
|
+
});
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
return found;
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
function injectComponents(currentElement, components, componentNames) {
|
|
1112
|
+
var _currentElement$data2, _currentElement$data3, _currentElement$data4;
|
|
1113
|
+
|
|
1114
|
+
var isNestedElement = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
|
1115
|
+
if (!currentElement) return;
|
|
1116
|
+
if ((_currentElement$data2 = currentElement.data) !== null && _currentElement$data2 !== void 0 && _currentElement$data2.componentsInjected) return currentElement;
|
|
1117
|
+
if (!isNestedElement && currentElement.data) currentElement.data.componentsInjected = true;
|
|
1118
|
+
var sel = currentElement.sel || 'NO SELECTOR';
|
|
1119
|
+
var isComponent = ['collection', 'swtichable'].concat(_toConsumableArray(componentNames)).includes(sel);
|
|
1120
|
+
var isCollection = currentElement === null || currentElement === void 0 ? void 0 : (_currentElement$data3 = currentElement.data) === null || _currentElement$data3 === void 0 ? void 0 : _currentElement$data3.isCollection;
|
|
1121
|
+
var isSwitchable = currentElement === null || currentElement === void 0 ? void 0 : (_currentElement$data4 = currentElement.data) === null || _currentElement$data4 === void 0 ? void 0 : _currentElement$data4.isSwitchable;
|
|
1122
|
+
var props = currentElement.data && currentElement.data.props || {};
|
|
1123
|
+
var children = currentElement.children || [];
|
|
1124
|
+
|
|
1125
|
+
if (isComponent) {
|
|
1126
|
+
var id = getComponentIdFromElement(currentElement);
|
|
1127
|
+
var _component = components[id];
|
|
1128
|
+
|
|
1129
|
+
if (isCollection) {
|
|
1130
|
+
currentElement.sel = 'div';
|
|
1131
|
+
delete currentElement.elm;
|
|
1132
|
+
currentElement.children = _component;
|
|
1133
|
+
return currentElement;
|
|
1134
|
+
} else {
|
|
1135
|
+
return _component;
|
|
1136
|
+
}
|
|
1137
|
+
} else if (children.length > 0) {
|
|
1138
|
+
currentElement.children = children.map(function (child) {
|
|
1139
|
+
return injectComponents(child, components, componentNames);
|
|
1140
|
+
}).flat();
|
|
1141
|
+
return currentElement;
|
|
1142
|
+
} else {
|
|
1143
|
+
return currentElement;
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
function getComponentIdFromElement(el) {
|
|
1148
|
+
var sel = el.sel;
|
|
1149
|
+
var props = el.data && el.data.props || {};
|
|
1150
|
+
var id = props.id && JSON.stringify(props.id) || JSON.stringify(props);
|
|
1151
|
+
return "".concat(sel, "::").concat(id);
|
|
1152
|
+
}
|