silentium-components 0.0.26 → 0.0.28
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 +34 -0
- package/dist/silentium-components.cjs +570 -342
- package/dist/silentium-components.cjs.map +1 -1
- package/dist/silentium-components.d.ts +169 -30
- package/dist/silentium-components.js +546 -319
- 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 +546 -319
- package/dist/silentium-components.mjs.map +1 -1
- package/package.json +2 -2
- package/src/behaviors/Branch._main.test.ts +22 -0
- package/src/behaviors/Branch.branchesDontAffectResult.test.ts +11 -11
- package/src/behaviors/Branch.dontRespondAfterRespond.test.ts +15 -17
- package/src/behaviors/Branch.ts +28 -21
- package/src/behaviors/{Deadline.test.ts → Deadline._main.test.ts} +5 -5
- package/src/behaviors/Deadline._value.test.ts +6 -6
- package/src/behaviors/Deadline.ts +45 -41
- package/src/behaviors/Deferred.test.ts +14 -14
- package/src/behaviors/Deferred.ts +21 -15
- package/src/behaviors/Dirty.test.ts +8 -8
- package/src/behaviors/Dirty.ts +43 -35
- package/src/behaviors/Loading.test.ts +8 -8
- package/src/behaviors/Loading.ts +24 -14
- package/src/behaviors/Lock.test.ts +14 -14
- package/src/behaviors/Lock.ts +21 -14
- package/src/behaviors/Memo.test.ts +16 -14
- package/src/behaviors/Memo.ts +20 -12
- package/src/behaviors/OnlyChanged.test.ts +7 -7
- package/src/behaviors/OnlyChanged.ts +22 -15
- package/src/behaviors/Path._main.test.ts +14 -0
- package/src/behaviors/Path.index.test.ts +4 -4
- package/src/behaviors/Path.nested.test.ts +4 -4
- package/src/behaviors/Path.ts +27 -22
- package/src/behaviors/Shot._main.test.ts +29 -0
- package/src/behaviors/Shot._onlyChanged.test.ts +15 -13
- package/src/behaviors/Shot.ts +24 -15
- package/src/behaviors/Sync.test.ts +10 -0
- package/src/behaviors/Sync.ts +38 -15
- package/src/behaviors/Tick.test.ts +8 -8
- package/src/behaviors/Tick.ts +20 -12
- package/src/boolean/And.test.ts +12 -12
- package/src/boolean/And.ts +18 -14
- package/src/boolean/Bool.ts +11 -2
- package/src/boolean/Not.test.ts +6 -6
- package/src/boolean/Not.ts +15 -10
- package/src/boolean/Or.test.ts +12 -12
- package/src/boolean/Or.ts +18 -14
- package/src/boolean/index.ts +1 -0
- package/src/formats/FromJson.test.ts +11 -0
- package/src/formats/FromJson.ts +22 -15
- package/src/formats/ToJson.test.ts +11 -0
- package/src/formats/ToJson.ts +23 -16
- package/src/lists/First.test.ts +4 -4
- package/src/lists/First.ts +11 -6
- package/src/navigation/Router.test.ts +13 -13
- package/src/navigation/Router.ts +36 -29
- package/src/strings/Concatenated.test.ts +6 -6
- package/src/strings/Concatenated.ts +18 -14
- package/src/structures/HashTable.test.ts +7 -7
- package/src/structures/HashTable.ts +17 -11
- package/src/structures/Record._main.test.ts +28 -0
- package/src/structures/Record.concatenated.test.ts +17 -15
- package/src/structures/Record.nested.test.ts +10 -10
- package/src/structures/RecordOf.ts +29 -0
- package/src/structures/index.ts +1 -1
- package/src/system/RegexpMatch._group.test.ts +9 -6
- package/src/system/RegexpMatch._main.test.ts +16 -0
- package/src/system/RegexpMatch.ts +20 -17
- package/src/system/RegexpMatched.test.ts +6 -6
- package/src/system/RegexpMatched.ts +19 -16
- package/src/system/RegexpReplaced.test.ts +10 -6
- package/src/system/RegexpReplaced.ts +25 -18
- package/src/system/Set.test.ts +6 -6
- package/src/system/Set.ts +20 -17
- package/src/behaviors/Branch.test.ts +0 -22
- package/src/behaviors/Path.test.ts +0 -14
- package/src/behaviors/Shot.test.ts +0 -29
- package/src/structures/Record.test.ts +0 -28
- package/src/structures/Record.ts +0 -22
- package/src/system/RegexpMatch.test.ts +0 -13
package/src/behaviors/Dirty.ts
CHANGED
|
@@ -1,45 +1,53 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { All, Applied, From, Late, TheInformation, TheOwner } from "silentium";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Takes source and remember it first value
|
|
5
5
|
* returns new record, what will contain only fields what was changed
|
|
6
6
|
* https://silentium-lab.github.io/silentium-components/#/behaviors/dirty
|
|
7
7
|
*/
|
|
8
|
-
export
|
|
9
|
-
|
|
10
|
-
alwaysKeep: string[] = [],
|
|
11
|
-
excludeKeys: string[] = [],
|
|
12
|
-
) => {
|
|
13
|
-
const [comparing, co] = of<T>();
|
|
8
|
+
export class Dirty<T> extends TheInformation<T> {
|
|
9
|
+
private comparingSrc = new Late<T>();
|
|
14
10
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
public constructor(
|
|
12
|
+
private baseEntitySource: TheInformation<T>,
|
|
13
|
+
private alwaysKeep: string[] = [],
|
|
14
|
+
private excludeKeys: string[] = [],
|
|
15
|
+
) {
|
|
16
|
+
super([baseEntitySource]);
|
|
17
|
+
}
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
)(([comparing, base]) => {
|
|
24
|
-
if (!comparing) {
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
19
|
+
public value(o: TheOwner<T>): this {
|
|
20
|
+
const comparingDetached = new Applied(this.comparingSrc, (value) =>
|
|
21
|
+
JSON.parse(JSON.stringify(value)),
|
|
22
|
+
);
|
|
27
23
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
if (excludeKeys.includes(key)) {
|
|
35
|
-
return false;
|
|
36
|
-
}
|
|
37
|
-
return value !== (base as any)[key];
|
|
38
|
-
}),
|
|
39
|
-
) as T,
|
|
40
|
-
);
|
|
41
|
-
});
|
|
42
|
-
};
|
|
24
|
+
const allSrc = new All(comparingDetached, this.baseEntitySource).value(
|
|
25
|
+
new From(([comparing, base]) => {
|
|
26
|
+
if (!comparing) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
43
29
|
|
|
44
|
-
|
|
45
|
-
|
|
30
|
+
o.give(
|
|
31
|
+
Object.fromEntries(
|
|
32
|
+
Object.entries(comparing).filter(([key, value]) => {
|
|
33
|
+
if (this.alwaysKeep.includes(key)) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
if (this.excludeKeys.includes(key)) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
return value !== (base as any)[key];
|
|
40
|
+
}),
|
|
41
|
+
) as T,
|
|
42
|
+
);
|
|
43
|
+
}),
|
|
44
|
+
);
|
|
45
|
+
this.addDep(allSrc);
|
|
46
|
+
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public owner() {
|
|
51
|
+
return this.comparingSrc.owner();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { From, Late } from "silentium";
|
|
2
2
|
import { expect, test, vi } from "vitest";
|
|
3
|
-
import {
|
|
3
|
+
import { Loading } from "../behaviors/Loading";
|
|
4
4
|
|
|
5
5
|
test("Loading.test", () => {
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const loadingSrc =
|
|
6
|
+
const loadingStartSource = new Late();
|
|
7
|
+
const loadingFinishSource = new Late();
|
|
8
|
+
const loadingSrc = new Loading(loadingStartSource, loadingFinishSource);
|
|
9
9
|
const g = vi.fn();
|
|
10
|
-
loadingSrc(g);
|
|
11
|
-
|
|
10
|
+
loadingSrc.value(new From(g));
|
|
11
|
+
loadingStartSource.owner().give({});
|
|
12
12
|
expect(g).toHaveBeenLastCalledWith(true);
|
|
13
|
-
|
|
13
|
+
loadingFinishSource.owner().give({});
|
|
14
14
|
expect(g).toHaveBeenLastCalledWith(false);
|
|
15
15
|
});
|
package/src/behaviors/Loading.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { From, TheInformation, TheOwner } from "silentium";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Representation of loading process
|
|
@@ -6,17 +6,27 @@ import { InformationType } from "silentium";
|
|
|
6
6
|
* second information source stops loading
|
|
7
7
|
* https://silentium-lab.github.io/silentium-components/#/behaviors/loading
|
|
8
8
|
*/
|
|
9
|
-
export
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
});
|
|
9
|
+
export class Loading extends TheInformation<boolean> {
|
|
10
|
+
public constructor(
|
|
11
|
+
private loadingStartSrc: TheInformation<unknown>,
|
|
12
|
+
private loadingFinishSrc: TheInformation<unknown>,
|
|
13
|
+
) {
|
|
14
|
+
super(loadingFinishSrc, loadingStartSrc);
|
|
15
|
+
}
|
|
17
16
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
17
|
+
public value(o: TheOwner<boolean>): this {
|
|
18
|
+
this.loadingStartSrc.value(
|
|
19
|
+
new From(() => {
|
|
20
|
+
o.give(true);
|
|
21
|
+
}),
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
this.loadingFinishSrc.value(
|
|
25
|
+
new From(() => {
|
|
26
|
+
o.give(false);
|
|
27
|
+
}),
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { From, Late, Shared } from "silentium";
|
|
2
|
+
import { Lock } from "../behaviors/Lock";
|
|
2
3
|
import { expect, test, vi } from "vitest";
|
|
3
|
-
import { lock } from "../behaviors/Lock";
|
|
4
4
|
|
|
5
5
|
test("Lock.test", () => {
|
|
6
|
-
const
|
|
7
|
-
const
|
|
6
|
+
const source = new Late<number>(1);
|
|
7
|
+
const lockSrc = new Late<boolean>(false);
|
|
8
8
|
|
|
9
|
-
const ls =
|
|
10
|
-
const
|
|
9
|
+
const ls = new Lock(source, lockSrc);
|
|
10
|
+
const lockedSrc = new Shared(ls);
|
|
11
11
|
const g = vi.fn();
|
|
12
|
-
lockedSrc(g);
|
|
12
|
+
lockedSrc.value(new From(g));
|
|
13
13
|
|
|
14
14
|
expect(g).toHaveBeenLastCalledWith(1);
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
source.owner().give(2);
|
|
17
17
|
|
|
18
18
|
expect(g).toHaveBeenLastCalledWith(2);
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
lockSrc.owner().give(true);
|
|
21
|
+
source.owner().give(3);
|
|
22
|
+
source.owner().give(4);
|
|
23
|
+
source.owner().give(5);
|
|
24
24
|
|
|
25
25
|
expect(g).toHaveBeenLastCalledWith(2);
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
lockSrc.owner().give(false);
|
|
28
|
+
source.owner().give(6);
|
|
29
29
|
expect(g).toHaveBeenLastCalledWith(6);
|
|
30
30
|
});
|
package/src/behaviors/Lock.ts
CHANGED
|
@@ -1,19 +1,26 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Filtered, From, TheInformation, TheOwner } from "silentium";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* https://silentium-lab.github.io/silentium-components/#/behaviors/lock
|
|
5
5
|
*/
|
|
6
|
-
export
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
export class Lock<T> extends TheInformation<T> {
|
|
7
|
+
public constructor(
|
|
8
|
+
private baseSrc: TheInformation<T>,
|
|
9
|
+
private lockSrc: TheInformation<boolean>,
|
|
10
|
+
) {
|
|
11
|
+
super(baseSrc, lockSrc);
|
|
12
|
+
}
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
14
|
+
public value(o: TheOwner<T>): this {
|
|
15
|
+
let locked = false;
|
|
16
|
+
this.lockSrc.value(
|
|
17
|
+
new From((newLock) => {
|
|
18
|
+
locked = newLock;
|
|
19
|
+
}),
|
|
20
|
+
);
|
|
21
|
+
const i = new Filtered(this.baseSrc, () => !locked);
|
|
22
|
+
this.addDep(i);
|
|
23
|
+
i.value(o);
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -1,28 +1,30 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { From, Late, Shared } from "silentium";
|
|
2
2
|
import { expect, test, vi } from "vitest";
|
|
3
|
-
import {
|
|
3
|
+
import { Memo } from "../behaviors/Memo";
|
|
4
4
|
|
|
5
5
|
test("Memo.test", () => {
|
|
6
|
-
const
|
|
7
|
-
const
|
|
6
|
+
const l = new Late<number>(1);
|
|
7
|
+
const mem = new Shared(new Memo(l));
|
|
8
8
|
const g = vi.fn();
|
|
9
|
-
mem(g);
|
|
9
|
+
mem.value(new From(g));
|
|
10
10
|
let counter = 0;
|
|
11
11
|
|
|
12
|
-
mem(
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
mem.value(
|
|
13
|
+
new From(() => {
|
|
14
|
+
counter += 1;
|
|
15
|
+
}),
|
|
16
|
+
);
|
|
15
17
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
l.owner().give(2);
|
|
19
|
+
l.owner().give(2);
|
|
20
|
+
l.owner().give(2);
|
|
21
|
+
l.owner().give(2);
|
|
22
|
+
l.owner().give(2);
|
|
21
23
|
|
|
22
24
|
expect(g).toHaveBeenLastCalledWith(2);
|
|
23
25
|
expect(counter).toBe(2);
|
|
24
26
|
|
|
25
|
-
|
|
27
|
+
l.owner().give(3);
|
|
26
28
|
|
|
27
29
|
expect(g).toHaveBeenLastCalledWith(3);
|
|
28
30
|
expect(counter).toBe(3);
|
package/src/behaviors/Memo.ts
CHANGED
|
@@ -1,18 +1,26 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { From, TheInformation, TheOwner } 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
|
|
8
|
-
|
|
7
|
+
export class Memo<T> extends TheInformation<T> {
|
|
8
|
+
public constructor(private baseSrc: TheInformation<T>) {
|
|
9
|
+
super(baseSrc);
|
|
10
|
+
}
|
|
9
11
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
12
|
+
public value(o: TheOwner<T>): this {
|
|
13
|
+
let lastValue: T | null = null;
|
|
14
|
+
|
|
15
|
+
this.baseSrc.value(
|
|
16
|
+
new From((v) => {
|
|
17
|
+
if (v !== lastValue) {
|
|
18
|
+
o.give(v);
|
|
19
|
+
lastValue = v;
|
|
20
|
+
}
|
|
21
|
+
}),
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { From, Late, Shared } from "silentium";
|
|
2
2
|
import { expect, test, vi } from "vitest";
|
|
3
|
-
import {
|
|
3
|
+
import { OnlyChanged } from "../behaviors/OnlyChanged";
|
|
4
4
|
|
|
5
5
|
test("OnlyChanged.test", () => {
|
|
6
|
-
const
|
|
7
|
-
const
|
|
6
|
+
const src = new Late<number>(1);
|
|
7
|
+
const changedSrc = new Shared(new OnlyChanged(src));
|
|
8
8
|
|
|
9
9
|
const g = vi.fn();
|
|
10
|
-
changedSrc(g);
|
|
10
|
+
changedSrc.value(new From(g));
|
|
11
11
|
expect(g).not.toBeCalled();
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
src.owner().give(2);
|
|
14
14
|
|
|
15
15
|
const g2 = vi.fn();
|
|
16
|
-
changedSrc(g2);
|
|
16
|
+
changedSrc.value(new From(g2));
|
|
17
17
|
expect(g2).toBeCalledWith(2);
|
|
18
18
|
});
|
|
@@ -1,20 +1,27 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { From, TheInformation, TheOwner } 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
|
|
8
|
-
baseSrc:
|
|
9
|
-
)
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
7
|
+
export class OnlyChanged<T> extends TheInformation<T> {
|
|
8
|
+
public constructor(private baseSrc: TheInformation<T>) {
|
|
9
|
+
super(baseSrc);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
public value(o: TheOwner<T>): this {
|
|
13
|
+
let firstValue = false;
|
|
14
|
+
|
|
15
|
+
this.baseSrc.value(
|
|
16
|
+
new From((v) => {
|
|
17
|
+
if (firstValue === false) {
|
|
18
|
+
firstValue = true;
|
|
19
|
+
} else {
|
|
20
|
+
o.give(v);
|
|
21
|
+
}
|
|
22
|
+
}),
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { From, Of } from "silentium";
|
|
2
|
+
import { expect, test, vi } from "vitest";
|
|
3
|
+
import { Path } from "./Path";
|
|
4
|
+
|
|
5
|
+
test("Path._main.test", () => {
|
|
6
|
+
const record = {
|
|
7
|
+
name: "Peter",
|
|
8
|
+
surname: "Parker",
|
|
9
|
+
};
|
|
10
|
+
const name = new Path<string>(new Of(record), new Of("name"));
|
|
11
|
+
const g = vi.fn();
|
|
12
|
+
name.value(new From(g));
|
|
13
|
+
expect(g).toHaveBeenLastCalledWith("Peter");
|
|
14
|
+
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { From, Of } from "silentium";
|
|
2
2
|
import { expect, test, vi } from "vitest";
|
|
3
|
-
import {
|
|
3
|
+
import { Path } from "./Path";
|
|
4
4
|
|
|
5
5
|
test("Path.index.test", () => {
|
|
6
6
|
const record = {
|
|
@@ -11,8 +11,8 @@ test("Path.index.test", () => {
|
|
|
11
11
|
name: "spider-man",
|
|
12
12
|
},
|
|
13
13
|
};
|
|
14
|
-
const bestColor =
|
|
14
|
+
const bestColor = new Path(new Of(record), new Of("colors.0"));
|
|
15
15
|
const g = vi.fn();
|
|
16
|
-
bestColor(g);
|
|
16
|
+
bestColor.value(new From(g));
|
|
17
17
|
expect(g).toHaveBeenLastCalledWith("blue");
|
|
18
18
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { From, Of } from "silentium";
|
|
2
2
|
import { expect, test, vi } from "vitest";
|
|
3
|
-
import {
|
|
3
|
+
import { Path } from "./Path";
|
|
4
4
|
|
|
5
5
|
test("Path.nested.test", () => {
|
|
6
6
|
const record = {
|
|
@@ -10,8 +10,8 @@ test("Path.nested.test", () => {
|
|
|
10
10
|
name: "spider-man",
|
|
11
11
|
},
|
|
12
12
|
};
|
|
13
|
-
const typeName =
|
|
13
|
+
const typeName = new Path(new Of(record), new Of("type.name"));
|
|
14
14
|
const g = vi.fn();
|
|
15
|
-
typeName(g);
|
|
15
|
+
typeName.value(new From(g));
|
|
16
16
|
expect(g).toHaveBeenLastCalledWith("spider-man");
|
|
17
17
|
});
|
package/src/behaviors/Path.ts
CHANGED
|
@@ -1,31 +1,36 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { All, From, TheInformation, TheOwner } from "silentium";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Return source of record path
|
|
5
5
|
* https://silentium-lab.github.io/silentium-components/#/behaviors/path
|
|
6
6
|
*/
|
|
7
|
-
export
|
|
7
|
+
export class Path<
|
|
8
8
|
R,
|
|
9
9
|
T extends Record<string, unknown> | Array<unknown> = any,
|
|
10
10
|
K extends string = any,
|
|
11
|
-
>
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
keySrc,
|
|
19
|
-
)(([base, key]) => {
|
|
20
|
-
const keyChunks = key.split(".");
|
|
21
|
-
let value: unknown = base;
|
|
22
|
-
keyChunks.forEach((keyChunk) => {
|
|
23
|
-
value = (value as Record<string, unknown>)[keyChunk];
|
|
24
|
-
});
|
|
11
|
+
> extends TheInformation<R> {
|
|
12
|
+
public constructor(
|
|
13
|
+
private baseSrc: TheInformation<T>,
|
|
14
|
+
private keySrc: TheInformation<K>,
|
|
15
|
+
) {
|
|
16
|
+
super(baseSrc, keySrc);
|
|
17
|
+
}
|
|
25
18
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
19
|
+
public value(o: TheOwner<R>): this {
|
|
20
|
+
const allSrc = new All(this.baseSrc, this.keySrc).value(
|
|
21
|
+
new From(([base, key]) => {
|
|
22
|
+
const keyChunks = key.split(".");
|
|
23
|
+
let value: unknown = base;
|
|
24
|
+
keyChunks.forEach((keyChunk) => {
|
|
25
|
+
value = (value as Record<string, unknown>)[keyChunk];
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
if (value !== undefined && value !== base) {
|
|
29
|
+
o.give(value as R);
|
|
30
|
+
}
|
|
31
|
+
}),
|
|
32
|
+
);
|
|
33
|
+
this.addDep(allSrc);
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { From, Late, Shared } from "silentium";
|
|
2
|
+
import { expect, test, vi } from "vitest";
|
|
3
|
+
import { Shot } from "../behaviors/Shot";
|
|
4
|
+
|
|
5
|
+
test("Shot._main.test", () => {
|
|
6
|
+
const baseSrc = new Late();
|
|
7
|
+
const shotSrc = new Late();
|
|
8
|
+
|
|
9
|
+
const shotResult = new Shared(new Shot(baseSrc, shotSrc));
|
|
10
|
+
const g = vi.fn();
|
|
11
|
+
shotResult.value(new From(g));
|
|
12
|
+
|
|
13
|
+
baseSrc.owner().give(1);
|
|
14
|
+
shotSrc.owner().give(1);
|
|
15
|
+
|
|
16
|
+
expect(g).toHaveBeenLastCalledWith(1);
|
|
17
|
+
|
|
18
|
+
baseSrc.owner().give(2);
|
|
19
|
+
|
|
20
|
+
expect(g).toHaveBeenLastCalledWith(1);
|
|
21
|
+
|
|
22
|
+
shotSrc.owner().give(1);
|
|
23
|
+
|
|
24
|
+
expect(g).toHaveBeenLastCalledWith(2);
|
|
25
|
+
|
|
26
|
+
const g2 = vi.fn();
|
|
27
|
+
shotResult.value(new From(g2));
|
|
28
|
+
expect(g2).toHaveBeenLastCalledWith(2);
|
|
29
|
+
});
|
|
@@ -1,34 +1,36 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { From, Late, Shared } from "silentium";
|
|
2
2
|
import { expect, test } from "vitest";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { OnlyChanged } from "../behaviors/OnlyChanged";
|
|
4
|
+
import { Shot } from "../behaviors/Shot";
|
|
5
5
|
|
|
6
6
|
test("Shot._onlyChanged.test", () => {
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const resultSrc =
|
|
7
|
+
const baseSrc = new Late<number>(123);
|
|
8
|
+
const sharedBase = new Shared(baseSrc, true);
|
|
9
|
+
const resultSrc = new Shot(sharedBase, new OnlyChanged(sharedBase));
|
|
10
10
|
|
|
11
11
|
const vals: number[] = [];
|
|
12
12
|
|
|
13
|
-
resultSrc(
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
resultSrc.value(
|
|
14
|
+
new From((v) => {
|
|
15
|
+
vals.push(v);
|
|
16
|
+
}),
|
|
17
|
+
);
|
|
16
18
|
|
|
17
19
|
expect(vals).toStrictEqual([]);
|
|
18
20
|
|
|
19
|
-
|
|
21
|
+
baseSrc.owner().give(222);
|
|
20
22
|
|
|
21
23
|
expect(vals).toStrictEqual([]);
|
|
22
24
|
|
|
23
|
-
|
|
25
|
+
baseSrc.owner().give(222);
|
|
24
26
|
|
|
25
27
|
expect(vals).toStrictEqual([222]);
|
|
26
28
|
|
|
27
|
-
|
|
29
|
+
baseSrc.owner().give(333);
|
|
28
30
|
|
|
29
31
|
expect(vals).toStrictEqual([222, 333]);
|
|
30
32
|
|
|
31
|
-
|
|
33
|
+
baseSrc.owner().give(123);
|
|
32
34
|
|
|
33
35
|
expect(vals).toStrictEqual([222, 333, 123]);
|
|
34
36
|
});
|
package/src/behaviors/Shot.ts
CHANGED
|
@@ -1,21 +1,30 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { From, TheInformation, TheOwner } from "silentium";
|
|
2
|
+
import { Sync } from "./Sync";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Helps to represent only last fresh value of some source, refreshing controls by shotSrc
|
|
6
6
|
* https://silentium-lab.github.io/silentium-components/#/behaviors/shot
|
|
7
7
|
*/
|
|
8
|
-
export
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
export class Shot<T> extends TheInformation<T> {
|
|
9
|
+
public constructor(
|
|
10
|
+
private targetSrc: TheInformation<T>,
|
|
11
|
+
private triggerSrc: TheInformation,
|
|
12
|
+
) {
|
|
13
|
+
super(targetSrc, triggerSrc);
|
|
14
|
+
}
|
|
14
15
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
public value(o: TheOwner<T>): this {
|
|
17
|
+
const targetSync = new Sync(this.targetSrc);
|
|
18
|
+
targetSync.initOwner();
|
|
19
|
+
|
|
20
|
+
this.triggerSrc.value(
|
|
21
|
+
new From(() => {
|
|
22
|
+
if (targetSync.valueExisted()) {
|
|
23
|
+
o.give(targetSync.valueSync());
|
|
24
|
+
}
|
|
25
|
+
}),
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
}
|