silentium-components 0.0.113 → 0.0.115

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.
@@ -0,0 +1,126 @@
1
+ import { Late, Value } from "silentium";
2
+ import { describe, expect, test } from "vitest";
3
+
4
+ import { StateRecord } from "./StateRecord";
5
+
6
+ describe("StateRecord", () => {
7
+ test("Work example", () => {
8
+ const state$ = Late("start");
9
+ const value$ = Late();
10
+ const record$ = Value(StateRecord(state$, value$, ["one", "two"]));
11
+ expect(record$.value).toBe(null);
12
+
13
+ state$.use("one");
14
+ value$.use(123);
15
+ state$.use("two");
16
+ value$.use(456);
17
+
18
+ expect(record$.value).toStrictEqual({
19
+ one: 123,
20
+ two: 456,
21
+ });
22
+ });
23
+
24
+ test("Broken sequence", () => {
25
+ const state$ = Late("start");
26
+ const value$ = Late();
27
+ const record$ = Value(StateRecord(state$, value$, ["one", "two", "three"]));
28
+ expect(record$.value).toBe(null);
29
+
30
+ state$.use("one");
31
+ value$.use(123);
32
+ state$.use("three");
33
+ value$.use(456);
34
+ state$.use("two");
35
+ value$.use(321);
36
+
37
+ expect(record$.value).toBeNull();
38
+ });
39
+
40
+ test("Broken sequence and restored", () => {
41
+ const state$ = Late("start");
42
+ const value$ = Late();
43
+ const record$ = Value(StateRecord(state$, value$, ["one", "two", "three"]));
44
+ expect(record$.value).toBe(null);
45
+
46
+ state$.use("one");
47
+ value$.use(123);
48
+ state$.use("three"); // Broken here, resets to looking for "one"
49
+ value$.use(456);
50
+ state$.use("two"); // Nothing here, it is not "one"
51
+ value$.use(321);
52
+ state$.use("one");
53
+ value$.use(123);
54
+ state$.use("two");
55
+ value$.use(321);
56
+ state$.use("three"); // Restored and returns value
57
+ value$.use(456);
58
+
59
+ expect(record$.value).toStrictEqual({
60
+ one: 123,
61
+ two: 321,
62
+ three: 456,
63
+ });
64
+ });
65
+
66
+ test("Changing", () => {
67
+ const state$ = Late("start");
68
+ const value$ = Late();
69
+ const results: any[] = [];
70
+ StateRecord(state$, value$, ["one", "two"]).then((v) => {
71
+ results.push(v);
72
+ });
73
+
74
+ state$.use("one");
75
+ value$.use(123);
76
+ state$.use("two");
77
+ value$.use(456);
78
+ value$.use(789);
79
+
80
+ expect(results).toStrictEqual([
81
+ {
82
+ one: 123,
83
+ two: 456,
84
+ },
85
+ ]);
86
+
87
+ state$.use("one");
88
+ value$.use(321);
89
+ state$.use("two");
90
+ value$.use(654);
91
+
92
+ expect(results).toStrictEqual([
93
+ {
94
+ one: 123,
95
+ two: 456,
96
+ },
97
+ {
98
+ one: 321,
99
+ two: 654,
100
+ },
101
+ ]);
102
+
103
+ // Many times setting "one"
104
+ state$.use("one");
105
+ value$.use(321);
106
+ value$.use(222);
107
+ value$.use(111);
108
+ state$.use("two");
109
+ value$.use(222);
110
+
111
+ expect(results).toStrictEqual([
112
+ {
113
+ one: 123,
114
+ two: 456,
115
+ },
116
+ {
117
+ one: 321,
118
+ two: 654,
119
+ },
120
+ {
121
+ one: 111,
122
+ two: 222,
123
+ },
124
+ ]);
125
+ });
126
+ });
@@ -0,0 +1,52 @@
1
+ import {
2
+ Actual,
3
+ DestroyContainer,
4
+ MaybeMessage,
5
+ Message,
6
+ MessageType,
7
+ Value,
8
+ } from "silentium";
9
+
10
+ export function StateRecord(
11
+ state$: MessageType,
12
+ values$: MessageType,
13
+ sequence: MaybeMessage<unknown[]>,
14
+ ) {
15
+ const dc = DestroyContainer();
16
+ let stateIndex = -1;
17
+ let latestState: string | null = null;
18
+ let result: Record<string, unknown> = {};
19
+ const sequence$ = Value(Actual(sequence));
20
+ return Message((resolve, reject) => {
21
+ dc.add(
22
+ state$
23
+ .then((state) => {
24
+ if (state === sequence$.value?.[stateIndex + 1]) {
25
+ stateIndex += 1;
26
+ latestState = state as string;
27
+ } else {
28
+ stateIndex = -1;
29
+ latestState = null;
30
+ result = {};
31
+ }
32
+ })
33
+ .catch(reject),
34
+ );
35
+ dc.add(
36
+ values$
37
+ .then((value) => {
38
+ if (latestState !== null) {
39
+ result[latestState] = value;
40
+ }
41
+ if (stateIndex + 1 === sequence$.value?.length) {
42
+ resolve(result);
43
+ stateIndex = -1;
44
+ latestState = null;
45
+ result = {};
46
+ }
47
+ })
48
+ .catch(reject),
49
+ );
50
+ return () => dc.destroy();
51
+ });
52
+ }
@@ -1,4 +1,4 @@
1
- import { Late, Message, Void } from "silentium";
1
+ import { Late, Message, Value, Void } from "silentium";
2
2
  import { describe, expect, test } from "vitest";
