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