@mulsense/xnew 0.6.4 → 0.6.5

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.
@@ -6,7 +6,7 @@
6
6
 
7
7
  var xmatter = {
8
8
  initialize({} = {}) {
9
- xnew(Root, {});
9
+ xnew.promise(xnew(Root, {}));
10
10
  },
11
11
  get engine() {
12
12
  var _a;
@@ -3,7 +3,7 @@ import Matter from 'matter-js';
3
3
 
4
4
  var xmatter = {
5
5
  initialize({} = {}) {
6
- xnew(Root, {});
6
+ xnew.promise(xnew(Root, {}));
7
7
  },
8
8
  get engine() {
9
9
  var _a;
@@ -25,7 +25,7 @@
25
25
 
26
26
  var xpixi = {
27
27
  initialize({ canvas = null } = {}) {
28
- xnew(Root, { canvas });
28
+ xnew.promise(xnew(Root, { canvas }));
29
29
  },
30
30
  nest(object) {
31
31
  xnew(Nest, { object });
@@ -49,7 +49,10 @@
49
49
  xnew.promise(PIXI__namespace.autoDetectRenderer({
50
50
  width: canvas.width, height: canvas.height, view: canvas,
51
51
  antialias: true, backgroundAlpha: 0,
52
- })).then((value) => renderer = value);
52
+ })).then((value) => {
53
+ renderer = value;
54
+ xnew.resolve();
55
+ });
53
56
  let scene = new PIXI__namespace.Container();
54
57
  return {
55
58
  get renderer() { return renderer; },
@@ -3,7 +3,7 @@ import * as PIXI from 'pixi.js';
3
3
 
4
4
  var xpixi = {
5
5
  initialize({ canvas = null } = {}) {
6
- xnew(Root, { canvas });
6
+ xnew.promise(xnew(Root, { canvas }));
7
7
  },
8
8
  nest(object) {
9
9
  xnew(Nest, { object });
@@ -27,7 +27,10 @@ function Root(unit, { canvas }) {
27
27
  xnew.promise(PIXI.autoDetectRenderer({
28
28
  width: canvas.width, height: canvas.height, view: canvas,
29
29
  antialias: true, backgroundAlpha: 0,
30
- })).then((value) => renderer = value);
30
+ })).then((value) => {
31
+ renderer = value;
32
+ xnew.resolve();
33
+ });
31
34
  let scene = new PIXI.Container();
32
35
  return {
33
36
  get renderer() { return renderer; },
@@ -6,7 +6,7 @@
6
6
 
7
7
  var xrapier2d = {
8
8
  initialize({ gravity = { x: 0.0, y: -9.81 } } = {}) {
9
- xnew(Root, { gravity });
9
+ xnew.promise(xnew(Root, { gravity }));
10
10
  },
11
11
  get world() {
12
12
  var _a;
@@ -3,7 +3,7 @@ import RAPIER from '@dimforge/rapier2d-compat';
3
3
 
4
4
  var xrapier2d = {
5
5
  initialize({ gravity = { x: 0.0, y: -9.81 } } = {}) {
6
- xnew(Root, { gravity });
6
+ xnew.promise(xnew(Root, { gravity }));
7
7
  },
8
8
  get world() {
9
9
  var _a;
@@ -6,7 +6,7 @@
6
6
 
7
7
  var xrapier3d = {
8
8
  initialize({ gravity = { x: 0.0, y: -9.81, z: 0.0 } } = {}) {
9
- xnew(Root, { gravity });
9
+ xnew.promise(xnew(Root, { gravity }));
10
10
  },
11
11
  get world() {
12
12
  var _a;
@@ -3,7 +3,7 @@ import RAPIER from '@dimforge/rapier3d-compat';
3
3
 
4
4
  var xrapier3d = {
5
5
  initialize({ gravity = { x: 0.0, y: -9.81, z: 0.0 } } = {}) {
6
- xnew(Root, { gravity });
6
+ xnew.promise(xnew(Root, { gravity }));
7
7
  },
8
8
  get world() {
9
9
  var _a;
@@ -25,7 +25,7 @@
25
25
 
26
26
  var xthree = {
27
27
  initialize({ canvas = null, camera = null } = {}) {
28
- xnew(Root, { canvas, camera });
28
+ xnew.promise(xnew(Root, { canvas, camera }));
29
29
  },
30
30
  nest(object) {
31
31
  xnew(Nest, { object });
@@ -53,6 +53,7 @@
53
53
  renderer.setClearColor(0x000000, 0);
54
54
  camera = camera !== null && camera !== void 0 ? camera : new THREE__namespace.PerspectiveCamera(45, renderer.domElement.width / renderer.domElement.height);
55
55
  const scene = new THREE__namespace.Scene();
56
+ xnew.resolve();
56
57
  return {
57
58
  get canvas() { return canvas; },
58
59
  get camera() { return camera; },
@@ -3,7 +3,7 @@ import * as THREE from 'three';
3
3
 
4
4
  var xthree = {
5
5
  initialize({ canvas = null, camera = null } = {}) {
6
- xnew(Root, { canvas, camera });
6
+ xnew.promise(xnew(Root, { canvas, camera }));
7
7
  },
8
8
  nest(object) {
9
9
  xnew(Nest, { object });
@@ -31,6 +31,7 @@ function Root(unit, { canvas, camera }) {
31
31
  renderer.setClearColor(0x000000, 0);
32
32
  camera = camera !== null && camera !== void 0 ? camera : new THREE.PerspectiveCamera(45, renderer.domElement.width / renderer.domElement.height);
33
33
  const scene = new THREE.Scene();
34
+ xnew.resolve();
34
35
  return {
35
36
  get canvas() { return canvas; },
36
37
  get camera() { return camera; },
package/dist/xnew.d.ts CHANGED
@@ -92,6 +92,11 @@ interface Internal {
92
92
  ancestors: Unit[];
93
93
  children: Unit[];
94
94
  promises: UnitPromise[];
95
+ done: {
96
+ promise: Promise<any>;
97
+ resolve: Function;
98
+ reject: Function;
99
+ };
95
100
  nestElements: {
96
101
  element: UnitElement;
97
102
  owned: boolean;
@@ -281,12 +286,14 @@ declare const xnew: CreateUnit & {
281
286
  promise(promise: Promise<any> | Unit): UnitPromise;
282
287
  then(callback: Function): UnitPromise;
283
288
  catch(callback: Function): UnitPromise;
289
+ resolve(value?: any): void;
290
+ reject(reason?: any): void;
284
291
  finally(callback: Function): UnitPromise;
285
292
  scope(callback: any): any;
286
293
  find(component: Function): Unit[];
287
294
  emit(type: string, ...args: any[]): void;
288
- timeout(timeout: Function, duration?: number): UnitTimer;
289
- interval(interval: Function, duration: number, iterations?: number): UnitTimer;
295
+ timeout(callback: Function, duration?: number): UnitTimer;
296
+ interval(callback: Function, duration: number, iterations?: number): UnitTimer;
290
297
  transition(transition: Function, duration?: number, easing?: string): UnitTimer;
291
298
  protect(): void;
292
299
  } & {
package/dist/xnew.js CHANGED
@@ -664,6 +664,12 @@
664
664
  static initialize(unit, anchor) {
665
665
  const backup = Unit.currentUnit;
666
666
  Unit.currentUnit = unit;
667
+ const done = {
668
+ promise: null,
669
+ resolve: null,
670
+ reject: null
671
+ };
672
+ done.promise = new Promise((resolve, reject) => { done.resolve = resolve; done.reject = reject; });
667
673
  unit._ = Object.assign(unit._, {
668
674
  currentElement: unit._.baseElement,
669
675
  currentContext: unit._.baseContext,
@@ -676,6 +682,7 @@
676
682
  children: [],
677
683
  nestElements: [],
678
684
  promises: [],
685
+ done,
679
686
  components: [],
680
687
  listeners: new MapMap(),
681
688
  defines: {},
@@ -689,7 +696,8 @@
689
696
  // setup component
690
697
  Unit.extend(unit, unit._.baseComponent, unit._.props);
691
698
  // whether the unit promise was resolved
692
- Promise.all(unit._.promises.map(p => p.promise)).then(() => unit._.state = 'initialized');
699
+ // Promise.all(unit._.promises.map(p => p.promise)).then(() => unit._.state = 'initialized');
700
+ unit._.state = 'initialized';
693
701
  Unit.currentUnit = backup;
694
702
  }
695
703
  static finalize(unit) {
@@ -1030,11 +1038,14 @@
1030
1038
  const component = Unit.currentUnit._.currentComponent;
1031
1039
  let unitPromise;
1032
1040
  if (promise instanceof Unit) {
1033
- unitPromise = new UnitPromise(Promise.all(promise._.promises.map(p => p.promise)), component);
1041
+ unitPromise = new UnitPromise(promise._.done.promise, component);
1034
1042
  }
1035
- else {
1043
+ else if (promise instanceof Promise) {
1036
1044
  unitPromise = new UnitPromise(promise, component);
1037
1045
  }
1046
+ else {
1047
+ unitPromise = new UnitPromise(new Promise(xnew$1.scope(promise)), component);
1048
+ }
1038
1049
  Unit.currentUnit._.promises.push(unitPromise);
1039
1050
  return unitPromise;
1040
1051
  }
@@ -1082,6 +1093,40 @@
1082
1093
  throw error;
1083
1094
  }
1084
1095
  },
1096
+ /**
1097
+ * Resolves the current unit's promise with the given value
1098
+ * @param value - Value to resolve the promise with
1099
+ * @returns void
1100
+ * @example
1101
+ * xnew.resolve('data');
1102
+ */
1103
+ resolve(value) {
1104
+ try {
1105
+ const done = Unit.currentUnit._.done;
1106
+ done.resolve(value);
1107
+ }
1108
+ catch (error) {
1109
+ console.error('xnew.resolve(value?: any): ', error);
1110
+ throw error;
1111
+ }
1112
+ },
1113
+ /**
1114
+ * Rejects the current unit's promise with the given reason
1115
+ * @param reason - Reason to reject the promise
1116
+ * @returns void
1117
+ * @example
1118
+ * xnew.reject(new Error('Something went wrong'));
1119
+ */
1120
+ reject(reason) {
1121
+ try {
1122
+ const done = Unit.currentUnit._.done;
1123
+ done.reject(reason);
1124
+ }
1125
+ catch (error) {
1126
+ console.error('xnew.reject(reason?: any): ', error);
1127
+ throw error;
1128
+ }
1129
+ },
1085
1130
  /**
1086
1131
  * Executes callback after all registered promises settle (resolve or reject)
1087
1132
  * @param callback - Function to call after promises settle
package/dist/xnew.mjs CHANGED
@@ -658,6 +658,12 @@ class Unit {
658
658
  static initialize(unit, anchor) {
659
659
  const backup = Unit.currentUnit;
660
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; });
661
667
  unit._ = Object.assign(unit._, {
662
668
  currentElement: unit._.baseElement,
663
669
  currentContext: unit._.baseContext,
@@ -670,6 +676,7 @@ class Unit {
670
676
  children: [],
671
677
  nestElements: [],
672
678
  promises: [],
679
+ done,
673
680
  components: [],
674
681
  listeners: new MapMap(),
675
682
  defines: {},
@@ -683,7 +690,8 @@ class Unit {
683
690
  // setup component
684
691
  Unit.extend(unit, unit._.baseComponent, unit._.props);
685
692
  // whether the unit promise was resolved
686
- Promise.all(unit._.promises.map(p => p.promise)).then(() => unit._.state = 'initialized');
693
+ // Promise.all(unit._.promises.map(p => p.promise)).then(() => unit._.state = 'initialized');
694
+ unit._.state = 'initialized';
687
695
  Unit.currentUnit = backup;
688
696
  }
689
697
  static finalize(unit) {
@@ -1024,11 +1032,14 @@ const xnew$1 = Object.assign(function (...args) {
1024
1032
  const component = Unit.currentUnit._.currentComponent;
1025
1033
  let unitPromise;
1026
1034
  if (promise instanceof Unit) {
1027
- unitPromise = new UnitPromise(Promise.all(promise._.promises.map(p => p.promise)), component);
1035
+ unitPromise = new UnitPromise(promise._.done.promise, component);
1028
1036
  }
1029
- else {
1037
+ else if (promise instanceof Promise) {
1030
1038
  unitPromise = new UnitPromise(promise, component);
1031
1039
  }
1040
+ else {
1041
+ unitPromise = new UnitPromise(new Promise(xnew$1.scope(promise)), component);
1042
+ }
1032
1043
  Unit.currentUnit._.promises.push(unitPromise);
1033
1044
  return unitPromise;
1034
1045
  }
@@ -1076,6 +1087,40 @@ const xnew$1 = Object.assign(function (...args) {
1076
1087
  throw error;
1077
1088
  }
1078
1089
  },
1090
+ /**
1091
+ * Resolves the current unit's promise with the given value
1092
+ * @param value - Value to resolve the promise with
1093
+ * @returns void
1094
+ * @example
1095
+ * xnew.resolve('data');
1096
+ */
1097
+ resolve(value) {
1098
+ try {
1099
+ const done = Unit.currentUnit._.done;
1100
+ done.resolve(value);
1101
+ }
1102
+ 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);
1121
+ throw error;
1122
+ }
1123
+ },
1079
1124
  /**
1080
1125
  * Executes callback after all registered promises settle (resolve or reject)
1081
1126
  * @param callback - Function to call after promises settle
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "keywords": [
5
5
  "Component-Oriented Programming"
6
6
  ],
7
- "version": "0.6.4",
7
+ "version": "0.6.5",
8
8
  "main": "dist/xnew.js",
9
9
  "module": "dist/xnew.mjs",
10
10
  "types": "dist/xnew.d.ts",