3
3
 
4
4
  import { Switch } from "./Switch";
@@ -28,7 +28,7 @@ describe("Switch.test", () => {
28
28
  ]);
29
29
  const data: string[] = [];
30
30
  src.then((v) => {
31
- data.push(v);
31
+ data.push(v as string);
32
32
  });
33
33
 
34
34
  expect(data).toStrictEqual(["Option A"]);
@@ -69,7 +69,7 @@ describe("Switch.test", () => {
69
69
  ]);
70
70
  const data: string[] = [];
71
71
  src.then((v) => {
72
- data.push(v);
72
+ data.push(v as string);
73
73
  });
74
74
 
75
75
  expect(data).toStrictEqual([]);
@@ -120,7 +120,7 @@ describe("Switch.test", () => {
120
120
  ]);
121
121
  const data: string[] = [];
122
122
  src.then((v) => {
123
- data.push(v);
123
+ data.push(v as string);
124
124
  });
125
125
  src.destroy();
126
126
 
@@ -129,4 +129,34 @@ describe("Switch.test", () => {
129
129
  $trigger.use("b");
130
130
  expect(data).toStrictEqual(["Option A"]);
131
131
  });
132
+
133
+ test("Switch.multipleVariants", () => {
134
+ const $trigger = Late("a");
135
+ const src = Value(
136
+ Switch($trigger, [
137
+ [
138
+ "a",
139
+ Message((resolve) => {
140
+ resolve("Option A");
141
+ }),
142
+ ],
143
+ [
144
+ ["b", "c"],
145
+ Message((resolve) => {
146
+ resolve("B or C");
147
+ }),
148
+ ],
149
+ ]),
150
+ );
151
+ expect(src.value).toBe("Option A");
152
+
153
+ $trigger.use("c");
154
+ expect(src.value).toBe("B or C");
155
+
156
+ $trigger.use("b");
157
+ expect(src.value).toBe("B or C");
158
+
159
+ $trigger.use("a");
160
+ expect(src.value).toBe("Option A");
161
+ });
132
162
  });
@@ -11,14 +11,16 @@ import {
11
11
  */
12
12
  export function Switch<T, K>(
13
13
  _base: MaybeMessage,
14
- options: [K, MessageType<T>][],
14
+ options: [K | K[], MessageType<T>][],
15
15
  ) {
16
16
  const $base = Actual(_base);
17
17
  return Message<T>((resolve, reject) => {
18
18
  const dc = DestroyContainer();
19
19
  $base
20
20
  .then((v) => {
21
- const msg = options.find((entry) => entry[0] === v);
21
+ const msg = options.find((entry) =>
22
+ Array.isArray(entry[0]) ? entry[0].includes(v as K) : entry[0] === v,
23
+ );
22
24
  if (msg) {
23
25
  dc.add(msg[1].then(resolve).catch(reject));
24
26
  }
@@ -14,6 +14,7 @@ export * from "./Part";
14
14
  export * from "./Path";
15
15
  export * from "./Polling";
16
16
  export * from "./RecordTruncated";
17
+ export * from "./StateRecord";
17
18
  export * from "./Switch";
18
19
  export * from "./Task";
19
20
  export * from "./Tick";