rumious 0.0.13-beta.2 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/README.md +7 -0
  2. package/dist/app/app.d.ts +13 -0
  3. package/dist/app/index.d.ts +2 -0
  4. package/dist/app/module.d.ts +6 -0
  5. package/dist/component/component.d.ts +29 -0
  6. package/dist/component/element.d.ts +17 -0
  7. package/dist/component/index.d.ts +2 -0
  8. package/dist/context/context.d.ts +12 -0
  9. package/dist/context/index.d.ts +1 -0
  10. package/dist/index.cjs +1100 -0
  11. package/dist/index.d.ts +29 -0
  12. package/dist/index.esm.js +1072 -0
  13. package/dist/index.global.d.ts +41 -0
  14. package/dist/index.min.js +2658 -1
  15. package/dist/jsx/index.d.ts +3 -0
  16. package/dist/performance/index.d.ts +5 -0
  17. package/dist/ref/children.d.ts +23 -0
  18. package/dist/ref/element.d.ts +49 -0
  19. package/dist/ref/index.d.ts +2 -0
  20. package/dist/render/array.d.ts +20 -0
  21. package/dist/render/context.d.ts +11 -0
  22. package/dist/render/directives.d.ts +1 -0
  23. package/dist/render/dynamic.d.ts +2 -0
  24. package/dist/render/index.d.ts +3 -0
  25. package/dist/render/injector.d.ts +14 -0
  26. package/dist/render/render.d.ts +3 -0
  27. package/dist/render/template.d.ts +5 -0
  28. package/dist/state/array.d.ts +21 -0
  29. package/dist/state/index.d.ts +3 -0
  30. package/dist/state/object.d.ts +31 -0
  31. package/dist/state/reactor.d.ts +19 -0
  32. package/dist/state/state.d.ts +15 -0
  33. package/dist/types/jsx.d.ts +8 -0
  34. package/dist/types/render.d.ts +5 -0
  35. package/dist/types/utils.d.ts +1 -0
  36. package/dist/utils/checkers.d.ts +1 -0
  37. package/dist/utils/name.d.ts +2 -0
  38. package/dist/utils/oberve.d.ts +1 -0
  39. package/package.json +49 -31
  40. package/dist/index.cjs.min.js +0 -1
  41. package/dist/index.esm.min.js +0 -1
  42. package/index.js +0 -3
