rumious 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.cjs.js CHANGED
@@ -1,2648 +1 @@
1
- 'use strict';
2
-
3
- function _arrayLikeToArray(r, a) {
4
- (null == a || a > r.length) && (a = r.length);
5
- for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
6
- return n;
7
- }
8
- function _arrayWithHoles(r) {
9
- if (Array.isArray(r)) return r;
10
- }
11
- function _assertThisInitialized(e) {
12
- if (undefined === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
13
- return e;
14
- }
15
- function asyncGeneratorStep(n, t, e, r, o, a, c) {
16
- try {
17
- var i = n[a](c),
18
- u = i.value;
19
- } catch (n) {
20
- return void e(n);
21
- }
22
- i.done ? t(u) : Promise.resolve(u).then(r, o);
23
- }
24
- function _asyncToGenerator(n) {
25
- return function () {
26
- var t = this,
27
- e = arguments;
28
- return new Promise(function (r, o) {
29
- var a = n.apply(t, e);
30
- function _next(n) {
31
- asyncGeneratorStep(a, r, o, _next, _throw, "next", n);
32
- }
33
- function _throw(n) {
34
- asyncGeneratorStep(a, r, o, _next, _throw, "throw", n);
35
- }
36
- _next(undefined);
37
- });
38
- };
39
- }
40
- function _callSuper(t, o, e) {
41
- return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e));
42
- }
43
- function _classCallCheck(a, n) {
44
- if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
45
- }
46
- function _construct(t, e, r) {
47
- if (_isNativeReflectConstruct()) return Reflect.construct.apply(null, arguments);
48
- var o = [null];
49
- o.push.apply(o, e);
50
- var p = new (t.bind.apply(t, o))();
51
- return r && _setPrototypeOf(p, r.prototype), p;
52
- }
53
- function _defineProperties(e, r) {
54
- for (var t = 0; t < r.length; t++) {
55
- var o = r[t];
56
- o.enumerable = o.enumerable || false, o.configurable = true, "value" in o && (o.writable = true), Object.defineProperty(e, _toPropertyKey(o.key), o);
57
- }
58
- }
59
- function _createClass(e, r, t) {
60
- return r && _defineProperties(e.prototype, r), Object.defineProperty(e, "prototype", {
61
- writable: false
62
- }), e;
63
- }
64
- function _createForOfIteratorHelper(r, e) {
65
- var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
66
- if (!t) {
67
- if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e) {
68
- t && (r = t);
69
- var n = 0,
70
- F = function () {};
71
- return {
72
- s: F,
73
- n: function () {
74
- return n >= r.length ? {
75
- done: true
76
- } : {
77
- done: false,
78
- value: r[n++]
79
- };
80
- },
81
- e: function (r) {
82
- throw r;
83
- },
84
- f: F
85
- };
86
- }
87
- throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
88
- }
89
- var o,
90
- a = true,
91
- u = false;
92
- return {
93
- s: function () {
94
- t = t.call(r);
95
- },
96
- n: function () {
97
- var r = t.next();
98
- return a = r.done, r;
99
- },
100
- e: function (r) {
101
- u = true, o = r;
102
- },
103
- f: function () {
104
- try {
105
- a || null == t.return || t.return();
106
- } finally {
107
- if (u) throw o;
108
- }
109
- }
110
- };
111
- }
112
- function _defineProperty(e, r, t) {
113
- return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
114
- value: t,
115
- enumerable: true,
116
- configurable: true,
117
- writable: true
118
- }) : e[r] = t, e;
119
- }
120
- function _getPrototypeOf(t) {
121
- return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) {
122
- return t.__proto__ || Object.getPrototypeOf(t);
123
- }, _getPrototypeOf(t);
124
- }
125
- function _inherits(t, e) {
126
- if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function");
127
- t.prototype = Object.create(e && e.prototype, {
128
- constructor: {
129
- value: t,
130
- writable: true,
131
- configurable: true
132
- }
133
- }), Object.defineProperty(t, "prototype", {
134
- writable: false
135
- }), e && _setPrototypeOf(t, e);
136
- }
137
- function _isNativeFunction(t) {
138
- try {
139
- return -1 !== Function.toString.call(t).indexOf("[native code]");
140
- } catch (n) {
141
- return "function" == typeof t;
142
- }
143
- }
144
- function _isNativeReflectConstruct() {
145
- try {
146
- var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
147
- } catch (t) {}
148
- return (_isNativeReflectConstruct = function () {
149
- return !!t;
150
- })();
151
- }
152
- function _iterableToArrayLimit(r, l) {
153
- var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
154
- if (null != t) {
155
- var e,
156
- n,
157
- i,
158
- u,
159
- a = [],
160
- f = true,
161
- o = false;
162
- try {
163
- if (i = (t = t.call(r)).next, 0 === l) {
164
- if (Object(t) !== t) return;
165
- f = !1;
166
- } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
167
- } catch (r) {
168
- o = true, n = r;
169
- } finally {
170
- try {
171
- if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
172
- } finally {
173
- if (o) throw n;
174
- }
175
- }
176
- return a;
177
- }
178
- }
179
- function _nonIterableRest() {
180
- throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
181
- }
182
- function ownKeys(e, r) {
183
- var t = Object.keys(e);
184
- if (Object.getOwnPropertySymbols) {
185
- var o = Object.getOwnPropertySymbols(e);
186
- r && (o = o.filter(function (r) {
187
- return Object.getOwnPropertyDescriptor(e, r).enumerable;
188
- })), t.push.apply(t, o);
189
- }
190
- return t;
191
- }
192
- function _objectSpread2(e) {
193
- for (var r = 1; r < arguments.length; r++) {
194
- var t = null != arguments[r] ? arguments[r] : {};
195
- r % 2 ? ownKeys(Object(t), true).forEach(function (r) {
196
- _defineProperty(e, r, t[r]);
197
- }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
198
- Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
199
- });
200
- }
201
- return e;
202
- }
203
- function _possibleConstructorReturn(t, e) {
204
- if (e && ("object" == typeof e || "function" == typeof e)) return e;
205
- if (undefined !== e) throw new TypeError("Derived constructors may only return object or undefined");
206
- return _assertThisInitialized(t);
207
- }
208
- function _regeneratorRuntime() {
209
- _regeneratorRuntime = function () {
210
- return e;
211
- };
212
- var t,
213
- e = {},
214
- r = Object.prototype,
215
- n = r.hasOwnProperty,
216
- o = Object.defineProperty || function (t, e, r) {
217
- t[e] = r.value;
218
- },
219
- i = "function" == typeof Symbol ? Symbol : {},
220
- a = i.iterator || "@@iterator",
221
- c = i.asyncIterator || "@@asyncIterator",
222
- u = i.toStringTag || "@@toStringTag";
223
- function define(t, e, r) {
224
- return Object.defineProperty(t, e, {
225
- value: r,
226
- enumerable: true,
227
- configurable: true,
228
- writable: true
229
- }), t[e];
230
- }
231
- try {
232
- define({}, "");
233
- } catch (t) {
234
- define = function (t, e, r) {
235
- return t[e] = r;
236
- };
237
- }
238
- function wrap(t, e, r, n) {
239
- var i = e && e.prototype instanceof Generator ? e : Generator,
240
- a = Object.create(i.prototype),
241
- c = new Context(n || []);
242
- return o(a, "_invoke", {
243
- value: makeInvokeMethod(t, r, c)
244
- }), a;
245
- }
246
- function tryCatch(t, e, r) {
247
- try {
248
- return {
249
- type: "normal",
250
- arg: t.call(e, r)
251
- };
252
- } catch (t) {
253
- return {
254
- type: "throw",
255
- arg: t
256
- };
257
- }
258
- }
259
- e.wrap = wrap;
260
- var h = "suspendedStart",
261
- l = "suspendedYield",
262
- f = "executing",
263
- s = "completed",
264
- y = {};
265
- function Generator() {}
266
- function GeneratorFunction() {}
267
- function GeneratorFunctionPrototype() {}
268
- var p = {};
269
- define(p, a, function () {
270
- return this;
271
- });
272
- var d = Object.getPrototypeOf,
273
- v = d && d(d(values([])));
274
- v && v !== r && n.call(v, a) && (p = v);
275
- var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p);
276
- function defineIteratorMethods(t) {
277
- ["next", "throw", "return"].forEach(function (e) {
278
- define(t, e, function (t) {
279
- return this._invoke(e, t);
280
- });
281
- });
282
- }
283
- function AsyncIterator(t, e) {
284
- function invoke(r, o, i, a) {
285
- var c = tryCatch(t[r], t, o);
286
- if ("throw" !== c.type) {
287
- var u = c.arg,
288
- h = u.value;
289
- return h && "object" == typeof h && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) {
290
- invoke("next", t, i, a);
291
- }, function (t) {
292
- invoke("throw", t, i, a);
293
- }) : e.resolve(h).then(function (t) {
294
- u.value = t, i(u);
295
- }, function (t) {
296
- return invoke("throw", t, i, a);
297
- });
298
- }
299
- a(c.arg);
300
- }
301
- var r;
302
- o(this, "_invoke", {
303
- value: function (t, n) {
304
- function callInvokeWithMethodAndArg() {
305
- return new e(function (e, r) {
306
- invoke(t, n, e, r);
307
- });
308
- }
309
- return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
310
- }
311
- });
312
- }
313
- function makeInvokeMethod(e, r, n) {
314
- var o = h;
315
- return function (i, a) {
316
- if (o === f) throw Error("Generator is already running");
317
- if (o === s) {
318
- if ("throw" === i) throw a;
319
- return {
320
- value: t,
321
- done: true
322
- };
323
- }
324
- for (n.method = i, n.arg = a;;) {
325
- var c = n.delegate;
326
- if (c) {
327
- var u = maybeInvokeDelegate(c, n);
328
- if (u) {
329
- if (u === y) continue;
330
- return u;
331
- }
332
- }
333
- if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) {
334
- if (o === h) throw o = s, n.arg;
335
- n.dispatchException(n.arg);
336
- } else "return" === n.method && n.abrupt("return", n.arg);
337
- o = f;
338
- var p = tryCatch(e, r, n);
339
- if ("normal" === p.type) {
340
- if (o = n.done ? s : l, p.arg === y) continue;
341
- return {
342
- value: p.arg,
343
- done: n.done
344
- };
345
- }
346
- "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg);
347
- }
348
- };
349
- }
350
- function maybeInvokeDelegate(e, r) {
351
- var n = r.method,
352
- o = e.iterator[n];
353
- if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y;
354
- var i = tryCatch(o, e.iterator, r.arg);
355
- if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y;
356
- var a = i.arg;
357
- return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y);
358
- }
359
- function pushTryEntry(t) {
360
- var e = {
361
- tryLoc: t[0]
362
- };
363
- 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e);
364
- }
365
- function resetTryEntry(t) {
366
- var e = t.completion || {};
367
- e.type = "normal", delete e.arg, t.completion = e;
368
- }
369
- function Context(t) {
370
- this.tryEntries = [{
371
- tryLoc: "root"
372
- }], t.forEach(pushTryEntry, this), this.reset(true);
373
- }
374
- function values(e) {
375
- if (e || "" === e) {
376
- var r = e[a];
377
- if (r) return r.call(e);
378
- if ("function" == typeof e.next) return e;
379
- if (!isNaN(e.length)) {
380
- var o = -1,
381
- i = function next() {
382
- for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = false, next;
383
- return next.value = t, next.done = true, next;
384
- };
385
- return i.next = i;
386
- }
387
- }
388
- throw new TypeError(typeof e + " is not iterable");
389
- }
390
- return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", {
391
- value: GeneratorFunctionPrototype,
392
- configurable: true
393
- }), o(GeneratorFunctionPrototype, "constructor", {
394
- value: GeneratorFunction,
395
- configurable: true
396
- }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) {
397
- var e = "function" == typeof t && t.constructor;
398
- return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name));
399
- }, e.mark = function (t) {
400
- return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t;
401
- }, e.awrap = function (t) {
402
- return {
403
- __await: t
404
- };
405
- }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () {
406
- return this;
407
- }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) {
408
- undefined === i && (i = Promise);
409
- var a = new AsyncIterator(wrap(t, r, n, o), i);
410
- return e.isGeneratorFunction(r) ? a : a.next().then(function (t) {
411
- return t.done ? t.value : a.next();
412
- });
413
- }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () {
414
- return this;
415
- }), define(g, "toString", function () {
416
- return "[object Generator]";
417
- }), e.keys = function (t) {
418
- var e = Object(t),
419
- r = [];
420
- for (var n in e) r.push(n);
421
- return r.reverse(), function next() {
422
- for (; r.length;) {
423
- var t = r.pop();
424
- if (t in e) return next.value = t, next.done = false, next;
425
- }
426
- return next.done = true, next;
427
- };
428
- }, e.values = values, Context.prototype = {
429
- constructor: Context,
430
- reset: function (e) {
431
- if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = false, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t);
432
- },
433
- stop: function () {
434
- this.done = true;
435
- var t = this.tryEntries[0].completion;
436
- if ("throw" === t.type) throw t.arg;
437
- return this.rval;
438
- },
439
- dispatchException: function (e) {
440
- if (this.done) throw e;
441
- var r = this;
442
- function handle(n, o) {
443
- return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o;
444
- }
445
- for (var o = this.tryEntries.length - 1; o >= 0; --o) {
446
- var i = this.tryEntries[o],
447
- a = i.completion;
448
- if ("root" === i.tryLoc) return handle("end");
449
- if (i.tryLoc <= this.prev) {
450
- var c = n.call(i, "catchLoc"),
451
- u = n.call(i, "finallyLoc");
452
- if (c && u) {
453
- if (this.prev < i.catchLoc) return handle(i.catchLoc, true);
454
- if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
455
- } else if (c) {
456
- if (this.prev < i.catchLoc) return handle(i.catchLoc, true);
457
- } else {
458
- if (!u) throw Error("try statement without catch or finally");
459
- if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
460
- }
461
- }
462
- }
463
- },
464
- abrupt: function (t, e) {
465
- for (var r = this.tryEntries.length - 1; r >= 0; --r) {
466
- var o = this.tryEntries[r];
467
- if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) {
468
- var i = o;
469
- break;
470
- }
471
- }
472
- i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null);
473
- var a = i ? i.completion : {};
474
- return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a);
475
- },
476
- complete: function (t, e) {
477
- if ("throw" === t.type) throw t.arg;
478
- return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y;
479
- },
480
- finish: function (t) {
481
- for (var e = this.tryEntries.length - 1; e >= 0; --e) {
482
- var r = this.tryEntries[e];
483
- if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y;
484
- }
485
- },
486
- catch: function (t) {
487
- for (var e = this.tryEntries.length - 1; e >= 0; --e) {
488
- var r = this.tryEntries[e];
489
- if (r.tryLoc === t) {
490
- var n = r.completion;
491
- if ("throw" === n.type) {
492
- var o = n.arg;
493
- resetTryEntry(r);
494
- }
495
- return o;
496
- }
497
- }
498
- throw Error("illegal catch attempt");
499
- },
500
- delegateYield: function (e, r, n) {
501
- return this.delegate = {
502
- iterator: values(e),
503
- resultName: r,
504
- nextLoc: n
505
- }, "next" === this.method && (this.arg = t), y;
506
- }
507
- }, e;
508
- }
509
- function _setPrototypeOf(t, e) {
510
- return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) {
511
- return t.__proto__ = e, t;
512
- }, _setPrototypeOf(t, e);
513
- }
514
- function _slicedToArray(r, e) {
515
- return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
516
- }
517
- function _toPrimitive(t, r) {
518
- if ("object" != typeof t || !t) return t;
519
- var e = t[Symbol.toPrimitive];
520
- if (undefined !== e) {
521
- var i = e.call(t, r || "default");
522
- if ("object" != typeof i) return i;
523
- throw new TypeError("@@toPrimitive must return a primitive value.");
524
- }
525
- return ("string" === r ? String : Number)(t);
526
- }
527
- function _toPropertyKey(t) {
528
- var i = _toPrimitive(t, "string");
529
- return "symbol" == typeof i ? i : i + "";
530
- }
531
- function _typeof(o) {
532
- "@babel/helpers - typeof";
533
-
534
- return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
535
- return typeof o;
536
- } : function (o) {
537
- return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
538
- }, _typeof(o);
539
- }
540
- function _unsupportedIterableToArray(r, a) {
541
- if (r) {
542
- if ("string" == typeof r) return _arrayLikeToArray(r, a);
543
- var t = {}.toString.call(r).slice(8, -1);
544
- return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : undefined;
545
- }
546
- }
547
- function _wrapNativeSuper(t) {
548
- var r = "function" == typeof Map ? new Map() : undefined;
549
- return _wrapNativeSuper = function (t) {
550
- if (null === t || !_isNativeFunction(t)) return t;
551
- if ("function" != typeof t) throw new TypeError("Super expression must either be null or a function");
552
- if (undefined !== r) {
553
- if (r.has(t)) return r.get(t);
554
- r.set(t, Wrapper);
555
- }
556
- function Wrapper() {
557
- return _construct(t, arguments, _getPrototypeOf(this).constructor);
558
- }
559
- return Wrapper.prototype = Object.create(t.prototype, {
560
- constructor: {
561
- value: Wrapper,
562
- enumerable: false,
563
- writable: true,
564
- configurable: true
565
- }
566
- }), _setPrototypeOf(Wrapper, t);
567
- }, _wrapNativeSuper(t);
568
- }
569
-
570
- var RumiousRenderContext = /*#__PURE__*/function () {
571
- function RumiousRenderContext(target) {
572
- _classCallCheck(this, RumiousRenderContext);
573
- this.target = target;
574
- }
575
- return _createClass(RumiousRenderContext, [{
576
- key: "find",
577
- value: function find(name) {
578
- return this.target[name];
579
- }
580
- }, {
581
- key: "get",
582
- value: function get(name) {
583
- return this.target[name];
584
- }
585
- }]);
586
- }();
587
-
588
- var RumiousComponent = /*#__PURE__*/function () {
589
- function RumiousComponent() {
590
- _classCallCheck(this, RumiousComponent);
591
- this.element = null;
592
- this.props = {};
593
- this.renderContext = new RumiousRenderContext(this);
594
- this.renderer = null;
595
- this.wrapped = null;
596
- }
597
- return _createClass(RumiousComponent, [{
598
- key: "prepare",
599
- value: function prepare(element, props) {
600
- var wrapped = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
601
- var renderer = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
602
- this.element = element;
603
- this.props = props;
604
- this.renderer = renderer;
605
- this.wrapped = wrapped;
606
- }
607
- }, {
608
- key: "template",
609
- value: function template() {
610
- return {};
611
- }
612
- }, {
613
- key: "requestRender",
614
- value: function requestRender() {
615
- var template = this.template();
616
- var fragment = this.renderer(template, document.createDocumentFragment(), this.renderContext);
617
- this.element.appendChild(fragment);
618
- this.onRender();
619
- }
620
- }, {
621
- key: "requestCleanUp",
622
- value: function () {
623
- var _requestCleanUp = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
624
- var cloned;
625
- return _regeneratorRuntime().wrap(function _callee$(_context) {
626
- while (1) switch (_context.prev = _context.next) {
627
- case 0:
628
- if (this.element) {
629
- cloned = this.element.cloneNode(true);
630
- this.element.replaceWith(cloned);
631
- this.element = cloned;
632
- while (this.element.firstChild) {
633
- this.element.removeChild(this.element.firstChild);
634
- }
635
- }
636
- case 1:
637
- case "end":
638
- return _context.stop();
639
- }
640
- }, _callee, this);
641
- }));
642
- function requestCleanUp() {
643
- return _requestCleanUp.apply(this, arguments);
644
- }
645
- return requestCleanUp;
646
- }()
647
- }, {
648
- key: "onInit",
649
- value: function onInit() {}
650
- }, {
651
- key: "onCreate",
652
- value: function onCreate() {}
653
- }, {
654
- key: "onRender",
655
- value: function onRender() {}
656
- }, {
657
- key: "onUpdate",
658
- value: function onUpdate() {}
659
- }, {
660
- key: "onDestroy",
661
- value: function onDestroy() {}
662
- }]);
663
- }();
664
- function isComponent(component) {
665
- return Object.getPrototypeOf(component) === RumiousComponent;
666
- }
667
-
668
- var RumiousElement = /*#__PURE__*/_createClass(function RumiousElement(type, props) {
669
- var children = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
670
- _classCallCheck(this, RumiousElement);
671
- this.type = type;
672
- this.props = props;
673
- this.children = children;
674
- });
675
- var RumiousElementList = /*#__PURE__*/function () {
676
- function RumiousElementList(list) {
677
- _classCallCheck(this, RumiousElementList);
678
- this.type = "ELEMENT_LIST";
679
- this.children = list;
680
- }
681
- return _createClass(RumiousElementList, [{
682
- key: "forEach",
683
- value: function forEach(callback) {
684
- this.children.forEach(callback);
685
- }
686
- }]);
687
- }();
688
-
689
- function isCamelCase(str) {
690
- var camelCaseRegex = /^[a-zA-Z]+([A-Z][a-z]*)*$/;
691
- return camelCaseRegex.test(str);
692
- }
693
- function isFunction(object) {
694
- return typeof object === 'function';
695
- }
696
-
697
- var RumiousDirective = /*#__PURE__*/function () {
698
- function RumiousDirective(type, name, value) {
699
- _classCallCheck(this, RumiousDirective);
700
- this.type = type;
701
- this.name = name;
702
- this.value = value;
703
- }
704
- return _createClass(RumiousDirective, [{
705
- key: "init",
706
- value: function init() {
707
- console.warn("Directive haven't handler !");
708
- }
709
- }]);
710
- }();
711
- var RumiousEventBindingDirective = /*#__PURE__*/function (_RumiousDirective) {
712
- function RumiousEventBindingDirective() {
713
- _classCallCheck(this, RumiousEventBindingDirective);
714
- return _callSuper(this, RumiousEventBindingDirective, arguments);
715
- }
716
- _inherits(RumiousEventBindingDirective, _RumiousDirective);
717
- return _createClass(RumiousEventBindingDirective, [{
718
- key: "init",
719
- value: function () {
720
- var _init = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(dom, renderContext) {
721
- var _this = this;
722
- var fn, getHandler;
723
- return _regeneratorRuntime().wrap(function _callee$(_context) {
724
- while (1) switch (_context.prev = _context.next) {
725
- case 0:
726
- fn = null;
727
- getHandler = function getHandler(value) {
728
- if (!renderContext.find(value)) {
729
- console.warn("Missing event handler for event: ".concat(_this.name));
730
- return null;
731
- }
732
- var handler = renderContext.get(value);
733
- if (!isFunction(handler)) {
734
- throw "".concat(value, " is not a function!");
735
- }
736
- return handler.bind(renderContext.target);
737
- };
738
- if (!(this.value.type === "string")) {
739
- _context.next = 6;
740
- break;
741
- }
742
- fn = getHandler(this.value.value);
743
- _context.next = 13;
744
- break;
745
- case 6:
746
- if (!(this.value.type === "expression")) {
747
- _context.next = 12;
748
- break;
749
- }
750
- fn = this.value.value;
751
- if (isFunction(fn)) {
752
- _context.next = 10;
753
- break;
754
- }
755
- throw "Event handler requires a function!";
756
- case 10:
757
- _context.next = 13;
758
- break;
759
- case 12:
760
- if (this.value.type === "dynamic_value") {
761
- fn = getHandler(this.value.value.objectName);
762
- }
763
- case 13:
764
- if (fn) {
765
- dom.addEventListener(this.name, fn);
766
- }
767
- case 14:
768
- case "end":
769
- return _context.stop();
770
- }
771
- }, _callee, this);
772
- }));
773
- function init(_x, _x2) {
774
- return _init.apply(this, arguments);
775
- }
776
- return init;
777
- }()
778
- }]);
779
- }(RumiousDirective);
780
- var RumiousPropsBindingDirective = /*#__PURE__*/function (_RumiousDirective2) {
781
- function RumiousPropsBindingDirective() {
782
- _classCallCheck(this, RumiousPropsBindingDirective);
783
- return _callSuper(this, RumiousPropsBindingDirective, arguments);
784
- }
785
- _inherits(RumiousPropsBindingDirective, _RumiousDirective2);
786
- return _createClass(RumiousPropsBindingDirective, [{
787
- key: "init",
788
- value: function () {
789
- var _init2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(dom, renderContext) {
790
- var _this2 = this;
791
- var _this$value$evaluator, _this$value, currentStateValue, currentState;
792
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
793
- while (1) switch (_context2.prev = _context2.next) {
794
- case 0:
795
- if (!(this.value.type === "expression")) {
796
- _context2.next = 4;
797
- break;
798
- }
799
- _context2.next = 13;
800
- break;
801
- case 4:
802
- if (!(this.value.type === "dynamic_value")) {
803
- _context2.next = 13;
804
- break;
805
- }
806
- currentStateValue = (_this$value$evaluator = (_this$value = this.value).evaluator) === null || _this$value$evaluator === undefined ? undefined : _this$value$evaluator.call(_this$value, renderContext.target);
807
- currentState = renderContext.find(this.value.value.objectName);
808
- if (currentState) {
809
- _context2.next = 11;
810
- break;
811
- }
812
- throw "Invalid props value: Directive bind: require a RumiousState Object !";
813
- case 11:
814
- currentState.reactor.addBinding(function () {
815
- var _this2$value$evaluato, _this2$value;
816
- var currentStateValue = (_this2$value$evaluato = (_this2$value = _this2.value).evaluator) === null || _this2$value$evaluato === undefined ? undefined : _this2$value$evaluato.call(_this2$value, renderContext.target);
817
- _this2.bindAttr(dom, currentStateValue);
818
- });
819
- case 12:
820
- this.bindAttr(dom, currentStateValue);
821
- case 13:
822
- case "end":
823
- return _context2.stop();
824
- }
825
- }, _callee2, this);
826
- }));
827
- function init(_x3, _x4) {
828
- return _init2.apply(this, arguments);
829
- }
830
- return init;
831
- }()
832
- }, {
833
- key: "normalizeValue",
834
- value: function normalizeValue(value) {
835
- if (_typeof(value) === "object") {
836
- var _value$toString, _value$toString2;
837
- return (_value$toString = (_value$toString2 = value.toString) === null || _value$toString2 === undefined ? undefined : _value$toString2.call(value)) !== null && _value$toString !== undefined ? _value$toString : "";
838
- }
839
- return value;
840
- }
841
- }, {
842
- key: "bindAttr",
843
- value: function () {
844
- var _bindAttr = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(dom, value) {
845
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
846
- while (1) switch (_context3.prev = _context3.next) {
847
- case 0:
848
- _context3.t0 = this.name;
849
- _context3.next = _context3.t0 === "html" ? 3 : 5;
850
- break;
851
- case 3:
852
- dom.innerHTML = this.normalizeValue(value);
853
- return _context3.abrupt("break", 5);
854
- case 5:
855
- case "end":
856
- return _context3.stop();
857
- }
858
- }, _callee3, this);
859
- }));
860
- function bindAttr(_x5, _x6) {
861
- return _bindAttr.apply(this, arguments);
862
- }
863
- return bindAttr;
864
- }()
865
- }]);
866
- }(RumiousDirective);
867
- var directives = {
868
- on: function on(event, value) {
869
- return new RumiousEventBindingDirective("on", event, value);
870
- },
871
- bind: function bind(attr, value) {
872
- return new RumiousPropsBindingDirective("bind", attr, value);
873
- }
874
- };
875
- function registerDirective(type, name, value) {
876
- var _directives$type;
877
- return (_directives$type = directives[type]) === null || _directives$type === undefined ? undefined : _directives$type.call(directives, name, value);
878
- }
879
-
880
- function createDirective(type, name, value) {
881
- return registerDirective(type, name, value);
882
- }
883
- function createElement(type, props) {
884
- for (var _len = arguments.length, children = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
885
- children[_key - 2] = arguments[_key];
886
- }
887
- if (isComponent(type)) {
888
- return createComponent(type, props, children);
889
- }
890
- if (type === createFragment) {
891
- return createFragment.apply(undefined, children);
892
- }
893
- return new RumiousElement(type, props || {}, normalizeChildren(children));
894
- }
895
- function createTextElement(text) {
896
- return new RumiousElement("TEXT_ELEMENT", {
897
- nodeValue: text
898
- }, []);
899
- }
900
- function createComponent(type, props, children) {
901
- return new RumiousElement("COMPONENT", _objectSpread2({
902
- component: type
903
- }, props), new RumiousElementList(normalizeChildren(children)));
904
- }
905
- function createFragment() {
906
- for (var _len2 = arguments.length, children = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
907
- children[_key2] = arguments[_key2];
908
- }
909
- return new RumiousElement("FRAGMENT", {}, normalizeChildren(children));
910
- }
911
- function normalizeChildren(children) {
912
- return children.map(function (child) {
913
- return _typeof(child) === "object" ? child : createTextElement(child);
914
- });
915
- }
916
- window.RUMIOUS_JSX_SUPPORT = {
917
- createElement: createElement,
918
- createFragment: createFragment,
919
- createDirective: createDirective
920
- };
921
-
922
- var RumiousComponentElement = /*#__PURE__*/function (_HTMLElement) {
923
- function RumiousComponentElement() {
924
- var _this;
925
- _classCallCheck(this, RumiousComponentElement);
926
- _this = _callSuper(this, RumiousComponentElement);
927
- _this.instance = null;
928
- return _this;
929
- }
930
- _inherits(RumiousComponentElement, _HTMLElement);
931
- return _createClass(RumiousComponentElement, [{
932
- key: "init",
933
- value: function init(componentConstructor, props) {
934
- var wrapped = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
935
- var renderer = arguments.length > 3 ? arguments[3] : undefined;
936
- this.instance = new componentConstructor();
937
- this.instance.prepare(this, props, wrapped, renderer);
938
- this.instance.onInit();
939
- }
940
- }, {
941
- key: "connectedCallback",
942
- value: function connectedCallback() {
943
- this.instance.onCreate();
944
- this.instance.requestRender();
945
- }
946
- }, {
947
- key: "disconnectCallback",
948
- value: function disconnectCallback() {
949
- this.instance.onDestroy();
950
- this.instance.requestCleanUp();
951
- }
952
- }]);
953
- }(/*#__PURE__*/_wrapNativeSuper(HTMLElement));
954
- function createComponentElement() {
955
- return document.createElement("a-component");
956
- }
957
- window.customElements.define("a-component", RumiousComponentElement);
958
-
959
- function renderComponent(componentConstructor, props, wrapped, renderer) {
960
- var componentElement = createComponentElement();
961
- componentElement.init(componentConstructor, props, wrapped, renderer);
962
- return componentElement;
963
- }
964
-
965
- function handleComponentElement(element, container, render) {
966
- var dom = renderComponent(element.props.component, element.props, element.children, render);
967
- container.appendChild(dom);
968
- return container;
969
- }
970
- function handleFragmentOrElementList(element, container, renderContext) {
971
- element.children.forEach(function (child) {
972
- return render(child, container, renderContext);
973
- });
974
- return container;
975
- }
976
- function handleTextElement(element) {
977
- return document.createTextNode(element.props.nodeValue);
978
- }
979
- function handleRegularElement(element, renderContext) {
980
- var dom = document.createElement(element.type);
981
- Object.entries(element.props || {}).forEach(function (_ref) {
982
- var _ref2 = _slicedToArray(_ref, 2),
983
- name = _ref2[0],
984
- propValue = _ref2[1];
985
- if (name.startsWith("on") && isCamelCase(name)) {
986
- dom.addEventListener(name.substring(2).toLowerCase(), propValue);
987
- } else {
988
- setElementProps(dom, name, propValue, renderContext);
989
- }
990
- });
991
- return dom;
992
- }
993
- function handleDirective(dom, directive, renderContext) {
994
- directive.init(dom, renderContext);
995
- }
996
- function setElementProps(dom, name, propValue, renderContext) {
997
- if (dom.nodeType === Node.TEXT_NODE) {
998
- dom.nodeValue = propValue;
999
- } else if (propValue instanceof RumiousDirective) {
1000
- handleDirective(dom, propValue, renderContext);
1001
- } else {
1002
- dom.setAttribute(name, propValue);
1003
- }
1004
- }
1005
- function render(element, container) {
1006
- var renderContext = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
1007
- if (!element) return container;
1008
- var dom;
1009
- if (element.type === "COMPONENT") {
1010
- return handleComponentElement(element, container, render);
1011
- }
1012
- if (element.type === "FRAGMENT" || element.type === "ELEMENT_LIST") {
1013
- return handleFragmentOrElementList(element, container, renderContext);
1014
- }
1015
- if (element.type === "TEXT_ELEMENT") {
1016
- dom = handleTextElement(element);
1017
- } else {
1018
- dom = handleRegularElement(element, renderContext);
1019
- }
1020
- element.children.forEach(function (child) {
1021
- return render(child, dom, renderContext);
1022
- });
1023
- container.appendChild(dom);
1024
- return container;
1025
- }
1026
-
1027
- var RumiousApp = /*#__PURE__*/function () {
1028
- function RumiousApp() {
1029
- var root = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : document.createElement("span");
1030
- var configs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1031
- _classCallCheck(this, RumiousApp);
1032
- this.root = root;
1033
- this.configs = configs;
1034
- this.renderContext = new RumiousRenderContext(this);
1035
- }
1036
- return _createClass(RumiousApp, [{
1037
- key: "render",
1038
- value: function render$1(element) {
1039
- render(element, this.root, this.renderContext);
1040
- }
1041
- }]);
1042
- }();
1043
-
1044
- var RumiousReactor = /*#__PURE__*/function () {
1045
- function RumiousReactor() {
1046
- var target = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
1047
- var bindings = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
1048
- _classCallCheck(this, RumiousReactor);
1049
- this.target = target;
1050
- this.bindings = bindings;
1051
- }
1052
- return _createClass(RumiousReactor, [{
1053
- key: "react",
1054
- value: function react() {
1055
- this.bindings.forEach(function (callback) {
1056
- return callback();
1057
- });
1058
- }
1059
- }, {
1060
- key: "addBinding",
1061
- value: function addBinding(fn) {
1062
- this.bindings.push(fn);
1063
- }
1064
- }]);
1065
- }();
1066
-
1067
- var RumiousReducer = /*#__PURE__*/function () {
1068
- function RumiousReducer(state) {
1069
- var path = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
1070
- var fn = arguments.length > 2 ? arguments[2] : undefined;
1071
- _classCallCheck(this, RumiousReducer);
1072
- this.state = state;
1073
- this.path = path;
1074
- this.fn = fn;
1075
- }
1076
- return _createClass(RumiousReducer, [{
1077
- key: "trigger",
1078
- value: function trigger() {
1079
- var value = this.fn.apply(this, arguments);
1080
- this.state.set(this.path, value);
1081
- }
1082
- }]);
1083
- }();
1084
-
1085
- var Operation = {
1086
- Remove: 'remove',
1087
- Replace: 'replace',
1088
- Add: 'add'
1089
- };
1090
-
1091
- // Don't use `Symbol()` just for 3rd party access the draft
1092
- var PROXY_DRAFT = Symbol["for"]('__MUTATIVE_PROXY_DRAFT__');
1093
- var RAW_RETURN_SYMBOL = Symbol('__MUTATIVE_RAW_RETURN_SYMBOL__');
1094
- var iteratorSymbol = Symbol.iterator;
1095
- var dataTypes = {
1096
- mutable: 'mutable',
1097
- immutable: 'immutable'
1098
- };
1099
- var internal = {};
1100
- function has(target, key) {
1101
- return target instanceof Map ? target.has(key) : Object.prototype.hasOwnProperty.call(target, key);
1102
- }
1103
- function getDescriptor(target, key) {
1104
- if (key in target) {
1105
- var prototype = Reflect.getPrototypeOf(target);
1106
- while (prototype) {
1107
- var descriptor = Reflect.getOwnPropertyDescriptor(prototype, key);
1108
- if (descriptor) return descriptor;
1109
- prototype = Reflect.getPrototypeOf(prototype);
1110
- }
1111
- }
1112
- return;
1113
- }
1114
- function isBaseSetInstance(obj) {
1115
- return Object.getPrototypeOf(obj) === Set.prototype;
1116
- }
1117
- function isBaseMapInstance(obj) {
1118
- return Object.getPrototypeOf(obj) === Map.prototype;
1119
- }
1120
- function latest(proxyDraft) {
1121
- var _a;
1122
- return (_a = proxyDraft.copy) !== null && _a !== undefined ? _a : proxyDraft.original;
1123
- }
1124
- /**
1125
- * Check if the value is a draft
1126
- */
1127
- function isDraft(target) {
1128
- return !!getProxyDraft(target);
1129
- }
1130
- function getProxyDraft(value) {
1131
- if (_typeof(value) !== 'object') return null;
1132
- return value === null || value === undefined ? undefined : value[PROXY_DRAFT];
1133
- }
1134
- function getValue(value) {
1135
- var _a;
1136
- var proxyDraft = getProxyDraft(value);
1137
- return proxyDraft ? (_a = proxyDraft.copy) !== null && _a !== undefined ? _a : proxyDraft.original : value;
1138
- }
1139
- /**
1140
- * Check if a value is draftable
1141
- */
1142
- function isDraftable(value, options) {
1143
- if (!value || _typeof(value) !== 'object') return false;
1144
- var markResult;
1145
- return Object.getPrototypeOf(value) === Object.prototype || Array.isArray(value) || value instanceof Map || value instanceof Set || !!(options === null || options === undefined ? undefined : options.mark) && ((markResult = options.mark(value, dataTypes)) === dataTypes.immutable || typeof markResult === 'function');
1146
- }
1147
- function getPath(target) {
1148
- var path = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
1149
- if (Object.hasOwnProperty.call(target, 'key')) {
1150
- // check if the parent is a draft and the original value is not equal to the current value
1151
- var parentCopy = target.parent.copy;
1152
- var proxyDraft = getProxyDraft(get(parentCopy, target.key));
1153
- if (proxyDraft !== null && (proxyDraft === null || proxyDraft === undefined ? undefined : proxyDraft.original) !== target.original) {
1154
- return null;
1155
- }
1156
- var isSet = target.parent.type === 3 /* DraftType.Set */;
1157
- var key = isSet ? Array.from(target.parent.setMap.keys()).indexOf(target.key) : target.key;
1158
- // check if the key is still in the next state parent
1159
- if (!(isSet && parentCopy.size > key || has(parentCopy, key))) return null;
1160
- path.push(key);
1161
- }
1162
- if (target.parent) {
1163
- return getPath(target.parent, path);
1164
- }
1165
- // `target` is root draft.
1166
- path.reverse();
1167
- try {
1168
- // check if the path is valid
1169
- resolvePath(target.copy, path);
1170
- } catch (e) {
1171
- return null;
1172
- }
1173
- return path;
1174
- }
1175
- function getType(target) {
1176
- if (Array.isArray(target)) return 1 /* DraftType.Array */;
1177
- if (target instanceof Map) return 2 /* DraftType.Map */;
1178
- if (target instanceof Set) return 3 /* DraftType.Set */;
1179
- return 0 /* DraftType.Object */;
1180
- }
1181
- function get(target, key) {
1182
- return getType(target) === 2 /* DraftType.Map */ ? target.get(key) : target[key];
1183
- }
1184
- function set(target, key, value) {
1185
- var type = getType(target);
1186
- if (type === 2 /* DraftType.Map */) {
1187
- target.set(key, value);
1188
- } else {
1189
- target[key] = value;
1190
- }
1191
- }
1192
- function peek(target, key) {
1193
- var state = getProxyDraft(target);
1194
- var source = state ? latest(state) : target;
1195
- return source[key];
1196
- }
1197
- function isEqual(x, y) {
1198
- if (x === y) {
1199
- return x !== 0 || 1 / x === 1 / y;
1200
- } else {
1201
- return x !== x && y !== y;
1202
- }
1203
- }
1204
- function revokeProxy(proxyDraft) {
1205
- if (!proxyDraft) return;
1206
- while (proxyDraft.finalities.revoke.length > 0) {
1207
- var revoke = proxyDraft.finalities.revoke.pop();
1208
- revoke();
1209
- }
1210
- }
1211
- // handle JSON Pointer path with spec https://www.rfc-editor.org/rfc/rfc6901
1212
- function escapePath(path, pathAsArray) {
1213
- return pathAsArray ? path : [''].concat(path).map(function (_item) {
1214
- var item = "".concat(_item);
1215
- if (item.indexOf('/') === -1 && item.indexOf('~') === -1) return item;
1216
- return item.replace(/~/g, '~0').replace(/\//g, '~1');
1217
- }).join('/');
1218
- }
1219
- function resolvePath(base, path) {
1220
- for (var index = 0; index < path.length - 1; index += 1) {
1221
- var key = path[index];
1222
- // use `index` in Set draft
1223
- base = get(getType(base) === 3 /* DraftType.Set */ ? Array.from(base) : base, key);
1224
- if (_typeof(base) !== 'object') {
1225
- throw new Error("Cannot resolve patch at '".concat(path.join('/'), "'."));
1226
- }
1227
- }
1228
- return base;
1229
- }
1230
- function strictCopy(target) {
1231
- var copy = Object.create(Object.getPrototypeOf(target));
1232
- Reflect.ownKeys(target).forEach(function (key) {
1233
- var desc = Reflect.getOwnPropertyDescriptor(target, key);
1234
- if (desc.enumerable && desc.configurable && desc.writable) {
1235
- copy[key] = target[key];
1236
- return;
1237
- }
1238
- // for freeze
1239
- if (!desc.writable) {
1240
- desc.writable = true;
1241
- desc.configurable = true;
1242
- }
1243
- if (desc.get || desc.set) desc = {
1244
- configurable: true,
1245
- writable: true,
1246
- enumerable: desc.enumerable,
1247
- value: target[key]
1248
- };
1249
- Reflect.defineProperty(copy, key, desc);
1250
- });
1251
- return copy;
1252
- }
1253
- var propIsEnum = Object.prototype.propertyIsEnumerable;
1254
- function shallowCopy(original, options) {
1255
- var markResult;
1256
- if (Array.isArray(original)) {
1257
- return Array.prototype.concat.call(original);
1258
- } else if (original instanceof Set) {
1259
- if (!isBaseSetInstance(original)) {
1260
- var SubClass = Object.getPrototypeOf(original).constructor;
1261
- return new SubClass(original.values());
1262
- }
1263
- return Set.prototype.difference ? Set.prototype.difference.call(original, new Set()) : new Set(original.values());
1264
- } else if (original instanceof Map) {
1265
- if (!isBaseMapInstance(original)) {
1266
- var _SubClass = Object.getPrototypeOf(original).constructor;
1267
- return new _SubClass(original);
1268
- }
1269
- return new Map(original);
1270
- } else if ((options === null || options === undefined ? undefined : options.mark) && (markResult = options.mark(original, dataTypes), markResult !== undefined) && markResult !== dataTypes.mutable) {
1271
- if (markResult === dataTypes.immutable) {
1272
- return strictCopy(original);
1273
- } else if (typeof markResult === 'function') {
1274
- if (options.enablePatches || options.enableAutoFreeze) {
1275
- throw new Error("You can't use mark and patches or auto freeze together.");
1276
- }
1277
- return markResult();
1278
- }
1279
- throw new Error("Unsupported mark result: ".concat(markResult));
1280
- } else if (_typeof(original) === 'object' && Object.getPrototypeOf(original) === Object.prototype) {
1281
- // For best performance with shallow copies,
1282
- // don't use `Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));` by default.
1283
- var copy = {};
1284
- Object.keys(original).forEach(function (key) {
1285
- copy[key] = original[key];
1286
- });
1287
- Object.getOwnPropertySymbols(original).forEach(function (key) {
1288
- if (propIsEnum.call(original, key)) {
1289
- copy[key] = original[key];
1290
- }
1291
- });
1292
- return copy;
1293
- } else {
1294
- throw new Error("Please check mark() to ensure that it is a stable marker draftable function.");
1295
- }
1296
- }
1297
- function ensureShallowCopy(target) {
1298
- if (target.copy) return;
1299
- target.copy = shallowCopy(target.original, target.options);
1300
- }
1301
- function deepClone(target) {
1302
- if (!isDraftable(target)) return getValue(target);
1303
- if (Array.isArray(target)) return target.map(deepClone);
1304
- if (target instanceof Map) {
1305
- var iterable = Array.from(target.entries()).map(function (_ref) {
1306
- var _ref2 = _slicedToArray(_ref, 2),
1307
- k = _ref2[0],
1308
- v = _ref2[1];
1309
- return [k, deepClone(v)];
1310
- });
1311
- if (!isBaseMapInstance(target)) {
1312
- var SubClass = Object.getPrototypeOf(target).constructor;
1313
- return new SubClass(iterable);
1314
- }
1315
- return new Map(iterable);
1316
- }
1317
- if (target instanceof Set) {
1318
- var _iterable = Array.from(target).map(deepClone);
1319
- if (!isBaseSetInstance(target)) {
1320
- var _SubClass2 = Object.getPrototypeOf(target).constructor;
1321
- return new _SubClass2(_iterable);
1322
- }
1323
- return new Set(_iterable);
1324
- }
1325
- var copy = Object.create(Object.getPrototypeOf(target));
1326
- for (var key in target) copy[key] = deepClone(target[key]);
1327
- return copy;
1328
- }
1329
- function cloneIfNeeded(target) {
1330
- return isDraft(target) ? deepClone(target) : target;
1331
- }
1332
- function markChanged(proxyDraft) {
1333
- var _a;
1334
- proxyDraft.assignedMap = (_a = proxyDraft.assignedMap) !== null && _a !== undefined ? _a : new Map();
1335
- if (!proxyDraft.operated) {
1336
- proxyDraft.operated = true;
1337
- if (proxyDraft.parent) {
1338
- markChanged(proxyDraft.parent);
1339
- }
1340
- }
1341
- }
1342
- function throwFrozenError() {
1343
- throw new Error('Cannot modify frozen object');
1344
- }
1345
- function deepFreeze(target, subKey, updatedValues, stack, keys) {
1346
- {
1347
- updatedValues = updatedValues !== null && updatedValues !== undefined ? updatedValues : new WeakMap();
1348
- stack = stack !== null && stack !== undefined ? stack : [];
1349
- keys = keys !== null && keys !== undefined ? keys : [];
1350
- var value = updatedValues.has(target) ? updatedValues.get(target) : target;
1351
- if (stack.length > 0) {
1352
- var index = stack.indexOf(value);
1353
- if (value && _typeof(value) === 'object' && index !== -1) {
1354
- if (stack[0] === value) {
1355
- throw new Error("Forbids circular reference");
1356
- }
1357
- throw new Error("Forbids circular reference: ~/".concat(keys.slice(0, index).map(function (key, index) {
1358
- if (_typeof(key) === 'symbol') return "[".concat(key.toString(), "]");
1359
- var parent = stack[index];
1360
- if (_typeof(key) === 'object' && (parent instanceof Map || parent instanceof Set)) return Array.from(parent.keys()).indexOf(key);
1361
- return key;
1362
- }).join('/')));
1363
- }
1364
- stack.push(value);
1365
- keys.push(subKey);
1366
- } else {
1367
- stack.push(value);
1368
- }
1369
- }
1370
- if (Object.isFrozen(target) || isDraft(target)) {
1371
- {
1372
- stack.pop();
1373
- keys.pop();
1374
- }
1375
- return;
1376
- }
1377
- var type = getType(target);
1378
- switch (type) {
1379
- case 2 /* DraftType.Map */:
1380
- var _iterator = _createForOfIteratorHelper(target),
1381
- _step;
1382
- try {
1383
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
1384
- var _step$value = _slicedToArray(_step.value, 2),
1385
- key = _step$value[0],
1386
- _value2 = _step$value[1];
1387
- deepFreeze(key, key, updatedValues, stack, keys);
1388
- deepFreeze(_value2, key, updatedValues, stack, keys);
1389
- }
1390
- } catch (err) {
1391
- _iterator.e(err);
1392
- } finally {
1393
- _iterator.f();
1394
- }
1395
- target.set = target.clear = target["delete"] = throwFrozenError;
1396
- break;
1397
- case 3 /* DraftType.Set */:
1398
- var _iterator2 = _createForOfIteratorHelper(target),
1399
- _step2;
1400
- try {
1401
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
1402
- var _value3 = _step2.value;
1403
- deepFreeze(_value3, _value3, updatedValues, stack, keys);
1404
- }
1405
- } catch (err) {
1406
- _iterator2.e(err);
1407
- } finally {
1408
- _iterator2.f();
1409
- }
1410
- target.add = target.clear = target["delete"] = throwFrozenError;
1411
- break;
1412
- case 1 /* DraftType.Array */:
1413
- Object.freeze(target);
1414
- var _index = 0;
1415
- var _iterator3 = _createForOfIteratorHelper(target),
1416
- _step3;
1417
- try {
1418
- for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
1419
- var _value4 = _step3.value;
1420
- deepFreeze(_value4, _index, updatedValues, stack, keys);
1421
- _index += 1;
1422
- }
1423
- } catch (err) {
1424
- _iterator3.e(err);
1425
- } finally {
1426
- _iterator3.f();
1427
- }
1428
- break;
1429
- default:
1430
- Object.freeze(target);
1431
- // ignore non-enumerable or symbol properties
1432
- Object.keys(target).forEach(function (name) {
1433
- var value = target[name];
1434
- deepFreeze(value, name, updatedValues, stack, keys);
1435
- });
1436
- }
1437
- {
1438
- stack.pop();
1439
- keys.pop();
1440
- }
1441
- }
1442
- function forEach(target, iter) {
1443
- var type = getType(target);
1444
- if (type === 0 /* DraftType.Object */) {
1445
- Reflect.ownKeys(target).forEach(function (key) {
1446
- iter(key, target[key], target);
1447
- });
1448
- } else if (type === 1 /* DraftType.Array */) {
1449
- var index = 0;
1450
- var _iterator4 = _createForOfIteratorHelper(target),
1451
- _step4;
1452
- try {
1453
- for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
1454
- var entry = _step4.value;
1455
- iter(index, entry, target);
1456
- index += 1;
1457
- }
1458
- } catch (err) {
1459
- _iterator4.e(err);
1460
- } finally {
1461
- _iterator4.f();
1462
- }
1463
- } else {
1464
- target.forEach(function (entry, index) {
1465
- return iter(index, entry, target);
1466
- });
1467
- }
1468
- }
1469
- function handleValue(target, handledSet, options) {
1470
- if (isDraft(target) || !isDraftable(target, options) || handledSet.has(target) || Object.isFrozen(target)) return;
1471
- var isSet = target instanceof Set;
1472
- var setMap = isSet ? new Map() : undefined;
1473
- handledSet.add(target);
1474
- forEach(target, function (key, value) {
1475
- var _a;
1476
- if (isDraft(value)) {
1477
- var proxyDraft = getProxyDraft(value);
1478
- ensureShallowCopy(proxyDraft);
1479
- // A draft where a child node has been changed, or assigned a value
1480
- var updatedValue = ((_a = proxyDraft.assignedMap) === null || _a === void 0 ? void 0 : _a.size) || proxyDraft.operated ? proxyDraft.copy : proxyDraft.original;
1481
- // final update value
1482
- set(isSet ? setMap : target, key, updatedValue);
1483
- } else {
1484
- handleValue(value, handledSet, options);
1485
- }
1486
- });
1487
- if (setMap) {
1488
- var _set = target;
1489
- var values = Array.from(_set);
1490
- _set.clear();
1491
- values.forEach(function (value) {
1492
- _set.add(setMap.has(value) ? setMap.get(value) : value);
1493
- });
1494
- }
1495
- }
1496
- function finalizeAssigned(proxyDraft, key) {
1497
- // handle the draftable assigned values, and the value is not a draft
1498
- var copy = proxyDraft.type === 3 /* DraftType.Set */ ? proxyDraft.setMap : proxyDraft.copy;
1499
- if (proxyDraft.finalities.revoke.length > 1 && proxyDraft.assignedMap.get(key) && copy) {
1500
- handleValue(get(copy, key), proxyDraft.finalities.handledSet, proxyDraft.options);
1501
- }
1502
- }
1503
- function finalizeSetValue(target) {
1504
- if (target.type === 3 /* DraftType.Set */ && target.copy) {
1505
- target.copy.clear();
1506
- target.setMap.forEach(function (value) {
1507
- target.copy.add(getValue(value));
1508
- });
1509
- }
1510
- }
1511
- function finalizePatches(target, generatePatches, patches, inversePatches) {
1512
- var shouldFinalize = target.operated && target.assignedMap && target.assignedMap.size > 0 && !target.finalized;
1513
- if (shouldFinalize) {
1514
- if (patches && inversePatches) {
1515
- var basePath = getPath(target);
1516
- if (basePath) {
1517
- generatePatches(target, basePath, patches, inversePatches);
1518
- }
1519
- }
1520
- target.finalized = true;
1521
- }
1522
- }
1523
- function markFinalization(target, key, value, generatePatches) {
1524
- var proxyDraft = getProxyDraft(value);
1525
- if (proxyDraft) {
1526
- // !case: assign the draft value
1527
- if (!proxyDraft.callbacks) {
1528
- proxyDraft.callbacks = [];
1529
- }
1530
- proxyDraft.callbacks.push(function (patches, inversePatches) {
1531
- var _a;
1532
- var copy = target.type === 3 /* DraftType.Set */ ? target.setMap : target.copy;
1533
- if (isEqual(get(copy, key), value)) {
1534
- var updatedValue = proxyDraft.original;
1535
- if (proxyDraft.copy) {
1536
- updatedValue = proxyDraft.copy;
1537
- }
1538
- finalizeSetValue(target);
1539
- finalizePatches(target, generatePatches, patches, inversePatches);
1540
- if (target.options.enableAutoFreeze) {
1541
- target.options.updatedValues = (_a = target.options.updatedValues) !== null && _a !== undefined ? _a : new WeakMap();
1542
- target.options.updatedValues.set(updatedValue, proxyDraft.original);
1543
- }
1544
- // final update value
1545
- set(copy, key, updatedValue);
1546
- }
1547
- });
1548
- if (target.options.enableAutoFreeze) {
1549
- // !case: assign the draft value in cross draft tree
1550
- if (proxyDraft.finalities !== target.finalities) {
1551
- target.options.enableAutoFreeze = false;
1552
- }
1553
- }
1554
- }
1555
- if (isDraftable(value, target.options)) {
1556
- // !case: assign the non-draft value
1557
- target.finalities.draft.push(function () {
1558
- var copy = target.type === 3 /* DraftType.Set */ ? target.setMap : target.copy;
1559
- if (isEqual(get(copy, key), value)) {
1560
- finalizeAssigned(target, key);
1561
- }
1562
- });
1563
- }
1564
- }
1565
- function generateArrayPatches(proxyState, basePath, patches, inversePatches, pathAsArray) {
1566
- var original = proxyState.original,
1567
- assignedMap = proxyState.assignedMap,
1568
- options = proxyState.options;
1569
- var copy = proxyState.copy;
1570
- if (copy.length < original.length) {
1571
- var _ref3 = [copy, original];
1572
- original = _ref3[0];
1573
- copy = _ref3[1];
1574
- var _ref4 = [inversePatches, patches];
1575
- patches = _ref4[0];
1576
- inversePatches = _ref4[1];
1577
- }
1578
- for (var index = 0; index < original.length; index += 1) {
1579
- if (assignedMap.get(index.toString()) && copy[index] !== original[index]) {
1580
- var _path = basePath.concat([index]);
1581
- var path = escapePath(_path, pathAsArray);
1582
- patches.push({
1583
- op: Operation.Replace,
1584
- path: path,
1585
- // If it is a draft, it needs to be deep cloned, and it may also be non-draft.
1586
- value: cloneIfNeeded(copy[index])
1587
- });
1588
- inversePatches.push({
1589
- op: Operation.Replace,
1590
- path: path,
1591
- // If it is a draft, it needs to be deep cloned, and it may also be non-draft.
1592
- value: cloneIfNeeded(original[index])
1593
- });
1594
- }
1595
- }
1596
- for (var _index2 = original.length; _index2 < copy.length; _index2 += 1) {
1597
- var _path2 = basePath.concat([_index2]);
1598
- var _path3 = escapePath(_path2, pathAsArray);
1599
- patches.push({
1600
- op: Operation.Add,
1601
- path: _path3,
1602
- // If it is a draft, it needs to be deep cloned, and it may also be non-draft.
1603
- value: cloneIfNeeded(copy[_index2])
1604
- });
1605
- }
1606
- if (original.length < copy.length) {
1607
- // https://www.rfc-editor.org/rfc/rfc6902#appendix-A.4
1608
- // For performance, here we only generate an operation that replaces the length of the array,
1609
- // which is inconsistent with JSON Patch specification
1610
- var _options$enablePatche = options.enablePatches.arrayLengthAssignment,
1611
- arrayLengthAssignment = _options$enablePatche === undefined ? true : _options$enablePatche;
1612
- if (arrayLengthAssignment) {
1613
- var _path4 = basePath.concat(['length']);
1614
- var _path5 = escapePath(_path4, pathAsArray);
1615
- inversePatches.push({
1616
- op: Operation.Replace,
1617
- path: _path5,
1618
- value: original.length
1619
- });
1620
- } else {
1621
- for (var _index3 = copy.length; original.length < _index3; _index3 -= 1) {
1622
- var _path6 = basePath.concat([_index3 - 1]);
1623
- var _path7 = escapePath(_path6, pathAsArray);
1624
- inversePatches.push({
1625
- op: Operation.Remove,
1626
- path: _path7
1627
- });
1628
- }
1629
- }
1630
- }
1631
- }
1632
- function generatePatchesFromAssigned(_ref5, basePath, patches, inversePatches, pathAsArray) {
1633
- var original = _ref5.original,
1634
- copy = _ref5.copy,
1635
- assignedMap = _ref5.assignedMap;
1636
- assignedMap.forEach(function (assignedValue, key) {
1637
- var originalValue = get(original, key);
1638
- var value = cloneIfNeeded(get(copy, key));
1639
- var op = !assignedValue ? Operation.Remove : has(original, key) ? Operation.Replace : Operation.Add;
1640
- if (isEqual(originalValue, value) && op === Operation.Replace) return;
1641
- var _path = basePath.concat(key);
1642
- var path = escapePath(_path, pathAsArray);
1643
- patches.push(op === Operation.Remove ? {
1644
- op: op,
1645
- path: path
1646
- } : {
1647
- op: op,
1648
- path: path,
1649
- value: value
1650
- });
1651
- inversePatches.push(op === Operation.Add ? {
1652
- op: Operation.Remove,
1653
- path: path
1654
- } : op === Operation.Remove ? {
1655
- op: Operation.Add,
1656
- path: path,
1657
- value: originalValue
1658
- } : {
1659
- op: Operation.Replace,
1660
- path: path,
1661
- value: originalValue
1662
- });
1663
- });
1664
- }
1665
- function generateSetPatches(_ref6, basePath, patches, inversePatches, pathAsArray) {
1666
- var original = _ref6.original,
1667
- copy = _ref6.copy;
1668
- var index = 0;
1669
- original.forEach(function (value) {
1670
- if (!copy.has(value)) {
1671
- var _path = basePath.concat([index]);
1672
- var path = escapePath(_path, pathAsArray);
1673
- patches.push({
1674
- op: Operation.Remove,
1675
- path: path,
1676
- value: value
1677
- });
1678
- inversePatches.unshift({
1679
- op: Operation.Add,
1680
- path: path,
1681
- value: value
1682
- });
1683
- }
1684
- index += 1;
1685
- });
1686
- index = 0;
1687
- copy.forEach(function (value) {
1688
- if (!original.has(value)) {
1689
- var _path = basePath.concat([index]);
1690
- var path = escapePath(_path, pathAsArray);
1691
- patches.push({
1692
- op: Operation.Add,
1693
- path: path,
1694
- value: value
1695
- });
1696
- inversePatches.unshift({
1697
- op: Operation.Remove,
1698
- path: path,
1699
- value: value
1700
- });
1701
- }
1702
- index += 1;
1703
- });
1704
- }
1705
- function generatePatches(proxyState, basePath, patches, inversePatches) {
1706
- var _proxyState$options$e = proxyState.options.enablePatches.pathAsArray,
1707
- pathAsArray = _proxyState$options$e === undefined ? true : _proxyState$options$e;
1708
- switch (proxyState.type) {
1709
- case 0 /* DraftType.Object */:
1710
- case 2 /* DraftType.Map */:
1711
- return generatePatchesFromAssigned(proxyState, basePath, patches, inversePatches, pathAsArray);
1712
- case 1 /* DraftType.Array */:
1713
- return generateArrayPatches(proxyState, basePath, patches, inversePatches, pathAsArray);
1714
- case 3 /* DraftType.Set */:
1715
- return generateSetPatches(proxyState, basePath, patches, inversePatches, pathAsArray);
1716
- }
1717
- }
1718
- var checkReadable = function checkReadable(value, options) {
1719
- var ignoreCheckDraftable = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
1720
- if (_typeof(value) === 'object' && value !== null && (!isDraftable(value, options) || ignoreCheckDraftable) && true) {
1721
- throw new Error("Strict mode: Mutable data cannot be accessed directly, please use 'unsafe(callback)' wrap.");
1722
- }
1723
- };
1724
- var mapHandler = _defineProperty({
1725
- get size() {
1726
- var current = latest(getProxyDraft(this));
1727
- return current.size;
1728
- },
1729
- has: function has(key) {
1730
- return latest(getProxyDraft(this)).has(key);
1731
- },
1732
- set: function set(key, value) {
1733
- var target = getProxyDraft(this);
1734
- var source = latest(target);
1735
- if (!source.has(key) || !isEqual(source.get(key), value)) {
1736
- ensureShallowCopy(target);
1737
- markChanged(target);
1738
- target.assignedMap.set(key, true);
1739
- target.copy.set(key, value);
1740
- markFinalization(target, key, value, generatePatches);
1741
- }
1742
- return this;
1743
- },
1744
- "delete": function _delete(key) {
1745
- if (!this.has(key)) {
1746
- return false;
1747
- }
1748
- var target = getProxyDraft(this);
1749
- ensureShallowCopy(target);
1750
- markChanged(target);
1751
- if (target.original.has(key)) {
1752
- target.assignedMap.set(key, false);
1753
- } else {
1754
- target.assignedMap["delete"](key);
1755
- }
1756
- target.copy["delete"](key);
1757
- return true;
1758
- },
1759
- clear: function clear() {
1760
- var target = getProxyDraft(this);
1761
- if (!this.size) return;
1762
- ensureShallowCopy(target);
1763
- markChanged(target);
1764
- target.assignedMap = new Map();
1765
- var _iterator5 = _createForOfIteratorHelper(target.original),
1766
- _step5;
1767
- try {
1768
- for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
1769
- var _step5$value = _slicedToArray(_step5.value, 1),
1770
- key = _step5$value[0];
1771
- target.assignedMap.set(key, false);
1772
- }
1773
- } catch (err) {
1774
- _iterator5.e(err);
1775
- } finally {
1776
- _iterator5.f();
1777
- }
1778
- target.copy.clear();
1779
- },
1780
- forEach: function forEach(callback, thisArg) {
1781
- var _this = this;
1782
- var target = getProxyDraft(this);
1783
- latest(target).forEach(function (_value, _key) {
1784
- callback.call(thisArg, _this.get(_key), _key, _this);
1785
- });
1786
- },
1787
- get: function get(key) {
1788
- var _a, _b;
1789
- var target = getProxyDraft(this);
1790
- var value = latest(target).get(key);
1791
- var mutable = ((_b = (_a = target.options).mark) === null || _b === undefined ? undefined : _b.call(_a, value, dataTypes)) === dataTypes.mutable;
1792
- if (target.options.strict) {
1793
- checkReadable(value, target.options, mutable);
1794
- }
1795
- if (mutable) {
1796
- return value;
1797
- }
1798
- if (target.finalized || !isDraftable(value, target.options)) {
1799
- return value;
1800
- }
1801
- // drafted or reassigned
1802
- if (value !== target.original.get(key)) {
1803
- return value;
1804
- }
1805
- var draft = internal.createDraft({
1806
- original: value,
1807
- parentDraft: target,
1808
- key: key,
1809
- finalities: target.finalities,
1810
- options: target.options
1811
- });
1812
- ensureShallowCopy(target);
1813
- target.copy.set(key, draft);
1814
- return draft;
1815
- },
1816
- keys: function keys() {
1817
- return latest(getProxyDraft(this)).keys();
1818
- },
1819
- values: function values() {
1820
- var _this2 = this;
1821
- var iterator = this.keys();
1822
- return _defineProperty(_defineProperty({}, iteratorSymbol, function () {
1823
- return _this2.values();
1824
- }), "next", function next() {
1825
- var result = iterator.next();
1826
- if (result.done) return result;
1827
- var value = _this2.get(result.value);
1828
- return {
1829
- done: false,
1830
- value: value
1831
- };
1832
- });
1833
- },
1834
- entries: function entries() {
1835
- var _this3 = this;
1836
- var iterator = this.keys();
1837
- return _defineProperty(_defineProperty({}, iteratorSymbol, function () {
1838
- return _this3.entries();
1839
- }), "next", function next() {
1840
- var result = iterator.next();
1841
- if (result.done) return result;
1842
- var value = _this3.get(result.value);
1843
- return {
1844
- done: false,
1845
- value: [result.value, value]
1846
- };
1847
- });
1848
- }
1849
- }, iteratorSymbol, function () {
1850
- return this.entries();
1851
- });
1852
- var mapHandlerKeys = Reflect.ownKeys(mapHandler);
1853
- var getNextIterator = function getNextIterator(target, iterator, _ref9) {
1854
- var isValuesIterator = _ref9.isValuesIterator;
1855
- return function () {
1856
- var _a, _b;
1857
- var result = iterator.next();
1858
- if (result.done) return result;
1859
- var key = result.value;
1860
- var value = target.setMap.get(key);
1861
- var currentDraft = getProxyDraft(value);
1862
- var mutable = ((_b = (_a = target.options).mark) === null || _b === undefined ? undefined : _b.call(_a, value, dataTypes)) === dataTypes.mutable;
1863
- if (target.options.strict) {
1864
- checkReadable(key, target.options, mutable);
1865
- }
1866
- if (!mutable && !currentDraft && isDraftable(key, target.options) && !target.finalized && target.original.has(key)) {
1867
- // draft a draftable original set item
1868
- var proxy = internal.createDraft({
1869
- original: key,
1870
- parentDraft: target,
1871
- key: key,
1872
- finalities: target.finalities,
1873
- options: target.options
1874
- });
1875
- target.setMap.set(key, proxy);
1876
- value = proxy;
1877
- } else if (currentDraft) {
1878
- // drafted
1879
- value = currentDraft.proxy;
1880
- }
1881
- return {
1882
- done: false,
1883
- value: isValuesIterator ? value : [value, value]
1884
- };
1885
- };
1886
- };
1887
- var setHandler = _defineProperty(_defineProperty({
1888
- get size() {
1889
- var target = getProxyDraft(this);
1890
- return target.setMap.size;
1891
- },
1892
- has: function has(value) {
1893
- var target = getProxyDraft(this);
1894
- // reassigned or non-draftable values
1895
- if (target.setMap.has(value)) return true;
1896
- ensureShallowCopy(target);
1897
- var valueProxyDraft = getProxyDraft(value);
1898
- // drafted
1899
- if (valueProxyDraft && target.setMap.has(valueProxyDraft.original)) return true;
1900
- return false;
1901
- },
1902
- add: function add(value) {
1903
- var target = getProxyDraft(this);
1904
- if (!this.has(value)) {
1905
- ensureShallowCopy(target);
1906
- markChanged(target);
1907
- target.assignedMap.set(value, true);
1908
- target.setMap.set(value, value);
1909
- markFinalization(target, value, value, generatePatches);
1910
- }
1911
- return this;
1912
- },
1913
- "delete": function _delete(value) {
1914
- if (!this.has(value)) {
1915
- return false;
1916
- }
1917
- var target = getProxyDraft(this);
1918
- ensureShallowCopy(target);
1919
- markChanged(target);
1920
- var valueProxyDraft = getProxyDraft(value);
1921
- if (valueProxyDraft && target.setMap.has(valueProxyDraft.original)) {
1922
- // delete drafted
1923
- target.assignedMap.set(valueProxyDraft.original, false);
1924
- return target.setMap["delete"](valueProxyDraft.original);
1925
- }
1926
- if (!valueProxyDraft && target.setMap.has(value)) {
1927
- // non-draftable values
1928
- target.assignedMap.set(value, false);
1929
- } else {
1930
- // reassigned
1931
- target.assignedMap["delete"](value);
1932
- }
1933
- // delete reassigned or non-draftable values
1934
- return target.setMap["delete"](value);
1935
- },
1936
- clear: function clear() {
1937
- if (!this.size) return;
1938
- var target = getProxyDraft(this);
1939
- ensureShallowCopy(target);
1940
- markChanged(target);
1941
- var _iterator6 = _createForOfIteratorHelper(target.original),
1942
- _step6;
1943
- try {
1944
- for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
1945
- var value = _step6.value;
1946
- target.assignedMap.set(value, false);
1947
- }
1948
- } catch (err) {
1949
- _iterator6.e(err);
1950
- } finally {
1951
- _iterator6.f();
1952
- }
1953
- target.setMap.clear();
1954
- },
1955
- values: function values() {
1956
- var _this4 = this;
1957
- var target = getProxyDraft(this);
1958
- ensureShallowCopy(target);
1959
- var iterator = target.setMap.keys();
1960
- return _defineProperty(_defineProperty({}, Symbol.iterator, function () {
1961
- return _this4.values();
1962
- }), "next", getNextIterator(target, iterator, {
1963
- isValuesIterator: true
1964
- }));
1965
- },
1966
- entries: function entries() {
1967
- var _this5 = this;
1968
- var target = getProxyDraft(this);
1969
- ensureShallowCopy(target);
1970
- var iterator = target.setMap.keys();
1971
- return _defineProperty(_defineProperty({}, Symbol.iterator, function () {
1972
- return _this5.entries();
1973
- }), "next", getNextIterator(target, iterator, {
1974
- isValuesIterator: false
1975
- }));
1976
- },
1977
- keys: function keys() {
1978
- return this.values();
1979
- }
1980
- }, iteratorSymbol, function () {
1981
- return this.values();
1982
- }), "forEach", function forEach(callback, thisArg) {
1983
- var iterator = this.values();
1984
- var result = iterator.next();
1985
- while (!result.done) {
1986
- callback.call(thisArg, result.value, result.value, this);
1987
- result = iterator.next();
1988
- }
1989
- });
1990
- if (Set.prototype.difference) {
1991
- // for compatibility with new Set methods
1992
- // https://github.com/tc39/proposal-set-methods
1993
- // And `https://github.com/tc39/proposal-set-methods/blob/main/details.md#symbolspecies` has some details about the `@@species` symbol.
1994
- // So we can't use SubSet instance constructor to get the constructor of the SubSet instance.
1995
- Object.assign(setHandler, {
1996
- intersection: function intersection(other) {
1997
- return Set.prototype.intersection.call(new Set(this.values()), other);
1998
- },
1999
- union: function union(other) {
2000
- return Set.prototype.union.call(new Set(this.values()), other);
2001
- },
2002
- difference: function difference(other) {
2003
- return Set.prototype.difference.call(new Set(this.values()), other);
2004
- },
2005
- symmetricDifference: function symmetricDifference(other) {
2006
- return Set.prototype.symmetricDifference.call(new Set(this.values()), other);
2007
- },
2008
- isSubsetOf: function isSubsetOf(other) {
2009
- return Set.prototype.isSubsetOf.call(new Set(this.values()), other);
2010
- },
2011
- isSupersetOf: function isSupersetOf(other) {
2012
- return Set.prototype.isSupersetOf.call(new Set(this.values()), other);
2013
- },
2014
- isDisjointFrom: function isDisjointFrom(other) {
2015
- return Set.prototype.isDisjointFrom.call(new Set(this.values()), other);
2016
- }
2017
- });
2018
- }
2019
- var setHandlerKeys = Reflect.ownKeys(setHandler);
2020
- var draftsCache = new WeakSet();
2021
- var proxyHandler = {
2022
- get: function get(target, key, receiver) {
2023
- var _a, _b;
2024
- var copy = (_a = target.copy) === null || _a === undefined ? undefined : _a[key];
2025
- // Improve draft reading performance by caching the draft copy.
2026
- if (copy && draftsCache.has(copy)) {
2027
- return copy;
2028
- }
2029
- if (key === PROXY_DRAFT) return target;
2030
- var markResult;
2031
- if (target.options.mark) {
2032
- // handle `Uncaught TypeError: Method get Map.prototype.size called on incompatible receiver #<Map>`
2033
- // or `Uncaught TypeError: Method get Set.prototype.size called on incompatible receiver #<Set>`
2034
- var _value5 = key === 'size' && (target.original instanceof Map || target.original instanceof Set) ? Reflect.get(target.original, key) : Reflect.get(target.original, key, receiver);
2035
- markResult = target.options.mark(_value5, dataTypes);
2036
- if (markResult === dataTypes.mutable) {
2037
- if (target.options.strict) {
2038
- checkReadable(_value5, target.options, true);
2039
- }
2040
- return _value5;
2041
- }
2042
- }
2043
- var source = latest(target);
2044
- if (source instanceof Map && mapHandlerKeys.includes(key)) {
2045
- if (key === 'size') {
2046
- return Object.getOwnPropertyDescriptor(mapHandler, 'size').get.call(target.proxy);
2047
- }
2048
- var handle = mapHandler[key];
2049
- if (handle) {
2050
- return handle.bind(target.proxy);
2051
- }
2052
- }
2053
- if (source instanceof Set && setHandlerKeys.includes(key)) {
2054
- if (key === 'size') {
2055
- return Object.getOwnPropertyDescriptor(setHandler, 'size').get.call(target.proxy);
2056
- }
2057
- var _handle = setHandler[key];
2058
- if (_handle) {
2059
- return _handle.bind(target.proxy);
2060
- }
2061
- }
2062
- if (!has(source, key)) {
2063
- var desc = getDescriptor(source, key);
2064
- return desc ? "value" in desc ? desc.value :
2065
- // !case: support for getter
2066
- (_b = desc.get) === null || _b === undefined ? undefined : _b.call(target.proxy) : undefined;
2067
- }
2068
- var value = source[key];
2069
- if (target.options.strict) {
2070
- checkReadable(value, target.options);
2071
- }
2072
- if (target.finalized || !isDraftable(value, target.options)) {
2073
- return value;
2074
- }
2075
- // Ensure that the assigned values are not drafted
2076
- if (value === peek(target.original, key)) {
2077
- ensureShallowCopy(target);
2078
- target.copy[key] = createDraft({
2079
- original: target.original[key],
2080
- parentDraft: target,
2081
- key: target.type === 1 /* DraftType.Array */ ? Number(key) : key,
2082
- finalities: target.finalities,
2083
- options: target.options
2084
- });
2085
- // !case: support for custom shallow copy function
2086
- if (typeof markResult === 'function') {
2087
- var subProxyDraft = getProxyDraft(target.copy[key]);
2088
- ensureShallowCopy(subProxyDraft);
2089
- // Trigger a custom shallow copy to update to a new copy
2090
- markChanged(subProxyDraft);
2091
- return subProxyDraft.copy;
2092
- }
2093
- return target.copy[key];
2094
- }
2095
- return value;
2096
- },
2097
- set: function set(target, key, value) {
2098
- var _a;
2099
- if (target.type === 3 /* DraftType.Set */ || target.type === 2 /* DraftType.Map */) {
2100
- throw new Error("Map/Set draft does not support any property assignment.");
2101
- }
2102
- var _key;
2103
- if (target.type === 1 /* DraftType.Array */ && key !== 'length' && !(Number.isInteger(_key = Number(key)) && _key >= 0 && (key === 0 || _key === 0 || String(_key) === String(key)))) {
2104
- throw new Error("Only supports setting array indices and the 'length' property.");
2105
- }
2106
- var desc = getDescriptor(latest(target), key);
2107
- if (desc === null || desc === undefined ? undefined : desc.set) {
2108
- // !case: cover the case of setter
2109
- desc.set.call(target.proxy, value);
2110
- return true;
2111
- }
2112
- var current = peek(latest(target), key);
2113
- var currentProxyDraft = getProxyDraft(current);
2114
- if (currentProxyDraft && isEqual(currentProxyDraft.original, value)) {
2115
- // !case: ignore the case of assigning the original draftable value to a draft
2116
- target.copy[key] = value;
2117
- target.assignedMap = (_a = target.assignedMap) !== null && _a !== undefined ? _a : new Map();
2118
- target.assignedMap.set(key, false);
2119
- return true;
2120
- }
2121
- // !case: handle new props with value 'undefined'
2122
- if (isEqual(value, current) && (value !== undefined || has(target.original, key))) return true;
2123
- ensureShallowCopy(target);
2124
- markChanged(target);
2125
- if (has(target.original, key) && isEqual(value, target.original[key])) {
2126
- // !case: handle the case of assigning the original non-draftable value to a draft
2127
- target.assignedMap["delete"](key);
2128
- } else {
2129
- target.assignedMap.set(key, true);
2130
- }
2131
- target.copy[key] = value;
2132
- markFinalization(target, key, value, generatePatches);
2133
- return true;
2134
- },
2135
- has: function has(target, key) {
2136
- return key in latest(target);
2137
- },
2138
- ownKeys: function ownKeys(target) {
2139
- return Reflect.ownKeys(latest(target));
2140
- },
2141
- getOwnPropertyDescriptor: function getOwnPropertyDescriptor(target, key) {
2142
- var source = latest(target);
2143
- var descriptor = Reflect.getOwnPropertyDescriptor(source, key);
2144
- if (!descriptor) return descriptor;
2145
- return {
2146
- writable: true,
2147
- configurable: target.type !== 1 /* DraftType.Array */ || key !== 'length',
2148
- enumerable: descriptor.enumerable,
2149
- value: source[key]
2150
- };
2151
- },
2152
- getPrototypeOf: function getPrototypeOf(target) {
2153
- return Reflect.getPrototypeOf(target.original);
2154
- },
2155
- setPrototypeOf: function setPrototypeOf() {
2156
- throw new Error("Cannot call 'setPrototypeOf()' on drafts");
2157
- },
2158
- defineProperty: function defineProperty() {
2159
- throw new Error("Cannot call 'defineProperty()' on drafts");
2160
- },
2161
- deleteProperty: function deleteProperty(target, key) {
2162
- var _a;
2163
- if (target.type === 1 /* DraftType.Array */) {
2164
- return proxyHandler.set.call(this, target, key, undefined, target.proxy);
2165
- }
2166
- if (peek(target.original, key) !== undefined || key in target.original) {
2167
- // !case: delete an existing key
2168
- ensureShallowCopy(target);
2169
- markChanged(target);
2170
- target.assignedMap.set(key, false);
2171
- } else {
2172
- target.assignedMap = (_a = target.assignedMap) !== null && _a !== undefined ? _a : new Map();
2173
- // The original non-existent key has been deleted
2174
- target.assignedMap["delete"](key);
2175
- }
2176
- if (target.copy) delete target.copy[key];
2177
- return true;
2178
- }
2179
- };
2180
- function createDraft(createDraftOptions) {
2181
- var original = createDraftOptions.original,
2182
- parentDraft = createDraftOptions.parentDraft,
2183
- key = createDraftOptions.key,
2184
- finalities = createDraftOptions.finalities,
2185
- options = createDraftOptions.options;
2186
- var type = getType(original);
2187
- var proxyDraft = {
2188
- type: type,
2189
- finalized: false,
2190
- parent: parentDraft,
2191
- original: original,
2192
- copy: null,
2193
- proxy: null,
2194
- finalities: finalities,
2195
- options: options,
2196
- // Mapping of draft Set items to their corresponding draft values.
2197
- setMap: type === 3 /* DraftType.Set */ ? new Map(original.entries()) : undefined
2198
- };
2199
- // !case: undefined as a draft map key
2200
- if (key || 'key' in createDraftOptions) {
2201
- proxyDraft.key = key;
2202
- }
2203
- var _Proxy$revocable = Proxy.revocable(type === 1 /* DraftType.Array */ ? Object.assign([], proxyDraft) : proxyDraft, proxyHandler),
2204
- proxy = _Proxy$revocable.proxy,
2205
- revoke = _Proxy$revocable.revoke;
2206
- finalities.revoke.push(revoke);
2207
- draftsCache.add(proxy);
2208
- proxyDraft.proxy = proxy;
2209
- if (parentDraft) {
2210
- var target = parentDraft;
2211
- target.finalities.draft.push(function (patches, inversePatches) {
2212
- var _a, _b;
2213
- var oldProxyDraft = getProxyDraft(proxy);
2214
- // if target is a Set draft, `setMap` is the real Set copies proxy mapping.
2215
- var copy = target.type === 3 /* DraftType.Set */ ? target.setMap : target.copy;
2216
- var draft = get(copy, key);
2217
- var proxyDraft = getProxyDraft(draft);
2218
- if (proxyDraft) {
2219
- // assign the updated value to the copy object
2220
- var updatedValue = proxyDraft.original;
2221
- if (proxyDraft.operated) {
2222
- updatedValue = getValue(draft);
2223
- }
2224
- finalizeSetValue(proxyDraft);
2225
- finalizePatches(proxyDraft, generatePatches, patches, inversePatches);
2226
- if (target.options.enableAutoFreeze) {
2227
- target.options.updatedValues = (_a = target.options.updatedValues) !== null && _a !== undefined ? _a : new WeakMap();
2228
- target.options.updatedValues.set(updatedValue, proxyDraft.original);
2229
- }
2230
- // final update value
2231
- set(copy, key, updatedValue);
2232
- }
2233
- // !case: handle the deleted key
2234
- (_b = oldProxyDraft.callbacks) === null || _b === undefined ? undefined : _b.forEach(function (callback) {
2235
- callback(patches, inversePatches);
2236
- });
2237
- });
2238
- } else {
2239
- // !case: handle the root draft
2240
- var _target = getProxyDraft(proxy);
2241
- _target.finalities.draft.push(function (patches, inversePatches) {
2242
- finalizeSetValue(_target);
2243
- finalizePatches(_target, generatePatches, patches, inversePatches);
2244
- });
2245
- }
2246
- return proxy;
2247
- }
2248
- internal.createDraft = createDraft;
2249
- function finalizeDraft(result, returnedValue, patches, inversePatches, enableAutoFreeze) {
2250
- var _a;
2251
- var proxyDraft = getProxyDraft(result);
2252
- var original = (_a = proxyDraft === null || proxyDraft === undefined ? undefined : proxyDraft.original) !== null && _a !== undefined ? _a : result;
2253
- var hasReturnedValue = !!returnedValue.length;
2254
- if (proxyDraft === null || proxyDraft === undefined ? undefined : proxyDraft.operated) {
2255
- while (proxyDraft.finalities.draft.length > 0) {
2256
- var finalize = proxyDraft.finalities.draft.pop();
2257
- finalize(patches, inversePatches);
2258
- }
2259
- }
2260
- var state = hasReturnedValue ? returnedValue[0] : proxyDraft ? proxyDraft.operated ? proxyDraft.copy : proxyDraft.original : result;
2261
- if (proxyDraft) revokeProxy(proxyDraft);
2262
- if (enableAutoFreeze) {
2263
- deepFreeze(state, state, proxyDraft === null || proxyDraft === undefined ? undefined : proxyDraft.options.updatedValues);
2264
- }
2265
- return [state, patches && hasReturnedValue ? [{
2266
- op: Operation.Replace,
2267
- path: [],
2268
- value: returnedValue[0]
2269
- }] : patches, inversePatches && hasReturnedValue ? [{
2270
- op: Operation.Replace,
2271
- path: [],
2272
- value: original
2273
- }] : inversePatches];
2274
- }
2275
- function draftify(baseState, options) {
2276
- var _a;
2277
- var finalities = {
2278
- draft: [],
2279
- revoke: [],
2280
- handledSet: new WeakSet()
2281
- };
2282
- var patches;
2283
- var inversePatches;
2284
- if (options.enablePatches) {
2285
- patches = [];
2286
- inversePatches = [];
2287
- }
2288
- var isMutable = ((_a = options.mark) === null || _a === undefined ? undefined : _a.call(options, baseState, dataTypes)) === dataTypes.mutable || !isDraftable(baseState, options);
2289
- var draft = isMutable ? baseState : createDraft({
2290
- original: baseState,
2291
- parentDraft: null,
2292
- finalities: finalities,
2293
- options: options
2294
- });
2295
- return [draft, function () {
2296
- var returnedValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
2297
- var _finalizeDraft = finalizeDraft(draft, returnedValue, patches, inversePatches, options.enableAutoFreeze),
2298
- _finalizeDraft2 = _slicedToArray(_finalizeDraft, 3),
2299
- finalizedState = _finalizeDraft2[0],
2300
- finalizedPatches = _finalizeDraft2[1],
2301
- finalizedInversePatches = _finalizeDraft2[2];
2302
- return options.enablePatches ? [finalizedState, finalizedPatches, finalizedInversePatches] : finalizedState;
2303
- }];
2304
- }
2305
- function handleReturnValue(options) {
2306
- var rootDraft = options.rootDraft,
2307
- value = options.value,
2308
- _options$useRawReturn = options.useRawReturn,
2309
- useRawReturn = _options$useRawReturn === undefined ? false : _options$useRawReturn,
2310
- _options$isRoot = options.isRoot,
2311
- isRoot = _options$isRoot === undefined ? true : _options$isRoot;
2312
- forEach(value, function (key, item, source) {
2313
- var proxyDraft = getProxyDraft(item);
2314
- // just handle the draft which is created by the same rootDraft
2315
- if (proxyDraft && rootDraft && proxyDraft.finalities === rootDraft.finalities) {
2316
- options.isContainDraft = true;
2317
- var currentValue = proxyDraft.original;
2318
- // final update value, but just handle return value
2319
- if (source instanceof Set) {
2320
- var arr = Array.from(source);
2321
- source.clear();
2322
- arr.forEach(function (_item) {
2323
- return source.add(key === _item ? currentValue : _item);
2324
- });
2325
- } else {
2326
- set(source, key, currentValue);
2327
- }
2328
- } else if (_typeof(item) === 'object' && item !== null) {
2329
- options.value = item;
2330
- options.isRoot = false;
2331
- handleReturnValue(options);
2332
- }
2333
- });
2334
- if (isRoot) {
2335
- if (!options.isContainDraft) console.warn("The return value does not contain any draft, please use 'rawReturn()' to wrap the return value to improve performance.");
2336
- if (useRawReturn) {
2337
- console.warn("The return value contains drafts, please don't use 'rawReturn()' to wrap the return value.");
2338
- }
2339
- }
2340
- }
2341
- function getCurrent(target) {
2342
- var _a;
2343
- var proxyDraft = getProxyDraft(target);
2344
- if (!isDraftable(target, proxyDraft === null || proxyDraft === undefined ? undefined : proxyDraft.options)) return target;
2345
- var type = getType(target);
2346
- if (proxyDraft && !proxyDraft.operated) return proxyDraft.original;
2347
- var currentValue;
2348
- function ensureShallowCopy() {
2349
- currentValue = type === 2 /* DraftType.Map */ ? !isBaseMapInstance(target) ? new (Object.getPrototypeOf(target).constructor)(target) : new Map(target) : type === 3 /* DraftType.Set */ ? Array.from(proxyDraft.setMap.values()) : shallowCopy(target, proxyDraft === null || proxyDraft === undefined ? undefined : proxyDraft.options);
2350
- }
2351
- if (proxyDraft) {
2352
- // It's a proxy draft, let's create a shallow copy eagerly
2353
- proxyDraft.finalized = true;
2354
- try {
2355
- ensureShallowCopy();
2356
- } finally {
2357
- proxyDraft.finalized = false;
2358
- }
2359
- } else {
2360
- // It's not a proxy draft, let's use the target directly and let's see
2361
- // lazily if we need to create a shallow copy
2362
- currentValue = target;
2363
- }
2364
- forEach(currentValue, function (key, value) {
2365
- if (proxyDraft && isEqual(get(proxyDraft.original, key), value)) return;
2366
- var newValue = getCurrent(value);
2367
- if (newValue !== value) {
2368
- if (currentValue === target) ensureShallowCopy();
2369
- set(currentValue, key, newValue);
2370
- }
2371
- });
2372
- if (type === 3 /* DraftType.Set */) {
2373
- var value = (_a = proxyDraft === null || proxyDraft === undefined ? undefined : proxyDraft.original) !== null && _a !== undefined ? _a : currentValue;
2374
- return !isBaseSetInstance(value) ? new (Object.getPrototypeOf(value).constructor)(currentValue) : new Set(currentValue);
2375
- }
2376
- return currentValue;
2377
- }
2378
- function current(target) {
2379
- if (!isDraft(target)) {
2380
- throw new Error("current() is only used for Draft, parameter: ".concat(target));
2381
- }
2382
- return getCurrent(target);
2383
- }
2384
-
2385
- /**
2386
- * `makeCreator(options)` to make a creator function.
2387
- *
2388
- * ## Example
2389
- *
2390
- * ```ts
2391
- * import { makeCreator } from '../index';
2392
- *
2393
- * const baseState = { foo: { bar: 'str' }, arr: [] };
2394
- * const create = makeCreator({ enableAutoFreeze: true });
2395
- * const state = create(
2396
- * baseState,
2397
- * (draft) => {
2398
- * draft.foo.bar = 'str2';
2399
- * },
2400
- * );
2401
- *
2402
- * expect(state).toEqual({ foo: { bar: 'str2' }, arr: [] });
2403
- * expect(state).not.toBe(baseState);
2404
- * expect(state.foo).not.toBe(baseState.foo);
2405
- * expect(state.arr).toBe(baseState.arr);
2406
- * expect(Object.isFrozen(state)).toBeTruthy();
2407
- * ```
2408
- */
2409
- var makeCreator = function makeCreator(arg) {
2410
- return function create(arg0, arg1, arg2) {
2411
- var _a, _b, _c;
2412
- if (typeof arg0 === 'function' && typeof arg1 !== 'function') {
2413
- return function (base) {
2414
- var _this6 = this;
2415
- for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key2 = 1; _key2 < _len; _key2++) {
2416
- args[_key2 - 1] = arguments[_key2];
2417
- }
2418
- return create(base, function (draft) {
2419
- return arg0.call.apply(arg0, [_this6, draft].concat(args));
2420
- }, arg1);
2421
- };
2422
- }
2423
- var base = arg0;
2424
- var mutate = arg1;
2425
- var options = arg2;
2426
- if (typeof arg1 !== 'function') {
2427
- options = arg1;
2428
- }
2429
- if (options !== undefined && Object.prototype.toString.call(options) !== '[object Object]') {
2430
- throw new Error("Invalid options: ".concat(options, ", 'options' should be an object."));
2431
- }
2432
- options = Object.assign(Object.assign({}, arg), options);
2433
- var state = isDraft(base) ? current(base) : base;
2434
- var mark = Array.isArray(options.mark) ? function (value, types) {
2435
- var _iterator7 = _createForOfIteratorHelper(options.mark),
2436
- _step7;
2437
- try {
2438
- for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
2439
- var _mark = _step7.value;
2440
- if (typeof _mark !== 'function') {
2441
- throw new Error("Invalid mark: ".concat(_mark, ", 'mark' should be a function."));
2442
- }
2443
- var _result = _mark(value, types);
2444
- if (_result) {
2445
- return _result;
2446
- }
2447
- }
2448
- } catch (err) {
2449
- _iterator7.e(err);
2450
- } finally {
2451
- _iterator7.f();
2452
- }
2453
- return;
2454
- } : options.mark;
2455
- var enablePatches = (_a = options.enablePatches) !== null && _a !== undefined ? _a : false;
2456
- var strict = (_b = options.strict) !== null && _b !== undefined ? _b : false;
2457
- var enableAutoFreeze = (_c = options.enableAutoFreeze) !== null && _c !== undefined ? _c : false;
2458
- var _options = {
2459
- enableAutoFreeze: enableAutoFreeze,
2460
- mark: mark,
2461
- strict: strict,
2462
- enablePatches: enablePatches
2463
- };
2464
- if (!isDraftable(state, _options) && _typeof(state) === 'object' && state !== null) {
2465
- throw new Error("Invalid base state: create() only supports plain objects, arrays, Set, Map or using mark() to mark the state as immutable.");
2466
- }
2467
- var _draftify = draftify(state, _options),
2468
- _draftify2 = _slicedToArray(_draftify, 2),
2469
- draft = _draftify2[0],
2470
- finalize = _draftify2[1];
2471
- if (typeof arg1 !== 'function') {
2472
- if (!isDraftable(state, _options)) {
2473
- throw new Error("Invalid base state: create() only supports plain objects, arrays, Set, Map or using mark() to mark the state as immutable.");
2474
- }
2475
- return [draft, finalize];
2476
- }
2477
- var result;
2478
- try {
2479
- result = mutate(draft);
2480
- } catch (error) {
2481
- revokeProxy(getProxyDraft(draft));
2482
- throw error;
2483
- }
2484
- var returnValue = function returnValue(value) {
2485
- var proxyDraft = getProxyDraft(draft);
2486
- if (!isDraft(value)) {
2487
- if (value !== undefined && !isEqual(value, draft) && (proxyDraft === null || proxyDraft === undefined ? undefined : proxyDraft.operated)) {
2488
- throw new Error("Either the value is returned as a new non-draft value, or only the draft is modified without returning any value.");
2489
- }
2490
- var rawReturnValue = value === null || value === undefined ? undefined : value[RAW_RETURN_SYMBOL];
2491
- if (rawReturnValue) {
2492
- var _value = rawReturnValue[0];
2493
- if (_options.strict && _typeof(value) === 'object' && value !== null) {
2494
- handleReturnValue({
2495
- rootDraft: proxyDraft,
2496
- value: value,
2497
- useRawReturn: true
2498
- });
2499
- }
2500
- return finalize([_value]);
2501
- }
2502
- if (value !== undefined) {
2503
- if (_typeof(value) === 'object' && value !== null) {
2504
- handleReturnValue({
2505
- rootDraft: proxyDraft,
2506
- value: value
2507
- });
2508
- }
2509
- return finalize([value]);
2510
- }
2511
- }
2512
- if (value === draft || value === undefined) {
2513
- return finalize([]);
2514
- }
2515
- var returnedProxyDraft = getProxyDraft(value);
2516
- if (_options === returnedProxyDraft.options) {
2517
- if (returnedProxyDraft.operated) {
2518
- throw new Error("Cannot return a modified child draft.");
2519
- }
2520
- return finalize([current(value)]);
2521
- }
2522
- return finalize([value]);
2523
- };
2524
- if (result instanceof Promise) {
2525
- return result.then(returnValue, function (error) {
2526
- revokeProxy(getProxyDraft(draft));
2527
- throw error;
2528
- });
2529
- }
2530
- return returnValue(result);
2531
- };
2532
- };
2533
-
2534
- /**
2535
- * `create(baseState, callback, options)` to create the next state
2536
- *
2537
- * ## Example
2538
- *
2539
- * ```ts
2540
- * import { create } from '../index';
2541
- *
2542
- * const baseState = { foo: { bar: 'str' }, arr: [] };
2543
- * const state = create(
2544
- * baseState,
2545
- * (draft) => {
2546
- * draft.foo.bar = 'str2';
2547
- * },
2548
- * );
2549
- *
2550
- * expect(state).toEqual({ foo: { bar: 'str2' }, arr: [] });
2551
- * expect(state).not.toBe(baseState);
2552
- * expect(state.foo).not.toBe(baseState.foo);
2553
- * expect(state.arr).toBe(baseState.arr);
2554
- * ```
2555
- */
2556
- var create = makeCreator();
2557
- Object.prototype.constructor.toString();
2558
-
2559
- function produceState(state, fn) {
2560
- state.set(create(state.get(), fn));
2561
- }
2562
-
2563
- var RumiousState = /*#__PURE__*/function () {
2564
- function RumiousState(target) {
2565
- var reactor = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
2566
- _classCallCheck(this, RumiousState);
2567
- this.value = target;
2568
- this.reactor = reactor !== null && reactor !== undefined ? reactor : new RumiousReactor(this, []);
2569
- }
2570
- return _createClass(RumiousState, [{
2571
- key: "produce",
2572
- value: function produce(recipe) {
2573
- return produceState(this, recipe);
2574
- }
2575
- }, {
2576
- key: "reducer",
2577
- value: function reducer() {
2578
- var computedObj;
2579
- if (arguments.length === 1) {
2580
- computedObj = new RumiousReducer(this, "", arguments.length <= 0 ? undefined : arguments[0]);
2581
- } else {
2582
- computedObj = new RumiousReducer(this, arguments.length <= 0 ? undefined : arguments[0], arguments.length <= 1 ? undefined : arguments[1]);
2583
- }
2584
- return computedObj.trigger.bind(computedObj);
2585
- }
2586
- }, {
2587
- key: "setObjectByPath",
2588
- value: function setObjectByPath() {
2589
- var path = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
2590
- var value = arguments.length > 1 ? arguments[1] : undefined;
2591
- if (path.length == 0) {
2592
- this.value = value;
2593
- return;
2594
- }
2595
- if (typeof path !== 'string' || !path) {
2596
- throw new Error('Invalid path');
2597
- }
2598
- var keys = path.split('.');
2599
- var obj = this.value;
2600
- for (var i = 0; i < keys.length - 1; i++) {
2601
- var key = keys[i];
2602
- if (!(key in obj)) {
2603
- obj[key] = {};
2604
- }
2605
- obj = obj[key];
2606
- }
2607
- obj[keys[keys.length - 1]] = value;
2608
- }
2609
- }, {
2610
- key: "set",
2611
- value: function set() {
2612
- if (arguments.length === 1) {
2613
- this.value = arguments.length <= 0 ? undefined : arguments[0];
2614
- } else {
2615
- var path = arguments.length <= 0 ? undefined : arguments[0];
2616
- var value = arguments.length <= 1 ? undefined : arguments[1];
2617
- this.setObjectByPath(path, value);
2618
- }
2619
- this.reactor.react();
2620
- }
2621
- }, {
2622
- key: "get",
2623
- value: function get() {
2624
- return this.value;
2625
- }
2626
- }]);
2627
- }();
2628
- function createState(value) {
2629
- return new RumiousState(value);
2630
- }
2631
- function isState(obj) {
2632
- return obj instanceof RumiousState;
2633
- }
2634
-
2635
- function watcher(state, callback) {
2636
- state.reactor.addBinding(callback);
2637
- }
2638
-
2639
- exports.RumiousApp = RumiousApp;
2640
- exports.RumiousComponent = RumiousComponent;
2641
- exports.RumiousComponentElement = RumiousComponentElement;
2642
- exports.RumiousState = RumiousState;
2643
- exports.createComponentElement = createComponentElement;
2644
- exports.createState = createState;
2645
- exports.isComponent = isComponent;
2646
- exports.isState = isState;
2647
- exports.watcher = watcher;
2648
- //# sourceMappingURL=index.cjs.js.map
1
+ "use strict";class e{constructor(e){this.target=e}find(e){return this.target[e]}get(e){return this.target[e]}}class t{constructor(){this.element=null,this.props={},this.renderContext=new e(this),this.renderer=null,this.wrapped=null}prepare(e,t,n={},r=null){this.element=e,this.props=t,this.renderer=r,this.wrapped=n}template(){return{}}requestRender(){let e=this.template(),t=this.renderer(e,document.createDocumentFragment(),this.renderContext);this.element.appendChild(t),this.onRender()}async requestCleanUp(){if(this.element){let e=this.element.cloneNode(!0);for(this.element.replaceWith(e),this.element=e;this.element.firstChild;)this.element.removeChild(this.element.firstChild)}}onInit(){}onCreate(){}onRender(){}onUpdate(){}onDestroy(){}}function n(e){return Object.getPrototypeOf(e)===t}class r{constructor(e,t,n=[]){this.type=e,this.props=t,this.children=n}}class o{constructor(e){this.type="ELEMENT_LIST",this.children=e}forEach(e){this.children.forEach(e)}}function i(e){return"function"==typeof e}class s{constructor(e,t,n){this.type=e,this.name=t,this.value=n}init(){console.warn("Directive haven't handler !")}}class a extends s{async init(e,t){let n=null;const r=e=>{if(!t.find(e))return console.warn(`Missing event handler for event: ${this.name}`),null;const n=t.get(e);if(!i(n))throw`${e} is not a function!`;return n.bind(t.target)};if("string"===this.value.type)n=r(this.value.value);else if("expression"===this.value.type){if(n=this.value.value,!i(n))throw"Event handler requires a function!"}else"dynamic_value"===this.value.type&&(n=r(this.value.value.objectName));n&&e.addEventListener(this.name,n)}}class l extends s{async init(e,t){if("expression"===this.value.type){let t=this.value.value;if(!t)throw"Invalid props value: Directive bind: require a RumiousState Object !";t.reactor.addBinding((()=>{let n=t.get();this.bindAttr(e,n)})),this.bindAttr(e,t.get())}else if("dynamic_value"===this.value.type){let n=this.value.evaluator?.(t.target),r=t.find(this.value.value.objectName);if(!r)throw"Invalid props value: Directive bind: require a RumiousState Object !";r.reactor.addBinding((()=>{let n=this.value.evaluator?.(t.target);this.bindAttr(e,n)})),this.bindAttr(e,n)}}normalizeValue(e){return"object"==typeof e?e.toString?.()??"":e}async bindAttr(e,t){if("html"===this.name)e.innerHTML=this.normalizeValue(t)}}class c extends s{async init(e,t){if("expression"===this.value.type){let t=this.value.value;if(!t)throw"Invalid props value: Directive ref: require a RumiousRef Object !";t.set(e)}else if("dynamic_value"===this.value.type){let n=this.value.evaluator?.(t.target);if(!n)throw"Invalid props value: Directive ref: require a RumiousRef Object !";n.set(e)}}}class u extends s{async init(e,t){if("expression"===this.value.type){let t=this.value.value;if(!t)throw"Invalid props value: Directive childsRef: require a RumiousChildRef Object !";t.set(e)}else if("dynamic_value"===this.value.type){let n=this.value.evaluator?.(t.target);if(!n)throw"Invalid props value: Directive childsRef: require a RumiousChildRef Object !";n.set(e)}}}const p={on:(e,t)=>new a("on",e,t),bind:(e,t)=>new l("bind",e,t),ref:(e,t,n="element")=>new c("ref",e,t),childsRef:(e,t)=>new u("childsRef",e,t)};function f(...e){return new r("FRAGMENT",{},h(e))}function h(e){return e.map((e=>"object"==typeof e?e:new r("TEXT_ELEMENT",{nodeValue:e},[])))}window.RUMIOUS_JSX_SUPPORT={createElement:function(e,t,...i){return n(e)?function(e,t,n){return new r("COMPONENT",{component:e,...t},new o(h(n)))}(e,t,i):e===f?f(...i):new r(e,t||{},h(i))},createFragment:f,createDirective:function(e,t,n){return function(e,t,n){return p[e]?.(t,n)}(e,t,n)}};class d extends HTMLElement{constructor(){super(),this.instance=null,this.ref=null}setRef(e){this.ref=e}init(e,t,n={},r){this.instance=new e,this.instance.prepare(this,t,n,r),this.instance.onInit()}connectedCallback(){this.instance.onCreate(),this.instance.requestRender(),this.instance.forwardRef=this.ref??{}}disconnectCallback(){this.instance.onDestroy(),this.instance.requestCleanUp()}}function y(){return document.createElement("a-component")}function g(e,t,n,r){const o=function(e,t,n,r){const o=y();return o.init(e,t,n,r),o}(e.props.component,e.props,e.children,n);return Object.entries(e.props||{}).forEach((([e,t])=>{t instanceof s&&m(o,t,r)})),t.appendChild(o),t}function v(e,t){const n=document.createElement(e.type);return Object.entries(e.props||{}).forEach((([e,r])=>{e.startsWith("on")&&/^[a-zA-Z]+([A-Z][a-z]*)*$/.test(e)?n.addEventListener(e.substring(2).toLowerCase(),r):function(e,t,n,r){e.nodeType===Node.TEXT_NODE?e.nodeValue=n:n instanceof s?m(e,n,r):e.setAttribute(t,n)}(n,e,r,t)})),n}function m(e,t,n){t.init(e,n)}function b(e,t,n={}){if(!e)return t;let r;return"COMPONENT"===e.type?g(e,t,b,n):"FRAGMENT"===e.type||"ELEMENT_LIST"===e.type?function(e,t,n){return e.children.forEach((e=>b(e,t,n))),t}(e,t,n):(r="TEXT_ELEMENT"===e.type?function(e){return document.createTextNode(e.props.nodeValue)}(e):v(e,n),e.children.forEach((e=>b(e,r,n))),t.appendChild(r),t)}window.customElements.define("a-component",d);class w{constructor(e=null,t=[]){this.target=e,this.bindings=t}react(){this.bindings.forEach((e=>e()))}addBinding(e){this.bindings.push(e)}}class O{constructor(e,t="",n){this.state=e,this.path=t,this.fn=n}trigger(...e){let t=this.fn(...e);this.state.set(this.path,t)}}const M="remove",E="replace",j="add",S=Symbol.for("__MUTATIVE_PROXY_DRAFT__"),k=Symbol("__MUTATIVE_RAW_RETURN_SYMBOL__"),R=Symbol.iterator,x={mutable:"mutable",immutable:"immutable"},A={};function P(e,t){return e instanceof Map?e.has(t):Object.prototype.hasOwnProperty.call(e,t)}function C(e,t){if(t in e){let n=Reflect.getPrototypeOf(e);for(;n;){const e=Reflect.getOwnPropertyDescriptor(n,t);if(e)return e;n=Reflect.getPrototypeOf(n)}}}function z(e){return Object.getPrototypeOf(e)===Set.prototype}function D(e){return Object.getPrototypeOf(e)===Map.prototype}function T(e){var t;return null!==(t=e.copy)&&void 0!==t?t:e.original}function _(e){return!!I(e)}function I(e){return"object"!=typeof e?null:null==e?void 0:e[S]}function N(e){var t;const n=I(e);return n?null!==(t=n.copy)&&void 0!==t?t:n.original:e}function F(e,t){if(!e||"object"!=typeof e)return!1;let n;return Object.getPrototypeOf(e)===Object.prototype||Array.isArray(e)||e instanceof Map||e instanceof Set||!!(null==t?void 0:t.mark)&&((n=t.mark(e,x))===x.immutable||"function"==typeof n)}function V(e,t=[]){if(Object.hasOwnProperty.call(e,"key")){const n=e.parent.copy,r=I(q(n,e.key));if(null!==r&&(null==r?void 0:r.original)!==e.original)return null;const o=3===e.parent.type,i=o?Array.from(e.parent.setMap.keys()).indexOf(e.key):e.key;if(!(o&&n.size>i||P(n,i)))return null;t.push(i)}if(e.parent)return V(e.parent,t);t.reverse();try{!function(e,t){for(let n=0;n<t.length-1;n+=1){const r=t[n];if("object"!=typeof(e=q(3===L(e)?Array.from(e):e,r)))throw new Error(`Cannot resolve patch at '${t.join("/")}'.`)}}(e.copy,t)}catch(e){return null}return t}function L(e){return Array.isArray(e)?1:e instanceof Map?2:e instanceof Set?3:0}function q(e,t){return 2===L(e)?e.get(t):e[t]}function $(e,t,n){2===L(e)?e.set(t,n):e[t]=n}function U(e,t){const n=I(e);return(n?T(n):e)[t]}function W(e,t){return e===t?0!==e||1/e==1/t:e!=e&&t!=t}function B(e){if(e)for(;e.finalities.revoke.length>0;){e.finalities.revoke.pop()()}}function K(e,t){return t?e:[""].concat(e).map((e=>{const t=`${e}`;return-1===t.indexOf("/")&&-1===t.indexOf("~")?t:t.replace(/~/g,"~0").replace(/\//g,"~1")})).join("/")}const X=Object.prototype.propertyIsEnumerable;function H(e,t){let n;if(Array.isArray(e))return Array.prototype.concat.call(e);if(e instanceof Set){if(!z(e)){return new(0,Object.getPrototypeOf(e).constructor)(e.values())}return Set.prototype.difference?Set.prototype.difference.call(e,new Set):new Set(e.values())}if(e instanceof Map){if(!D(e)){return new(0,Object.getPrototypeOf(e).constructor)(e)}return new Map(e)}if((null==t?void 0:t.mark)&&(n=t.mark(e,x),void 0!==n)&&n!==x.mutable){if(n===x.immutable)return function(e){const t=Object.create(Object.getPrototypeOf(e));return Reflect.ownKeys(e).forEach((n=>{let r=Reflect.getOwnPropertyDescriptor(e,n);r.enumerable&&r.configurable&&r.writable?t[n]=e[n]:(r.writable||(r.writable=!0,r.configurable=!0),(r.get||r.set)&&(r={configurable:!0,writable:!0,enumerable:r.enumerable,value:e[n]}),Reflect.defineProperty(t,n,r))})),t}(e);if("function"==typeof n){if(t.enablePatches||t.enableAutoFreeze)throw new Error("You can't use mark and patches or auto freeze together.");return n()}throw new Error(`Unsupported mark result: ${n}`)}if("object"==typeof e&&Object.getPrototypeOf(e)===Object.prototype){const t={};return Object.keys(e).forEach((n=>{t[n]=e[n]})),Object.getOwnPropertySymbols(e).forEach((n=>{X.call(e,n)&&(t[n]=e[n])})),t}throw new Error("Please check mark() to ensure that it is a stable marker draftable function.")}function Y(e){e.copy||(e.copy=H(e.original,e.options))}function G(e){if(!F(e))return N(e);if(Array.isArray(e))return e.map(G);if(e instanceof Map){const t=Array.from(e.entries()).map((([e,t])=>[e,G(t)]));if(!D(e)){return new(0,Object.getPrototypeOf(e).constructor)(t)}return new Map(t)}if(e instanceof Set){const t=Array.from(e).map(G);if(!z(e)){return new(0,Object.getPrototypeOf(e).constructor)(t)}return new Set(t)}const t=Object.create(Object.getPrototypeOf(e));for(const n in e)t[n]=G(e[n]);return t}function Z(e){return _(e)?G(e):e}function J(e){var t;e.assignedMap=null!==(t=e.assignedMap)&&void 0!==t?t:new Map,e.operated||(e.operated=!0,e.parent&&J(e.parent))}function Q(){throw new Error("Cannot modify frozen object")}function ee(e,t,n,r,o){{n=null!=n?n:new WeakMap,r=null!=r?r:[],o=null!=o?o:[];const i=n.has(e)?n.get(e):e;if(r.length>0){const e=r.indexOf(i);if(i&&"object"==typeof i&&-1!==e){if(r[0]===i)throw new Error("Forbids circular reference");throw new Error(`Forbids circular reference: ~/${o.slice(0,e).map(((e,t)=>{if("symbol"==typeof e)return`[${e.toString()}]`;const n=r[t];return"object"==typeof e&&(n instanceof Map||n instanceof Set)?Array.from(n.keys()).indexOf(e):e})).join("/")}`)}r.push(i),o.push(t)}else r.push(i)}if(Object.isFrozen(e)||_(e))return r.pop(),void o.pop();switch(L(e)){case 2:for(const[t,i]of e)ee(t,t,n,r,o),ee(i,t,n,r,o);e.set=e.clear=e.delete=Q;break;case 3:for(const t of e)ee(t,t,n,r,o);e.add=e.clear=e.delete=Q;break;case 1:Object.freeze(e);let t=0;for(const i of e)ee(i,t,n,r,o),t+=1;break;default:Object.freeze(e),Object.keys(e).forEach((t=>{ee(e[t],t,n,r,o)}))}r.pop(),o.pop()}function te(e,t){const n=L(e);if(0===n)Reflect.ownKeys(e).forEach((n=>{t(n,e[n],e)}));else if(1===n){let n=0;for(const r of e)t(n,r,e),n+=1}else e.forEach(((n,r)=>t(r,n,e)))}function ne(e,t,n){if(_(e)||!F(e,n)||t.has(e)||Object.isFrozen(e))return;const r=e instanceof Set,o=r?new Map:void 0;if(t.add(e),te(e,((i,s)=>{var a;if(_(s)){const t=I(s);Y(t);const n=(null===(a=t.assignedMap)||void 0===a?void 0:a.size)||t.operated?t.copy:t.original;$(r?o:e,i,n)}else ne(s,t,n)})),o){const t=e,n=Array.from(t);t.clear(),n.forEach((e=>{t.add(o.has(e)?o.get(e):e)}))}}function re(e,t){const n=3===e.type?e.setMap:e.copy;e.finalities.revoke.length>1&&e.assignedMap.get(t)&&n&&ne(q(n,t),e.finalities.handledSet,e.options)}function oe(e){3===e.type&&e.copy&&(e.copy.clear(),e.setMap.forEach((t=>{e.copy.add(N(t))})))}function ie(e,t,n,r){if(e.operated&&e.assignedMap&&e.assignedMap.size>0&&!e.finalized){if(n&&r){const o=V(e);o&&t(e,o,n,r)}e.finalized=!0}}function se(e,t,n,r){const o=I(n);o&&(o.callbacks||(o.callbacks=[]),o.callbacks.push(((i,s)=>{var a;const l=3===e.type?e.setMap:e.copy;if(W(q(l,t),n)){let n=o.original;o.copy&&(n=o.copy),oe(e),ie(e,r,i,s),e.options.enableAutoFreeze&&(e.options.updatedValues=null!==(a=e.options.updatedValues)&&void 0!==a?a:new WeakMap,e.options.updatedValues.set(n,o.original)),$(l,t,n)}})),e.options.enableAutoFreeze&&o.finalities!==e.finalities&&(e.options.enableAutoFreeze=!1)),F(n,e.options)&&e.finalities.draft.push((()=>{W(q(3===e.type?e.setMap:e.copy,t),n)&&re(e,t)}))}function ae(e,t,n,r){const{pathAsArray:o=!0}=e.options.enablePatches;switch(e.type){case 0:case 2:return function({original:e,copy:t,assignedMap:n},r,o,i,s){n.forEach(((n,a)=>{const l=q(e,a),c=Z(q(t,a)),u=n?P(e,a)?E:j:M;if(W(l,c)&&u===E)return;const p=K(r.concat(a),s);o.push(u===M?{op:u,path:p}:{op:u,path:p,value:c}),i.push(u===j?{op:M,path:p}:u===M?{op:j,path:p,value:l}:{op:E,path:p,value:l})}))}(e,t,n,r,o);case 1:return function(e,t,n,r,o){let{original:i,assignedMap:s,options:a}=e,l=e.copy;l.length<i.length&&([i,l]=[l,i],[n,r]=[r,n]);for(let e=0;e<i.length;e+=1)if(s.get(e.toString())&&l[e]!==i[e]){const s=K(t.concat([e]),o);n.push({op:E,path:s,value:Z(l[e])}),r.push({op:E,path:s,value:Z(i[e])})}for(let e=i.length;e<l.length;e+=1){const r=K(t.concat([e]),o);n.push({op:j,path:r,value:Z(l[e])})}if(i.length<l.length){const{arrayLengthAssignment:e=!0}=a.enablePatches;if(e){const e=K(t.concat(["length"]),o);r.push({op:E,path:e,value:i.length})}else for(let e=l.length;i.length<e;e-=1){const n=K(t.concat([e-1]),o);r.push({op:M,path:n})}}}(e,t,n,r,o);case 3:return function({original:e,copy:t},n,r,o,i){let s=0;e.forEach((e=>{if(!t.has(e)){const t=K(n.concat([s]),i);r.push({op:M,path:t,value:e}),o.unshift({op:j,path:t,value:e})}s+=1})),s=0,t.forEach((t=>{if(!e.has(t)){const e=K(n.concat([s]),i);r.push({op:j,path:e,value:t}),o.unshift({op:M,path:e,value:t})}s+=1}))}(e,t,n,r,o)}}const le=(e,t,n=!1)=>{if("object"==typeof e&&null!==e&&(!F(e,t)||n))throw new Error("Strict mode: Mutable data cannot be accessed directly, please use 'unsafe(callback)' wrap.")},ce={get size(){return T(I(this)).size},has(e){return T(I(this)).has(e)},set(e,t){const n=I(this),r=T(n);return r.has(e)&&W(r.get(e),t)||(Y(n),J(n),n.assignedMap.set(e,!0),n.copy.set(e,t),se(n,e,t,ae)),this},delete(e){if(!this.has(e))return!1;const t=I(this);return Y(t),J(t),t.original.has(e)?t.assignedMap.set(e,!1):t.assignedMap.delete(e),t.copy.delete(e),!0},clear(){const e=I(this);if(this.size){Y(e),J(e),e.assignedMap=new Map;for(const[t]of e.original)e.assignedMap.set(t,!1);e.copy.clear()}},forEach(e,t){T(I(this)).forEach(((n,r)=>{e.call(t,this.get(r),r,this)}))},get(e){var t,n;const r=I(this),o=T(r).get(e),i=(null===(n=(t=r.options).mark)||void 0===n?void 0:n.call(t,o,x))===x.mutable;if(r.options.strict&&le(o,r.options,i),i)return o;if(r.finalized||!F(o,r.options))return o;if(o!==r.original.get(e))return o;const s=A.createDraft({original:o,parentDraft:r,key:e,finalities:r.finalities,options:r.options});return Y(r),r.copy.set(e,s),s},keys(){return T(I(this)).keys()},values(){const e=this.keys();return{[R]:()=>this.values(),next:()=>{const t=e.next();if(t.done)return t;return{done:!1,value:this.get(t.value)}}}},entries(){const e=this.keys();return{[R]:()=>this.entries(),next:()=>{const t=e.next();if(t.done)return t;const n=this.get(t.value);return{done:!1,value:[t.value,n]}}}},[R](){return this.entries()}},ue=Reflect.ownKeys(ce),pe=(e,t,{isValuesIterator:n})=>()=>{var r,o;const i=t.next();if(i.done)return i;const s=i.value;let a=e.setMap.get(s);const l=I(a),c=(null===(o=(r=e.options).mark)||void 0===o?void 0:o.call(r,a,x))===x.mutable;if(e.options.strict&&le(s,e.options,c),c||l||!F(s,e.options)||e.finalized||!e.original.has(s))l&&(a=l.proxy);else{const t=A.createDraft({original:s,parentDraft:e,key:s,finalities:e.finalities,options:e.options});e.setMap.set(s,t),a=t}return{done:!1,value:n?a:[a,a]}},fe={get size(){return I(this).setMap.size},has(e){const t=I(this);if(t.setMap.has(e))return!0;Y(t);const n=I(e);return!(!n||!t.setMap.has(n.original))},add(e){const t=I(this);return this.has(e)||(Y(t),J(t),t.assignedMap.set(e,!0),t.setMap.set(e,e),se(t,e,e,ae)),this},delete(e){if(!this.has(e))return!1;const t=I(this);Y(t),J(t);const n=I(e);return n&&t.setMap.has(n.original)?(t.assignedMap.set(n.original,!1),t.setMap.delete(n.original)):(!n&&t.setMap.has(e)?t.assignedMap.set(e,!1):t.assignedMap.delete(e),t.setMap.delete(e))},clear(){if(!this.size)return;const e=I(this);Y(e),J(e);for(const t of e.original)e.assignedMap.set(t,!1);e.setMap.clear()},values(){const e=I(this);Y(e);const t=e.setMap.keys();return{[Symbol.iterator]:()=>this.values(),next:pe(e,t,{isValuesIterator:!0})}},entries(){const e=I(this);Y(e);const t=e.setMap.keys();return{[Symbol.iterator]:()=>this.entries(),next:pe(e,t,{isValuesIterator:!1})}},keys(){return this.values()},[R](){return this.values()},forEach(e,t){const n=this.values();let r=n.next();for(;!r.done;)e.call(t,r.value,r.value,this),r=n.next()}};Set.prototype.difference&&Object.assign(fe,{intersection(e){return Set.prototype.intersection.call(new Set(this.values()),e)},union(e){return Set.prototype.union.call(new Set(this.values()),e)},difference(e){return Set.prototype.difference.call(new Set(this.values()),e)},symmetricDifference(e){return Set.prototype.symmetricDifference.call(new Set(this.values()),e)},isSubsetOf(e){return Set.prototype.isSubsetOf.call(new Set(this.values()),e)},isSupersetOf(e){return Set.prototype.isSupersetOf.call(new Set(this.values()),e)},isDisjointFrom(e){return Set.prototype.isDisjointFrom.call(new Set(this.values()),e)}});const he=Reflect.ownKeys(fe),de=new WeakSet,ye={get(e,t,n){var r,o;const i=null===(r=e.copy)||void 0===r?void 0:r[t];if(i&&de.has(i))return i;if(t===S)return e;let s;if(e.options.mark){const r="size"===t&&(e.original instanceof Map||e.original instanceof Set)?Reflect.get(e.original,t):Reflect.get(e.original,t,n);if(s=e.options.mark(r,x),s===x.mutable)return e.options.strict&&le(r,e.options,!0),r}const a=T(e);if(a instanceof Map&&ue.includes(t)){if("size"===t)return Object.getOwnPropertyDescriptor(ce,"size").get.call(e.proxy);const n=ce[t];if(n)return n.bind(e.proxy)}if(a instanceof Set&&he.includes(t)){if("size"===t)return Object.getOwnPropertyDescriptor(fe,"size").get.call(e.proxy);const n=fe[t];if(n)return n.bind(e.proxy)}if(!P(a,t)){const n=C(a,t);return n?"value"in n?n.value:null===(o=n.get)||void 0===o?void 0:o.call(e.proxy):void 0}const l=a[t];if(e.options.strict&&le(l,e.options),e.finalized||!F(l,e.options))return l;if(l===U(e.original,t)){if(Y(e),e.copy[t]=ge({original:e.original[t],parentDraft:e,key:1===e.type?Number(t):t,finalities:e.finalities,options:e.options}),"function"==typeof s){const n=I(e.copy[t]);return Y(n),J(n),n.copy}return e.copy[t]}return l},set(e,t,n){var r;if(3===e.type||2===e.type)throw new Error("Map/Set draft does not support any property assignment.");let o;if(1===e.type&&"length"!==t&&(!(Number.isInteger(o=Number(t))&&o>=0)||0!==t&&0!==o&&String(o)!==String(t)))throw new Error("Only supports setting array indices and the 'length' property.");const i=C(T(e),t);if(null==i?void 0:i.set)return i.set.call(e.proxy,n),!0;const s=U(T(e),t),a=I(s);return a&&W(a.original,n)?(e.copy[t]=n,e.assignedMap=null!==(r=e.assignedMap)&&void 0!==r?r:new Map,e.assignedMap.set(t,!1),!0):(W(n,s)&&(void 0!==n||P(e.original,t))||(Y(e),J(e),P(e.original,t)&&W(n,e.original[t])?e.assignedMap.delete(t):e.assignedMap.set(t,!0),e.copy[t]=n,se(e,t,n,ae)),!0)},has:(e,t)=>t in T(e),ownKeys:e=>Reflect.ownKeys(T(e)),getOwnPropertyDescriptor(e,t){const n=T(e),r=Reflect.getOwnPropertyDescriptor(n,t);return r?{writable:!0,configurable:1!==e.type||"length"!==t,enumerable:r.enumerable,value:n[t]}:r},getPrototypeOf:e=>Reflect.getPrototypeOf(e.original),setPrototypeOf(){throw new Error("Cannot call 'setPrototypeOf()' on drafts")},defineProperty(){throw new Error("Cannot call 'defineProperty()' on drafts")},deleteProperty(e,t){var n;return 1===e.type?ye.set.call(this,e,t,void 0,e.proxy):(void 0!==U(e.original,t)||t in e.original?(Y(e),J(e),e.assignedMap.set(t,!1)):(e.assignedMap=null!==(n=e.assignedMap)&&void 0!==n?n:new Map,e.assignedMap.delete(t)),e.copy&&delete e.copy[t],!0)}};function ge(e){const{original:t,parentDraft:n,key:r,finalities:o,options:i}=e,s=L(t),a={type:s,finalized:!1,parent:n,original:t,copy:null,proxy:null,finalities:o,options:i,setMap:3===s?new Map(t.entries()):void 0};(r||"key"in e)&&(a.key=r);const{proxy:l,revoke:c}=Proxy.revocable(1===s?Object.assign([],a):a,ye);if(o.revoke.push(c),de.add(l),a.proxy=l,n){const e=n;e.finalities.draft.push(((t,n)=>{var o,i;const s=I(l);let a=3===e.type?e.setMap:e.copy;const c=q(a,r),u=I(c);if(u){let i=u.original;u.operated&&(i=N(c)),oe(u),ie(u,ae,t,n),e.options.enableAutoFreeze&&(e.options.updatedValues=null!==(o=e.options.updatedValues)&&void 0!==o?o:new WeakMap,e.options.updatedValues.set(i,u.original)),$(a,r,i)}null===(i=s.callbacks)||void 0===i||i.forEach((e=>{e(t,n)}))}))}else{const e=I(l);e.finalities.draft.push(((t,n)=>{oe(e),ie(e,ae,t,n)}))}return l}function ve(e,t,n,r,o){var i;const s=I(e),a=null!==(i=null==s?void 0:s.original)&&void 0!==i?i:e,l=!!t.length;if(null==s?void 0:s.operated)for(;s.finalities.draft.length>0;){s.finalities.draft.pop()(n,r)}const c=l?t[0]:s?s.operated?s.copy:s.original:e;return s&&B(s),o&&ee(c,c,null==s?void 0:s.options.updatedValues),[c,n&&l?[{op:E,path:[],value:t[0]}]:n,r&&l?[{op:E,path:[],value:a}]:r]}function me(e){const{rootDraft:t,value:n,useRawReturn:r=!1,isRoot:o=!0}=e;te(n,((n,r,o)=>{const i=I(r);if(i&&t&&i.finalities===t.finalities){e.isContainDraft=!0;const t=i.original;if(o instanceof Set){const e=Array.from(o);o.clear(),e.forEach((e=>o.add(n===e?t:e)))}else $(o,n,t)}else"object"==typeof r&&null!==r&&(e.value=r,e.isRoot=!1,me(e))})),o&&(e.isContainDraft||console.warn("The return value does not contain any draft, please use 'rawReturn()' to wrap the return value to improve performance."),r&&console.warn("The return value contains drafts, please don't use 'rawReturn()' to wrap the return value."))}function be(e){var t;const n=I(e);if(!F(e,null==n?void 0:n.options))return e;const r=L(e);if(n&&!n.operated)return n.original;let o;function i(){o=2===r?D(e)?new Map(e):new(Object.getPrototypeOf(e).constructor)(e):3===r?Array.from(n.setMap.values()):H(e,null==n?void 0:n.options)}if(n){n.finalized=!0;try{i()}finally{n.finalized=!1}}else o=e;if(te(o,((t,r)=>{if(n&&W(q(n.original,t),r))return;const s=be(r);s!==r&&(o===e&&i(),$(o,t,s))})),3===r){const e=null!==(t=null==n?void 0:n.original)&&void 0!==t?t:o;return z(e)?new Set(o):new(Object.getPrototypeOf(e).constructor)(o)}return o}function we(e){if(!_(e))throw new Error(`current() is only used for Draft, parameter: ${e}`);return be(e)}A.createDraft=ge;const Oe=function e(t,n,r){var o,i,s;if("function"==typeof t&&"function"!=typeof n)return function(r,...o){return e(r,(e=>t.call(this,e,...o)),n)};const a=t,l=n;let c=r;if("function"!=typeof n&&(c=n),void 0!==c&&"[object Object]"!==Object.prototype.toString.call(c))throw new Error(`Invalid options: ${c}, 'options' should be an object.`);c=Object.assign(Object.assign({},Me),c);const u=_(a)?we(a):a,p=Array.isArray(c.mark)?(e,t)=>{for(const n of c.mark){if("function"!=typeof n)throw new Error(`Invalid mark: ${n}, 'mark' should be a function.`);const r=n(e,t);if(r)return r}}:c.mark,f=null!==(o=c.enablePatches)&&void 0!==o&&o,h=null!==(i=c.strict)&&void 0!==i&&i,d={enableAutoFreeze:null!==(s=c.enableAutoFreeze)&&void 0!==s&&s,mark:p,strict:h,enablePatches:f};if(!F(u,d)&&"object"==typeof u&&null!==u)throw new Error("Invalid base state: create() only supports plain objects, arrays, Set, Map or using mark() to mark the state as immutable.");const[y,g]=function(e,t){var n;const r={draft:[],revoke:[],handledSet:new WeakSet};let o,i;t.enablePatches&&(o=[],i=[]);const s=(null===(n=t.mark)||void 0===n?void 0:n.call(t,e,x))!==x.mutable&&F(e,t)?ge({original:e,parentDraft:null,finalities:r,options:t}):e;return[s,(e=[])=>{const[n,r,a]=ve(s,e,o,i,t.enableAutoFreeze);return t.enablePatches?[n,r,a]:n}]}(u,d);if("function"!=typeof n){if(!F(u,d))throw new Error("Invalid base state: create() only supports plain objects, arrays, Set, Map or using mark() to mark the state as immutable.");return[y,g]}let v;try{v=l(y)}catch(e){throw B(I(y)),e}const m=e=>{const t=I(y);if(!_(e)){if(void 0!==e&&!W(e,y)&&(null==t?void 0:t.operated))throw new Error("Either the value is returned as a new non-draft value, or only the draft is modified without returning any value.");const n=null==e?void 0:e[k];if(n){const r=n[0];return d.strict&&"object"==typeof e&&null!==e&&me({rootDraft:t,value:e,useRawReturn:!0}),g([r])}if(void 0!==e)return"object"==typeof e&&null!==e&&me({rootDraft:t,value:e}),g([e])}if(e===y||void 0===e)return g([]);const n=I(e);if(d===n.options){if(n.operated)throw new Error("Cannot return a modified child draft.");return g([we(e)])}return g([e])};return v instanceof Promise?v.then(m,(e=>{throw B(I(y)),e})):m(v)};var Me;Object.prototype.constructor.toString();class Ee{constructor(e,t=null){this.value=e,this.reactor=t??new w(this,[])}produce(e){return n=e,void(t=this).set(Oe(t.get(),n));var t,n}reducer(...e){let t;return t=1===e.length?new O(this,"",e[0]):new O(this,e[0],e[1]),t.trigger.bind(t)}setObjectByPath(e="",t){if(0==e.length)return void(this.value=t);if("string"!=typeof e||!e)throw new Error("Invalid path");let n=e.split("."),r=this.value;for(let e=0;e<n.length-1;e++){let t=n[e];t in r||(r[t]={}),r=r[t]}r[n[n.length-1]]=t}set(...e){if(1===e.length)this.value=e[0];else{let t=e[0],n=e[1];this.setObjectByPath(t,n)}this.reactor.react()}get(){return this.value}}class je{constructor(e){this.target=e}set html(e){this.target.innerHTML=e}query(e){return this.target.querySelectorAll(e)}set text(e){this.target.textContent=e}on(e,t){this.target.addEventListener(e,t)}off(e,t){this.target.removeEventListener(e,t)}remove(){this.target.remove()}addChild(e){this.target.appendChild(e)}set(e){this.target=e}}class Se{constructor(e){this.target=e}query(e){return this.target.querySelectorAll(e)}index(e){return Array.from(this.target.children)[e]}list(){return Array.from(this.target.children)}get parent(){return this.target}remove(){this.target.remove()}addChild(e){this.target.appendChild(e)}set(e){this.target=e}}class ke{constructor(e){this.target=e}set(e){this.target=e,e.setRef(this)}}exports.RumiousApp=class{constructor(t=document.createElement("span"),n={}){this.root=t,this.configs=n,this.renderContext=new e(this)}render(e){b(e,this.root,this.renderContext)}},exports.RumiousChildrensRef=Se,exports.RumiousComponent=t,exports.RumiousComponentElement=d,exports.RumiousComponentRef=ke,exports.RumiousElementRef=je,exports.RumiousState=Ee,exports.createChildrensRef=function(e){return new Se(e)},exports.createComponentElement=y,exports.createComponentRef=function(e){return new ke(e)},exports.createElementRef=function(e){return new je(e)},exports.createState=function(e){return new Ee(e)},exports.isComponent=n,exports.isState=function(e){return e instanceof Ee},exports.watcher=function(e,t){e.reactor.addBinding(t)};