silentium-components 0.0.83 → 0.0.85
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/CHANGELOG.md +14 -0
- package/dist/silentium-components.cjs +326 -411
- package/dist/silentium-components.cjs.map +1 -1
- package/dist/silentium-components.d.ts +80 -87
- package/dist/silentium-components.js +328 -412
- package/dist/silentium-components.js.map +1 -1
- package/dist/silentium-components.min.js +1 -1
- package/dist/silentium-components.min.mjs +1 -1
- package/dist/silentium-components.min.mjs.map +1 -1
- package/dist/silentium-components.mjs +328 -412
- package/dist/silentium-components.mjs.map +1 -1
- package/eslint.config.mjs +26 -12
- package/package.json +4 -2
- package/rollup.config.js +2 -1
- package/src/behaviors/Branch._main.test.ts +4 -3
- package/src/behaviors/Branch._values.test.ts +3 -2
- package/src/behaviors/Branch.branchesDontAffectResult.test.ts +4 -3
- package/src/behaviors/Branch.ts +13 -21
- package/src/behaviors/BranchLazy._main.test.ts +5 -4
- package/src/behaviors/BranchLazy.ts +17 -20
- package/src/behaviors/Const.test.ts +6 -7
- package/src/behaviors/Const.ts +5 -7
- package/src/behaviors/Deadline._main.test.ts +5 -4
- package/src/behaviors/Deadline._value.test.ts +4 -3
- package/src/behaviors/Deadline.ts +20 -27
- package/src/behaviors/Deferred.test.ts +5 -4
- package/src/behaviors/Deferred.ts +8 -10
- package/src/behaviors/Detached.test.ts +4 -3
- package/src/behaviors/Detached.ts +2 -2
- package/src/behaviors/Dirty.test.ts +4 -3
- package/src/behaviors/Dirty.ts +18 -45
- package/src/behaviors/Loading.test.ts +4 -3
- package/src/behaviors/Loading.ts +4 -4
- package/src/behaviors/Lock.test.ts +4 -3
- package/src/behaviors/Lock.ts +6 -8
- package/src/behaviors/Memo.test.ts +7 -8
- package/src/behaviors/Memo.ts +8 -10
- package/src/behaviors/OnlyChanged.test.ts +5 -4
- package/src/behaviors/OnlyChanged.ts +9 -11
- package/src/behaviors/Part.test.ts +12 -15
- package/src/behaviors/Part.ts +23 -40
- package/src/behaviors/Path._keyRaw.test.ts +3 -2
- package/src/behaviors/Path._main.test.ts +3 -2
- package/src/behaviors/Path.index.test.ts +3 -2
- package/src/behaviors/Path.nested.test.ts +4 -3
- package/src/behaviors/Path.ts +11 -14
- package/src/behaviors/Polling.test.ts +3 -2
- package/src/behaviors/Polling.ts +5 -7
- package/src/behaviors/Shot._main.test.ts +5 -4
- package/src/behaviors/Shot._onlyChanged.test.ts +9 -10
- package/src/behaviors/Shot.ts +8 -10
- package/src/behaviors/Task.test.ts +9 -12
- package/src/behaviors/Task.ts +11 -5
- package/src/behaviors/Tick.test.ts +5 -4
- package/src/behaviors/Tick.ts +9 -11
- package/src/behaviors/index.ts +0 -1
- package/src/boolean/And.test.ts +3 -2
- package/src/boolean/And.ts +5 -7
- package/src/boolean/Bool.test.ts +4 -3
- package/src/boolean/Bool.ts +2 -2
- package/src/boolean/Not.test.ts +4 -3
- package/src/boolean/Not.ts +5 -7
- package/src/boolean/Or.test.ts +4 -3
- package/src/boolean/Or.ts +5 -7
- package/src/boolean/index.ts +2 -2
- package/src/formats/FromJson.test.ts +2 -1
- package/src/formats/FromJson.ts +9 -12
- package/src/formats/ToJson.test.ts +1 -0
- package/src/formats/ToJson.ts +10 -12
- package/src/index.ts +4 -4
- package/src/lists/First.test.ts +3 -2
- package/src/lists/First.ts +2 -2
- package/src/navigation/Router._destroy.test.ts +14 -24
- package/src/navigation/Router._firstMatch.test.ts +7 -13
- package/src/navigation/Router._main.test.ts +7 -13
- package/src/navigation/Router._nested.test.ts +19 -30
- package/src/navigation/Router.ts +30 -34
- package/src/strings/Concatenated.test.ts +4 -3
- package/src/strings/Concatenated.ts +5 -7
- package/src/strings/Template._main.test.ts +5 -4
- package/src/strings/Template._place.test.ts +4 -3
- package/src/strings/Template.ts +21 -9
- package/src/structures/HashTable.test.ts +4 -3
- package/src/structures/HashTable.ts +6 -8
- package/src/structures/Record._main.test.ts +3 -2
- package/src/structures/Record._mixed.test.ts +3 -2
- package/src/structures/Record.concatenated.test.ts +7 -8
- package/src/structures/Record.nested.test.ts +3 -2
- package/src/structures/Record.ts +10 -14
- package/src/system/RegexpMatch._group.test.ts +3 -2
- package/src/system/RegexpMatch._main.test.ts +3 -2
- package/src/system/RegexpMatch.ts +12 -11
- package/src/system/RegexpMatched.test.ts +4 -3
- package/src/system/RegexpMatched.ts +11 -10
- package/src/system/RegexpReplaced.test.ts +4 -3
- package/src/system/RegexpReplaced.ts +6 -8
- package/src/system/Set.test.ts +4 -3
- package/src/system/Set.ts +9 -8
- package/src/system/index.ts +1 -1
- package/src/behaviors/Branch.dontRespondAfterRespond.test.ts +0 -35
- package/src/behaviors/Transaction.test.ts +0 -44
- package/src/behaviors/Transaction.ts +0 -42
|
@@ -5,8 +5,6 @@ import {
|
|
|
5
5
|
Message,
|
|
6
6
|
MessageType,
|
|
7
7
|
Shared,
|
|
8
|
-
Tap,
|
|
9
|
-
TapType,
|
|
10
8
|
} from "silentium";
|
|
11
9
|
|
|
12
10
|
/**
|
|
@@ -15,38 +13,33 @@ import {
|
|
|
15
13
|
* respond before $timeout then the value from base will be returned
|
|
16
14
|
*/
|
|
17
15
|
export function Deadline<T>(
|
|
18
|
-
error: TapType<Error>,
|
|
19
16
|
$base: MessageType<T>,
|
|
20
17
|
_timeout: MaybeMessage<number>,
|
|
21
18
|
) {
|
|
22
19
|
const $timeout = ActualMessage(_timeout);
|
|
23
|
-
return Message<T>(function () {
|
|
20
|
+
return Message<T>(function DeadlineImpl(resolve, reject) {
|
|
24
21
|
let timer: ReturnType<typeof setTimeout> | number = 0;
|
|
25
|
-
const base = Shared($base
|
|
26
|
-
$timeout.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
let timeoutReached = false;
|
|
22
|
+
const base = Shared($base);
|
|
23
|
+
$timeout.then((timeout) => {
|
|
24
|
+
if (timer) {
|
|
25
|
+
clearTimeout(timer);
|
|
26
|
+
}
|
|
27
|
+
let timeoutReached = false;
|
|
32
28
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
29
|
+
timer = setTimeout(() => {
|
|
30
|
+
if (timeoutReached) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
timeoutReached = true;
|
|
34
|
+
reject(new Error("Timeout reached in Deadline"));
|
|
35
|
+
}, timeout);
|
|
40
36
|
|
|
41
|
-
|
|
42
|
-
|
|
37
|
+
const f = Filtered(base, () => !timeoutReached);
|
|
38
|
+
f.then(resolve);
|
|
43
39
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
);
|
|
49
|
-
}),
|
|
50
|
-
);
|
|
40
|
+
base.then(() => {
|
|
41
|
+
timeoutReached = true;
|
|
42
|
+
});
|
|
43
|
+
});
|
|
51
44
|
});
|
|
52
45
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { Late, Primitive, Shared
|
|
2
|
-
import { Deferred } from "../behaviors/Deferred";
|
|
1
|
+
import { Late, Primitive, Shared } from "silentium";
|
|
3
2
|
import { expect, test, vi } from "vitest";
|
|
4
3
|
|
|
4
|
+
import { Deferred } from "../behaviors/Deferred";
|
|
5
|
+
|
|
5
6
|
test("Deferred.test", () => {
|
|
6
7
|
const $url = Late<string>("http://hello.com");
|
|
7
8
|
const $layout = Late<string>();
|
|
@@ -9,13 +10,13 @@ test("Deferred.test", () => {
|
|
|
9
10
|
const urlWithLayoutSrc = Shared(Deferred($url, $layout));
|
|
10
11
|
|
|
11
12
|
const g1 = vi.fn();
|
|
12
|
-
urlWithLayoutSrc.
|
|
13
|
+
urlWithLayoutSrc.then(g1);
|
|
13
14
|
expect(g1).not.toHaveBeenCalled();
|
|
14
15
|
|
|
15
16
|
$layout.use("layout here");
|
|
16
17
|
|
|
17
18
|
const g2 = vi.fn();
|
|
18
|
-
urlWithLayoutSrc.
|
|
19
|
+
urlWithLayoutSrc.then(g2);
|
|
19
20
|
$url.use("http://new.com");
|
|
20
21
|
expect(g2).toHaveBeenCalledWith("http://hello.com");
|
|
21
22
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isFilled, Message, MessageType, Primitive
|
|
1
|
+
import { isFilled, Message, MessageType, Primitive } from "silentium";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Defer one source after another, gives values Of baseSrc only when triggerSrc responds
|
|
@@ -8,15 +8,13 @@ export function Deferred<T>(
|
|
|
8
8
|
$base: MessageType<T>,
|
|
9
9
|
$trigger: MessageType<unknown>,
|
|
10
10
|
) {
|
|
11
|
-
return Message(function () {
|
|
11
|
+
return Message<T>(function DeferredImpl(r) {
|
|
12
12
|
const base = Primitive($base);
|
|
13
|
-
$trigger.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}),
|
|
20
|
-
);
|
|
13
|
+
$trigger.then(() => {
|
|
14
|
+
const value = base.primitive();
|
|
15
|
+
if (isFilled(value)) {
|
|
16
|
+
r(value);
|
|
17
|
+
}
|
|
18
|
+
});
|
|
21
19
|
});
|
|
22
20
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { LateShared
|
|
1
|
+
import { LateShared } from "silentium";
|
|
2
2
|
import { expect, test, vi } from "vitest";
|
|
3
|
+
|
|
3
4
|
import { Detached } from "../behaviors/Detached";
|
|
4
5
|
|
|
5
6
|
test("Detached.test.ts", function DetachedTest() {
|
|
@@ -7,14 +8,14 @@ test("Detached.test.ts", function DetachedTest() {
|
|
|
7
8
|
const l2 = Detached(l);
|
|
8
9
|
|
|
9
10
|
const g1 = vi.fn();
|
|
10
|
-
l2.
|
|
11
|
+
l2.then(g1);
|
|
11
12
|
|
|
12
13
|
l.use(2);
|
|
13
14
|
|
|
14
15
|
expect(g1).toHaveBeenCalledWith(1);
|
|
15
16
|
|
|
16
17
|
const g2 = vi.fn();
|
|
17
|
-
l.
|
|
18
|
+
l.then(g2);
|
|
18
19
|
|
|
19
20
|
expect(g2).toHaveBeenCalledWith(2);
|
|
20
21
|
});
|
|
@@ -6,10 +6,10 @@ import { isFilled, Message, MessageType, Primitive } from "silentium";
|
|
|
6
6
|
* but not react to new values of the base message
|
|
7
7
|
*/
|
|
8
8
|
export function Detached<T>($base: MessageType<T>): MessageType<T> {
|
|
9
|
-
return Message(function () {
|
|
9
|
+
return Message<T>(function DetachedImpl(r) {
|
|
10
10
|
const v = Primitive($base).primitive();
|
|
11
11
|
if (isFilled(v)) {
|
|
12
|
-
|
|
12
|
+
r(v);
|
|
13
13
|
}
|
|
14
14
|
});
|
|
15
15
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { Late
|
|
2
|
-
import { Dirty } from "../behaviors/Dirty";
|
|
1
|
+
import { Late } from "silentium";
|
|
3
2
|
import { expect, test, vi } from "vitest";
|
|
4
3
|
|
|
4
|
+
import { Dirty } from "../behaviors/Dirty";
|
|
5
|
+
|
|
5
6
|
test("Dirty.test", () => {
|
|
6
7
|
const form = Late({
|
|
7
8
|
name: "one",
|
|
@@ -9,7 +10,7 @@ test("Dirty.test", () => {
|
|
|
9
10
|
});
|
|
10
11
|
const d = Dirty(form);
|
|
11
12
|
const g = vi.fn();
|
|
12
|
-
d.
|
|
13
|
+
d.then(g);
|
|
13
14
|
|
|
14
15
|
d.use({
|
|
15
16
|
name: "new",
|
package/src/behaviors/Dirty.ts
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
All,
|
|
3
|
-
Applied,
|
|
4
|
-
Late,
|
|
5
|
-
MessageType,
|
|
6
|
-
SourceType,
|
|
7
|
-
Tap,
|
|
8
|
-
TapType,
|
|
9
|
-
} from "silentium";
|
|
1
|
+
import { All, Applied, Late, MessageSource, MessageType } from "silentium";
|
|
10
2
|
|
|
11
3
|
/**
|
|
12
4
|
* Takes source and remember it first value
|
|
@@ -18,54 +10,35 @@ export function Dirty<T>(
|
|
|
18
10
|
keep: string[] = [],
|
|
19
11
|
exclude: string[] = [],
|
|
20
12
|
cloner?: (v: T) => T,
|
|
21
|
-
)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
class DirtySource<T> implements SourceType<T> {
|
|
26
|
-
private $comparing = Late<T>();
|
|
27
|
-
private cloner: (v: T) => T;
|
|
28
|
-
|
|
29
|
-
public constructor(
|
|
30
|
-
private $base: MessageType<T>,
|
|
31
|
-
private keep: string[] = [],
|
|
32
|
-
private exclude: string[] = [],
|
|
33
|
-
cloner?: (v: T) => T,
|
|
34
|
-
) {
|
|
35
|
-
if (cloner === undefined) {
|
|
36
|
-
this.cloner = (value) => JSON.parse(JSON.stringify(value));
|
|
37
|
-
} else {
|
|
38
|
-
this.cloner = cloner;
|
|
39
|
-
}
|
|
13
|
+
) {
|
|
14
|
+
const $comparing = Late<T>();
|
|
15
|
+
if (cloner === undefined) {
|
|
16
|
+
cloner = (value) => JSON.parse(JSON.stringify(value));
|
|
40
17
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
Tap(([comparing, base]) => {
|
|
18
|
+
return MessageSource<T>(
|
|
19
|
+
function DirtyImpl(r) {
|
|
20
|
+
const $comparingClone = Applied($comparing, cloner);
|
|
21
|
+
All($comparingClone, $base).then(([comparing, base]) => {
|
|
46
22
|
if (!comparing) {
|
|
47
23
|
return;
|
|
48
24
|
}
|
|
49
|
-
|
|
25
|
+
r(
|
|
50
26
|
Object.fromEntries(
|
|
51
27
|
Object.entries(comparing).filter(([key, value]) => {
|
|
52
|
-
if (
|
|
28
|
+
if (keep.includes(key)) {
|
|
53
29
|
return true;
|
|
54
30
|
}
|
|
55
|
-
if (
|
|
31
|
+
if (exclude.includes(key)) {
|
|
56
32
|
return false;
|
|
57
33
|
}
|
|
58
34
|
return value !== (base as Record<string, unknown>)[key];
|
|
59
35
|
}),
|
|
60
36
|
) as T,
|
|
61
37
|
);
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
this.$comparing.use(v);
|
|
69
|
-
return this;
|
|
70
|
-
}
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
(v) => {
|
|
41
|
+
$comparing.use(v);
|
|
42
|
+
},
|
|
43
|
+
);
|
|
71
44
|
}
|
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import { Late
|
|
2
|
-
import { Loading } from "../behaviors/Loading";
|
|
1
|
+
import { Late } from "silentium";
|
|
3
2
|
import { expect, test, vi } from "vitest";
|
|
4
3
|
|
|
4
|
+
import { Loading } from "../behaviors/Loading";
|
|
5
|
+
|
|
5
6
|
test("Loading.test", () => {
|
|
6
7
|
const $loadingStart = Late();
|
|
7
8
|
const $loadingFinish = Late();
|
|
8
9
|
const $loading = Loading($loadingStart, $loadingFinish);
|
|
9
10
|
const g = vi.fn();
|
|
10
|
-
$loading.
|
|
11
|
+
$loading.then(g);
|
|
11
12
|
$loadingStart.use({});
|
|
12
13
|
expect(g).toHaveBeenLastCalledWith(true);
|
|
13
14
|
$loadingFinish.use({});
|
package/src/behaviors/Loading.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Message, MessageType
|
|
1
|
+
import { Message, MessageType } from "silentium";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Representation Of loading process
|
|
@@ -10,8 +10,8 @@ export function Loading(
|
|
|
10
10
|
$start: MessageType<unknown>,
|
|
11
11
|
$finish: MessageType<unknown>,
|
|
12
12
|
) {
|
|
13
|
-
return Message<boolean>(function () {
|
|
14
|
-
$start.
|
|
15
|
-
$finish.
|
|
13
|
+
return Message<boolean>(function LoadingImpl(r) {
|
|
14
|
+
$start.then(() => r(true));
|
|
15
|
+
$finish.then(() => r(false));
|
|
16
16
|
});
|
|
17
17
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { Late, Shared
|
|
2
|
-
import { Lock } from "../behaviors/Lock";
|
|
1
|
+
import { Late, Shared } from "silentium";
|
|
3
2
|
import { expect, test, vi } from "vitest";
|
|
4
3
|
|
|
4
|
+
import { Lock } from "../behaviors/Lock";
|
|
5
|
+
|
|
5
6
|
test("Lock.test", () => {
|
|
6
7
|
const source = Late<number>(1);
|
|
7
8
|
const lockSrc = Late<boolean>(false);
|
|
@@ -9,7 +10,7 @@ test("Lock.test", () => {
|
|
|
9
10
|
const ls = Lock(source, lockSrc);
|
|
10
11
|
const lockedSrc = Shared(ls);
|
|
11
12
|
const g = vi.fn();
|
|
12
|
-
lockedSrc.
|
|
13
|
+
lockedSrc.then(g);
|
|
13
14
|
|
|
14
15
|
expect(g).toHaveBeenLastCalledWith(1);
|
|
15
16
|
|
package/src/behaviors/Lock.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Filtered, Message, MessageType
|
|
1
|
+
import { Filtered, Message, MessageType } from "silentium";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Allows locking messages
|
|
@@ -6,14 +6,12 @@ import { Filtered, Message, MessageType, Tap } from "silentium";
|
|
|
6
6
|
* https://silentium-lab.github.io/silentium-components/#/behaviors/lock
|
|
7
7
|
*/
|
|
8
8
|
export function Lock<T>($base: MessageType<T>, $lock: MessageType<boolean>) {
|
|
9
|
-
return Message<T>(function () {
|
|
9
|
+
return Message<T>(function LockImpl(r) {
|
|
10
10
|
let locked = false;
|
|
11
|
-
$lock.
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}),
|
|
15
|
-
);
|
|
11
|
+
$lock.then((newLock) => {
|
|
12
|
+
locked = newLock;
|
|
13
|
+
});
|
|
16
14
|
const i = Filtered($base, () => !locked);
|
|
17
|
-
i.
|
|
15
|
+
i.then(r);
|
|
18
16
|
});
|
|
19
17
|
}
|
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
import { Late, Shared
|
|
2
|
-
import { Memo } from "../behaviors/Memo";
|
|
1
|
+
import { Late, Shared } from "silentium";
|
|
3
2
|
import { expect, test, vi } from "vitest";
|
|
4
3
|
|
|
4
|
+
import { Memo } from "../behaviors/Memo";
|
|
5
|
+
|
|
5
6
|
test("Memo.test", () => {
|
|
6
7
|
const l = Late<number>(1);
|
|
7
8
|
const mem = Shared(Memo(l));
|
|
8
9
|
const g = vi.fn();
|
|
9
|
-
mem.
|
|
10
|
+
mem.then(g);
|
|
10
11
|
let counter = 0;
|
|
11
12
|
|
|
12
|
-
mem.
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}),
|
|
16
|
-
);
|
|
13
|
+
mem.then(() => {
|
|
14
|
+
counter += 1;
|
|
15
|
+
});
|
|
17
16
|
|
|
18
17
|
l.use(2);
|
|
19
18
|
l.use(2);
|
package/src/behaviors/Memo.ts
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
|
-
import { isFilled, Message, MessageType
|
|
1
|
+
import { isFilled, Message, MessageType } from "silentium";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Didn't respond if new value Of baseSrc equals to old value
|
|
5
5
|
* https://silentium-lab.github.io/silentium-components/#/behaviors/memo
|
|
6
6
|
*/
|
|
7
7
|
export function Memo<T>($base: MessageType<T>) {
|
|
8
|
-
return Message<T>(function () {
|
|
8
|
+
return Message<T>(function MemoImpl(r) {
|
|
9
9
|
let last: T | null = null;
|
|
10
|
-
$base.
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}),
|
|
17
|
-
);
|
|
10
|
+
$base.then((v) => {
|
|
11
|
+
if (v !== last && isFilled(v)) {
|
|
12
|
+
r(v);
|
|
13
|
+
last = v;
|
|
14
|
+
}
|
|
15
|
+
});
|
|
18
16
|
});
|
|
19
17
|
}
|
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
import { Late, Shared
|
|
2
|
-
import { OnlyChanged } from "../behaviors/OnlyChanged";
|
|
1
|
+
import { Late, Shared } from "silentium";
|
|
3
2
|
import { expect, test, vi } from "vitest";
|
|
4
3
|
|
|
4
|
+
import { OnlyChanged } from "../behaviors/OnlyChanged";
|
|
5
|
+
|
|
5
6
|
test("OnlyChanged.test", () => {
|
|
6
7
|
const src = Late<number>(1);
|
|
7
8
|
const changedSrc = Shared(OnlyChanged(src));
|
|
8
9
|
|
|
9
10
|
const g = vi.fn();
|
|
10
|
-
changedSrc.
|
|
11
|
+
changedSrc.then(g);
|
|
11
12
|
expect(g).not.toBeCalled();
|
|
12
13
|
|
|
13
14
|
src.use(2);
|
|
14
15
|
|
|
15
16
|
const g2 = vi.fn();
|
|
16
|
-
changedSrc.
|
|
17
|
+
changedSrc.then(g2);
|
|
17
18
|
expect(g2).toBeCalledWith(2);
|
|
18
19
|
});
|
|
@@ -1,20 +1,18 @@
|
|
|
1
|
-
import { Message, MessageType
|
|
1
|
+
import { Message, MessageType } from "silentium";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Represents source what was changed at least once
|
|
5
5
|
* https://silentium-lab.github.io/silentium-components/#/behaviors/only-changed
|
|
6
6
|
*/
|
|
7
7
|
export function OnlyChanged<T>($base: MessageType<T>) {
|
|
8
|
-
return Message<T>(function () {
|
|
8
|
+
return Message<T>(function OnlyChangedImpl(r) {
|
|
9
9
|
let first = false;
|
|
10
|
-
$base.
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}),
|
|
18
|
-
);
|
|
10
|
+
$base.then((v) => {
|
|
11
|
+
if (first === false) {
|
|
12
|
+
first = true;
|
|
13
|
+
} else {
|
|
14
|
+
r(v);
|
|
15
|
+
}
|
|
16
|
+
});
|
|
19
17
|
});
|
|
20
18
|
}
|
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LateShared, Of } from "silentium";
|
|
2
2
|
import { describe, expect, test, vi } from "vitest";
|
|
3
|
+
|
|
3
4
|
import { Part } from "../behaviors/Part";
|
|
4
5
|
|
|
5
6
|
describe("Part.test", () => {
|
|
6
7
|
test("regular", () => {
|
|
7
|
-
const $record =
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}),
|
|
12
|
-
);
|
|
8
|
+
const $record = LateShared({
|
|
9
|
+
name: "Peter",
|
|
10
|
+
surname: "Parker",
|
|
11
|
+
});
|
|
13
12
|
const $name = Part<string>($record, Of("name"));
|
|
14
13
|
const g = vi.fn();
|
|
15
|
-
$record.
|
|
14
|
+
$record.then(g);
|
|
16
15
|
expect(g).toHaveBeenLastCalledWith({ name: "Peter", surname: "Parker" });
|
|
17
16
|
|
|
18
17
|
$name.use("Shmiter");
|
|
@@ -20,15 +19,13 @@ describe("Part.test", () => {
|
|
|
20
19
|
});
|
|
21
20
|
|
|
22
21
|
test("raw key", () => {
|
|
23
|
-
const $record =
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}),
|
|
28
|
-
);
|
|
22
|
+
const $record = LateShared({
|
|
23
|
+
name: "Peter",
|
|
24
|
+
surname: "Parker",
|
|
25
|
+
});
|
|
29
26
|
const $name = Part<string>($record, "name");
|
|
30
27
|
const g = vi.fn();
|
|
31
|
-
$record.
|
|
28
|
+
$record.then(g);
|
|
32
29
|
expect(g).toHaveBeenLastCalledWith({ name: "Peter", surname: "Parker" });
|
|
33
30
|
|
|
34
31
|
$name.use("Shmiter");
|
package/src/behaviors/Part.ts
CHANGED
|
@@ -3,13 +3,11 @@ import {
|
|
|
3
3
|
All,
|
|
4
4
|
isFilled,
|
|
5
5
|
MaybeMessage,
|
|
6
|
-
|
|
6
|
+
MessageSource,
|
|
7
|
+
MessageSourceType,
|
|
7
8
|
Primitive,
|
|
8
9
|
Shared,
|
|
9
|
-
SharedSource,
|
|
10
10
|
SourceType,
|
|
11
|
-
Tap,
|
|
12
|
-
TapType,
|
|
13
11
|
} from "silentium";
|
|
14
12
|
|
|
15
13
|
/**
|
|
@@ -20,46 +18,31 @@ export function Part<
|
|
|
20
18
|
R,
|
|
21
19
|
T extends object | Array<any> = any,
|
|
22
20
|
K extends string = any,
|
|
23
|
-
>($base:
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
{
|
|
30
|
-
private $base: SourceType<T>;
|
|
31
|
-
private $keyed: MessageType<K>;
|
|
32
|
-
|
|
33
|
-
public constructor($base: SourceType<T>, $key: MessageType<K>) {
|
|
34
|
-
this.$base = SharedSource($base);
|
|
35
|
-
this.$keyed = Shared($key);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
public pipe(transport: TapType<R, null>): this {
|
|
39
|
-
All(this.$base, this.$keyed).pipe(
|
|
40
|
-
Tap(([base, keyed]) => {
|
|
21
|
+
>($base: MessageSourceType<T>, key: MaybeMessage<K>): SourceType<R> {
|
|
22
|
+
const $baseShared = Shared($base);
|
|
23
|
+
const $keyedShared = Shared(ActualMessage(key));
|
|
24
|
+
return MessageSource(
|
|
25
|
+
function PartImpl(r) {
|
|
26
|
+
All($baseShared, $keyedShared).then(([base, keyed]) => {
|
|
41
27
|
const keys = keyed.split(".");
|
|
42
28
|
let value: unknown = base;
|
|
43
|
-
keys.forEach((key) => {
|
|
29
|
+
keys.forEach((key: string) => {
|
|
44
30
|
value = (value as Record<string, unknown>)[key];
|
|
45
31
|
});
|
|
46
32
|
if (value !== undefined && value !== base) {
|
|
47
|
-
|
|
33
|
+
r(value as R);
|
|
48
34
|
}
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
return this;
|
|
64
|
-
}
|
|
35
|
+
});
|
|
36
|
+
},
|
|
37
|
+
(value) => {
|
|
38
|
+
const key = Primitive($keyedShared);
|
|
39
|
+
if (isFilled(key)) {
|
|
40
|
+
const base = Primitive($base);
|
|
41
|
+
$base.use({
|
|
42
|
+
...base.primitiveWithException(),
|
|
43
|
+
[key.primitiveWithException()]: value,
|
|
44
|
+
} as T);
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
);
|
|
65
48
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { Of
|
|
1
|
+
import { Of } from "silentium";
|
|
2
2
|
import { expect, test, vi } from "vitest";
|
|
3
|
+
|
|
3
4
|
import { Path } from "./Path";
|
|
4
5
|
|
|
5
6
|
test("Path._keyRaw.test", () => {
|
|
@@ -9,6 +10,6 @@ test("Path._keyRaw.test", () => {
|
|
|
9
10
|
};
|
|
10
11
|
const name = Path<string>(Of(record), "name");
|
|
11
12
|
const g = vi.fn();
|
|
12
|
-
name.
|
|
13
|
+
name.then(g);
|
|
13
14
|
expect(g).toHaveBeenLastCalledWith("Peter");
|
|
14
15
|
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { Of
|
|
1
|
+
import { Of } from "silentium";
|
|
2
2
|
import { expect, test, vi } from "vitest";
|
|
3
|
+
|
|
3
4
|
import { Path } from "./Path";
|
|
4
5
|
|
|
5
6
|
test("Path._main.test", () => {
|
|
@@ -9,6 +10,6 @@ test("Path._main.test", () => {
|
|
|
9
10
|
};
|
|
10
11
|
const name = Path<string>(Of(record), Of("name"));
|
|
11
12
|
const g = vi.fn();
|
|
12
|
-
name.
|
|
13
|
+
name.then(g);
|
|
13
14
|
expect(g).toHaveBeenLastCalledWith("Peter");
|
|
14
15
|
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { Of
|
|
1
|
+
import { Of } from "silentium";
|
|
2
2
|
import { expect, test, vi } from "vitest";
|
|
3
|
+
|
|
3
4
|
import { Path } from "./Path";
|
|
4
5
|
|
|
5
6
|
test("Path.index.test", () => {
|
|
@@ -13,6 +14,6 @@ test("Path.index.test", () => {
|
|
|
13
14
|
};
|
|
14
15
|
const bestColor = Path(Of(record), Of("colors.0"));
|
|
15
16
|
const g = vi.fn();
|
|
16
|
-
bestColor.
|
|
17
|
+
bestColor.then(g);
|
|
17
18
|
expect(g).toHaveBeenLastCalledWith("blue");
|
|
18
19
|
});
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { Of
|
|
2
|
-
import { Path } from "../behaviors/Path";
|
|
1
|
+
import { Of } from "silentium";
|
|
3
2
|
import { expect, test, vi } from "vitest";
|
|
4
3
|
|
|
4
|
+
import { Path } from "../behaviors/Path";
|
|
5
|
+
|
|
5
6
|
test("Path.nested.test", () => {
|
|
6
7
|
const record = {
|
|
7
8
|
name: "Peter",
|
|
@@ -12,6 +13,6 @@ test("Path.nested.test", () => {
|
|
|
12
13
|
};
|
|
13
14
|
const typeName = Path(Of(record), Of("type.name"));
|
|
14
15
|
const g = vi.fn();
|
|
15
|
-
typeName.
|
|
16
|
+
typeName.then(g);
|
|
16
17
|
expect(g).toHaveBeenLastCalledWith("spider-man");
|
|
17
18
|
});
|