amateras 0.4.1 → 0.5.0

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/src/node/$Node.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { chain } from "#lib/chain";
2
2
  import { _document } from "#lib/env";
3
- import { _Array_from, _instanceof, _JSON_stringify, forEach, isFunction, isNull, isObject, isUndefined } from "#lib/native";
3
+ import { _Array_from, _instanceof, _JSON_stringify, _null, _Promise, forEach, isBoolean, isFunction, isNull, isObject, isUndefined } from "#lib/native";
4
+ import { toArray } from "#lib/toArray";
4
5
  import { Signal } from "#structure/Signal";
5
6
 
6
7
  export class $Node {
@@ -12,15 +13,15 @@ export class $Node {
12
13
  }
13
14
 
14
15
  content(children: $NodeContentResolver<this>) {
15
- return chain(this, null, null, children, children => {
16
- forEach(this.childNodes, node => node.remove());
16
+ return chain(this, _null, _null, children, children => {
17
+ forEach(_Array_from(this.childNodes), node => node.remove());
17
18
  this.insert(children);
18
19
  })
19
20
  }
20
21
 
21
22
  insert(resolver: $NodeContentResolver<this>, position = -1) {
22
23
  // process nodes
23
- forEach($.toArray(resolver), resolve_child => forEach($Node.process(this, resolve_child), $node => $Node.append(this, $node, position)));
24
+ forEach(toArray(resolver), resolve_child => forEach($Node.process(this, resolve_child), $node => $Node.append(this, $node, position)));
24
25
  return this;
25
26
  }
26
27
 
@@ -33,7 +34,7 @@ export class $Node {
33
34
  replace($node: $NodeContentResolver<$Node>) {
34
35
  if ($node)
35
36
  this.replaceWith(
36
- ...$.toArray($Node.process(this, $node)).filter($node => $node).map($node => $node?.node) as Node[]
37
+ ...toArray($Node.process(this, $node)).filter($node => $node).map($node => $node?.node) as Node[]
37
38
  )
38
39
  return this;
39
40
  }
@@ -57,10 +58,22 @@ export class $Node {
57
58
  return _instanceof(this, instance) ? this : null;
58
59
  }
59
60
 
61
+ on(type: string, listener: any, options?: boolean | AddEventListenerOptions) {
62
+ return this.addEventListener(type, listener, options);
63
+ }
64
+
65
+ off(type: string, listener: any, options?: boolean | EventListenerOptions) {
66
+ return this.removeEventListener(type, listener, options);
67
+ }
68
+
69
+ once(type: string, listener: any, options?: boolean | AddEventListenerOptions) {
70
+ return this.on(type, listener, { once: true, ...(isBoolean(options) ? {capture: options} : options ?? {}) })
71
+ }
72
+
60
73
  static process<T extends $Node>($node: T, content: $NodeContentResolver<any>): Array<$Node | undefined | null> {
61
74
  if (isUndefined(content) || isNull(content) || _instanceof(content, $Node)) return [content];
62
75
  // is Promise
63
- if (_instanceof(content, Promise)) return [$('async').await(content, ($async, $child) => $async.replace($child as any))];
76
+ if (_instanceof(content, _Promise)) return [$('async').await(content, ($async, $child) => $async.replace($child as any))];
64
77
  // is SignalFunction or ContentHandler
65
78
  if (isFunction(content)) {
66
79
  const signal = (content as any).signal;
@@ -86,8 +99,8 @@ export class $Node {
86
99
  }
87
100
  } else {
88
101
  const _content = content($node) as $NodeContentResolver<$Node>;
89
- if (_instanceof(_content, Promise)) return this.process($node, _content as any);
90
- else return $.toArray(_content).map(content => this.process($node, content)).flat();
102
+ if (_instanceof(_content, _Promise)) return this.process($node, _content as any);
103
+ else return toArray(_content).map(content => this.process($node, content)).flat();
91
104
  }
92
105
  }
93
106
  // is nested array
@@ -1,4 +1,4 @@
1
- import { _instanceof, forEach, isUndefined } from "#lib/native";
1
+ import { _instanceof, forEach, isFunction, isUndefined } from "#lib/native";
2
2
 
3
3
  export class Signal<T> {
4
4
  #value: T;
@@ -16,7 +16,7 @@ export class Signal<T> {
16
16
  forEach(Signal.listeners, fn => fn(this));
17
17
  return this.#value;
18
18
  }
19
- if (_instanceof(resolver, Function)) this.value(resolver(this.#value));
19
+ if (isFunction(resolver)) this.value(resolver(this.#value));
20
20
  else if (!isUndefined(resolver)) {
21
21
  this.#value = resolver;
22
22
  this.emit();