@@ -0,0 +1,1072 @@
1
+ import { create } from 'mutative';
2
+
3
+ class RumiousRenderContext {
4
+ constructor(target, app) {
5
+ this.target = target;
6
+ this.app = app;
7
+ this.onRenderFinished = [];
8
+ }
9
+ findName(name) {
10
+ return this.target[name];
11
+ }
12
+ }
13
+
14
+ function render(context, template, target) {
15
+ let generator = template.generator.bind(context.target);
16
+ context.renderHelper = render;
17
+ generator(target, context);
18
+ setTimeout(async () => {
19
+ const callbacks = [...context.onRenderFinished];
20
+ context.onRenderFinished = [];
21
+ for (const callback of callbacks) {
22
+ await callback();
23
+ }
24
+ }, 0);
25
+ }
26
+
27
+ class RumiousComponent {
28
+ static classNames = "";
29
+ static tagName = "rumious-component";
30
+ constructor() {
31
+ this.renderOptions = {
32
+ mode: "idle"
33
+ };
34
+ }
35
+ onCreate() {}
36
+ onRender() {}
37
+ onDestroy() {}
38
+ async onBeforeRender() {}
39
+ prepare(currentContext, props) {
40
+ this.props = props;
41
+ this.context = new RumiousRenderContext(this, currentContext.app);
42
+ }
43
+ async requestRender() {
44
+ await this.onBeforeRender();
45
+ let template = this.template();
46
+ render(this.context, template, this.element);
47
+ this.onRender();
48
+ }
49
+ requestCleanup() {}
50
+ }
51
+
52
+ class RumiousComponentElement extends HTMLElement {
53
+ constructor() {
54
+ super();
55
+ this.props = {};
56
+ }
57
+ setup(context, componentConstructor) {
58
+ this.context = context;
59
+ this.componentConstructor = componentConstructor;
60
+ }
61
+ connectedCallback() {
62
+ if (!this.componentConstructor) {
63
+ console.warn("Rumious: Cannot find matching component constructor.");
64
+ return;
65
+ }
66
+ this.componentInstance = new this.componentConstructor();
67
+ this.componentInstance.element = this;
68
+ if (this.componentConstructor.classNames) {
69
+ this.className = this.componentConstructor.classNames;
70
+ }
71
+ this.componentInstance.prepare(this.context, this.props);
72
+ this.componentInstance.onCreate();
73
+ this.componentInstance.requestRender();
74
+ }
75
+ disconnectedCallback() {
76
+ this.componentInstance?.onDestroy();
77
+ }
78
+ }
79
+
80
+ class RumiousApp {
81
+ constructor(root, options = {}) {
82
+ this.root = root;
83
+ this.options = options;
84
+ this.modules = [];
85
+ this.context = new RumiousRenderContext(this, this);
86
+ }
87
+ addModule(module, options) {
88
+ let instance = module.init(this, options);
89
+ this.modules.push(instance);
90
+ return instance;
91
+ }
92
+ render(template) {
93
+ render(this.context, template, this.root);
94
+ }
95
+ }
96
+ function createApp(root, options) {
97
+ return new RumiousApp(root, options);
98
+ }
99
+
100
+ class RumiousRenderTemplate {
101
+ constructor(generator) {
102
+ this.generator = generator;
103
+ }
104
+ }
105
+
106
+ class RumiousDymanicInjector {
107
+ constructor(contents) {
108
+ this.contents = contents;
109
+ this.targets = new Map();
110
+ if (!contents || contents.length === 0) {
111
+ throw new Error("Injector must be initialized with non-empty content");
112
+ }
113
+ const first = contents[0];
114
+ this.type = typeof first === "string" ? "string" : "element";
115
+ }
116
+ addTarget(element) {
117
+ this.targets.set(element, 1);
118
+ }
119
+ inject(element) {
120
+ if (!this.targets.has(element)) return;
121
+ if (!this.contents) return;
122
+ element.innerHTML = "";
123
+ if (this.type === "string") {
124
+ for (const content of this.contents) {
125
+ element.insertAdjacentHTML("beforeend", content);
126
+ }
127
+ } else {
128
+ for (const content of this.contents) {
129
+ element.appendChild(content);
130
+ }
131
+ }
132
+ }
133
+ injectAll() {
134
+ for (const target of this.targets.keys()) {
135
+ this.inject(target);
136
+ }
137
+ }
138
+ removeTarget(element) {
139
+ this.targets.delete(element);
140
+ }
141
+ clear() {
142
+ this.targets.clear();
143
+ }
144
+ }
145
+ function createHTMLInjector(html) {
146
+ return new RumiousDymanicInjector([html]);
147
+ }
148
+
149
+ class RumiousElementRef {
150
+ constructor(target) {
151
+ this.target = target;
152
+ }
153
+ getElement() {
154
+ return this.target;
155
+ }
156
+ remove() {
157
+ this.target.remove();
158
+ }
159
+ addChild(child) {
160
+ this.target.appendChild(child);
161
+ }
162
+ listChild() {
163
+ return this.target.childNodes;
164
+ }
165
+ querySelector(query) {
166
+ return this.target.querySelector(query);
167
+ }
168
+ querySelectorAll(query) {
169
+ return this.target.querySelectorAll(query);
170
+ }
171
+ set text(t) {
172
+ this.target.textContent = t;
173
+ }
174
+ get text() {
175
+ return this.target.textContent;
176
+ }
177
+ set value(v) {
178
+ if (this.target instanceof HTMLInputElement || this.target instanceof HTMLTextAreaElement) {
179
+ this.target.value = v;
180
+ }
181
+ }
182
+ get value() {
183
+ if (this.target instanceof HTMLInputElement || this.target instanceof HTMLTextAreaElement) {
184
+ return this.target.value;
185
+ }
186
+ return undefined;
187
+ }
188
+ addClassName(className) {
189
+ this.target.classList.add(className);
190
+ }
191
+ removeClassName(className) {
192
+ this.target.classList.remove(className);
193
+ }
194
+ hasClassName(className) {
195
+ return this.target.classList.contains(className);
196
+ }
197
+ toggleClass(className, force) {
198
+ return this.target.classList.toggle(className, force);
199
+ }
200
+ setStyle(styles) {
201
+ Object.assign(this.target.style, styles);
202
+ }
203
+ getStyle(property) {
204
+ return getComputedStyle(this.target).getPropertyValue(property);
205
+ }
206
+ setAttribute(key, value) {
207
+ this.target.setAttribute(key, value);
208
+ }
209
+ getAttribute(key) {
210
+ return this.target.getAttribute(key);
211
+ }
212
+ removeAttribute(key) {
213
+ this.target.removeAttribute(key);
214
+ }
215
+ on(event, callback, options) {
216
+ this.target.addEventListener(event, callback, options);
217
+ }
218
+ off(event, callback, options) {
219
+ this.target.removeEventListener(event, callback, options);
220
+ }
221
+ set html(t) {
222
+ this.target.innerHTML = t;
223
+ }
224
+ get html() {
225
+ return this.target.innerHTML;
226
+ }
227
+ getBoundingRect() {
228
+ return this.target.getBoundingClientRect();
229
+ }
230
+ isInViewport() {
231
+ const rect = this.target.getBoundingClientRect();
232
+ return rect.top >= 0 && rect.left >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && rect.right <= (window.innerWidth || document.documentElement.clientWidth);
233
+ }
234
+ prependChild(child) {
235
+ this.target.prepend(child);
236
+ }
237
+ setDisabled(disabled) {
238
+ if (this.target instanceof HTMLButtonElement || this.target instanceof HTMLInputElement || this.target instanceof HTMLTextAreaElement) {
239
+ this.target.disabled = disabled;
240
+ }
241
+ }
242
+ addClasses(...classNames) {
243
+ this.target.classList.add(...classNames);
244
+ }
245
+ removeClasses(...classNames) {
246
+ this.target.classList.remove(...classNames);
247
+ }
248
+ replaceClass(oldClass, newClass) {
249
+ this.target.classList.replace(oldClass, newClass);
250
+ }
251
+ moveTo(newParent) {
252
+ newParent.appendChild(this.target);
253
+ }
254
+ getParent() {
255
+ return this.target.parentElement;
256
+ }
257
+ getNextSibling() {
258
+ return this.target.nextElementSibling;
259
+ }
260
+ getPreviousSibling() {
261
+ return this.target.previousElementSibling;
262
+ }
263
+ hide() {
264
+ this.target.style.display = 'none';
265
+ }
266
+ show() {
267
+ this.target.style.removeProperty('display');
268
+ }
269
+ isHidden() {
270
+ return window.getComputedStyle(this.target).display === 'none';
271
+ }
272
+ scrollIntoView(options = {
273
+ behavior: 'smooth'
274
+ }) {
275
+ this.target.scrollIntoView(options);
276
+ }
277
+ matches(selector) {
278
+ return this.target.matches(selector);
279
+ }
280
+ getChildren() {
281
+ return Array.from(this.target.children);
282
+ }
283
+ insertAfter(newNode) {
284
+ if (this.target.parentNode) {
285
+ this.target.parentNode.insertBefore(newNode, this.target.nextSibling);
286
+ }
287
+ }
288
+ insertBefore(newNode) {
289
+ if (this.target.parentNode) {
290
+ this.target.parentNode.insertBefore(newNode, this.target);
291
+ }
292
+ }
293
+ clearChildren() {
294
+ while (this.target.firstChild) {
295
+ this.target.removeChild(this.target.firstChild);
296
+ }
297
+ }
298
+ animate(keyframes, options) {
299
+ return this.target.animate(keyframes, options);
300
+ }
301
+ }
302
+ function createElementRef() {
303
+ return new RumiousElementRef(document.createElement("span"));
304
+ }
305
+
306
+ function extractName(str) {
307
+ const index = str.indexOf('$');
308
+ if (index !== -1) {
309
+ return str.slice(index + 1);
310
+ }
311
+ return '';
312
+ }
313
+ function generateName(prefix = "_") {
314
+ return `${prefix}${(Math.floor(Math.random() * 9999) * Date.now()).toString(32)}`;
315
+ }
316
+
317
+ class RumiousReactor {
318
+ constructor(target) {
319
+ this.target = target;
320
+ this.bindings = [];
321
+ }
322
+ addBinding(bind) {
323
+ this.bindings.push(bind);
324
+ }
325
+ removeBinding(bind) {
326
+ this.bindings = this.bindings.filter(_ => _ !== bind);
327
+ }
328
+ async emit(commit) {
329
+ await Promise.allSettled(this.bindings.map(bind => {
330
+ try {
331
+ const result = bind(commit);
332
+ return result instanceof Promise ? result : Promise.resolve(result);
333
+ } catch (err) {
334
+ return Promise.reject(err);
335
+ }
336
+ }));
337
+ }
338
+ }
339
+
340
+ class RumiousState {
341
+ constructor(value, reactor) {
342
+ this.value = value;
343
+ this.reactor = reactor ?? new RumiousReactor(this);
344
+ }
345
+ set(value) {
346
+ this.value = value;
347
+ this.reactor.emit({
348
+ type: "SET",
349
+ target: this,
350
+ value: value
351
+ });
352
+ }
353
+ get() {
354
+ return this.value;
355
+ }
356
+ increase(count = 1) {
357
+ if (typeof this.value === "number") {
358
+ this.set(this.value + count);
359
+ }
360
+ }
361
+ produce(callback) {
362
+ const [draft, finalize] = create(this.value);
363
+ callback(draft);
364
+ this.set(finalize());
365
+ }
366
+ }
367
+ function watch(state, fn) {
368
+ state.reactor.addBinding(fn);
369
+ }
370
+ function unwatch(state, fn) {
371
+ state.reactor.removeBinding(fn);
372
+ }
373
+ function createState(value) {
374
+ return new RumiousState(value);
375
+ }
376
+
377
+ function eventBindingDirective(context, target, modifier, data) {
378
+ if (typeof data === "string") {
379
+ data = context.findName(extractName(data));
380
+ }
381
+ target.addEventListener(modifier, data);
382
+ }
383
+ function refBindingDirective(context, target, modifier, data) {
384
+ if (typeof data === "string") {
385
+ data = context.findName(extractName(data));
386
+ }
387
+ if (data instanceof RumiousElementRef) {
388
+ data.target = target;
389
+ } else {
390
+ throw Error("Rumious: ref directive required RumiousElementRef !");
391
+ }
392
+ }
393
+ function injectDirective(context, target, modifier, data) {
394
+ if (typeof data === "string") {
395
+ data = context.findName(extractName(data));
396
+ }
397
+ if (data instanceof RumiousDymanicInjector) {
398
+ data.addTarget(target);
399
+ data.inject(target);
400
+ } else {
401
+ throw Error("Rumious: inject directive required RumiousInjector !");
402
+ }
403
+ }
404
+ function bindDirective(context, target, modifier, data) {
405
+ if (typeof data === "string") {
406
+ data = context.findName(extractName(data));
407
+ }
408
+ if (data instanceof RumiousState) {
409
+ target.setAttribute(modifier, data.value);
410
+ data.reactor.addBinding(({}) => {
411
+ target.setAttribute(modifier, data.value);
412
+ });
413
+ } else {
414
+ throw Error("Rumious: bind directive required RumiousState !");
415
+ }
416
+ }
417
+ function modelDirective(context, target, modifier, data) {
418
+ if (typeof data === "string") {
419
+ data = context.findName(extractName(data));
420
+ }
421
+ if (data instanceof RumiousState && (target instanceof HTMLInputElement || target instanceof HTMLSelectElement || target instanceof HTMLTextAreaElement)) {
422
+ const type = target.type;
423
+ target.addEventListener("input", () => {
424
+ if (target instanceof HTMLInputElement) {
425
+ switch (type) {
426
+ case "checkbox":
427
+ data.set(target.checked);
428
+ break;
429
+ case "radio":
430
+ if (target.checked) data.set(target.value);
431
+ break;
432
+ default:
433
+ data.set(target.value);
434
+ }
435
+ } else if (target instanceof HTMLSelectElement || target instanceof HTMLTextAreaElement) {
436
+ data.set(target.value);
437
+ }
438
+ });
439
+ } else {
440
+ throw Error("Rumious: model directive requires RumiousState and a valid form element!");
441
+ }
442
+ }
443
+ const directives = {
444
+ "on": eventBindingDirective,
445
+ "ref": refBindingDirective,
446
+ "inject": injectDirective,
447
+ "bind": bindDirective,
448
+ "model": modelDirective
449
+ };
450
+
451
+ class RumiousDynamicArrayRenderer {
452
+ domMap = new Map();
453
+ constructor(state, callback, keyFn) {
454
+ this.state = state;
455
+ this.callback = callback;
456
+ this.keyFn = keyFn ?? ((_, i) => i);
457
+ }
458
+ prepare(anchor, context) {
459
+ this.anchorElement = anchor;
460
+ this.context = context;
461
+ }
462
+ async render() {
463
+ await this.reconcile(this.state.value);
464
+ this.state.reactor.addBinding(this.onStateChange.bind(this));
465
+ }
466
+ async onStateChange(commit) {
467
+ if (commit.type === "APPEND") {
468
+ const key = this.keyFn(commit.item, commit.key);
469
+ if (!this.domMap.has(key)) {
470
+ const template = await this.callback(commit.item, commit.key);
471
+ const frag = document.createDocumentFragment();
472
+ this.context.renderHelper(this.context, template, frag);
473
+ this.anchorElement.appendChild(frag);
474
+ this.domMap.set(key, frag);
475
+ }
476
+ } else if (commit.type === "SET_BY_KEY") {
477
+ const key = this.keyFn(commit.item, commit.key);
478
+ const oldNode = this.anchorElement.childNodes[commit.key];
479
+ if (oldNode) {
480
+ const template = await this.callback(commit.item, commit.key);
481
+ const frag = document.createDocumentFragment();
482
+ this.context.renderHelper(this.context, template, frag);
483
+ this.anchorElement.replaceChild(frag, oldNode);
484
+ this.domMap.set(key, frag);
485
+ }
486
+ } else if (commit.type === "REMOVE_BY_KEY") {
487
+ const node = this.anchorElement.childNodes[commit.key];
488
+ if (node) {
489
+ this.anchorElement.removeChild(node);
490
+ const entry = [...this.domMap.entries()].find(([, n]) => n === node);
491
+ if (entry) this.domMap.delete(entry[0]);
492
+ }
493
+ } else if (commit.type === "INSERT_BY_KEY") {
494
+ const key = this.keyFn(commit.item, commit.key);
495
+ if (!this.domMap.has(key)) {
496
+ const template = await this.callback(commit.item, commit.key);
497
+ const frag = document.createDocumentFragment();
498
+ this.context.renderHelper(this.context, template, frag);
499
+ const refNode = this.anchorElement.childNodes[commit.key] ?? null;
500
+ this.anchorElement.insertBefore(frag, refNode);
501
+ this.domMap.set(key, frag);
502
+ }
503
+ } else {
504
+ await this.reconcile(this.state.value);
505
+ }
506
+ }
507
+ async reconcile(nextItems) {
508
+ const oldMap = this.domMap;
509
+ const newMap = new Map();
510
+ const newNodes = [];
511
+ for (let i = 0; i < nextItems.length; i++) {
512
+ const item = nextItems[i];
513
+ const key = this.keyFn(item, i);
514
+ let node = oldMap.get(key);
515
+ if (!node) {
516
+ const template = await this.callback(item, i);
517
+ const frag = document.createDocumentFragment();
518
+ this.context.renderHelper(this.context, template, frag);
519
+ node = frag;
520
+ }
521
+ newMap.set(key, node);
522
+ newNodes.push(node);
523
+ }
524
+ this.anchorElement.textContent = "";
525
+ for (const node of newNodes) {
526
+ this.anchorElement.appendChild(node);
527
+ }
528
+ this.domMap = newMap;
529
+ }
530
+ }
531
+ function renderDynamicArray(state, callback) {
532
+ return new RumiousDynamicArrayRenderer(state, callback);
533
+ }
534
+
535
+ function isPrimitive(value) {
536
+ return value !== Object(value);
537
+ }
538
+
539
+ async function dynamicValue$1(target, textNode, value, context) {
540
+ const parent = textNode.parentNode;
541
+ if (!parent) return;
542
+ if (isPrimitive(value)) {
543
+ textNode.textContent = String(value);
544
+ } else if (value && value instanceof RumiousState) {
545
+ textNode.textContent = value.value;
546
+ function onValueChange({}) {
547
+ if (document.contains(textNode)) {
548
+ textNode.textContent = value.value;
549
+ } else {
550
+ value.reactor.removeBinding(onValueChange);
551
+ }
552
+ }
553
+ value.reactor.addBinding(onValueChange);
554
+ } else if (Array.isArray(value)) {
555
+ textNode.textContent = value.map(String).join("");
556
+ } else if (value instanceof RumiousDynamicArrayRenderer) {
557
+ value.prepare(textNode.parentElement, context);
558
+ value.render();
559
+ } else if (value instanceof HTMLElement) {
560
+ textNode.replaceWith(value);
561
+ } else if (value instanceof RumiousRenderTemplate) {
562
+ let fragment = document.createDocumentFragment();
563
+ context.renderHelper?.(context, value, fragment);
564
+ textNode.replaceWith(fragment);
565
+ } else if (value instanceof NodeList || value instanceof HTMLCollection) {
566
+ if (value.length === 0) {
567
+ textNode.remove();
568
+ return;
569
+ }
570
+ const fragment = document.createDocumentFragment();
571
+ for (const node of Array.from(value)) {
572
+ fragment.appendChild(node.cloneNode(true));
573
+ }
574
+ textNode.replaceWith(fragment);
575
+ textNode.remove();
576
+ } else if (value && typeof value.toString === "function") {
577
+ try {
578
+ textNode.textContent = value.toString();
579
+ } catch {
580
+ textNode.textContent = "";
581
+ }
582
+ } else {
583
+ textNode.textContent = "";
584
+ }
585
+ }
586
+
587
+ function template(generator) {
588
+ return new RumiousRenderTemplate(generator);
589
+ }
590
+ function addDirective(element, context, name, modifier = "", data) {
591
+ let callback = directives[name];
592
+ if (callback) {
593
+ callback(context, element, modifier, data);
594
+ } else {
595
+ throw Error("Rumious: Cannot solve directive !");
596
+ }
597
+ }
598
+ function dynamicValue(target, textNode, value, context) {
599
+ dynamicValue$1(target, textNode, value, context);
600
+ }
601
+ function createComponent(componentConstructor) {
602
+ let tagName = componentConstructor.tagName;
603
+ if (!window.customElements.get(tagName)) {
604
+ window.customElements.define(tagName, class extends RumiousComponentElement {
605
+ static tag = tagName;
606
+ });
607
+ }
608
+ return document.createElement(tagName);
609
+ }
610
+
611
+ // This is just to satisfy TypeScript's JSX requirement.
612
+ // Rumious doesn't use createElement — we do things differently.
613
+
614
+ function createElement(...args) {
615
+ throw Error("Rumious doesn't use createElement !");
616
+ }
617
+ window.RUMIOUS_JSX = {
618
+ template,
619
+ createElement,
620
+ addDirective,
621
+ dynamicValue,
622
+ createComponent
623
+ };
624
+
625
+ class RumiousChildrenRef {
626
+ constructor(target) {
627
+ this.target = target;
628
+ }
629
+ list() {
630
+ return Array.from(this.target.children);
631
+ }
632
+ getChild(index) {
633
+ return this.list()[index];
634
+ }
635
+ remove(index) {
636
+ this.list()[index]?.remove();
637
+ }
638
+ add(child) {
639
+ this.target.appendChild(child);
640
+ }
641
+ querySelector(query) {
642
+ return this.target.querySelector(query);
643
+ }
644
+ querySelectorAll(query) {
645
+ return this.target.querySelectorAll(query);
646
+ }
647
+ clear() {
648
+ this.target.innerHTML = '';
649
+ }
650
+ replaceChild(index, newChild) {
651
+ const oldChild = this.getChild(index);
652
+ if (oldChild) {
653
+ this.target.replaceChild(newChild, oldChild);
654
+ }
655
+ }
656
+ insertBefore(newChild, index) {
657
+ const referenceChild = this.getChild(index);
658
+ if (referenceChild) {
659
+ this.target.insertBefore(newChild, referenceChild);
660
+ } else {
661
+ this.add(newChild);
662
+ }
663
+ }
664
+ prepend(child) {
665
+ this.target.prepend(child);
666
+ }
667
+ getFirstChild() {
668
+ return this.list()[0];
669
+ }
670
+ getLastChild() {
671
+ return this.list()[this.list().length - 1];
672
+ }
673
+ hasChildren() {
674
+ return this.target.hasChildNodes();
675
+ }
676
+ count() {
677
+ return this.target.children.length;
678
+ }
679
+ find(predicate) {
680
+ return this.list().find(predicate);
681
+ }
682
+ forEach(callback) {
683
+ this.list().forEach(callback);
684
+ }
685
+ removeAllMatching(query) {
686
+ this.querySelectorAll(query).forEach(element => element.remove());
687
+ }
688
+ toggleClass(className) {
689
+ this.list().forEach(child => child.classList.toggle(className));
690
+ }
691
+ setAttribute(key, value) {
692
+ this.list().forEach(child => child.setAttribute(key, value));
693
+ }
694
+ }
695
+
696
+ class RumiousArrayState extends RumiousState {
697
+ constructor(value, reactor) {
698
+ super(value, reactor);
699
+ }
700
+ set(arg1, arg2) {
701
+ if (typeof arg1 === "number" && arg2 !== undefined) {
702
+ const index = arg1;
703
+ const newValue = arg2;
704
+ this.value[index] = newValue;
705
+ this.reactor.emit({
706
+ type: "SET_BY_KEY",
707
+ value: [...this.value],
708
+ target: this,
709
+ key: index,
710
+ item: newValue
711
+ });
712
+ } else if (Array.isArray(arg1)) {
713
+ super.set(arg1);
714
+ } else {
715
+ throw new Error("Invalid arguments passed to set()");
716
+ }
717
+ }
718
+ get(arg) {
719
+ return typeof arg === "number" ? this.value[arg] : this.value;
720
+ }
721
+ insert(index, newItem) {
722
+ this.value.splice(index, 0, newItem);
723
+ this.reactor.emit({
724
+ type: "INSERT_BY_KEY",
725
+ value: this.value,
726
+ target: this,
727
+ key: index,
728
+ item: newItem
729
+ });
730
+ return this;
731
+ }
732
+ remove(index) {
733
+ this.value.splice(index, 1);
734
+ this.reactor.emit({
735
+ type: "REMOVE_BY_KEY",
736
+ value: this.value,
737
+ target: this,
738
+ key: index
739
+ });
740
+ return this;
741
+ }
742
+ append(value) {
743
+ this.value.push(value);
744
+ this.reactor.emit({
745
+ type: "APPEND",
746
+ value: this.value,
747
+ target: this,
748
+ item: value
749
+ });
750
+ return this;
751
+ }
752
+ clear() {
753
+ this.value.length = 0;
754
+ this.reactor.emit({
755
+ type: "SET",
756
+ value: [],
757
+ target: this
758
+ });
759
+ return this;
760
+ }
761
+ replace(index, newItem) {
762
+ if (index < 0 || index >= this.value.length) {
763
+ throw new Error("Index out of bounds");
764
+ }
765
+ this.value[index] = newItem;
766
+ this.reactor.emit({
767
+ type: "SET_BY_KEY",
768
+ value: [...this.value],
769
+ target: this,
770
+ key: index,
771
+ item: newItem
772
+ });
773
+ return this;
774
+ }
775
+ filter(callback) {
776
+ this.value = this.value.filter(callback);
777
+ this.reactor.emit({
778
+ type: "SET",
779
+ value: this.value,
780
+ target: this
781
+ });
782
+ return this;
783
+ }
784
+ map(callback) {
785
+ this.value = this.value.map(callback);
786
+ this.reactor.emit({
787
+ type: "SET",
788
+ value: this.value,
789
+ target: this
790
+ });
791
+ return this;
792
+ }
793
+ sort(compareFn) {
794
+ this.value.sort(compareFn);
795
+ this.reactor.emit({
796
+ type: "SET",
797
+ value: this.value,
798
+ target: this
799
+ });
800
+ return this;
801
+ }
802
+ reverse() {
803
+ this.value.reverse();
804
+ this.reactor.emit({
805
+ type: "SET",
806
+ value: this.value,
807
+ target: this
808
+ });
809
+ return this;
810
+ }
811
+ get length() {
812
+ return this.value.length;
813
+ }
814
+ forEach(callback) {
815
+ this.value.forEach(callback);
816
+ }
817
+ }
818
+ function createArrayState(value) {
819
+ return new RumiousArrayState(value);
820
+ }
821
+
822
+ class RumiousObjectState extends RumiousState {
823
+ #locked = false;
824
+ constructor(value, reactor) {
825
+ super(value, reactor);
826
+ }
827
+ set(arg1, arg2) {
828
+ if (this.#locked) throw new Error("Object is locked");
829
+ if (typeof arg1 === "string") {
830
+ const key = arg1;
831
+ const newValue = arg2;
832
+ this.value[key] = newValue;
833
+ this.reactor.emit({
834
+ type: "SET_BY_KEY",
835
+ value: {
836
+ ...this.value
837
+ },
838
+ target: this,
839
+ key,
840
+ item: newValue
841
+ });
842
+ return this;
843
+ } else if (typeof arg1 === "object") {
844
+ super.set(arg1);
845
+ } else {
846
+ throw new Error("Invalid arguments passed to set()");
847
+ }
848
+ }
849
+ remove(key) {
850
+ if (this.#locked) throw new Error("Object is locked");
851
+ if (key in this.value) {
852
+ delete this.value[key];
853
+ this.reactor.emit({
854
+ type: "REMOVE_BY_KEY",
855
+ value: {
856
+ ...this.value
857
+ },
858
+ target: this,
859
+ key
860
+ });
861
+ }
862
+ return this;
863
+ }
864
+ merge(partial) {
865
+ if (this.#locked) throw new Error("Object is locked");
866
+ Object.assign(this.value, partial);
867
+ this.reactor.emit({
868
+ type: "SET",
869
+ value: {
870
+ ...this.value
871
+ },
872
+ target: this
873
+ });
874
+ return this;
875
+ }
876
+ assign(obj) {
877
+ if (this.#locked) throw new Error("Object is locked");
878
+ this.value = {
879
+ ...this.value,
880
+ ...obj
881
+ };
882
+ this.reactor.emit({
883
+ type: "SET",
884
+ value: {
885
+ ...this.value
886
+ },
887
+ target: this
888
+ });
889
+ return this;
890
+ }
891
+ clear() {
892
+ if (this.#locked) throw new Error("Object is locked");
893
+ for (const key in this.value) {
894
+ delete this.value[key];
895
+ }
896
+ this.reactor.emit({
897
+ type: "SET",
898
+ value: {},
899
+ target: this
900
+ });
901
+ return this;
902
+ }
903
+ get(arg) {
904
+ return typeof arg === "string" ? this.value[arg] : this.value;
905
+ }
906
+ keys() {
907
+ return Object.keys(this.value);
908
+ }
909
+ values() {
910
+ return Object.values(this.value);
911
+ }
912
+ entries() {
913
+ return Object.entries(this.value);
914
+ }
915
+ has(key) {
916
+ return key in this.value;
917
+ }
918
+ get size() {
919
+ return Object.keys(this.value).length;
920
+ }
921
+ forEach(callback) {
922
+ Object.entries(this.value).forEach(([key, value]) => {
923
+ callback(value, key, this.value);
924
+ });
925
+ }
926
+ map(callback) {
927
+ const result = {};
928
+ Object.entries(this.value).forEach(([key, value]) => {
929
+ result[key] = callback(value, key, this.value);
930
+ });
931
+ return result;
932
+ }
933
+ clone() {
934
+ return JSON.parse(JSON.stringify(this.value));
935
+ }
936
+ toJSON() {
937
+ return JSON.parse(JSON.stringify(this.value));
938
+ }
939
+ toObject() {
940
+ return this.get();
941
+ }
942
+ lock() {
943
+ this.#locked = true;
944
+ return this;
945
+ }
946
+ unlock() {
947
+ this.#locked = false;
948
+ return this;
949
+ }
950
+ get isLocked() {
951
+ return this.#locked;
952
+ }
953
+ freeze() {
954
+ Object.freeze(this.value);
955
+ return this;
956
+ }
957
+ unfreeze() {
958
+ this.value = JSON.parse(JSON.stringify(this.value));
959
+ return this;
960
+ }
961
+ get isFrozen() {
962
+ return Object.isFrozen(this.value);
963
+ }
964
+ }
965
+ function createObjectState(value) {
966
+ return new RumiousObjectState(value);
967
+ }
968
+
969
+ window.RUMIOUS_CONTEXTS = {};
970
+ class RumiousContext {
971
+ constructor(initialValues = {}) {
972
+ this.events = {};
973
+ this.values = initialValues;
974
+ }
975
+ has(key) {
976
+ return key in this.values;
977
+ }
978
+ set(key, value) {
979
+ this.values[key] = value;
980
+ }
981
+ get(key) {
982
+ return this.values[key];
983
+ }
984
+ on(event, callback) {
985
+ if (!this.events[event]) this.events[event] = [];
986
+ this.events[event].push(callback);
987
+ }
988
+ off(event, callback) {
989
+ if (!this.events[event]) return;
990
+ this.events[event] = this.events[event].filter(fn => fn !== callback);
991
+ }
992
+ emit(event, payload) {
993
+ if (!this.events[event]) return;
994
+ this.events[event].forEach(fn => fn(payload));
995
+ }
996
+ }
997
+ function createContext(values, name = generateName("rctx_")) {
998
+ if (window.RUMIOUS_CONTEXTS[name]) return window.RUMIOUS_CONTEXTS[name];else {
999
+ let context = new RumiousContext(values);
1000
+ window.RUMIOUS_CONTEXTS[name] = context;
1001
+ return context;
1002
+ }
1003
+ }
1004
+
1005
+ function tholle(func, limit) {
1006
+ let lastCall = 0;
1007
+ return function (...args) {
1008
+ const now = Date.now();
1009
+ if (now - lastCall >= limit) {
1010
+ lastCall = now;
1011
+ func.apply(this, args);
1012
+ }
1013
+ };
1014
+ }
1015
+ function denounce(func, delay) {
1016
+ let timer;
1017
+ return function (...args) {
1018
+ clearTimeout(timer);
1019
+ timer = setTimeout(() => func.apply(this, args), delay);
1020
+ };
1021
+ }
1022
+ function trailingThrottle(func, limit) {
1023
+ let lastCall = 0;
1024
+ let lastArgs = null;
1025
+ let timeout = null;
1026
+ function invoke() {
1027
+ lastCall = Date.now();
1028
+ func.apply(this, lastArgs);
1029
+ lastArgs = null;
1030
+ }
1031
+ return function (...args) {
1032
+ const now = Date.now();
1033
+ lastArgs = args;
1034
+ if (now - lastCall >= limit) {
1035
+ invoke.call(this);
1036
+ } else if (!timeout) {
1037
+ timeout = setTimeout(() => {
1038
+ timeout = null;
1039
+ invoke.call(this);
1040
+ }, limit - (now - lastCall));
1041
+ }
1042
+ };
1043
+ }
1044
+ function leadingTrailingDebounce(func, delay) {
1045
+ let timer;
1046
+ let isFirstCall = true;
1047
+ return function (...args) {
1048
+ if (isFirstCall) {
1049
+ func.apply(this, args);
1050
+ isFirstCall = false;
1051
+ }
1052
+ clearTimeout(timer);
1053
+ timer = setTimeout(() => {
1054
+ func.apply(this, args);
1055
+ isFirstCall = true;
1056
+ }, delay);
1057
+ };
1058
+ }
1059
+ function rafThrottle(func) {
1060
+ let ticking = false;
1061
+ return function (...args) {
1062
+ if (!ticking) {
1063
+ ticking = true;
1064
+ requestAnimationFrame(() => {
1065
+ func.apply(this, args);
1066
+ ticking = false;
1067
+ });
1068
+ }
1069
+ };
1070
+ }
1071
+
1072
+ export { RumiousApp, RumiousArrayState, RumiousChildrenRef, RumiousComponent, RumiousComponentElement, RumiousContext, RumiousDymanicInjector, RumiousElementRef, RumiousObjectState, RumiousRenderTemplate, RumiousState, createApp, createArrayState, createContext, createElementRef, createHTMLInjector, createObjectState, createState, denounce, leadingTrailingDebounce, rafThrottle, renderDynamicArray, template, tholle, trailingThrottle, unwatch, watch };