-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
1619 lines (1384 loc) · 144 KB
/
server.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 2);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
// internal function that wraps JSON.stringify
var _createClass = function () { 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
var prettyPrint = function prettyPrint(obj) {
// uses a custom replacer to correctly handle functions
var stringified = JSON.stringify(obj, function (key, value) {
return typeof value === 'function' ? value.toString() : value;
}, 2);
// stringified value is passed through the String constructor to
// correct for the "undefined" case. each line is then indented.
var indented = String(stringified).replace(/\n/g, '\n ');
return '\n>>> ' + indented;
};
// all type-checks should only return boolean values.
var isDefined = function isDefined(value) {
return value !== undefined;
};
var isNull = function isNull(value) {
return value === null;
};
var isArray = function isArray(value) {
return Array.isArray(value);
};
var isFunction = function isFunction(value) {
return typeof value === 'function';
};
var isString = function isString(value) {
return typeof value === 'string';
};
var isNumber = function isNumber(value) {
return typeof value === 'number';
};
var isBoolean = function isBoolean(value) {
return typeof value === 'boolean';
};
var isObject = function isObject(value) {
return !!value && value.constructor === Object;
};
var isNode = function isNode(value) {
return !!(value && value.tagName && value.nodeName && value.ownerDocument && value.removeAttribute);
};
var isRegExp = function isRegExp(value) {
return value instanceof RegExp;
};
var isBrowser = function isBrowser() {
return typeof window !== 'undefined';
};
var deepCopy = function deepCopy(obj) {
// undefined value would otherwise throw an error.
if (obj === undefined) {
return undefined;
}
return JSON.parse(JSON.stringify(obj));
};
// will throw an error containing the message and the culprits if the
// assertion is falsy. the message is expected to contain information
// about the location of the error followed by a meaningful error message.
// (ex. "router.redirect : url is not a string")
var assert = function assert(assertion, message) {
for (var _len = arguments.length, culprits = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
culprits[_key - 2] = arguments[_key];
}
if (!assertion) {
throw new Error('@okwolo.' + message + culprits.map(prettyPrint).join(''));
}
};
// this function will create a queue object which can be used to defer
// the execution of functions.
var makeQueue = function makeQueue() {
var queue = [];
// runs the first function in the queue if it exists. this specifically
// does not call done or remove the function from the queue since there
// is no knowledge about whether or not the function has completed. the
// queue will wait for a done signal before running any other item.
var run = function run() {
var func = queue[0];
if (func) {
func();
}
};
// adds a function to the queue. it will be run instantly if the queue
// is not in a waiting state.
var add = function add(func) {
queue.push(func);
if (queue.length === 1) {
run();
}
};
// removes the first element from the queue and calls run. note that
// it is not possible to pre-call done in order to have multiple
// functions execute immediately.
var done = function done() {
// calling shift on an empty array does nothing.
queue.shift();
run();
};
return { add: add, done: done };
};
// creates a cache with a bounded number of elements. getting values from
// the cache has almost the same performance as using a naked object. setting
// keys will only become slower after the max size is reached. returns
// undefined when key is not in cache.
var cache = function cache() {
var size = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 500;
var map = {};
var order = [];
// set does not check that the key is already in the cache or in the
// delete order. it is assumed the user of the cache will be calling
// get before set and will therefore know if the key was already defined.
var set = function set(key, value) {
map[key] = value;
order.push(key);
if (order.length > size) {
map[order.shift()] = undefined;
}
};
// querying a key will not update its position in the delete order. this
// option was not implemented because it would be a significant performance
// hit with limited benefits for the current use case.
var get = function get(key) {
return map[key];
};
return { set: set, get: get };
};
// simulates the behavior of the classnames npm package. strings are concatenated,
// arrays are spread and objects keys are included if their value is truthy.
var classnames = function classnames() {
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
return args.map(function (arg) {
if (isString(arg)) {
return arg;
} else if (isArray(arg)) {
return classnames.apply(undefined, _toConsumableArray(arg));
} else if (isObject(arg)) {
return classnames(Object.keys(arg).map(function (key) {
return arg[key] && key;
}));
}
}).filter(Boolean).join(' ');
};
// ancestry helper which handles immutability and common logic. this code is
// implemented as a class contrarily to the patterns in the rest of this
// project. the decision was made as an optimization to prevent new functions
// from being created on each instantiation.
var Genealogist = function () {
function Genealogist() {
var list = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
_classCallCheck(this, Genealogist);
this.list = list;
// precalculating the formatted address for use in error assertions.
var formatted = '#';
for (var i = 0; i < this.list.length; ++i) {
formatted += '→ ';
var tag = this.list[i].tag;
// tag's length is capped to reduce clutter.
formatted += tag.substr(0, 16);
if (tag.length > 16) {
formatted += '...';
}
}
this.formatted = formatted;
}
// formats the address with the parent index appended to the end.
// this is useful for errors that happen before an element's tagName
// is parsed and only the parentIndex is known.
_createClass(Genealogist, [{
key: 'f',
value: function f(parentIndex) {
if (parentIndex === undefined) {
return this.formatted;
}
return this.formatted + '\u2192 {{' + parentIndex + '}}';
}
// adding a level returns a new instance of genealogist and does not
// mutate the underlying list.
}, {
key: 'add',
value: function add(tag, key) {
return new Genealogist(this.list.concat([{ tag: tag, key: key }]));
}
// adds a level to the current instance. this method should be used
// with caution since it modifies the list directly. should be used
// in conjunction with copy method to ensure no list made invalid.
}, {
key: 'addUnsafe',
value: function addUnsafe(tag, key) {
this.list.push({ tag: tag, key: key });
return this;
}
// returns a new instance of genealogist with a copy of the underlying list.
}, {
key: 'copy',
value: function copy() {
return new Genealogist(this.list.slice());
}
// returns the list of keys in the ancestry. this value represents the
// element's "address".
}, {
key: 'keys',
value: function keys() {
if (this.list.length < 2) {
return [];
}
// skip the first array element (root vnode has no parent)
var temp = Array(this.list.length - 1);
for (var i = 0; i < temp.length; ++i) {
temp[i] = this.list[i + 1].key;
}
return temp;
}
}]);
return Genealogist;
}();
// finds the longest common of equal items between two input arrays.
// this function can make some optimizations by assuming that both
// arrays are of equal length, that all keys are unique and that all
// keys are found in both arrays. start and end indices of the chain
// in the second argument are returned.
var longestChain = function longestChain(original, successor) {
var count = successor.length;
var half = count / 2;
// current longest chain reference is saved to compare against new
// contenders. the chain's index in the second argument is also kept.
var longest = 0;
var chainStart = 0;
for (var i = 0; i < count; ++i) {
var startInc = original.indexOf(successor[i]);
var maxInc = Math.min(count - startInc, count - i);
// start looking after the current index since it is already
// known to be equal.
var currentLength = 1;
// loop through all following values until either array is fully
// read or the chain of identical values is broken.
for (var inc = 1; inc < maxInc; ++inc) {
if (successor[i + inc] !== original[startInc + inc]) {
break;
}
currentLength += 1;
}
if (currentLength > longest) {
longest = currentLength;
chainStart = i;
}
// quick exit if a chain is found that is longer or equal to half
// the length of the input arrays since it means there can be no
// longer chains.
if (longest >= half) {
break;
}
}
return {
start: chainStart,
end: chainStart + longest - 1
};
};
// shallow diff of two objects which returns an array of keys where the value is
// different. differences include keys who's values have been deleted or added.
// because there is no reliable way to compare function equality, they are always
// considered to be different.
var diff = function diff(original, successor) {
var keys = Object.keys(Object.assign({}, original, successor));
var modifiedKeys = [];
for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
var valueOriginal = original[key];
var valueSuccessor = successor[key];
if (isFunction(valueOriginal) || isFunction(valueSuccessor)) {
modifiedKeys.push(key);
continue;
}
if (valueOriginal !== valueSuccessor) {
modifiedKeys.push(key);
}
}
return modifiedKeys;
};
var makeBus = function makeBus() {
// stores handlers for each event key.
var handlers = {};
// attaches a handler to a specific event key.
var on = function on(type, handler) {
assert(isString(type), 'on : handler type is not a string', type);
assert(isFunction(handler), 'on : handler is not a function', handler);
if (!isDefined(handlers[type])) {
handlers[type] = [];
}
handlers[type].push(handler);
};
var send = function send(type) {
for (var _len3 = arguments.length, args = Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
args[_key3 - 1] = arguments[_key3];
}
assert(isString(type), 'send : event type is not a string', type);
var eventHandlers = handlers[type];
// events that do not match any handlers are ignored silently.
if (!isDefined(eventHandlers)) {
return;
}
for (var i = 0; i < eventHandlers.length; ++i) {
eventHandlers[i].apply(eventHandlers, args);
}
};
return { on: on, send: send };
};
var makeUse = function makeUse(send) {
var names = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return function (blob) {
for (var _len4 = arguments.length, args = Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) {
args[_key4 - 1] = arguments[_key4];
}
// scopes event type to the blob namespace.
if (isString(blob)) {
send.apply(undefined, ['blob.' + blob].concat(args));
return;
}
assert(isObject(blob), 'use : blob is not an object', blob);
var name = blob.name;
if (isDefined(name)) {
assert(isString(name), 'utils.bus : blob name is not a string', name);
// early return if the name has already been seen.
if (isDefined(names[name])) {
return;
}
names[name] = true;
}
// calling send for each blob key.
Object.keys(blob).forEach(function (key) {
send('blob.' + key, blob[key]);
});
};
};
module.exports.is = {
array: isArray,
boolean: isBoolean,
defined: isDefined,
function: isFunction,
node: isNode,
null: isNull,
number: isNumber,
object: isObject,
regExp: isRegExp,
string: isString
};
module.exports.assert = assert;
module.exports.cache = cache;
module.exports.classnames = classnames;
module.exports.deepCopy = deepCopy;
module.exports.diff = diff;
module.exports.Genealogist = Genealogist;
module.exports.isBrowser = isBrowser;
module.exports.longestChain = longestChain;
module.exports.makeBus = makeBus;
module.exports.makeQueue = makeQueue;
module.exports.makeUse = makeUse;
/***/ }),
/* 1 */
/***/ (function(module, exports) {
var g;
// This works in non-strict mode
g = (function() {
return this;
})();
try {
// This works if eval is allowed (see CSP)
g = g || Function("return this")() || (1,eval)("this");
} catch(e) {
// This works if the window reference is available
if(typeof window === "object")
g = window;
}
// g can still be undefined, but nothing to do about it...
// We return undefined, instead of nothing here, so it's
// easier to handle this case. if(!global) { ...}
module.exports = g;
/***/ }),
/* 2 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
var core = __webpack_require__(3);
module.exports = core({
modules: [__webpack_require__(4), __webpack_require__(5), __webpack_require__(6), __webpack_require__(9)],
options: {
kit: 'server',
browser: false
}
});
/***/ }),
/* 3 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
// @listens blob.api
// @listens blob.primary
var _require = __webpack_require__(0),
assert = _require.assert,
is = _require.is,
isBrowser = _require.isBrowser,
makeBus = _require.makeBus,
makeUse = _require.makeUse;
// version not taken from package.json to avoid including the whole file
// in the un-minified bundle. value is checked to be consistent in a
// unit test.
var version = '3.4.5';
module.exports = function () {
var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var _config$modules = config.modules,
modules = _config$modules === undefined ? [] : _config$modules,
_config$options = config.options,
options = _config$options === undefined ? {} : _config$options;
assert(is.array(modules), 'core : passed modules must be an array');
assert(is.object(options), 'core : passed options must be an object');
// both arguments are optional or can be left undefined, except when the
// kit options require the browser, but the window global is not defined.
var okwolo = function okwolo(target, global) {
if (options.browser) {
// global defaults to browser environment's window
if (!is.defined(global)) {
assert(isBrowser(), 'app : must be run in a browser environment');
global = window;
}
}
// primary function will be called when app is called. It is stored
// outside of the app function so that it can be replaced without
// re-creating the app instance.
var primary = function primary() {};
// the api will be added to this variable. It is also returned by the
// enclosing function.
var app = function app() {
return primary.apply(undefined, arguments);
};
var _makeBus = makeBus(),
on = _makeBus.on,
send = _makeBus.send;
var use = makeUse(send, {});
Object.assign(app, { on: on, send: send, use: use });
app.on('blob.api', function (api, override) {
assert(is.object(api), 'on.blob.api : additional api is not an object', api);
Object.keys(api).forEach(function (key) {
if (!override) {
assert(!app[key], 'on.blob.api : cannot add key "' + key + '" because it is already defined');
}
app[key] = api[key];
});
});
app.on('blob.primary', function (_primary) {
assert(is.function(_primary), 'on.blob.primary : primary is not a function', _primary);
primary = _primary;
});
// each module is instantiated on the app.
modules.forEach(function (_module) {
_module({
on: app.on,
send: app.send
}, global);
});
// target is used if it is defined, can be done later if
// it is not convenient to pass the target on app creation.
if (is.defined(target)) {
app.send('blob.target', target);
}
return app;
};
// okwolo attempts to define itself globally and includes information about
// the version number and kit name. note that different kits can coexist,
// but not two versions of the same kit.
if (isBrowser()) {
okwolo.kit = options.kit;
okwolo.version = version;
if (!is.defined(window.okwolo)) {
window.okwolo = okwolo;
}
window.okwolo[options.kit] = okwolo;
}
return okwolo;
};
/***/ }),
/* 4 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
/* WEBPACK VAR INJECTION */(function(global) {
// @fires update [view]
// @fires blob.api [core]
// @fires blob.primary [core]
// @listens state
// @listens sync
// @listens update
// @listens blob.build
// @listens blob.builder
// @listens blob.draw
// @listens blob.target
// @listens blob.update
var _require = __webpack_require__(0),
assert = _require.assert,
is = _require.is,
makeQueue = _require.makeQueue;
module.exports = function (_ref) {
var on = _ref.on,
send = _ref.send;
var target = void 0;
var builder = void 0;
var build = void 0;
var draw = void 0;
var update = void 0;
// stores an object returned by the draw and update functions. Since it is
// also passed as an argument to update, it is convenient to store some
// information about the current application's view in this variable.
var view = void 0;
// a copy of the state must be kept so that the view can be re-computed as
// soon as any part of the rendering pipeline is modified.
var state = void 0;
on('blob.target', function (_target) {
target = _target;
send('update', true);
});
on('blob.builder', function (_builder) {
assert(is.function(_builder), 'on.blob.builder : builder is not a function', _builder);
builder = _builder;
send('update', false);
});
on('blob.draw', function (_draw) {
assert(is.function(_draw), 'on.blob.draw : new draw is not a function', _draw);
draw = _draw;
send('update', true);
});
on('blob.update', function (_update) {
assert(is.function(_update), 'on.blob.update : new target updater is not a function', _update);
update = _update;
send('update', false);
});
on('blob.build', function (_build) {
assert(is.function(_build), 'on.blob.build : new build is not a function', _build);
build = _build;
send('update', false);
});
on('state', function (_state) {
assert(is.defined(_state), 'on.state : new state is not defined', _state);
state = _state;
send('update', false);
});
// tracks whether the app has been drawn. this information is used to
// determining if the update or draw function should be called.
var hasDrawn = false;
// tracks whether there are enough pieces of the rendering pipeline to
// successfully create and render.
var canDraw = false;
// logs an error if the view has not been drawn after the delay since
// the first time it was called. the update event calls this function
// each time it cannot draw.
var delay = 3000;
var waitTimer = null;
var waiting = function waiting() {
if (waitTimer) {
return;
}
waitTimer = setTimeout(function () {
// formatting all blocking variables into an error message.
var values = { builder: builder, state: state, target: target };
Object.keys(values).forEach(function (key) {
values[key] = values[key] ? 'ok' : 'waiting';
});
// assertion error in the timeout will not interrupt execution.
assert(canDraw, 'view : still waiting to draw after ' + Math.round(delay / 1000) + 's', values);
}, delay);
};
// if the view has already been drawn, it is assumed that it can be updated
// instead of redrawing again. the force argument can override this assumption
// and require a redraw.
on('update', function (redraw) {
// canDraw is saved to avoid doing the four checks on every update/draw.
// it is assumed that once all four variables are set the first time, they
// will never again be invalid. this should be enforced by the bus listeners.
if (!canDraw) {
if (is.defined(target) && is.defined(builder) && is.defined(state)) {
canDraw = true;
} else {
return waiting();
}
}
// queue is passed to build to allow it to block component updates until the
// view object in this module is updated. this is necessary because otherwise,
// the sync event could fire with an old version of the view. calling the done
// method on an empty queue does not produce an error, so the builder has no
// obligation to use it.
var queue = makeQueue();
if (redraw || !hasDrawn) {
view = draw(target, build(builder(state), queue));
hasDrawn = true;
queue.done();
return;
}
view = update(target, build(builder(state), queue), [], view);
queue.done();
});
// message which allows for scoped updates. since the successor argument is
// not passed through the build/builder pipeline, its use is loosely
// restricted to the build module (which should have a reference to itself).
on('sync', function (address, successor, identity) {
assert(hasDrawn, 'view.sync : cannot sync component before app has drawn');
view = update(target, successor, address, view, identity);
});
// the only functionality from the dom module that is directly exposed
// is the update event.
send('blob.api', {
update: function update() {
global.console.warn('@okwolo.update : function will be deprecated in next major version (4.x)');
send('update', false);
}
});
// primary functionality will be to replace builder. this is overwritten
// by router modules to more easily associate routes to builders.
send('blob.primary', function (init) {
// calling init for consistency with the router primary which passes
// route params to init;
send('blob.builder', init());
});
};
/* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(1)))
/***/ }),
/* 5 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
// @fires sync [view]
// @fires blob.build [view]
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_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"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
var _require = __webpack_require__(0),
assert = _require.assert,
cache = _require.cache,
classnames = _require.classnames,
Genealogist = _require.Genealogist,
is = _require.is;
module.exports = function (_ref, global) {
var send = _ref.send;
var tagCache = cache(1000);
// will build a vdom structure from the output of the app's builder functions. this
// output must be valid element syntax, or an exception will be thrown.
var build = function build(element, queue, ancestry, parentIndex, updateAncestry, componentIdentity) {
// boolean values will produce no visible output to make it easier to use inline
// logical expressions without worrying about unexpected strings on the page.
if (is.boolean(element)) {
element = '';
}
// null elements will produce no visible output. undefined is intentionally not
// handled since it is often produced as a result of an unexpected builder output
// and it should be made clear that something went wrong.
if (is.null(element)) {
element = '';
}
// in order to simplify type checking, numbers are stringified.
if (is.number(element)) {
element += '';
}
// strings will produce textNodes when rendered to the browser.
if (is.string(element)) {
// the updateAncestry argument is set to truthy when the
// direct parent of the current element is a component. a value
// implies the child is responsible for adding its key to the
// ancestry, even if the resulting element is a text node.
if (updateAncestry) {
ancestry.addUnsafe('textNode', parentIndex);
}
return {
text: element,
componentIdentity: componentIdentity
};
}
// element's address generated once and stored for the error assertions.
var addr = ancestry.f(parentIndex);
// the only remaining element types are formatted as arrays.
assert(is.array(element), 'view.build : vdom object is not a recognized type', addr, element);
// early recursive return when the element is seen to be have the component syntax.
if (is.function(element[0])) {
// leaving the props or children items undefined should not throw an error.
var _element = element,
_element2 = _slicedToArray(_element, 3),
component = _element2[0],
_element2$ = _element2[1],
props = _element2$ === undefined ? {} : _element2$,
_element2$2 = _element2[2],
_children = _element2$2 === undefined ? [] : _element2$2;
assert(is.object(props), 'view.build : component\'s props is not an object', addr, element, props);
assert(is.array(_children), 'view.build : component\'s children is not an array', addr, element, _children);
// component generator is given to update function and used to create
// the initial version of the component.
var gen = void 0;
// the child ancestry will be modified after the component is built
// for the first time by setting the fromComponent argument to true.
var childAncestry = ancestry;
// if this iteration of component is the direct child of another
// component, it should share its ancestry and identity. this is
// caused by the design choice of having components produce no extra
// level in the vdom structure. instead, the element that represents
// a component will have a populated componentIdentity key and be
// otherwise exactly the same as any other element.
if (!updateAncestry) {
childAncestry = ancestry.copy();
// when a component is updated, the update blob in the view.dom
// module compares the provided identity with the vdom element's
// identity. if both values are strictly equal, a component update
// is allowed to happen. the mechanism is used to prevent update
// events from occurring on vdom elements that are not the expected
// component. this can happen if the component's update function
// is called after the component's position is replaced in the view.
componentIdentity = {};
}
var update = function update() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
// build caller's queue is used to make sure the childAncestry
// has been modified and that the vdom stored in the view module
// has been updated. this is necessary because the sync event
// requires the component's complete address as well as a vdom
// tree which actually contains the parsed element.
queue.add(function () {
send('sync', childAncestry.keys(), build(gen.apply(undefined, args), queue, childAncestry, parentIndex, false, componentIdentity), componentIdentity);
queue.done();
});
};
gen = component(Object.assign({}, props, { children: _children }), update);
assert(is.function(gen), 'view.build : component should return a function', addr, gen);
// initial component is built with the fromComponent argument set to true.
return build(gen(), queue, childAncestry, parentIndex, true, componentIdentity);
}
var _element3 = element,
_element4 = _slicedToArray(_element3, 3),
tagType = _element4[0],
_element4$ = _element4[1],
attributes = _element4$ === undefined ? {} : _element4$,
_element4$2 = _element4[2],
childList = _element4$2 === undefined ? [] : _element4$2;
assert(is.string(tagType), 'view.build : tag property is not a string', addr, element, tagType);
assert(is.object(attributes), 'view.build : attributes is not an object', addr, element, attributes);
assert(is.array(childList), 'view.build : children of vdom object is not an array', addr, element, childList);
var match = tagCache.get(tagType);
if (!is.defined(match)) {
// regular expression to capture values from the shorthand element tag syntax.
// it allows each section to be separated by any amount of spaces, but enforces
// the order of the capture groups (tagName #id .className | style)
match = /^ *?(\w+?) *?(?:#([-\w\d]+?))? *?((?:\.[-\w\d]+?)*?)? *?(?:\|\s*?([^\s][^]*?))? *?$/.exec(tagType);
assert(is.array(match), 'view.build : tag property cannot be parsed', addr, tagType);
// first element is not needed since it is the entire matched string. default
// values are not used to avoid adding blank attributes to the nodes.
tagCache.set(tagType, match);
}
var _match = match,
_match2 = _slicedToArray(_match, 5),
tagName = _match2[1],
id = _match2[2],
className = _match2[3],
style = _match2[4];
// priority is given to the id defined in the attributes.
if (is.defined(id) && !is.defined(attributes.id)) {
attributes.id = id.trim();
}
// class names from both the tag and the attributes are used.
if (is.defined(attributes.className) || is.defined(className)) {
attributes.className = classnames(attributes.className, className).replace(/\./g, ' ').replace(/ +/g, ' ').trim();
}
if (is.defined(style)) {
if (!is.defined(attributes.style)) {
attributes.style = style;
} else {
// extra semicolon is added if not present to prevent conflicts.
// styles defined in the attributes are given priority by being
// placed after the ones from the tag.
attributes.style = (style + ';').replace(/;;$/g, ';') + attributes.style;
}
}
// this element's key is either defined in the attributes or it defaults