@mulsense/xnew 0.1.0 → 0.1.2

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
@@ -102,56 +102,42 @@ class Timer {
102
102
  }
103
103
  }
104
104
 
105
- //----------------------------------------------------------------------------------------------------
106
- // map ex
107
- //----------------------------------------------------------------------------------------------------
108
- class MapEx {
109
- constructor() {
110
- this.map = new Map;
111
- }
112
- get size() {
113
- return this.map.size;
114
- }
115
- forEach(callback) {
116
- this.map.forEach(callback);
117
- }
118
- clear() {
119
- this.map.clear();
120
- }
121
- }
122
105
  //----------------------------------------------------------------------------------------------------
123
106
  // map set
124
107
  //----------------------------------------------------------------------------------------------------
125
- class MapSet extends MapEx {
108
+ class MapSet extends Map {
126
109
  has(key, value) {
127
110
  var _a, _b;
128
111
  if (value === undefined) {
129
- return this.map.has(key);
112
+ return super.has(key);
130
113
  }
131
114
  else {
132
- return (_b = (_a = this.map.get(key)) === null || _a === void 0 ? void 0 : _a.has(value)) !== null && _b !== void 0 ? _b : false;
115
+ return (_b = (_a = super.get(key)) === null || _a === void 0 ? void 0 : _a.has(value)) !== null && _b !== void 0 ? _b : false;
133
116
  }
134
117
  }
135
- get(key) {
136
- return this.map.get(key);
137
- }
138
- keys() {
139
- return this.map.keys();
140
- }
141
118
  add(key, value) {
142
119
  var _a;
143
- this.map.set(key, ((_a = this.map.get(key)) !== null && _a !== void 0 ? _a : new Set).add(value));
120
+ super.set(key, ((_a = super.get(key)) !== null && _a !== void 0 ? _a : new Set).add(value));
144
121
  return this;
145
122
  }
123
+ keys(key) {
124
+ var _a, _b;
125
+ if (key === undefined) {
126
+ return super.keys();
127
+ }
128
+ else {
129
+ return (_b = (_a = super.get(key)) === null || _a === void 0 ? void 0 : _a.values()) !== null && _b !== void 0 ? _b : [].values();
130
+ }
131
+ }
146
132
  delete(key, value) {
147
133
  var _a, _b, _c, _d;
148
134
  let ret = false;
149
135
  if (value === undefined) {
150
- ret = (((_a = this.map.get(key)) === null || _a === void 0 ? void 0 : _a.size) === 0) ? this.map.delete(key) : false;
136
+ ret = (((_a = super.get(key)) === null || _a === void 0 ? void 0 : _a.size) === 0) ? super.delete(key) : false;
151
137
  }
152
138
  else {
153
- ret = (_c = (_b = this.map.get(key)) === null || _b === void 0 ? void 0 : _b.delete(value)) !== null && _c !== void 0 ? _c : false;
154
- (((_d = this.map.get(key)) === null || _d === void 0 ? void 0 : _d.size) === 0) && this.map.delete(key);
139
+ ret = (_c = (_b = super.get(key)) === null || _b === void 0 ? void 0 : _b.delete(value)) !== null && _c !== void 0 ? _c : false;
140
+ (((_d = super.get(key)) === null || _d === void 0 ? void 0 : _d.size) === 0) && super.delete(key);
155
141
  }
156
142
  return ret;
157
143
  }
@@ -159,130 +145,70 @@ class MapSet extends MapEx {
159
145
  //----------------------------------------------------------------------------------------------------
160
146
  // map map
161
147
  //----------------------------------------------------------------------------------------------------
162
- class MapMap extends MapEx {
148
+ class MapMap extends Map {
163
149
  has(key1, key2) {
164
150
  var _a, _b;
165
151
  if (key2 === undefined) {
166
- return this.map.has(key1);
152
+ return super.has(key1);
167
153
  }
168
154
  else {
169
- return (_b = (_a = this.map.get(key1)) === null || _a === void 0 ? void 0 : _a.has(key2)) !== null && _b !== void 0 ? _b : false;
155
+ return (_b = (_a = super.get(key1)) === null || _a === void 0 ? void 0 : _a.has(key2)) !== null && _b !== void 0 ? _b : false;
170
156
  }
171
157
  }
172
- set(key1, key2, value) {
158
+ set(key1, key2OrValue, value) {
173
159
  var _a;
174
- this.map.set(key1, ((_a = this.map.get(key1)) !== null && _a !== void 0 ? _a : new Map).set(key2, value));
160
+ if (value === undefined) {
161
+ // 2 args: directly set Map<Key2, Value>
162
+ super.set(key1, key2OrValue);
163
+ }
164
+ else {
165
+ // 3 args: set nested value
166
+ super.set(key1, ((_a = super.get(key1)) !== null && _a !== void 0 ? _a : new Map).set(key2OrValue, value));
167
+ }
175
168
  return this;
176
169
  }
177
170
  get(key1, key2) {
178
171
  var _a;
179
172
  if (key2 === undefined) {
180
- return this.map.get(key1);
173
+ return super.get(key1);
181
174
  }
182
175
  else {
183
- return (_a = this.map.get(key1)) === null || _a === void 0 ? void 0 : _a.get(key2);
176
+ return (_a = super.get(key1)) === null || _a === void 0 ? void 0 : _a.get(key2);
184
177
  }
185
178
  }
186
179
  keys(key1) {
187
180
  var _a, _b;
188
181
  if (key1 === undefined) {
189
- return this.map.keys();
182
+ return super.keys();
190
183
  }
191
184
  else {
192
- return (_b = (_a = this.map.get(key1)) === null || _a === void 0 ? void 0 : _a.keys()) !== null && _b !== void 0 ? _b : (function* () { })();
185
+ return (_b = (_a = super.get(key1)) === null || _a === void 0 ? void 0 : _a.keys()) !== null && _b !== void 0 ? _b : [].values();
193
186
  }
194
187
  }
195
188
  delete(key1, key2) {
196
189
  var _a, _b, _c, _d;
197
190
  let ret = false;
198
191
  if (key2 === undefined) {
199
- ret = (((_a = this.map.get(key1)) === null || _a === void 0 ? void 0 : _a.size) === 0) ? this.map.delete(key1) : false;
200
- }
201
- else {
202
- ret = (_c = (_b = this.map.get(key1)) === null || _b === void 0 ? void 0 : _b.delete(key2)) !== null && _c !== void 0 ? _c : false;
203
- (((_d = this.map.get(key1)) === null || _d === void 0 ? void 0 : _d.size) === 0) && this.map.delete(key1);
204
- }
205
- return ret;
206
- }
207
- }
208
- //----------------------------------------------------------------------------------------------------
209
- // map map map
210
- //----------------------------------------------------------------------------------------------------
211
- class MapMapMap extends MapEx {
212
- has(key1, key2, key3) {
213
- var _a, _b;
214
- if (key2 === undefined) {
215
- return this.map.has(key1);
216
- }
217
- else {
218
- return (_b = (_a = this.map.get(key1)) === null || _a === void 0 ? void 0 : _a.has(key2, key3)) !== null && _b !== void 0 ? _b : false;
219
- }
220
- }
221
- set(key1, key2, key3, value) {
222
- var _a;
223
- this.map.set(key1, ((_a = this.map.get(key1)) !== null && _a !== void 0 ? _a : new MapMap).set(key2, key3, value));
224
- return this;
225
- }
226
- get(key1, key2, key3) {
227
- var _a, _b;
228
- if (key2 === undefined) {
229
- return this.map.get(key1);
230
- }
231
- else if (key3 === undefined) {
232
- return (_a = this.map.get(key1)) === null || _a === void 0 ? void 0 : _a.get(key2);
233
- }
234
- else {
235
- return (_b = this.map.get(key1)) === null || _b === void 0 ? void 0 : _b.get(key2, key3);
236
- }
237
- }
238
- keys(key1, key2) {
239
- var _a, _b, _c, _d, _e;
240
- if (key1 === undefined) {
241
- return this.map.keys();
242
- }
243
- else if (key2 === undefined) {
244
- return (_b = (_a = this.map.get(key1)) === null || _a === void 0 ? void 0 : _a.keys()) !== null && _b !== void 0 ? _b : (function* () { })();
245
- }
246
- else {
247
- return (_e = (_d = (_c = this.map.get(key1)) === null || _c === void 0 ? void 0 : _c.get(key2)) === null || _d === void 0 ? void 0 : _d.keys()) !== null && _e !== void 0 ? _e : (function* () { })();
248
- }
249
- }
250
- delete(key1, key2, key3) {
251
- var _a, _b, _c, _d;
252
- let ret = false;
253
- if (key2 === undefined) {
254
- ret = (((_a = this.map.get(key1)) === null || _a === void 0 ? void 0 : _a.size) === 0) ? this.map.delete(key1) : false;
192
+ ret = (((_a = super.get(key1)) === null || _a === void 0 ? void 0 : _a.size) === 0) ? super.delete(key1) : false;
255
193
  }
256
194
  else {
257
- ret = (_c = (_b = this.map.get(key1)) === null || _b === void 0 ? void 0 : _b.delete(key2, key3)) !== null && _c !== void 0 ? _c : false;
258
- (((_d = this.map.get(key1)) === null || _d === void 0 ? void 0 : _d.size) === 0) && this.map.delete(key1);
195
+ ret = (_c = (_b = super.get(key1)) === null || _b === void 0 ? void 0 : _b.delete(key2)) !== null && _c !== void 0 ? _c : false;
196
+ (((_d = super.get(key1)) === null || _d === void 0 ? void 0 : _d.size) === 0) && super.delete(key1);
259
197
  }
260
198
  return ret;
261
199
  }
262
200
  }
263
201
 
264
202
  //----------------------------------------------------------------------------------------------------
265
- // Constants
203
+ // Definitions
266
204
  //----------------------------------------------------------------------------------------------------
267
- const LIFECYCLE_EVENTS = ['start', 'update', 'stop', 'finalize'];
268
- const LIFECYCLE_STATES = {
269
- INVOKED: 'invoked',
270
- INITIALIZED: 'initialized',
271
- STARTED: 'started',
272
- STOPPED: 'stopped',
273
- PRE_FINALIZED: 'pre finalized',
274
- FINALIZED: 'finalized',
275
- };
276
- const CUSTOM_EVENT_PREFIX = {
277
- GLOBAL: '+',
278
- INTERNAL: '-',
279
- };
205
+ const SYSTEM_EVENTS = ['start', 'update', 'stop', 'finalize'];
280
206
  //----------------------------------------------------------------------------------------------------
281
- // unit main
207
+ // Unit
282
208
  //----------------------------------------------------------------------------------------------------
283
209
  class Unit {
284
210
  constructor(target, component, props) {
285
- var _a, _b, _c, _d, _e;
211
+ var _a, _b, _c;
286
212
  const parent = UnitScope.current;
287
213
  let baseElement;
288
214
  if (target instanceof HTMLElement || target instanceof SVGElement) {
@@ -292,27 +218,28 @@ class Unit {
292
218
  baseElement = (_a = parent._.currentElement) !== null && _a !== void 0 ? _a : parent._.baseElement;
293
219
  }
294
220
  else {
295
- baseElement = (_c = (_b = document.currentScript) === null || _b === void 0 ? void 0 : _b.parentElement) !== null && _c !== void 0 ? _c : document.body;
221
+ baseElement = document.body;
296
222
  }
297
- let baseComponent = null;
223
+ let baseComponent;
298
224
  if (typeof component === 'function') {
299
225
  baseComponent = component;
300
226
  }
301
227
  else if (typeof component === 'string') {
302
228
  baseComponent = (self) => { self.element.textContent = component; };
303
229
  }
230
+ else {
231
+ baseComponent = (self) => { };
232
+ }
304
233
  this._ = {
305
- root: (_d = parent === null || parent === void 0 ? void 0 : parent._.root) !== null && _d !== void 0 ? _d : this,
306
234
  parent,
307
235
  target,
236
+ baseContext: (_b = UnitScope.contexts.get(parent)) !== null && _b !== void 0 ? _b : null,
237
+ baseElement,
308
238
  baseComponent,
309
239
  props,
310
- baseElement,
311
- nextNest: { element: baseElement, position: 'beforeend' },
312
- baseContext: UnitScope.get(parent),
313
240
  };
314
- ((_e = parent === null || parent === void 0 ? void 0 : parent._.children) !== null && _e !== void 0 ? _e : Unit.roots).push(this);
315
- Unit.initialize(this);
241
+ ((_c = parent === null || parent === void 0 ? void 0 : parent._.children) !== null && _c !== void 0 ? _c : Unit.roots).push(this);
242
+ Unit.initialize(this, { element: baseElement, position: 'beforeend' });
316
243
  }
317
244
  get element() {
318
245
  return this._.currentElement;
@@ -336,80 +263,32 @@ class Unit {
336
263
  }
337
264
  reboot() {
338
265
  Unit.stop(this);
339
- let trace = this._.currentElement;
340
- while (trace.parentElement && trace.parentElement !== this._.baseElement) {
341
- trace = trace.parentElement;
266
+ let first = this._.currentElement;
267
+ while (first.parentElement && first.parentElement !== this._.baseElement) {
268
+ first = first.parentElement;
342
269
  }
343
- if (trace.parentElement === this._.baseElement) {
344
- this._.nextNest.element = trace.nextElementSibling;
345
- this._.nextNest.position = 'beforebegin';
270
+ let nextNest;
271
+ if (first.parentElement === this._.baseElement && first.nextElementSibling) {
272
+ nextNest = { element: first.nextElementSibling, position: 'beforebegin' };
346
273
  }
347
- Unit.finalize(this);
348
- Unit.initialize(this);
349
- }
350
- get components() {
351
- return this._.components;
352
- }
353
- on(type, listener, options) {
354
- try {
355
- if (typeof type === 'string') {
356
- const filtered = type.trim().split(/\s+/).filter((type) => LIFECYCLE_EVENTS.includes(type));
357
- filtered.forEach((type) => {
358
- this._.system[type].push(listener);
359
- });
360
- }
361
- UnitEvent.on(this, type, listener, options);
362
- }
363
- catch (error) {
364
- console.error('unit.on(type, listener, option?): ', error);
365
- }
366
- return this;
367
- }
368
- off(type, listener) {
369
- try {
370
- if (type === undefined) {
371
- this._.system = { start: [], update: [], stop: [], finalize: [] };
372
- }
373
- else if (typeof type === 'string') {
374
- const filtered = type.trim().split(/\s+/).filter((type) => LIFECYCLE_EVENTS.includes(type));
375
- filtered.forEach((type) => {
376
- if (listener === undefined) {
377
- this._.system[type] = [];
378
- }
379
- else {
380
- this._.system[type] = this._.system[type].filter((l) => l !== listener);
381
- }
382
- });
383
- }
384
- UnitEvent.off(this, type, listener);
385
- }
386
- catch (error) {
387
- console.error('unit.off(type, listener): ', error);
388
- }
389
- return this;
390
- }
391
- emit(type, ...args) {
392
- try {
393
- UnitEvent.emit(this, type, ...args);
394
- }
395
- catch (error) {
396
- console.error('unit.emit(type, ...args): ', error);
274
+ else {
275
+ nextNest = { element: this._.baseElement, position: 'beforeend' };
397
276
  }
277
+ Unit.finalize(this);
278
+ Unit.initialize(this, nextNest);
398
279
  }
399
- //----------------------------------------------------------------------------------------------------
400
- // internal
401
- //----------------------------------------------------------------------------------------------------
402
- static initialize(unit) {
280
+ static initialize(unit, nextNest) {
403
281
  var _a;
404
282
  unit._ = Object.assign(unit._, {
283
+ nextNest,
405
284
  children: [],
406
- components: [],
285
+ components: new Set(),
286
+ listeners: new MapMap(),
287
+ sublisteners: new MapMap(),
407
288
  captures: [],
408
- state: LIFECYCLE_STATES.INVOKED,
289
+ state: 'invoked',
409
290
  tostart: true,
410
291
  currentElement: unit._.baseElement,
411
- upcount: 0,
412
- resolved: false,
413
292
  defines: {},
414
293
  system: { start: [], update: [], stop: [], finalize: [] },
415
294
  });
@@ -423,12 +302,10 @@ class Unit {
423
302
  UnitScope.execute({ unit, context: null, element: null }, () => Unit.extend(unit, unit._.baseComponent, unit._.props));
424
303
  }
425
304
  // whether the unit promise was resolved
426
- (_a = UnitPromise.get(unit)) === null || _a === void 0 ? void 0 : _a.then(() => {
427
- unit._.resolved = true;
428
- });
429
- unit._.state = LIFECYCLE_STATES.INITIALIZED;
305
+ (_a = UnitPromise.get(unit)) === null || _a === void 0 ? void 0 : _a.then(() => unit._.state = 'initialized');
306
+ // setup capture
430
307
  let current = unit;
431
- while (current !== null) {
308
+ while (1) {
432
309
  let captured = false;
433
310
  for (const capture of current._.captures) {
434
311
  if (capture.checker(unit)) {
@@ -436,7 +313,7 @@ class Unit {
436
313
  captured = true;
437
314
  }
438
315
  }
439
- if (captured === false) {
316
+ if (captured === false && current._.parent !== null) {
440
317
  current = current._.parent;
441
318
  }
442
319
  else {
@@ -445,16 +322,18 @@ class Unit {
445
322
  }
446
323
  }
447
324
  static finalize(unit) {
448
- if (unit._.state !== LIFECYCLE_STATES.FINALIZED && unit._.state !== LIFECYCLE_STATES.PRE_FINALIZED) {
449
- unit._.state = LIFECYCLE_STATES.PRE_FINALIZED;
325
+ if (unit._.state !== 'finalized' && unit._.state !== 'pre-finalized') {
326
+ unit._.state = 'pre-finalized';
450
327
  unit._.children.forEach((child) => child.finalize());
451
328
  unit._.system.finalize.forEach((listener) => {
452
329
  UnitScope.execute(UnitScope.snapshot(unit), listener);
453
330
  });
454
- UnitEvent.off(unit);
455
- UnitEvent.suboff(unit, null);
331
+ unit.off();
332
+ Unit.suboff(unit, null);
333
+ unit._.components.forEach((component) => {
334
+ Unit.componentUnits.delete(component, unit);
335
+ });
456
336
  UnitScope.finalize(unit);
457
- UnitComponent.finalize(unit);
458
337
  UnitPromise.finalize(unit);
459
338
  while (unit._.currentElement !== unit._.baseElement && unit._.currentElement.parentElement !== null) {
460
339
  const parent = unit._.currentElement.parentElement;
@@ -463,12 +342,12 @@ class Unit {
463
342
  }
464
343
  // reset defines
465
344
  Object.keys(unit._.defines).forEach((key) => {
466
- if (!LIFECYCLE_EVENTS.includes(key)) {
345
+ if (SYSTEM_EVENTS.includes(key) === false) {
467
346
  delete unit[key];
468
347
  }
469
348
  });
470
349
  unit._.defines = {};
471
- unit._.state = LIFECYCLE_STATES.FINALIZED;
350
+ unit._.state = 'finalized';
472
351
  }
473
352
  }
474
353
  static nest(unit, tag) {
@@ -490,51 +369,49 @@ class Unit {
490
369
  }
491
370
  static extend(unit, component, props) {
492
371
  var _a;
493
- unit._.components.push(component);
494
- UnitComponent.add(unit, component);
372
+ unit._.components.add(component);
373
+ Unit.componentUnits.add(component, unit);
495
374
  const defines = (_a = component(unit, props)) !== null && _a !== void 0 ? _a : {};
496
- const snapshot = UnitScope.snapshot(unit);
497
375
  Object.keys(defines).forEach((key) => {
498
- const descriptor = Object.getOwnPropertyDescriptor(defines, key);
499
376
  if (unit[key] !== undefined && unit._.defines[key] === undefined) {
500
377
  throw new Error(`The property "${key}" already exists.`);
501
378
  }
502
- const newDescriptor = { configurable: true, enumerable: true };
379
+ const descriptor = Object.getOwnPropertyDescriptor(defines, key);
380
+ const wrappedDesc = { configurable: true, enumerable: true };
503
381
  if (descriptor === null || descriptor === void 0 ? void 0 : descriptor.get) {
504
- newDescriptor.get = (...args) => UnitScope.execute(snapshot, descriptor.get, ...args);
382
+ wrappedDesc.get = UnitScope.wrap(descriptor.get);
505
383
  }
506
384
  if (descriptor === null || descriptor === void 0 ? void 0 : descriptor.set) {
507
- newDescriptor.set = (...args) => UnitScope.execute(snapshot, descriptor.set, ...args);
385
+ wrappedDesc.set = UnitScope.wrap(descriptor.set);
508
386
  }
509
387
  if (typeof (descriptor === null || descriptor === void 0 ? void 0 : descriptor.value) === 'function') {
510
- newDescriptor.value = (...args) => UnitScope.execute(snapshot, descriptor.value, ...args);
388
+ wrappedDesc.value = UnitScope.wrap(descriptor.value);
511
389
  }
512
390
  else if ((descriptor === null || descriptor === void 0 ? void 0 : descriptor.value) !== undefined) {
513
- newDescriptor.writable = true;
514
- newDescriptor.value = descriptor.value;
391
+ wrappedDesc.writable = true;
392
+ wrappedDesc.value = descriptor.value;
515
393
  }
516
- Object.defineProperty(unit._.defines, key, newDescriptor);
517
- Object.defineProperty(unit, key, newDescriptor);
394
+ Object.defineProperty(unit._.defines, key, wrappedDesc);
395
+ Object.defineProperty(unit, key, wrappedDesc);
518
396
  });
519
397
  }
520
398
  static start(unit, time) {
521
- if (!unit._.resolved || !unit._.tostart)
399
+ if (unit._.tostart === false)
522
400
  return;
523
- const { state } = unit._;
524
- if (state === LIFECYCLE_STATES.INVOKED || state === LIFECYCLE_STATES.INITIALIZED || state === LIFECYCLE_STATES.STOPPED) {
525
- unit._.state = LIFECYCLE_STATES.STARTED;
401
+ if (unit._.state === 'initialized' || unit._.state === 'stopped') {
402
+ unit._.state = 'started';
526
403
  unit._.children.forEach((child) => Unit.start(child, time));
527
404
  unit._.system.start.forEach((listener) => {
528
405
  UnitScope.execute(UnitScope.snapshot(unit), listener);
529
406
  });
530
407
  }
531
- else if (state === LIFECYCLE_STATES.STARTED) {
408
+ else if (unit._.state === 'started') {
532
409
  unit._.children.forEach((child) => Unit.start(child, time));
533
410
  }
534
411
  }
535
412
  static stop(unit) {
536
- if (unit._.state === LIFECYCLE_STATES.STARTED) {
537
- unit._.state = LIFECYCLE_STATES.STOPPED;
413
+ if (unit._.state === 'started') {
414
+ unit._.state = 'stopped';
538
415
  unit._.children.forEach((child) => Unit.stop(child));
539
416
  unit._.system.stop.forEach((listener) => {
540
417
  UnitScope.execute(UnitScope.snapshot(unit), listener);
@@ -542,14 +419,11 @@ class Unit {
542
419
  }
543
420
  }
544
421
  static update(unit, time) {
545
- if (unit._.state === LIFECYCLE_STATES.STARTED) {
422
+ if (unit._.state === 'started') {
546
423
  unit._.children.forEach((child) => Unit.update(child, time));
547
- if (unit._.state === LIFECYCLE_STATES.STARTED) {
548
- unit._.system.update.forEach((listener) => {
549
- UnitScope.execute(UnitScope.snapshot(unit), listener, unit._.upcount);
550
- });
551
- unit._.upcount++;
552
- }
424
+ unit._.system.update.forEach((listener) => {
425
+ UnitScope.execute(UnitScope.snapshot(unit), listener);
426
+ });
553
427
  }
554
428
  }
555
429
  static ticker(time) {
@@ -564,8 +438,99 @@ class Unit {
564
438
  Ticker.clear(Unit.ticker);
565
439
  Ticker.set(Unit.ticker);
566
440
  }
441
+ get components() {
442
+ return this._.components;
443
+ }
444
+ static find(component) {
445
+ var _a;
446
+ return [...((_a = Unit.componentUnits.get(component)) !== null && _a !== void 0 ? _a : [])];
447
+ }
448
+ on(type, listener, options) {
449
+ if (this._.state === 'finalized')
450
+ return;
451
+ type.trim().split(/\s+/).forEach((type) => {
452
+ if (SYSTEM_EVENTS.includes(type)) {
453
+ this._.system[type].push(listener);
454
+ }
455
+ if (this._.listeners.has(type, listener) === false) {
456
+ const execute = UnitScope.wrap(listener);
457
+ this._.listeners.set(type, listener, [this.element, execute]);
458
+ Unit.typeUnits.add(type, this);
459
+ if (/^[A-Za-z]/.test(type)) {
460
+ this.element.addEventListener(type, execute, options);
461
+ }
462
+ }
463
+ });
464
+ }
465
+ off(type, listener) {
466
+ const types = typeof type === 'string' ? type.trim().split(/\s+/) : [...this._.listeners.keys()];
467
+ types.forEach((type) => {
468
+ if (SYSTEM_EVENTS.includes(type)) {
469
+ this._.system[type] = this._.system[type].filter((lis) => listener ? lis !== listener : false);
470
+ }
471
+ (listener ? [listener] : [...this._.listeners.keys(type)]).forEach((lis) => {
472
+ const tuple = this._.listeners.get(type, lis);
473
+ if (tuple !== undefined) {
474
+ const [target, execute] = tuple;
475
+ this._.listeners.delete(type, lis);
476
+ if (/^[A-Za-z]/.test(type)) {
477
+ target.removeEventListener(type, execute);
478
+ }
479
+ }
480
+ });
481
+ if (this._.listeners.has(type) === false) {
482
+ Unit.typeUnits.delete(type, this);
483
+ }
484
+ });
485
+ }
486
+ emit(type, ...args) {
487
+ var _a, _b;
488
+ if (this._.state === 'finalized')
489
+ return;
490
+ if (type[0] === '+') {
491
+ (_a = Unit.typeUnits.get(type)) === null || _a === void 0 ? void 0 : _a.forEach((unit) => {
492
+ var _a;
493
+ (_a = unit._.listeners.get(type)) === null || _a === void 0 ? void 0 : _a.forEach(([_, execute]) => execute(...args));
494
+ });
495
+ }
496
+ else if (type[0] === '-') {
497
+ (_b = this._.listeners.get(type)) === null || _b === void 0 ? void 0 : _b.forEach(([_, execute]) => execute(...args));
498
+ }
499
+ }
500
+ static subon(unit, target, type, listener, options) {
501
+ type.trim().split(/\s+/).forEach((type) => {
502
+ if (unit._.sublisteners.has(type, listener) === false) {
503
+ const execute = UnitScope.wrap(listener);
504
+ unit._.sublisteners.set(type, listener, [target, execute]);
505
+ target.addEventListener(type, execute, options);
506
+ }
507
+ });
508
+ }
509
+ static suboff(unit, target, type, listener) {
510
+ const types = typeof type === 'string' ? type.trim().split(/\s+/) : [...unit._.sublisteners.keys()];
511
+ types.forEach((type) => {
512
+ (listener ? [listener] : [...unit._.sublisteners.keys(type)]).forEach((lis) => {
513
+ const tuple = unit._.sublisteners.get(type, lis);
514
+ if (tuple !== undefined) {
515
+ const [element, execute] = tuple;
516
+ if (target === null || target === element) {
517
+ unit._.sublisteners.delete(type, lis);
518
+ element.removeEventListener(type, execute);
519
+ }
520
+ }
521
+ });
522
+ });
523
+ }
567
524
  }
568
525
  Unit.roots = [];
526
+ //----------------------------------------------------------------------------------------------------
527
+ // component
528
+ //----------------------------------------------------------------------------------------------------
529
+ Unit.componentUnits = new MapSet();
530
+ //----------------------------------------------------------------------------------------------------
531
+ // event
532
+ //----------------------------------------------------------------------------------------------------
533
+ Unit.typeUnits = new MapSet();
569
534
  Unit.reset();
570
535
  //----------------------------------------------------------------------------------------------------
571
536
  // unit scope
@@ -579,15 +544,13 @@ class UnitScope {
579
544
  static finalize(unit) {
580
545
  UnitScope.contexts.delete(unit);
581
546
  }
582
- static set(unit, context) {
583
- UnitScope.contexts.set(unit, context);
584
- }
585
- static get(unit) {
586
- var _a;
587
- return (_a = UnitScope.contexts.get(unit)) !== null && _a !== void 0 ? _a : null;
547
+ static wrap(listener) {
548
+ const snapshot = UnitScope.snapshot();
549
+ return (...args) => UnitScope.execute(snapshot, listener, ...args);
588
550
  }
589
551
  static execute(snapshot, func, ...args) {
590
- if (!snapshot)
552
+ var _a;
553
+ if (snapshot === null)
591
554
  return;
592
555
  const current = UnitScope.current;
593
556
  let context = null;
@@ -596,7 +559,7 @@ class UnitScope {
596
559
  UnitScope.current = snapshot.unit;
597
560
  if (snapshot.unit !== null) {
598
561
  if (snapshot.context !== null) {
599
- context = UnitScope.get(snapshot.unit);
562
+ context = (_a = UnitScope.contexts.get(snapshot.unit)) !== null && _a !== void 0 ? _a : null;
600
563
  UnitScope.contexts.set(snapshot.unit, snapshot.context);
601
564
  }
602
565
  if (snapshot.element !== null) {
@@ -622,16 +585,19 @@ class UnitScope {
622
585
  }
623
586
  }
624
587
  static snapshot(unit = UnitScope.current) {
588
+ var _a;
625
589
  if (unit !== null) {
626
- return { unit, context: UnitScope.get(unit), element: unit.element };
590
+ return { unit, context: (_a = UnitScope.contexts.get(unit)) !== null && _a !== void 0 ? _a : null, element: unit.element };
627
591
  }
628
592
  return null;
629
593
  }
630
594
  static stack(unit, key, value) {
631
- UnitScope.contexts.set(unit, { stack: UnitScope.get(unit), key, value });
595
+ var _a;
596
+ UnitScope.contexts.set(unit, { stack: (_a = UnitScope.contexts.get(unit)) !== null && _a !== void 0 ? _a : null, key, value });
632
597
  }
633
598
  static trace(unit, key) {
634
- for (let context = UnitScope.get(unit); context !== null; context = context.stack) {
599
+ var _a;
600
+ for (let context = (_a = UnitScope.contexts.get(unit)) !== null && _a !== void 0 ? _a : null; context !== null; context = context.stack) {
635
601
  if (context.key === key) {
636
602
  return context.value;
637
603
  }
@@ -641,142 +607,6 @@ class UnitScope {
641
607
  UnitScope.current = null;
642
608
  UnitScope.contexts = new Map();
643
609
  //----------------------------------------------------------------------------------------------------
644
- // unit component
645
- //----------------------------------------------------------------------------------------------------
646
- class UnitComponent {
647
- static finalize(unit) {
648
- var _a;
649
- (_a = UnitComponent.components.get(unit)) === null || _a === void 0 ? void 0 : _a.forEach((component) => {
650
- UnitComponent.units.delete(component, unit);
651
- });
652
- UnitComponent.components.delete(unit);
653
- }
654
- static add(unit, component) {
655
- UnitComponent.components.add(unit, component);
656
- UnitComponent.units.add(component, unit);
657
- }
658
- static find(component) {
659
- var _a;
660
- return [...((_a = UnitComponent.units.get(component)) !== null && _a !== void 0 ? _a : [])];
661
- }
662
- }
663
- UnitComponent.components = new MapSet();
664
- UnitComponent.units = new MapSet();
665
- //----------------------------------------------------------------------------------------------------
666
- // unit event
667
- //----------------------------------------------------------------------------------------------------
668
- class UnitEvent {
669
- static on(unit, type, listener, options) {
670
- if (typeof type !== 'string' || type.trim() === '') {
671
- throw new Error('"type" is invalid.');
672
- }
673
- else if (typeof listener !== 'function') {
674
- throw new Error('"listener" is invalid.');
675
- }
676
- const snapshot = UnitScope.snapshot();
677
- const types = type.trim().split(/\s+/);
678
- types.forEach((type) => {
679
- if (!UnitEvent.listeners.has(unit, type, listener)) {
680
- const execute = (...args) => {
681
- UnitScope.execute(snapshot, listener, ...args);
682
- };
683
- UnitEvent.listeners.set(unit, type, listener, [unit.element, execute]);
684
- UnitEvent.units.add(type, unit);
685
- if (/^[A-Za-z]/.test(type)) {
686
- unit.element.addEventListener(type, execute, options);
687
- }
688
- }
689
- });
690
- }
691
- static off(unit, type, listener) {
692
- if (typeof type === 'string' && type.trim() === '') {
693
- throw new Error('"type" is invalid.');
694
- }
695
- else if (listener !== undefined && typeof listener !== 'function') {
696
- throw new Error('"listener" is invalid.');
697
- }
698
- const types = typeof type === 'string' ? type.trim().split(/\s+/) : [...UnitEvent.listeners.keys(unit)];
699
- types.forEach((type) => {
700
- const listeners = listener ? [listener] : [...UnitEvent.listeners.keys(unit, type)];
701
- listeners.forEach((lis) => {
702
- const tuple = UnitEvent.listeners.get(unit, type, lis);
703
- if (tuple !== undefined) {
704
- const [target, execute] = tuple;
705
- UnitEvent.listeners.delete(unit, type, lis);
706
- if (/^[A-Za-z]/.test(type)) {
707
- target.removeEventListener(type, execute);
708
- }
709
- }
710
- });
711
- if (!UnitEvent.listeners.has(unit, type)) {
712
- UnitEvent.units.delete(type, unit);
713
- }
714
- });
715
- }
716
- static emit(unit, type, ...args) {
717
- var _a, _b;
718
- if (typeof type !== 'string') {
719
- throw new Error('The argument [type] is invalid.');
720
- }
721
- else if ((unit === null || unit === void 0 ? void 0 : unit._.state) === LIFECYCLE_STATES.FINALIZED) {
722
- throw new Error('This function can not be called after finalized.');
723
- }
724
- if (type[0] === CUSTOM_EVENT_PREFIX.GLOBAL) {
725
- (_a = UnitEvent.units.get(type)) === null || _a === void 0 ? void 0 : _a.forEach((unit) => {
726
- var _a;
727
- (_a = UnitEvent.listeners.get(unit, type)) === null || _a === void 0 ? void 0 : _a.forEach(([_, execute]) => execute(...args));
728
- });
729
- }
730
- else if (type[0] === CUSTOM_EVENT_PREFIX.INTERNAL && unit !== null) {
731
- (_b = UnitEvent.listeners.get(unit, type)) === null || _b === void 0 ? void 0 : _b.forEach(([_, execute]) => execute(...args));
732
- }
733
- }
734
- static subon(unit, target, type, listener, options) {
735
- if (typeof type !== 'string' || type.trim() === '') {
736
- throw new Error('"type" is invalid.');
737
- }
738
- else if (typeof listener !== 'function') {
739
- throw new Error('"listener" is invalid.');
740
- }
741
- const snapshot = UnitScope.snapshot();
742
- const types = type.trim().split(/\s+/);
743
- types.forEach((type) => {
744
- if (!UnitEvent.sublisteners.has(unit, type, listener)) {
745
- const execute = (...args) => {
746
- UnitScope.execute(snapshot, listener, ...args);
747
- };
748
- UnitEvent.sublisteners.set(unit, type, listener, [target, execute]);
749
- target.addEventListener(type, execute, options);
750
- }
751
- });
752
- }
753
- static suboff(unit, target, type, listener) {
754
- if (typeof type === 'string' && type.trim() === '') {
755
- throw new Error('"type" is invalid.');
756
- }
757
- else if (listener !== undefined && typeof listener !== 'function') {
758
- throw new Error('"listener" is invalid.');
759
- }
760
- const types = typeof type === 'string' ? type.trim().split(/\s+/) : [...UnitEvent.sublisteners.keys(unit)];
761
- types.forEach((type) => {
762
- const listeners = listener ? [listener] : [...UnitEvent.sublisteners.keys(unit, type)];
763
- listeners.forEach((lis) => {
764
- const tuple = UnitEvent.sublisteners.get(unit, type, lis);
765
- if (tuple !== undefined) {
766
- const [element, execute] = tuple;
767
- if (target === null || target === element) {
768
- UnitEvent.sublisteners.delete(unit, type, lis);
769
- element.removeEventListener(type, execute);
770
- }
771
- }
772
- });
773
- });
774
- }
775
- }
776
- UnitEvent.units = new MapSet;
777
- UnitEvent.listeners = new MapMapMap;
778
- UnitEvent.sublisteners = new MapMapMap;
779
- //----------------------------------------------------------------------------------------------------
780
610
  // unit promise
781
611
  //----------------------------------------------------------------------------------------------------
782
612
  class UnitPromise {
@@ -784,18 +614,15 @@ class UnitPromise {
784
614
  this.promise = new Promise(executor);
785
615
  }
786
616
  then(callback) {
787
- const snapshot = UnitScope.snapshot();
788
- this.promise = this.promise.then((...args) => UnitScope.execute(snapshot, callback, ...args));
617
+ this.promise = this.promise.then(UnitScope.wrap(callback));
789
618
  return this;
790
619
  }
791
620
  catch(callback) {
792
- const snapshot = UnitScope.snapshot();
793
- this.promise = this.promise.catch((...args) => UnitScope.execute(snapshot, callback, ...args));
621
+ this.promise = this.promise.catch(UnitScope.wrap(callback));
794
622
  return this;
795
623
  }
796
624
  finally(callback) {
797
- const snapshot = UnitScope.snapshot();
798
- this.promise = this.promise.finally((...args) => UnitScope.execute(snapshot, callback, ...args));
625
+ this.promise = this.promise.finally(UnitScope.wrap(callback));
799
626
  return this;
800
627
  }
801
628
  static get(unit) {
@@ -806,20 +633,14 @@ class UnitPromise {
806
633
  UnitPromise.promises.delete(unit);
807
634
  }
808
635
  static execute(unit, promise) {
636
+ const inner = promise !== null && promise !== void 0 ? promise : UnitPromise.get(unit);
637
+ const unitPromise = new UnitPromise((resolve, reject) => {
638
+ inner.then((...args) => resolve(...args)).catch((...args) => reject(...args));
639
+ });
809
640
  if (promise !== undefined) {
810
- const upromise = new UnitPromise((resolve, reject) => {
811
- promise.then((...args) => resolve(...args)).catch((...args) => reject(...args));
812
- });
813
- UnitPromise.promises.add(unit, upromise);
814
- return upromise;
815
- }
816
- else {
817
- const promiseall = UnitPromise.get(unit);
818
- const upromise = new UnitPromise((resolve, reject) => {
819
- promiseall.then((...args) => resolve(...args)).catch((...args) => reject(...args));
820
- });
821
- return upromise;
641
+ UnitPromise.promises.add(unit, unitPromise);
822
642
  }
643
+ return unitPromise;
823
644
  }
824
645
  }
825
646
  UnitPromise.promises = new MapSet();
@@ -832,8 +653,7 @@ const xnew$1 = (() => {
832
653
  }
833
654
  else if (typeof args[0] === 'string') {
834
655
  const str = args.shift(); // a selector for an existing html element
835
- const match = str.match(/<([^>]*)\/?>/);
836
- if (match) {
656
+ if (str.match(/<([^>]*)\/?>/)) {
837
657
  target = str;
838
658
  }
839
659
  else {
@@ -972,7 +792,7 @@ const xnew$1 = (() => {
972
792
  };
973
793
  fn.find = (component) => {
974
794
  if (typeof component === 'function') {
975
- return UnitComponent.find(component);
795
+ return Unit.find(component);
976
796
  }
977
797
  else {
978
798
  throw new Error(`The argument [component] is invalid.`);
@@ -980,7 +800,7 @@ const xnew$1 = (() => {
980
800
  };
981
801
  fn.append = (base, ...args) => {
982
802
  if (typeof base === 'function') {
983
- for (let unit of UnitComponent.find(base)) {
803
+ for (let unit of Unit.find(base)) {
984
804
  UnitScope.execute(UnitScope.snapshot(unit), xnew$1, ...args);
985
805
  }
986
806
  }
@@ -1071,10 +891,10 @@ const xnew$1 = (() => {
1071
891
  fn.listener = function (target) {
1072
892
  return {
1073
893
  on(type, listener, options) {
1074
- UnitEvent.subon(UnitScope.current, target, type, listener, options);
894
+ Unit.subon(UnitScope.current, target, type, listener, options);
1075
895
  },
1076
896
  off(type, listener) {
1077
- UnitEvent.suboff(UnitScope.current, target, type, listener);
897
+ Unit.suboff(UnitScope.current, target, type, listener);
1078
898
  }
1079
899
  };
1080
900
  };
@@ -1315,7 +1135,7 @@ function ModalFrame(frame, {} = {}) {
1315
1135
  xnew$1.context('xnew.modalframe', frame);
1316
1136
  xnew$1.nest('<div style="position: fixed; inset: 0;">');
1317
1137
  let content = null;
1318
- xnew$1.capture((unit) => unit.components.includes(ModalContent), (unit) => {
1138
+ xnew$1.capture((unit) => unit.components.has(ModalContent), (unit) => {
1319
1139
  content = unit;
1320
1140
  });
1321
1141
  xnew$1().on('click', (event) => {
@@ -1363,10 +1183,10 @@ function TabFrame(frame, { select = 0 } = {}) {
1363
1183
  xnew$1.context('xnew.tabframe', frame);
1364
1184
  const buttons = [];
1365
1185
  const contents = [];
1366
- xnew$1.capture((unit) => unit.components.includes(TabButton), (unit) => {
1186
+ xnew$1.capture((unit) => unit.components.has(TabButton), (unit) => {
1367
1187
  buttons.push(unit);
1368
1188
  });
1369
- xnew$1.capture((unit) => unit.components.includes(TabContent), (unit) => {
1189
+ xnew$1.capture((unit) => unit.components.has(TabContent), (unit) => {
1370
1190
  contents.push(unit);
1371
1191
  });
1372
1192
  frame.on('-click', ({ unit }) => execute(buttons.indexOf(unit)));
@@ -1410,7 +1230,7 @@ function TabContent(self, {} = {}) {
1410
1230
  function AccordionFrame(frame, {} = {}) {
1411
1231
  xnew$1.context('xnew.accordionframe', frame);
1412
1232
  let content = null;
1413
- xnew$1.capture((unit) => unit.components.includes(AccordionContent), (unit) => {
1233
+ xnew$1.capture((unit) => unit.components.has(AccordionContent), (unit) => {
1414
1234
  content = unit;
1415
1235
  });
1416
1236
  return {