@thi.ng/rdom 0.6.9 → 0.7.4

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/replace.js ADDED
@@ -0,0 +1,62 @@
1
+ import { $compile } from "./compile.js";
2
+ import { Component } from "./component.js";
3
+ import { $sub } from "./sub.js";
4
+ import { $wrapText } from "./wrap.js";
5
+ /**
6
+ * Similar to {@link $refresh}, but more basic/simple. Takes a reactive value
7
+ * `src` and wraps it in a {@link $sub} component using an inner
8
+ * {@link Replace}, which then passes the value to {@link $compile} for each
9
+ * change and replaces the result in the target DOM. If the value evaluates to
10
+ * `null`ish, the previously mounted component will be unmounted and stays so
11
+ * until the value becomes non-null again.
12
+ *
13
+ * @remarks
14
+ * If the reactive value is null-ish when the wrapper component is first
15
+ * mounted, a hidden dummy `<span>` element will be created instead. This is to
16
+ * ensure the general {@link IComponent.mount} contract will not be broken. The
17
+ * dummy element will later be removed/replaced as soon as the reactive value
18
+ * becomes non-null.
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * import { $compile, $replace } from "@thi.ng/rdom";
23
+ * import { fromInterval } from "@thi.ng/rstream";
24
+ *
25
+ * // reactive counter component
26
+ * const counter = fromInterval(16).map((x) => [
27
+ * "div",
28
+ * { style: { "font-size": `${(x % 100) + 10}px` } },
29
+ * x,
30
+ * ]);
31
+ *
32
+ * $compile($replace(counter)).mount(document.body);
33
+ * ```
34
+ *
35
+ * @param src
36
+ */
37
+ export const $replace = (src) => $sub(src, new Replace());
38
+ export class Replace extends Component {
39
+ async mount(parent, index, val) {
40
+ this.parent = parent;
41
+ this.index = index;
42
+ await this.update(val);
43
+ if (!this.inner) {
44
+ this.inner = $wrapText("span", { hidden: true });
45
+ await this.inner.mount(parent, index);
46
+ }
47
+ return this.inner.el;
48
+ }
49
+ async unmount() {
50
+ this.inner && (await this.inner.unmount());
51
+ this.parent = undefined;
52
+ this.inner = undefined;
53
+ }
54
+ async update(val) {
55
+ this.inner && (await this.inner.unmount());
56
+ this.inner = undefined;
57
+ if (val != null) {
58
+ this.inner = $compile(val);
59
+ this.inner && (await this.inner.mount(this.parent, this.index));
60
+ }
61
+ }
62
+ }
package/scheduler.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Task, IScheduler } from "./api";
1
+ import type { Task, IScheduler } from "./api.js";
2
2
  /**
3
3
  * {@link IScheduler} implementation which queues component updates (or other
4
4
  * tasks) and then only processes them during next RAF cycle. Supports task
package/sub.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import type { Fn2, Path } from "@thi.ng/api";
2
- import { ISubscribable, Subscription } from "@thi.ng/rstream";
3
- import type { IComponent, IMountWithState, NumOrElement } from "./api";
2
+ import type { ISubscribable } from "@thi.ng/rstream";
3
+ import { Subscription } from "@thi.ng/rstream/subscription";
4
+ import type { IComponent, IMountWithState, NumOrElement } from "./api.js";
4
5
  /**
5
6
  * Takes an {@link @thi.ng/rstream#ISubscribable} and creates a simple component
6
7
  * wrapper for its reactively produced values.
package/sub.js CHANGED
@@ -1,15 +1,16 @@
1
- import { isString } from "@thi.ng/checks";
2
- import { defSetterUnsafe } from "@thi.ng/paths";
3
- import { nextID, Subscription } from "@thi.ng/rstream";
4
- import { $attribs } from "./dom";
5
- import { SCHEDULER } from "./scheduler";
6
- import { $wrapText } from "./wrap";
1
+ import { isString } from "@thi.ng/checks/is-string";
2
+ import { defSetterUnsafe } from "@thi.ng/paths/setter";
3
+ import { __nextID } from "@thi.ng/rstream/idgen";
4
+ import { Subscription } from "@thi.ng/rstream/subscription";
5
+ import { $attribs } from "./dom.js";
6
+ import { SCHEDULER } from "./scheduler.js";
7
+ import { $wrapText } from "./wrap.js";
7
8
  export function $sub(src, tag, attribs) {
8
9
  return (src.subscribe(new $Sub(isString(tag) ? $wrapText(tag, attribs) : tag)));
9
10
  }
10
11
  export class $Sub extends Subscription {
11
12
  constructor(inner) {
12
- super(undefined, { id: `rdom$sub-${nextID()}` });
13
+ super(undefined, { id: `rdom$sub-${__nextID()}` });
13
14
  this.inner = inner;
14
15
  }
15
16
  async mount(parent, index = -1) {
@@ -30,7 +31,7 @@ export class $Sub extends Subscription {
30
31
  }
31
32
  export class $SubA extends Subscription {
32
33
  constructor(comp, path) {
33
- super(undefined, { id: `rdom$sub-${nextID()}` });
34
+ super(undefined, { id: `rdom$sub-${__nextID()}` });
34
35
  this.comp = comp;
35
36
  this.attr = {};
36
37
  this.setter = defSetterUnsafe(path);
package/switch.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { Fn, NumOrString } from "@thi.ng/api";
1
+ import type { Fn, NumOrString } from "@thi.ng/api";
2
2
  import type { ISubscribable } from "@thi.ng/rstream";
3
- import type { IComponent, IMountWithState, NumOrElement } from "./api";
4
- import { Component } from "./component";
3
+ import type { IComponent, IMountWithState, NumOrElement } from "./api.js";
4
+ import { Component } from "./component.js";
5
5
  /**
6
6
  * Reactive component wrapper to dynamically switch/replace itself with one of
7
7
  * the given components depending on subscribed value.
package/switch.js CHANGED
@@ -1,8 +1,8 @@
1
- import { assert } from "@thi.ng/api";
2
- import { $compile } from "./compile";
3
- import { Component } from "./component";
4
- import { $sub } from "./sub";
5
- import { $wrapText } from "./wrap";
1
+ import { assert } from "@thi.ng/errors/assert";
2
+ import { $compile } from "./compile.js";
3
+ import { Component } from "./component.js";
4
+ import { $sub } from "./sub.js";
5
+ import { $wrapText } from "./wrap.js";
6
6
  /**
7
7
  * Reactive component wrapper to dynamically switch/replace itself with one of
8
8
  * the given components depending on subscribed value.
package/wrap.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { IMountWithState } from "./api";
1
+ import type { IMountWithState } from "./api.js";
2
2
  /**
3
3
  * Returns a component wrapper for a single DOM element whose TEXT body can be
4
4
  * later updated/replaced via `.update()`, similarly to setting `.innerText`.
package/wrap.js CHANGED
@@ -1,5 +1,5 @@
1
- import { $el, $html, $remove, $text } from "./dom";
2
- import { SCHEDULER } from "./scheduler";
1
+ import { $el, $html, $remove, $text } from "./dom.js";
2
+ import { SCHEDULER } from "./scheduler.js";
3
3
  const wrapper = (update) => (tag, attribs, body) => ({
4
4
  el: undefined,
5
5
  async mount(parent, index, state) {