silentium-components 0.0.70 → 0.0.71
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 +12 -0
- package/dist/silentium-components.cjs +491 -436
- package/dist/silentium-components.cjs.map +1 -1
- package/dist/silentium-components.d.ts +42 -36
- package/dist/silentium-components.js +493 -438
- 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 +493 -438
- package/dist/silentium-components.mjs.map +1 -1
- package/package.json +2 -2
- package/src/behaviors/Branch._main.test.ts +3 -3
- package/src/behaviors/Branch.branchesDontAffectResult.test.ts +4 -4
- package/src/behaviors/Branch.dontRespondAfterRespond.test.ts +6 -6
- package/src/behaviors/Branch.ts +23 -24
- package/src/behaviors/BranchLazy._main.test.ts +5 -5
- package/src/behaviors/BranchLazy.ts +31 -23
- package/src/behaviors/Const.test.ts +10 -8
- package/src/behaviors/Const.ts +9 -10
- package/src/behaviors/Deadline._main.test.ts +4 -4
- package/src/behaviors/Deadline._value.test.ts +5 -5
- package/src/behaviors/Deadline.ts +37 -28
- package/src/behaviors/Deferred.test.ts +10 -10
- package/src/behaviors/Deferred.ts +14 -12
- package/src/behaviors/Detached.test.ts +4 -4
- package/src/behaviors/Detached.ts +6 -6
- package/src/behaviors/Dirty.test.ts +3 -3
- package/src/behaviors/Dirty.ts +46 -26
- package/src/behaviors/Loading.test.ts +7 -10
- package/src/behaviors/Loading.ts +9 -9
- package/src/behaviors/Lock.test.ts +3 -3
- package/src/behaviors/Lock.ts +12 -10
- package/src/behaviors/Memo.test.ts +8 -6
- package/src/behaviors/Memo.ts +13 -12
- package/src/behaviors/OnlyChanged.test.ts +4 -4
- package/src/behaviors/OnlyChanged.ts +14 -13
- package/src/behaviors/Part.test.ts +6 -6
- package/src/behaviors/Part.ts +53 -30
- package/src/behaviors/Path._main.test.ts +2 -2
- package/src/behaviors/Path.index.test.ts +2 -2
- package/src/behaviors/Path.nested.test.ts +2 -2
- package/src/behaviors/Path.ts +20 -21
- package/src/behaviors/Polling.test.ts +10 -10
- package/src/behaviors/Polling.ts +10 -8
- package/src/behaviors/Shot._main.test.ts +10 -10
- package/src/behaviors/Shot._onlyChanged.test.ts +13 -11
- package/src/behaviors/Shot.ts +15 -12
- package/src/behaviors/Task.ts +4 -4
- package/src/behaviors/Tick.test.ts +7 -7
- package/src/behaviors/Tick.ts +13 -11
- package/src/behaviors/Transaction.test.ts +3 -3
- package/src/behaviors/Transaction.ts +13 -12
- package/src/boolean/And.test.ts +11 -11
- package/src/boolean/And.ts +10 -11
- package/src/boolean/Bool.test.ts +2 -2
- package/src/boolean/Bool.ts +5 -5
- package/src/boolean/Not.test.ts +3 -3
- package/src/boolean/Not.ts +9 -7
- package/src/boolean/Or.test.ts +3 -3
- package/src/boolean/Or.ts +10 -11
- package/src/formats/FromJson.ts +14 -12
- package/src/formats/ToJson.ts +14 -12
- package/src/lists/First.test.ts +2 -2
- package/src/lists/First.ts +5 -5
- package/src/navigation/Router._main.test.ts +20 -13
- package/src/navigation/Router._nested.test.ts +41 -33
- package/src/navigation/Router.ts +48 -50
- package/src/strings/Concatenated.test.ts +3 -3
- package/src/strings/Concatenated.ts +8 -9
- package/src/strings/Template._main.test.ts +3 -3
- package/src/strings/Template._place.test.ts +2 -2
- package/src/strings/Template.ts +54 -40
- package/src/structures/HashTable.test.ts +6 -6
- package/src/structures/HashTable.ts +10 -8
- package/src/structures/Record._main.test.ts +6 -6
- package/src/structures/Record.concatenated.test.ts +12 -10
- package/src/structures/Record.nested.test.ts +5 -5
- package/src/structures/RecordOf.ts +14 -12
- package/src/system/RegexpMatch._group.test.ts +4 -4
- package/src/system/RegexpMatch._main.test.ts +4 -4
- package/src/system/RegexpMatch.ts +9 -11
- package/src/system/RegexpMatched.test.ts +5 -5
- package/src/system/RegexpMatched.ts +8 -10
- package/src/system/RegexpReplaced.test.ts +5 -5
- package/src/system/RegexpReplaced.ts +10 -11
- package/src/system/Set.test.ts +5 -5
- package/src/system/Set.ts +9 -11
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import { Late, Primitive, Shared } from "silentium";
|
|
1
|
+
import { Late, Primitive, Shared, Transport } from "silentium";
|
|
2
2
|
import { Deferred } from "../behaviors/Deferred";
|
|
3
3
|
import { expect, test, vi } from "vitest";
|
|
4
4
|
|
|
5
5
|
test("Deferred.test", () => {
|
|
6
|
-
const
|
|
7
|
-
const
|
|
6
|
+
const $url = Late<string>("http://hello.com");
|
|
7
|
+
const $layout = Late<string>();
|
|
8
8
|
|
|
9
|
-
const urlWithLayoutSrc = Shared(Deferred(
|
|
9
|
+
const urlWithLayoutSrc = Shared(Deferred($url, $layout));
|
|
10
10
|
|
|
11
11
|
const g1 = vi.fn();
|
|
12
|
-
urlWithLayoutSrc.event(g1);
|
|
12
|
+
urlWithLayoutSrc.event(Transport(g1));
|
|
13
13
|
expect(g1).not.toHaveBeenCalled();
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
$layout.use("layout here");
|
|
16
16
|
|
|
17
17
|
const g2 = vi.fn();
|
|
18
|
-
urlWithLayoutSrc.event(g2);
|
|
19
|
-
|
|
18
|
+
urlWithLayoutSrc.event(Transport(g2));
|
|
19
|
+
$url.use("http://new.com");
|
|
20
20
|
expect(g2).toHaveBeenCalledWith("http://hello.com");
|
|
21
21
|
|
|
22
|
-
const urlSync = Primitive(urlWithLayoutSrc
|
|
22
|
+
const urlSync = Primitive(urlWithLayoutSrc);
|
|
23
23
|
|
|
24
24
|
expect(urlSync.primitive()).toBe("http://hello.com");
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
$layout.use("layout here again");
|
|
27
27
|
|
|
28
28
|
expect(urlSync.primitive()).toBe("http://new.com");
|
|
29
29
|
});
|
|
@@ -1,20 +1,22 @@
|
|
|
1
|
-
import { EventType, isFilled, Primitive } from "silentium";
|
|
1
|
+
import { Event, EventType, isFilled, Primitive, Transport } from "silentium";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Defer one source after another, gives values Of baseSrc only when triggerSrc responds
|
|
5
5
|
* https://silentium-lab.github.io/silentium-components/#/behaviors/deferred
|
|
6
6
|
*/
|
|
7
7
|
export function Deferred<T>(
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
$base: EventType<T>,
|
|
9
|
+
$trigger: EventType<unknown>,
|
|
10
10
|
): EventType<T> {
|
|
11
|
-
return (
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
11
|
+
return Event((transport) => {
|
|
12
|
+
const base = Primitive($base);
|
|
13
|
+
$trigger.event(
|
|
14
|
+
Transport(() => {
|
|
15
|
+
const value = base.primitive();
|
|
16
|
+
if (isFilled(value)) {
|
|
17
|
+
transport.use(value);
|
|
18
|
+
}
|
|
19
|
+
}),
|
|
20
|
+
);
|
|
21
|
+
});
|
|
20
22
|
}
|
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import { LateShared } from "silentium";
|
|
1
|
+
import { LateShared, Transport } from "silentium";
|
|
2
2
|
import { expect, test, vi } from "vitest";
|
|
3
3
|
import { Detached } from "../behaviors/Detached";
|
|
4
4
|
|
|
5
5
|
test("Detached.test.ts", function DetachedTest() {
|
|
6
6
|
const l = LateShared(1);
|
|
7
|
-
const l2 = Detached(l
|
|
7
|
+
const l2 = Detached(l);
|
|
8
8
|
|
|
9
9
|
const g1 = vi.fn();
|
|
10
|
-
l2(g1);
|
|
10
|
+
l2.event(Transport(g1));
|
|
11
11
|
|
|
12
12
|
l.use(2);
|
|
13
13
|
|
|
14
14
|
expect(g1).toHaveBeenCalledWith(1);
|
|
15
15
|
|
|
16
16
|
const g2 = vi.fn();
|
|
17
|
-
l.event(g2);
|
|
17
|
+
l.event(Transport(g2));
|
|
18
18
|
|
|
19
19
|
expect(g2).toHaveBeenCalledWith(2);
|
|
20
20
|
});
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { EventType, isFilled, Primitive } from "silentium";
|
|
1
|
+
import { Event, EventType, isFilled, Primitive } from "silentium";
|
|
2
2
|
|
|
3
|
-
export function Detached<T>(
|
|
4
|
-
return
|
|
5
|
-
const v = Primitive(
|
|
3
|
+
export function Detached<T>($base: EventType<T>): EventType<T> {
|
|
4
|
+
return Event((transport) => {
|
|
5
|
+
const v = Primitive($base).primitive();
|
|
6
6
|
if (isFilled(v)) {
|
|
7
|
-
|
|
7
|
+
transport.use(v);
|
|
8
8
|
}
|
|
9
|
-
};
|
|
9
|
+
});
|
|
10
10
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Late } from "silentium";
|
|
1
|
+
import { Late, Transport } from "silentium";
|
|
2
2
|
import { Dirty } from "../behaviors/Dirty";
|
|
3
3
|
import { expect, test, vi } from "vitest";
|
|
4
4
|
|
|
@@ -7,9 +7,9 @@ test("Dirty.test", () => {
|
|
|
7
7
|
name: "one",
|
|
8
8
|
surname: "two",
|
|
9
9
|
});
|
|
10
|
-
const d = Dirty(form
|
|
10
|
+
const d = Dirty(form);
|
|
11
11
|
const g = vi.fn();
|
|
12
|
-
d.event(g);
|
|
12
|
+
d.event(Transport(g));
|
|
13
13
|
|
|
14
14
|
d.use({
|
|
15
15
|
name: "new",
|
package/src/behaviors/Dirty.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
All,
|
|
3
|
+
Applied,
|
|
4
|
+
EventType,
|
|
5
|
+
Late,
|
|
6
|
+
SourceType,
|
|
7
|
+
Transport,
|
|
8
|
+
TransportType,
|
|
9
|
+
} from "silentium";
|
|
2
10
|
|
|
3
11
|
/**
|
|
4
12
|
* Takes source and remember it first value
|
|
@@ -6,46 +14,58 @@ import { All, Applied, EventType, Late, SourceType } from "silentium";
|
|
|
6
14
|
* https://silentium-lab.github.io/silentium-components/#/behaviors/dirty
|
|
7
15
|
*/
|
|
8
16
|
export function Dirty<T>(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
17
|
+
$base: EventType<T>,
|
|
18
|
+
keep: string[] = [],
|
|
19
|
+
exclude: string[] = [],
|
|
20
|
+
cloner?: (v: T) => T,
|
|
13
21
|
): SourceType<T> {
|
|
14
|
-
|
|
22
|
+
return new DirtySource($base, keep, exclude, cloner);
|
|
23
|
+
}
|
|
15
24
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
25
|
+
class DirtySource<T> implements SourceType<T> {
|
|
26
|
+
private $comparing = Late<T>();
|
|
27
|
+
private cloner: (v: T) => T;
|
|
19
28
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
29
|
+
public constructor(
|
|
30
|
+
private $base: EventType<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
|
+
}
|
|
40
|
+
}
|
|
23
41
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
42
|
+
public event(transport: TransportType<T>) {
|
|
43
|
+
const $comparing = Applied(this.$comparing, this.cloner);
|
|
44
|
+
All($comparing, this.$base).event(
|
|
45
|
+
Transport(([comparing, base]) => {
|
|
28
46
|
if (!comparing) {
|
|
29
47
|
return;
|
|
30
48
|
}
|
|
31
|
-
|
|
32
|
-
user(
|
|
49
|
+
transport.use(
|
|
33
50
|
Object.fromEntries(
|
|
34
51
|
Object.entries(comparing).filter(([key, value]) => {
|
|
35
|
-
if (
|
|
52
|
+
if (this.keep.includes(key)) {
|
|
36
53
|
return true;
|
|
37
54
|
}
|
|
38
|
-
if (
|
|
55
|
+
if (this.exclude.includes(key)) {
|
|
39
56
|
return false;
|
|
40
57
|
}
|
|
41
58
|
return value !== (base as any)[key];
|
|
42
59
|
}),
|
|
43
60
|
) as T,
|
|
44
61
|
);
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
62
|
+
}),
|
|
63
|
+
);
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public use(v: T) {
|
|
68
|
+
this.$comparing.use(v);
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
51
71
|
}
|
|
@@ -1,18 +1,15 @@
|
|
|
1
|
-
import { Late } from "silentium";
|
|
1
|
+
import { Late, Transport } from "silentium";
|
|
2
2
|
import { Loading } from "../behaviors/Loading";
|
|
3
3
|
import { expect, test, vi } from "vitest";
|
|
4
4
|
|
|
5
5
|
test("Loading.test", () => {
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
loadingStartSource.event,
|
|
10
|
-
loadingFinishSource.event,
|
|
11
|
-
);
|
|
6
|
+
const $loadingStart = Late();
|
|
7
|
+
const $loadingFinish = Late();
|
|
8
|
+
const $loading = Loading($loadingStart, $loadingFinish);
|
|
12
9
|
const g = vi.fn();
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
$loading.event(Transport(g));
|
|
11
|
+
$loadingStart.use({});
|
|
15
12
|
expect(g).toHaveBeenLastCalledWith(true);
|
|
16
|
-
|
|
13
|
+
$loadingFinish.use({});
|
|
17
14
|
expect(g).toHaveBeenLastCalledWith(false);
|
|
18
15
|
});
|
package/src/behaviors/Loading.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { EventType } from "silentium";
|
|
1
|
+
import { Event, EventType, Transport } from "silentium";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Representation Of loading process
|
|
5
|
-
* first
|
|
6
|
-
* second
|
|
5
|
+
* first event begins loading
|
|
6
|
+
* second event stops loading
|
|
7
7
|
* https://silentium-lab.github.io/silentium-components/#/behaviors/loading
|
|
8
8
|
*/
|
|
9
9
|
export function Loading(
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
$loadingStart: EventType<unknown>,
|
|
11
|
+
$loadingFinish: EventType<unknown>,
|
|
12
12
|
): EventType<boolean> {
|
|
13
|
-
return (
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
};
|
|
13
|
+
return Event((transport) => {
|
|
14
|
+
$loadingStart.event(Transport(() => transport.use(true)));
|
|
15
|
+
$loadingFinish.event(Transport(() => transport.use(false)));
|
|
16
|
+
});
|
|
17
17
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Late, Shared } from "silentium";
|
|
1
|
+
import { Late, Shared, Transport } from "silentium";
|
|
2
2
|
import { Lock } from "../behaviors/Lock";
|
|
3
3
|
import { expect, test, vi } from "vitest";
|
|
4
4
|
|
|
@@ -6,10 +6,10 @@ test("Lock.test", () => {
|
|
|
6
6
|
const source = Late<number>(1);
|
|
7
7
|
const lockSrc = Late<boolean>(false);
|
|
8
8
|
|
|
9
|
-
const ls = Lock(source
|
|
9
|
+
const ls = Lock(source, lockSrc);
|
|
10
10
|
const lockedSrc = Shared(ls);
|
|
11
11
|
const g = vi.fn();
|
|
12
|
-
lockedSrc.event(g);
|
|
12
|
+
lockedSrc.event(Transport(g));
|
|
13
13
|
|
|
14
14
|
expect(g).toHaveBeenLastCalledWith(1);
|
|
15
15
|
|
package/src/behaviors/Lock.ts
CHANGED
|
@@ -1,18 +1,20 @@
|
|
|
1
|
-
import { EventType, Filtered } from "silentium";
|
|
1
|
+
import { Event, EventType, Filtered, Transport } from "silentium";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* https://silentium-lab.github.io/silentium-components/#/behaviors/lock
|
|
5
5
|
*/
|
|
6
6
|
export function Lock<T>(
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
$base: EventType<T>,
|
|
8
|
+
$lock: EventType<boolean>,
|
|
9
9
|
): EventType<T> {
|
|
10
|
-
return (
|
|
10
|
+
return Event((transport) => {
|
|
11
11
|
let locked = false;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
$lock.event(
|
|
13
|
+
Transport((newLock) => {
|
|
14
|
+
locked = newLock;
|
|
15
|
+
}),
|
|
16
|
+
);
|
|
17
|
+
const i = Filtered($base, () => !locked);
|
|
18
|
+
i.event(transport);
|
|
19
|
+
});
|
|
18
20
|
}
|
|
@@ -1,17 +1,19 @@
|
|
|
1
|
-
import { Late, Shared } from "silentium";
|
|
1
|
+
import { Late, Shared, Transport } from "silentium";
|
|
2
2
|
import { Memo } from "../behaviors/Memo";
|
|
3
3
|
import { expect, test, vi } from "vitest";
|
|
4
4
|
|
|
5
5
|
test("Memo.test", () => {
|
|
6
6
|
const l = Late<number>(1);
|
|
7
|
-
const mem = Shared(Memo(l
|
|
7
|
+
const mem = Shared(Memo(l));
|
|
8
8
|
const g = vi.fn();
|
|
9
|
-
mem.event(g);
|
|
9
|
+
mem.event(Transport(g));
|
|
10
10
|
let counter = 0;
|
|
11
11
|
|
|
12
|
-
mem.event(
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
mem.event(
|
|
13
|
+
Transport(() => {
|
|
14
|
+
counter += 1;
|
|
15
|
+
}),
|
|
16
|
+
);
|
|
15
17
|
|
|
16
18
|
l.use(2);
|
|
17
19
|
l.use(2);
|
package/src/behaviors/Memo.ts
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
import { EventType } from "silentium";
|
|
1
|
+
import { Event, EventType, Transport } 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
|
-
export function Memo<T>(
|
|
8
|
-
return (
|
|
9
|
-
let
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
7
|
+
export function Memo<T>($base: EventType<T>): EventType<T> {
|
|
8
|
+
return Event((transport) => {
|
|
9
|
+
let last: T | null = null;
|
|
10
|
+
$base.event(
|
|
11
|
+
Transport((v) => {
|
|
12
|
+
if (v !== last) {
|
|
13
|
+
transport.use(v);
|
|
14
|
+
last = v;
|
|
15
|
+
}
|
|
16
|
+
}),
|
|
17
|
+
);
|
|
18
|
+
});
|
|
18
19
|
}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { Late, Shared } from "silentium";
|
|
1
|
+
import { Late, Shared, Transport } from "silentium";
|
|
2
2
|
import { OnlyChanged } from "../behaviors/OnlyChanged";
|
|
3
3
|
import { expect, test, vi } from "vitest";
|
|
4
4
|
|
|
5
5
|
test("OnlyChanged.test", () => {
|
|
6
6
|
const src = Late<number>(1);
|
|
7
|
-
const changedSrc = Shared(OnlyChanged(src
|
|
7
|
+
const changedSrc = Shared(OnlyChanged(src));
|
|
8
8
|
|
|
9
9
|
const g = vi.fn();
|
|
10
|
-
changedSrc.event(g);
|
|
10
|
+
changedSrc.event(Transport(g));
|
|
11
11
|
expect(g).not.toBeCalled();
|
|
12
12
|
|
|
13
13
|
src.use(2);
|
|
14
14
|
|
|
15
15
|
const g2 = vi.fn();
|
|
16
|
-
changedSrc.event(g2);
|
|
16
|
+
changedSrc.event(Transport(g2));
|
|
17
17
|
expect(g2).toBeCalledWith(2);
|
|
18
18
|
});
|
|
@@ -1,19 +1,20 @@
|
|
|
1
|
-
import { EventType } from "silentium";
|
|
1
|
+
import { Event, EventType, Transport } 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
|
-
export function OnlyChanged<T>(
|
|
8
|
-
return (
|
|
9
|
-
let
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
7
|
+
export function OnlyChanged<T>($base: EventType<T>): EventType<T> {
|
|
8
|
+
return Event((transport) => {
|
|
9
|
+
let first = false;
|
|
10
|
+
$base.event(
|
|
11
|
+
Transport((v) => {
|
|
12
|
+
if (first === false) {
|
|
13
|
+
first = true;
|
|
14
|
+
} else {
|
|
15
|
+
transport.use(v);
|
|
16
|
+
}
|
|
17
|
+
}),
|
|
18
|
+
);
|
|
19
|
+
});
|
|
19
20
|
}
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import { Late, Of, SharedSource } from "silentium";
|
|
2
|
-
import { Part } from "../behaviors/Part";
|
|
1
|
+
import { Late, Of, SharedSource, Transport } from "silentium";
|
|
3
2
|
import { expect, test, vi } from "vitest";
|
|
3
|
+
import { Part } from "../behaviors/Part";
|
|
4
4
|
|
|
5
5
|
test("Part.test", () => {
|
|
6
|
-
const
|
|
6
|
+
const $record = SharedSource(
|
|
7
7
|
Late({
|
|
8
8
|
name: "Peter",
|
|
9
9
|
surname: "Parker",
|
|
10
10
|
}),
|
|
11
11
|
);
|
|
12
|
-
const
|
|
12
|
+
const $name = Part<string>($record, Of("name"));
|
|
13
13
|
const g = vi.fn();
|
|
14
|
-
|
|
14
|
+
$record.event(Transport(g));
|
|
15
15
|
expect(g).toHaveBeenLastCalledWith({ name: "Peter", surname: "Parker" });
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
$name.use("Shmiter");
|
|
18
18
|
expect(g).toHaveBeenLastCalledWith({ name: "Shmiter", surname: "Parker" });
|
|
19
19
|
});
|
package/src/behaviors/Part.ts
CHANGED
|
@@ -1,40 +1,63 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
All,
|
|
3
|
+
EventType,
|
|
4
|
+
isFilled,
|
|
5
|
+
Primitive,
|
|
6
|
+
Shared,
|
|
7
|
+
SharedSource,
|
|
8
|
+
SourceType,
|
|
9
|
+
Transport,
|
|
10
|
+
TransportType,
|
|
11
|
+
} from "silentium";
|
|
2
12
|
|
|
3
13
|
/**
|
|
4
14
|
* Return source Of record path
|
|
5
15
|
* https://silentium-lab.github.io/silentium-components/#/behaviors/path
|
|
6
16
|
*/
|
|
7
|
-
export function Part<
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
export function Part<
|
|
18
|
+
R,
|
|
19
|
+
T extends object | Array<any> = any,
|
|
20
|
+
K extends string = any,
|
|
21
|
+
>($base: SourceType<T>, $key: EventType<K>): SourceType<R> {
|
|
22
|
+
return new PartEvent($base, $key);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
class PartEvent<R, T extends object | Array<any>, K extends string = any>
|
|
26
|
+
implements SourceType<R>
|
|
27
|
+
{
|
|
28
|
+
private $base: SourceType<T>;
|
|
29
|
+
private $keyed: EventType<K>;
|
|
30
|
+
|
|
31
|
+
public constructor($base: SourceType<T>, $key: EventType<K>) {
|
|
32
|
+
this.$base = SharedSource($base);
|
|
33
|
+
this.$keyed = Shared($key);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public event(transport: TransportType<R, null>): this {
|
|
37
|
+
All(this.$base, this.$keyed).event(
|
|
38
|
+
Transport(([base, keyed]) => {
|
|
39
|
+
const keys = keyed.split(".");
|
|
20
40
|
let value: unknown = base;
|
|
21
|
-
|
|
22
|
-
value = (value as Record<string, unknown>)[
|
|
41
|
+
keys.forEach((key) => {
|
|
42
|
+
value = (value as Record<string, unknown>)[key];
|
|
23
43
|
});
|
|
24
|
-
|
|
25
44
|
if (value !== undefined && value !== base) {
|
|
26
|
-
|
|
45
|
+
transport.use(value as R);
|
|
27
46
|
}
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
47
|
+
}),
|
|
48
|
+
);
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public use(value: R): this {
|
|
53
|
+
const key = Primitive(this.$keyed);
|
|
54
|
+
if (isFilled(key)) {
|
|
55
|
+
const base = Primitive(this.$base);
|
|
56
|
+
this.$base.use({
|
|
57
|
+
...base.primitiveWithException(),
|
|
58
|
+
[key.primitiveWithException()]: value,
|
|
59
|
+
} as T);
|
|
60
|
+
}
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
40
63
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Of } from "silentium";
|
|
1
|
+
import { Of, Transport } from "silentium";
|
|
2
2
|
import { expect, test, vi } from "vitest";
|
|
3
3
|
import { Path } from "./Path";
|
|
4
4
|
|
|
@@ -9,6 +9,6 @@ test("Path._main.test", () => {
|
|
|
9
9
|
};
|
|
10
10
|
const name = Path<string>(Of(record), Of("name"));
|
|
11
11
|
const g = vi.fn();
|
|
12
|
-
name(g);
|
|
12
|
+
name.event(Transport(g));
|
|
13
13
|
expect(g).toHaveBeenLastCalledWith("Peter");
|
|
14
14
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Of } from "silentium";
|
|
1
|
+
import { Of, Transport } from "silentium";
|
|
2
2
|
import { expect, test, vi } from "vitest";
|
|
3
3
|
import { Path } from "./Path";
|
|
4
4
|
|
|
@@ -13,6 +13,6 @@ test("Path.index.test", () => {
|
|
|
13
13
|
};
|
|
14
14
|
const bestColor = Path(Of(record), Of("colors.0"));
|
|
15
15
|
const g = vi.fn();
|
|
16
|
-
bestColor(g);
|
|
16
|
+
bestColor.event(Transport(g));
|
|
17
17
|
expect(g).toHaveBeenLastCalledWith("blue");
|
|
18
18
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Of } from "silentium";
|
|
1
|
+
import { Of, Transport } from "silentium";
|
|
2
2
|
import { Path } from "../behaviors/Path";
|
|
3
3
|
import { expect, test, vi } from "vitest";
|
|
4
4
|
|
|
@@ -12,6 +12,6 @@ test("Path.nested.test", () => {
|
|
|
12
12
|
};
|
|
13
13
|
const typeName = Path(Of(record), Of("type.name"));
|
|
14
14
|
const g = vi.fn();
|
|
15
|
-
typeName(g);
|
|
15
|
+
typeName.event(Transport(g));
|
|
16
16
|
expect(g).toHaveBeenLastCalledWith("spider-man");
|
|
17
17
|
});
|