@mulsense/xnew 0.6.5 → 0.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/xnew.mjs CHANGED
@@ -517,92 +517,15 @@ function pointer(element, event) {
517
517
  // utils
518
518
  //----------------------------------------------------------------------------------------------------
519
519
  const SYSTEM_EVENTS = ['start', 'update', 'render', 'stop', 'finalize'];
520
- class UnitPromise {
521
- constructor(promise, Component) {
522
- this.promise = promise;
523
- this.Component = Component;
524
- }
525
- then(callback) { return this.wrap('then', callback); }
526
- catch(callback) { return this.wrap('catch', callback); }
527
- finally(callback) { return this.wrap('finally', callback); }
528
- wrap(key, callback) {
529
- const snapshot = Unit.snapshot(Unit.currentUnit);
530
- this.promise = this.promise[key]((...args) => Unit.scope(snapshot, callback, ...args));
531
- return this;
532
- }
533
- }
534
- class UnitTimer {
535
- constructor() {
536
- this.unit = null;
537
- this.stack = [];
538
- }
539
- clear() {
540
- var _a;
541
- this.stack = [];
542
- (_a = this.unit) === null || _a === void 0 ? void 0 : _a.finalize();
543
- this.unit = null;
544
- }
545
- timeout(callback, duration = 0) {
546
- return UnitTimer.execute(this, { callback, duration }, 1);
547
- }
548
- interval(callback, duration = 0, iterations = 0) {
549
- return UnitTimer.execute(this, { callback, duration }, iterations);
550
- }
551
- transition(transition, duration = 0, easing) {
552
- return UnitTimer.execute(this, { transition, duration, easing }, 1);
553
- }
554
- static execute(timer, options, iterations) {
555
- const props = { options, iterations, snapshot: Unit.snapshot(Unit.currentUnit) };
556
- if (timer.unit === null || timer.unit._.state === 'finalized') {
557
- timer.unit = new Unit(Unit.currentUnit, null, UnitTimer.Component, props);
558
- }
559
- else if (timer.stack.length === 0) {
560
- timer.stack.push(props);
561
- timer.unit.on('finalize', () => UnitTimer.next(timer));
562
- }
563
- else {
564
- timer.stack.push(props);
565
- }
566
- return timer;
567
- }
568
- static next(timer) {
569
- if (timer.stack.length > 0) {
570
- timer.unit = new Unit(Unit.currentUnit, null, UnitTimer.Component, timer.stack.shift());
571
- timer.unit.on('finalize', () => UnitTimer.next(timer));
572
- }
573
- }
574
- static Component(unit, { options, iterations, snapshot }) {
575
- let counter = 0;
576
- let timer = new Timer({ callback, transition, duration: options.duration, easing: options.easing });
577
- function callback() {
578
- if (options.callback)
579
- Unit.scope(snapshot, options.callback);
580
- if (iterations <= 0 || counter < iterations - 1) {
581
- timer = new Timer({ callback, transition, duration: options.duration, easing: options.easing });
582
- }
583
- else {
584
- unit.finalize();
585
- }
586
- counter++;
587
- }
588
- function transition(value) {
589
- if (options.transition)
590
- Unit.scope(snapshot, options.transition, { value });
591
- }
592
- unit.on('finalize', () => timer.clear());
593
- }
594
- }
595
- function DefaultComponent(unit, { text }) {
596
- if (text !== undefined) {
597
- unit.element.textContent = text;
598
- }
599
- }
600
520
  //----------------------------------------------------------------------------------------------------
601
521
  // unit
602
522
  //----------------------------------------------------------------------------------------------------
603
523
  class Unit {
604
- constructor(parent, target, component, props) {
524
+ constructor(parent, target, Component, props) {
605
525
  var _a;
526
+ const backup = Unit.currentUnit;
527
+ Unit.currentUnit = this;
528
+ parent === null || parent === void 0 ? void 0 : parent._.children.push(this);
606
529
  let baseElement;
607
530
  if (target instanceof HTMLElement || target instanceof SVGElement) {
608
531
  baseElement = target;
@@ -614,20 +537,45 @@ class Unit {
614
537
  baseElement = (document === null || document === void 0 ? void 0 : document.body) ? document.body : null;
615
538
  }
616
539
  let baseComponent;
617
- if (typeof component === 'function') {
618
- baseComponent = component;
540
+ if (typeof Component === 'function') {
541
+ baseComponent = Component;
619
542
  }
620
- else if (typeof component === 'string' || typeof component === 'number') {
621
- baseComponent = DefaultComponent;
622
- props = { text: component.toString() };
543
+ else if (typeof Component === 'string' || typeof Component === 'number') {
544
+ baseComponent = (unit) => { unit.element.textContent = Component.toString(); };
623
545
  }
624
546
  else {
625
- baseComponent = DefaultComponent;
547
+ baseComponent = (unit) => { };
626
548
  }
627
- const baseContext = (_a = parent === null || parent === void 0 ? void 0 : parent._.currentContext) !== null && _a !== void 0 ? _a : { stack: null };
628
- this._ = { parent, target, baseElement, baseContext, baseComponent, props };
629
- parent === null || parent === void 0 ? void 0 : parent._.children.push(this);
630
- Unit.initialize(this, null);
549
+ const baseContext = (_a = parent === null || parent === void 0 ? void 0 : parent._.currentContext) !== null && _a !== void 0 ? _a : { previous: null };
550
+ this._ = {
551
+ parent,
552
+ state: 'invoked',
553
+ tostart: true,
554
+ protected: false,
555
+ currentElement: baseElement,
556
+ currentContext: baseContext,
557
+ currentComponent: null,
558
+ ancestors: parent ? [parent, ...parent._.ancestors] : [],
559
+ children: [],
560
+ nestElements: [],
561
+ promises: [],
562
+ results: {},
563
+ Components: [],
564
+ listeners: new MapMap(),
565
+ defines: {},
566
+ systems: { start: [], update: [], render: [], stop: [], finalize: [] },
567
+ eventor: new Eventor(),
568
+ };
569
+ // nest html element
570
+ if (typeof target === 'string') {
571
+ Unit.nest(this, target);
572
+ }
573
+ // setup Component
574
+ Unit.extend(this, baseComponent, props);
575
+ if (this._.state === 'invoked') {
576
+ this._.state = 'initialized';
577
+ }
578
+ Unit.currentUnit = backup;
631
579
  }
632
580
  get element() {
633
581
  return this._.currentElement;
@@ -642,77 +590,40 @@ class Unit {
642
590
  finalize() {
643
591
  Unit.stop(this);
644
592
  Unit.finalize(this);
645
- if (this._.parent) {
646
- this._.parent._.children = this._.parent._.children.filter((unit) => unit !== this);
647
- }
648
- }
649
- reboot() {
650
- let anchor = null;
651
- if (this._.nestElements[0] && this._.nestElements[0].owned === true) {
652
- anchor = this._.nestElements[0].element.nextElementSibling;
653
- }
654
- Unit.stop(this);
655
- Unit.finalize(this);
656
- Unit.initialize(this, anchor);
657
- }
658
- static initialize(unit, anchor) {
659
- const backup = Unit.currentUnit;
660
- Unit.currentUnit = unit;
661
- const done = {
662
- promise: null,
663
- resolve: null,
664
- reject: null
665
- };
666
- done.promise = new Promise((resolve, reject) => { done.resolve = resolve; done.reject = reject; });
667
- unit._ = Object.assign(unit._, {
668
- currentElement: unit._.baseElement,
669
- currentContext: unit._.baseContext,
670
- currentComponent: null,
671
- anchor,
672
- state: 'invoked',
673
- tostart: true,
674
- protected: false,
675
- ancestors: unit._.parent ? [unit._.parent, ...unit._.parent._.ancestors] : [],
676
- children: [],
677
- nestElements: [],
678
- promises: [],
679
- done,
680
- components: [],
681
- listeners: new MapMap(),
682
- defines: {},
683
- systems: { start: [], update: [], render: [], stop: [], finalize: [] },
684
- eventor: new Eventor(),
685
- });
686
- // nest html element
687
- if (typeof unit._.target === 'string') {
688
- Unit.nest(unit, unit._.target);
689
- }
690
- // setup component
691
- Unit.extend(unit, unit._.baseComponent, unit._.props);
692
- // whether the unit promise was resolved
693
- // Promise.all(unit._.promises.map(p => p.promise)).then(() => unit._.state = 'initialized');
694
- unit._.state = 'initialized';
695
- Unit.currentUnit = backup;
696
593
  }
697
594
  static finalize(unit) {
698
595
  if (unit._.state !== 'finalized' && unit._.state !== 'finalizing') {
699
596
  unit._.state = 'finalizing';
700
- [...unit._.children].reverse().forEach((child) => child.finalize());
701
- [...unit._.systems.finalize].reverse().forEach(({ execute }) => execute());
597
+ unit._.children.reverse().forEach((child) => child.finalize());
598
+ unit._.systems.finalize.reverse().forEach(({ execute }) => execute());
702
599
  unit.off();
703
- unit._.components.forEach((component) => Unit.component2units.delete(component, unit));
704
- for (const { element, owned } of unit._.nestElements.reverse()) {
705
- if (owned === true) {
706
- element.remove();
600
+ unit._.nestElements.reverse().filter(item => item.owned).forEach(item => item.element.remove());
601
+ unit._.Components.forEach((Component) => Unit.component2units.delete(Component, unit));
602
+ // remove contexts
603
+ const contexts = Unit.unit2Contexts.get(unit);
604
+ contexts === null || contexts === void 0 ? void 0 : contexts.forEach((context) => {
605
+ let temp = context.previous;
606
+ while (temp !== null) {
607
+ if (contexts.has(temp) === false && temp.key !== undefined) {
608
+ context.previous = temp;
609
+ context.key = undefined;
610
+ context.value = undefined;
611
+ break;
612
+ }
613
+ temp = temp.previous;
707
614
  }
708
- }
709
- unit._.currentElement = unit._.baseElement;
615
+ });
616
+ Unit.unit2Contexts.delete(unit);
617
+ unit._.currentContext = { previous: null };
710
618
  // reset defines
711
619
  Object.keys(unit._.defines).forEach((key) => {
712
620
  delete unit[key];
713
621
  });
714
622
  unit._.defines = {};
715
623
  unit._.state = 'finalized';
624
+ if (unit._.parent) {
625
+ unit._.parent._.children = unit._.parent._.children.filter((u) => u !== unit);
626
+ }
716
627
  }
717
628
  }
718
629
  static nest(unit, target, textContent) {
@@ -724,16 +635,8 @@ class Unit {
724
635
  else {
725
636
  const match = target.match(/<((\w+)[^>]*?)\/?>/);
726
637
  if (match !== null) {
727
- let element;
728
- if (unit._.anchor !== null) {
729
- unit._.anchor.insertAdjacentHTML('beforebegin', `<${match[1]}></${match[2]}>`);
730
- element = unit._.anchor.previousElementSibling;
731
- unit._.anchor = null;
732
- }
733
- else {
734
- unit._.currentElement.insertAdjacentHTML('beforeend', `<${match[1]}></${match[2]}>`);
735
- element = unit._.currentElement.children[unit._.currentElement.children.length - 1];
736
- }
638
+ unit._.currentElement.insertAdjacentHTML('beforeend', `<${match[1]}></${match[2]}>`);
639
+ const element = unit._.currentElement.children[unit._.currentElement.children.length - 1];
737
640
  unit._.currentElement = element;
738
641
  if (textContent !== undefined) {
739
642
  element.textContent = textContent.toString();
@@ -748,25 +651,16 @@ class Unit {
748
651
  }
749
652
  static extend(unit, Component, props) {
750
653
  var _a;
751
- if (unit._.components.includes(Component) === true) {
752
- throw new Error(`The component is already extended.`);
654
+ if (unit._.Components.includes(Component) === true) {
655
+ throw new Error(`The Component is already extended.`);
753
656
  }
754
657
  else {
755
658
  const backupComponent = unit._.currentComponent;
756
659
  unit._.currentComponent = Component;
757
660
  const defines = (_a = Component(unit, props !== null && props !== void 0 ? props : {})) !== null && _a !== void 0 ? _a : {};
758
- if (unit._.parent && Component !== DefaultComponent) {
759
- if (Component === unit._.baseComponent) {
760
- Unit.context(unit._.parent, Component, unit);
761
- }
762
- else {
763
- Unit.context(unit, Component, unit);
764
- Unit.context(unit._.parent, Component, unit);
765
- }
766
- }
767
661
  unit._.currentComponent = backupComponent;
768
662
  Unit.component2units.add(Component, unit);
769
- unit._.components.push(Component);
663
+ unit._.Components.push(Component);
770
664
  Object.keys(defines).forEach((key) => {
771
665
  if (unit[key] !== undefined && unit._.defines[key] === undefined) {
772
666
  throw new Error(`The property "${key}" already exists.`);
@@ -784,7 +678,7 @@ class Unit {
784
678
  wrapper.value = (...args) => Unit.scope(snapshot, descriptor.value, ...args);
785
679
  }
786
680
  else {
787
- throw new Error(`Only function properties can be defined as component defines. [${key}]`);
681
+ throw new Error(`Only function properties can be defined as Component defines. [${key}]`);
788
682
  }
789
683
  Object.defineProperty(unit._.defines, key, wrapper);
790
684
  Object.defineProperty(unit, key, wrapper);
@@ -844,7 +738,7 @@ class Unit {
844
738
  Unit.currentUnit = snapshot.unit;
845
739
  snapshot.unit._.currentContext = snapshot.context;
846
740
  snapshot.unit._.currentElement = snapshot.element;
847
- snapshot.unit._.currentComponent = snapshot.component;
741
+ snapshot.unit._.currentComponent = snapshot.Component;
848
742
  return func(...args);
849
743
  }
850
744
  catch (error) {
@@ -854,21 +748,20 @@ class Unit {
854
748
  Unit.currentUnit = currentUnit;
855
749
  snapshot.unit._.currentContext = backup.context;
856
750
  snapshot.unit._.currentElement = backup.element;
857
- snapshot.unit._.currentComponent = backup.component;
751
+ snapshot.unit._.currentComponent = backup.Component;
858
752
  }
859
753
  }
860
754
  static snapshot(unit) {
861
- return { unit, context: unit._.currentContext, element: unit._.currentElement, component: unit._.currentComponent };
755
+ return { unit, context: unit._.currentContext, element: unit._.currentElement, Component: unit._.currentComponent };
862
756
  }
863
- static context(unit, key, value) {
864
- if (value !== undefined) {
865
- unit._.currentContext = { stack: unit._.currentContext, key, value };
866
- }
867
- else {
868
- for (let context = unit._.currentContext; context.stack !== null; context = context.stack) {
869
- if (context.key === key)
870
- return context.value;
871
- }
757
+ static addContext(unit, orner, key, value) {
758
+ unit._.currentContext = { previous: unit._.currentContext, key, value };
759
+ Unit.unit2Contexts.add(orner, unit._.currentContext);
760
+ }
761
+ static getContext(unit, key) {
762
+ for (let context = unit._.currentContext; context.previous !== null; context = context.previous) {
763
+ if (context.key === key)
764
+ return context.value;
872
765
  }
873
766
  }
874
767
  static find(Component) {
@@ -892,7 +785,7 @@ class Unit {
892
785
  unit._.systems[type].push({ listener, execute });
893
786
  }
894
787
  if (unit._.listeners.has(type, listener) === false) {
895
- unit._.listeners.set(type, listener, { element: unit.element, component: unit._.currentComponent, execute });
788
+ unit._.listeners.set(type, listener, { element: unit.element, Component: unit._.currentComponent, execute });
896
789
  Unit.type2units.add(type, unit);
897
790
  if (/^[A-Za-z]/.test(type)) {
898
791
  unit._.eventor.add(unit.element, type, execute, options);
@@ -934,11 +827,90 @@ class Unit {
934
827
  }
935
828
  }
936
829
  Unit.currentComponent = () => { };
830
+ Unit.unit2Contexts = new MapSet();
937
831
  Unit.component2units = new MapSet();
938
832
  //----------------------------------------------------------------------------------------------------
939
833
  // event
940
834
  //----------------------------------------------------------------------------------------------------
941
835
  Unit.type2units = new MapSet();
836
+ //----------------------------------------------------------------------------------------------------
837
+ // extensions
838
+ //----------------------------------------------------------------------------------------------------
839
+ class UnitPromise {
840
+ constructor(promise, Component) {
841
+ this.promise = promise;
842
+ this.Component = Component;
843
+ }
844
+ then(callback) { return this.wrap('then', callback); }
845
+ catch(callback) { return this.wrap('catch', callback); }
846
+ finally(callback) { return this.wrap('finally', callback); }
847
+ wrap(key, callback) {
848
+ const snapshot = Unit.snapshot(Unit.currentUnit);
849
+ this.promise = this.promise[key]((...args) => Unit.scope(snapshot, callback, ...args));
850
+ return this;
851
+ }
852
+ }
853
+ class UnitTimer {
854
+ constructor() {
855
+ this.unit = null;
856
+ this.queue = [];
857
+ }
858
+ clear() {
859
+ var _a;
860
+ this.queue = [];
861
+ (_a = this.unit) === null || _a === void 0 ? void 0 : _a.finalize();
862
+ this.unit = null;
863
+ }
864
+ timeout(callback, duration = 0) {
865
+ return UnitTimer.execute(this, { callback, duration }, 1);
866
+ }
867
+ interval(callback, duration = 0, iterations = 0) {
868
+ return UnitTimer.execute(this, { callback, duration }, iterations);
869
+ }
870
+ transition(transition, duration = 0, easing) {
871
+ return UnitTimer.execute(this, { transition, duration, easing }, 1);
872
+ }
873
+ static execute(timer, options, iterations) {
874
+ const props = { options, iterations, snapshot: Unit.snapshot(Unit.currentUnit) };
875
+ if (timer.unit === null || timer.unit._.state === 'finalized') {
876
+ timer.unit = new Unit(Unit.currentUnit, null, UnitTimer.Component, props);
877
+ }
878
+ else if (timer.queue.length === 0) {
879
+ timer.queue.push(props);
880
+ timer.unit.on('finalize', () => UnitTimer.next(timer));
881
+ }
882
+ else {
883
+ timer.queue.push(props);
884
+ }
885
+ return timer;
886
+ }
887
+ static next(timer) {
888
+ if (timer.queue.length > 0) {
889
+ timer.unit = new Unit(Unit.currentUnit, null, UnitTimer.Component, timer.queue.shift());
890
+ timer.unit.on('finalize', () => UnitTimer.next(timer));
891
+ }
892
+ }
893
+ static Component(unit, { options, iterations, snapshot }) {
894
+ let counter = 0;
895
+ let timer = new Timer({ callback, transition, duration: options.duration, easing: options.easing });
896
+ function callback() {
897
+ if (options.callback)
898
+ Unit.scope(snapshot, options.callback);
899
+ if (iterations <= 0 || counter < iterations - 1) {
900
+ timer = new Timer({ callback, transition, duration: options.duration, easing: options.easing });
901
+ }
902
+ else {
903
+ unit.finalize();
904
+ }
905
+ counter++;
906
+ }
907
+ function transition(value) {
908
+ if (options.transition)
909
+ Unit.scope(snapshot, options.transition, { value });
910
+ }
911
+ unit.on('finalize', () => timer.clear());
912
+ }
913
+ }
942
914
 
943
915
  const xnew$1 = Object.assign(function (...args) {
944
916
  if (Unit.rootUnit === undefined)
@@ -955,7 +927,9 @@ const xnew$1 = Object.assign(function (...args) {
955
927
  }
956
928
  const Component = args.shift();
957
929
  const props = args.shift();
958
- return new Unit(Unit.currentUnit, target, Component, props);
930
+ const unit = new Unit(Unit.currentUnit, target, Component, props);
931
+ Unit.addContext(Unit.currentUnit, unit, Component, unit);
932
+ return unit;
959
933
  }, {
960
934
  /**
961
935
  * Creates a nested HTML/SVG element within the current component
@@ -992,7 +966,9 @@ const xnew$1 = Object.assign(function (...args) {
992
966
  if (Unit.currentUnit._.state !== 'invoked') {
993
967
  throw new Error('xnew.extend can not be called after initialized.');
994
968
  }
995
- return Unit.extend(Unit.currentUnit, Component, props);
969
+ const defines = Unit.extend(Unit.currentUnit, Component, props);
970
+ Unit.addContext(Unit.currentUnit, Unit.currentUnit, Component, Unit.currentUnit);
971
+ return defines;
996
972
  }
997
973
  catch (error) {
998
974
  console.error('xnew.extend(component: Function, props?: Object): ', error);
@@ -1001,7 +977,7 @@ const xnew$1 = Object.assign(function (...args) {
1001
977
  },
1002
978
  /**
1003
979
  * Gets a context value that can be accessed in follow context
1004
- * @param component - component function
980
+ * @param key - component function
1005
981
  * @returns The context value
1006
982
  * @example
1007
983
  * // Create unit
@@ -1011,12 +987,12 @@ const xnew$1 = Object.assign(function (...args) {
1011
987
  * // Get context in child
1012
988
  * const a = xnew.context(A)
1013
989
  */
1014
- context(component) {
990
+ context(key) {
1015
991
  try {
1016
- return Unit.context(Unit.currentUnit, component);
992
+ return Unit.getContext(Unit.currentUnit, key);
1017
993
  }
1018
994
  catch (error) {
1019
- console.error('xnew.context(component: Function): ', error);
995
+ console.error('xnew.context(key: any): ', error);
1020
996
  throw error;
1021
997
  }
1022
998
  },
@@ -1029,16 +1005,18 @@ const xnew$1 = Object.assign(function (...args) {
1029
1005
  */
1030
1006
  promise(promise) {
1031
1007
  try {
1032
- const component = Unit.currentUnit._.currentComponent;
1008
+ const Component = Unit.currentUnit._.currentComponent;
1033
1009
  let unitPromise;
1034
1010
  if (promise instanceof Unit) {
1035
- unitPromise = new UnitPromise(promise._.done.promise, component);
1011
+ const unit = promise;
1012
+ unitPromise = new UnitPromise(Promise.all(unit._.promises.map(p => p.promise)), Component)
1013
+ .then(() => unit._.results);
1036
1014
  }
1037
1015
  else if (promise instanceof Promise) {
1038
- unitPromise = new UnitPromise(promise, component);
1016
+ unitPromise = new UnitPromise(promise, Component);
1039
1017
  }
1040
1018
  else {
1041
- unitPromise = new UnitPromise(new Promise(xnew$1.scope(promise)), component);
1019
+ unitPromise = new UnitPromise(new Promise(xnew$1.scope(promise)), Component);
1042
1020
  }
1043
1021
  Unit.currentUnit._.promises.push(unitPromise);
1044
1022
  return unitPromise;
@@ -1057,11 +1035,12 @@ const xnew$1 = Object.assign(function (...args) {
1057
1035
  */
1058
1036
  then(callback) {
1059
1037
  try {
1038
+ const currentUnit = Unit.currentUnit;
1060
1039
  const Component = Unit.currentUnit._.currentComponent;
1061
1040
  const promises = Unit.currentUnit._.promises;
1062
1041
  return new UnitPromise(Promise.all(promises.map(p => p.promise)), null)
1063
- .then((results) => {
1064
- callback(results.filter((_, index) => promises[index].Component !== null && promises[index].Component === Component));
1042
+ .then(() => {
1043
+ callback(currentUnit._.results);
1065
1044
  });
1066
1045
  }
1067
1046
  catch (error) {
@@ -1088,36 +1067,18 @@ const xnew$1 = Object.assign(function (...args) {
1088
1067
  }
1089
1068
  },
1090
1069
  /**
1091
- * Resolves the current unit's promise with the given value
1092
- * @param value - Value to resolve the promise with
1070
+ * Assigns a value to the current unit's promise
1071
+ * @param object - object to assign to the promise
1093
1072
  * @returns void
1094
1073
  * @example
1095
- * xnew.resolve('data');
1074
+ * xnew.assign({ data: 123});
1096
1075
  */
1097
- resolve(value) {
1076
+ assign(object) {
1098
1077
  try {
1099
- const done = Unit.currentUnit._.done;
1100
- done.resolve(value);
1078
+ Object.assign(Unit.currentUnit._.results, object);
1101
1079
  }
1102
1080
  catch (error) {
1103
- console.error('xnew.resolve(value?: any): ', error);
1104
- throw error;
1105
- }
1106
- },
1107
- /**
1108
- * Rejects the current unit's promise with the given reason
1109
- * @param reason - Reason to reject the promise
1110
- * @returns void
1111
- * @example
1112
- * xnew.reject(new Error('Something went wrong'));
1113
- */
1114
- reject(reason) {
1115
- try {
1116
- const done = Unit.currentUnit._.done;
1117
- done.reject(reason);
1118
- }
1119
- catch (error) {
1120
- console.error('xnew.reject(reason?: any): ', error);
1081
+ console.error('xnew.assign(object?: Record<string, any>): ', error);
1121
1082
  throw error;
1122
1083
  }
1123
1084
  },
@@ -1154,19 +1115,19 @@ const xnew$1 = Object.assign(function (...args) {
1154
1115
  },
1155
1116
  /**
1156
1117
  * Finds all instances of a component in the component tree
1157
- * @param component - Component function to search for
1118
+ * @param Component - Component function to search for
1158
1119
  * @returns Array of Unit instances matching the component
1159
1120
  * @throws Error if component parameter is invalid
1160
1121
  * @example
1161
1122
  * const buttons = xnew.find(ButtonComponent)
1162
1123
  * buttons.forEach(btn => btn.finalize())
1163
1124
  */
1164
- find(component) {
1125
+ find(Component) {
1165
1126
  try {
1166
- return Unit.find(component);
1127
+ return Unit.find(Component);
1167
1128
  }
1168
1129
  catch (error) {
1169
- console.error('xnew.find(component: Function): ', error);
1130
+ console.error('xnew.find(Component: Function): ', error);
1170
1131
  throw error;
1171
1132
  }
1172
1133
  },
@@ -1437,23 +1398,23 @@ function Panel(unit, { name, open = false, params }) {
1437
1398
  const button = xnew$1(Button, { key });
1438
1399
  return button;
1439
1400
  },
1440
- select(key, { options = [] } = {}) {
1401
+ select(key, { value, items = [] } = {}) {
1441
1402
  var _a, _b;
1442
- object[key] = (_b = (_a = object[key]) !== null && _a !== void 0 ? _a : options[0]) !== null && _b !== void 0 ? _b : '';
1443
- const select = xnew$1(Select, { key, value: object[key], options });
1403
+ object[key] = (_b = (_a = value !== null && value !== void 0 ? value : object[key]) !== null && _a !== void 0 ? _a : items[0]) !== null && _b !== void 0 ? _b : '';
1404
+ const select = xnew$1(Select, { key, value: object[key], items });
1444
1405
  select.on('input', ({ value }) => object[key] = value);
1445
1406
  return select;
1446
1407
  },
1447
- range(key, options = {}) {
1448
- var _a, _b;
1449
- object[key] = (_b = (_a = object[key]) !== null && _a !== void 0 ? _a : options.min) !== null && _b !== void 0 ? _b : 0;
1450
- const number = xnew$1(Range, Object.assign({ key, value: object[key] }, options));
1408
+ range(key, { value, min = 0, max = 100, step = 1 } = {}) {
1409
+ var _a;
1410
+ object[key] = (_a = value !== null && value !== void 0 ? value : object[key]) !== null && _a !== void 0 ? _a : min;
1411
+ const number = xnew$1(Range, { key, value: object[key], min, max, step });
1451
1412
  number.on('input', ({ value }) => object[key] = value);
1452
1413
  return number;
1453
1414
  },
1454
- checkbox(key) {
1415
+ checkbox(key, { value } = {}) {
1455
1416
  var _a;
1456
- object[key] = (_a = object[key]) !== null && _a !== void 0 ? _a : false;
1417
+ object[key] = (_a = value !== null && value !== void 0 ? value : object[key]) !== null && _a !== void 0 ? _a : false;
1457
1418
  const checkbox = xnew$1(Checkbox, { key, value: object[key] });
1458
1419
  checkbox.on('input', ({ value }) => object[key] = value);
1459
1420
  return checkbox;
@@ -1537,14 +1498,14 @@ function Checkbox(unit, { key = '', value } = {}) {
1537
1498
  update(value);
1538
1499
  });
1539
1500
  }
1540
- function Select(_, { key = '', value, options = [] } = {}) {
1501
+ function Select(_, { key = '', value, items = [] } = {}) {
1541
1502
  var _a;
1542
- const initial = (_a = value !== null && value !== void 0 ? value : options[0]) !== null && _a !== void 0 ? _a : '';
1503
+ const initial = (_a = value !== null && value !== void 0 ? value : items[0]) !== null && _a !== void 0 ? _a : '';
1543
1504
  xnew$1.nest(`<div style="position: relative; height: 2em; margin: 0.125em 0; padding: 0 0.5em; display: flex; align-items: center;">`);
1544
1505
  xnew$1('<div style="flex: 1;">', key);
1545
1506
  const native = xnew$1(`<select name="${key}" style="display: none;">`, () => {
1546
- for (const option of options) {
1547
- xnew$1(`<option value="${option}" ${option === initial ? 'selected' : ''}>`, option);
1507
+ for (const item of items) {
1508
+ xnew$1(`<option value="${item}" ${item === initial ? 'selected' : ''}>`, item);
1548
1509
  }
1549
1510
  });
1550
1511
  const button = xnew$1(`<div style="height: 2em; padding: 0 1.5em 0 0.5em; display: flex; align-items: center; border: 1px solid ${currentColorA}; border-radius: 0.25em; cursor: pointer; user-select: none; min-width: 3em; white-space: nowrap;">`, initial);
@@ -1564,13 +1525,13 @@ function Select(_, { key = '', value, options = [] } = {}) {
1564
1525
  });
1565
1526
  xnew$1.extend(Accordion);
1566
1527
  xnew$1.nest(`<div style="position: relative; border: 1px solid ${currentColorA}; border-radius: 0.25em; overflow: hidden;">`);
1567
- for (const option of options) {
1568
- const item = xnew$1(`<div style="height: 2em; padding: 0 0.5em; display: flex; align-items: center; cursor: pointer; user-select: none;">`, option);
1569
- item.on('pointerover', () => item.element.style.background = currentColorB);
1570
- item.on('pointerout', () => item.element.style.background = '');
1571
- item.on('click', () => {
1572
- button.element.textContent = option;
1573
- native.element.value = option;
1528
+ for (const item of items) {
1529
+ const div = xnew$1(`<div style="height: 2em; padding: 0 0.5em; display: flex; align-items: center; cursor: pointer; user-select: none;">`, item);
1530
+ div.on('pointerover', () => div.element.style.background = currentColorB);
1531
+ div.on('pointerout', () => div.element.style.background = '');
1532
+ div.on('click', () => {
1533
+ button.element.textContent = item;
1534
+ native.element.value = item;
1574
1535
  native.element.dispatchEvent(new Event('input', { bubbles: false }));
1575
1536
  list.finalize();
1576
1537
  });