silentium-components 0.0.89 → 0.0.91

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.
@@ -1,10 +1,15 @@
1
1
  import {
2
+ ActualMessage,
2
3
  All,
3
4
  Applied,
4
5
  ConstructorType,
5
6
  DestroyableType,
6
7
  DestroyContainer,
7
8
  isDestroyable,
9
+ isMessage,
10
+ LateShared,
11
+ MaybeMessage,
12
+ Message,
8
13
  MessageType,
9
14
  Of,
10
15
  Rejections,
@@ -18,10 +23,28 @@ import { Record } from "../structures";
18
23
  * the template value will change
19
24
  */
20
25
  export function Template(
21
- $src: MessageType<string> = Of(""),
22
- $places: MessageType<Record<string, unknown>> = Of({}),
26
+ src: MaybeMessage<string> | ((t: TemplateImpl) => string) = "",
27
+ $places: MaybeMessage<Record<string, unknown>> = Of({}),
23
28
  ) {
24
- return new TemplateImpl($src, $places);
29
+ const $src = LateShared<string>();
30
+ if (typeof src === "string" || isMessage(src)) {
31
+ $src.chain(ActualMessage(src));
32
+ }
33
+
34
+ const t = new TemplateImpl(
35
+ $src,
36
+ $places ? ActualMessage($places) : undefined,
37
+ );
38
+
39
+ if (typeof src === "function") {
40
+ $src.chain(
41
+ Message((r) => {
42
+ r(src(t));
43
+ }),
44
+ );
45
+ }
46
+
47
+ return t;
25
48
  }
26
49
 
27
50
  class TemplateImpl implements MessageType<string>, DestroyableType {
@@ -64,8 +87,9 @@ class TemplateImpl implements MessageType<string>, DestroyableType {
64
87
  * in concrete place Of template
65
88
  */
66
89
  public var(src: MessageType<string>) {
67
- const places = Object.keys(this.vars).length;
68
- const varName = `$var${places}`;
90
+ const hash =
91
+ Date.now().toString(36) + Math.random().toString(36).substring(2);
92
+ const varName = `$var${hash}`;
69
93
  if (isDestroyable(src)) {
70
94
  this.dc.add(src);
71
95
  }
@@ -1,29 +0,0 @@
1
- import { Of } from "silentium";
2
- import { expect, test, vi } from "vitest";
3
-
4
- import { Template } from "../strings/Template";
5
- import { Record } from "../structures";
6
-
7
- test("Template._main.test", () => {
8
- const tpl = Template(
9
- Of("<h1>$1</h1>"),
10
- Record({
11
- $1: Of("one value"),
12
- }),
13
- );
14
- const g = vi.fn();
15
- tpl.then(g);
16
-
17
- expect(g).toHaveBeenLastCalledWith("<h1>one value</h1>");
18
-
19
- const tpl2 = Template(
20
- Of("<h2>$1</h2>"),
21
- Record({
22
- $1: Of("second value"),
23
- }),
24
- );
25
- const g2 = vi.fn();
26
- tpl2.then(g2);
27
-
28
- expect(g2).toHaveBeenLastCalledWith("<h2>second value</h2>");
29
- });
@@ -1,13 +0,0 @@
1
- import { Of } from "silentium";
2
- import { expect, test, vi } from "vitest";
3
-
4
- import { Template } from "../strings/Template";
5
-
6
- test("Template._place.test", () => {
7
- const t = Template();
8
- t.template(`<div class="greeting">Hello ${t.var(Of("User"))}</div>`);
9
- const g = vi.fn();
10
- t.then(g);
11
-
12
- expect(g).toHaveBeenLastCalledWith('<div class="greeting">Hello User</div>');
13
- });