patron-oop 1.5.2 → 1.7.0
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +18 -0
- package/dist/patron.d.ts +61 -82
- package/dist/patron.js +75 -114
- package/dist/patron.js.map +1 -1
- package/dist/patron.min.js +1 -1
- package/dist/patron.mjs +70 -113
- package/dist/patron.mjs.map +1 -1
- package/examples/elegant_objects.html +15 -13
- package/examples/reactive.html +6 -6
- package/package.json +1 -1
- package/src/Guest/Guest.test.ts +10 -0
- package/src/Guest/Guest.ts +28 -35
- package/src/Guest/GuestAware.test.ts +6 -8
- package/src/Guest/GuestAware.ts +1 -1
- package/src/Guest/GuestCast.test.ts +6 -6
- package/src/Guest/GuestCast.ts +7 -3
- package/src/Guest/GuestChain.test.ts +12 -12
- package/src/Guest/GuestChain.ts +11 -11
- package/src/Guest/GuestInTheMiddle.test.ts +9 -9
- package/src/Guest/GuestInTheMiddle.ts +3 -3
- package/src/Guest/GuestPool.test.ts +10 -17
- package/src/Guest/GuestPool.ts +10 -6
- package/src/Guest/GuestSync.test.ts +2 -2
- package/src/Guest/GuestSync.ts +2 -2
- package/src/Patron/{PatronOfGuest.test.ts → Patron.test.ts} +4 -7
- package/src/Patron/Patron.ts +16 -12
- package/src/Patron/PatronOnce.test.ts +3 -6
- package/src/Patron/PatronOnce.ts +8 -3
- package/src/Patron/PatronPool.test.ts +9 -14
- package/src/Patron/PatronPool.ts +16 -10
- package/src/Source/SourceOfValue.test.ts +3 -6
- package/src/Source/SourceOfValue.ts +7 -3
- package/src/index.ts +35 -13
- package/src/Guest/GuestCallback.test.ts +0 -13
- package/src/Guest/GuestCallback.ts +0 -21
- package/src/Patron/PatronOfGuest.ts +0 -17
- package/src/Source/Source.ts +0 -12
- package/src/Source/SourcesApplied.test.ts +0 -14
- package/src/Source/SourcesApplied.ts +0 -40
@@ -1,21 +1,21 @@
|
|
1
1
|
import { expect, test } from "vitest";
|
2
2
|
import { SourceOfValue } from "../Source/SourceOfValue";
|
3
|
-
import {
|
4
|
-
import {
|
3
|
+
import { Patron } from "../Patron/Patron";
|
4
|
+
import { give, Guest } from "./Guest";
|
5
5
|
import { GuestCast } from "./GuestCast";
|
6
6
|
|
7
7
|
test("chain guest returns 2 values after result guest", () => {
|
8
8
|
const source = new SourceOfValue(1);
|
9
9
|
let acc = 0;
|
10
|
-
const mainGuest = new
|
11
|
-
new
|
10
|
+
const mainGuest = new Patron(
|
11
|
+
new Guest((value: number) => {
|
12
12
|
acc += value;
|
13
13
|
}),
|
14
14
|
);
|
15
15
|
// Становится патроном тоже, тк наследует это сойство от mainGuest
|
16
16
|
const secondGuest = new GuestCast(
|
17
17
|
mainGuest,
|
18
|
-
new
|
18
|
+
new Guest((value: number) => {
|
19
19
|
acc += value;
|
20
20
|
}),
|
21
21
|
);
|
@@ -23,7 +23,7 @@ test("chain guest returns 2 values after result guest", () => {
|
|
23
23
|
source.receiving(mainGuest);
|
24
24
|
source.receiving(secondGuest);
|
25
25
|
|
26
|
-
|
26
|
+
give(2, source);
|
27
27
|
|
28
28
|
setTimeout(() => {
|
29
29
|
expect(acc).toBe(6);
|
package/src/Guest/GuestCast.ts
CHANGED
@@ -1,12 +1,16 @@
|
|
1
|
-
import { GuestType, ReceiveOptions } from "./
|
1
|
+
import { give, GuestObjectType, GuestType, ReceiveOptions } from "./Guest";
|
2
2
|
|
3
|
-
export class GuestCast<T> implements
|
3
|
+
export class GuestCast<T> implements GuestObjectType<T> {
|
4
4
|
public constructor(
|
5
5
|
private sourceGuest: GuestType<unknown>,
|
6
6
|
private targetGuest: GuestType<T>,
|
7
7
|
) {}
|
8
8
|
|
9
9
|
introduction() {
|
10
|
+
if (typeof this.sourceGuest === "function") {
|
11
|
+
return "guest";
|
12
|
+
}
|
13
|
+
|
10
14
|
if (!this.sourceGuest.introduction) {
|
11
15
|
return "guest";
|
12
16
|
}
|
@@ -14,7 +18,7 @@ export class GuestCast<T> implements GuestType<T> {
|
|
14
18
|
}
|
15
19
|
|
16
20
|
receive(value: T, options?: ReceiveOptions): this {
|
17
|
-
this.targetGuest
|
21
|
+
give(value, this.targetGuest, options);
|
18
22
|
return this;
|
19
23
|
}
|
20
24
|
}
|
@@ -1,8 +1,8 @@
|
|
1
1
|
import { expect, test } from "vitest";
|
2
2
|
import { GuestChain } from "./GuestChain";
|
3
|
-
import {
|
3
|
+
import { Guest } from "./Guest";
|
4
4
|
import { SourceOfValue } from "../Source/SourceOfValue";
|
5
|
-
import {
|
5
|
+
import { Patron } from "../Patron/Patron";
|
6
6
|
|
7
7
|
test("chain guest returns 2 values after result guest", () => {
|
8
8
|
const one = new SourceOfValue(1);
|
@@ -10,7 +10,7 @@ test("chain guest returns 2 values after result guest", () => {
|
|
10
10
|
const chain = new GuestChain<{ one: number; two: number }>();
|
11
11
|
|
12
12
|
chain.result(
|
13
|
-
new
|
13
|
+
new Guest((value) => {
|
14
14
|
expect(Object.values(value).join()).toBe("1,2");
|
15
15
|
}),
|
16
16
|
);
|
@@ -28,7 +28,7 @@ test("chain guest returns 2 values before result guest", () => {
|
|
28
28
|
two.receiving(chain.receiveKey("two"));
|
29
29
|
|
30
30
|
chain.result(
|
31
|
-
new
|
31
|
+
new Guest((value) => {
|
32
32
|
expect(Object.values(value).join()).toBe("1,2");
|
33
33
|
}),
|
34
34
|
);
|
@@ -39,15 +39,15 @@ test("chain with patron", () => {
|
|
39
39
|
const two = new SourceOfValue(2);
|
40
40
|
const chain = new GuestChain<{ one: number; two: number }>();
|
41
41
|
|
42
|
-
one.receiving(new
|
43
|
-
two.receiving(new
|
42
|
+
one.receiving(new Patron(chain.receiveKey("one")));
|
43
|
+
two.receiving(new Patron(chain.receiveKey("two")));
|
44
44
|
|
45
45
|
one.receive(3);
|
46
46
|
one.receive(4);
|
47
47
|
|
48
48
|
chain.result(
|
49
|
-
new
|
50
|
-
new
|
49
|
+
new Patron(
|
50
|
+
new Guest((value: Record<string, unknown>) => {
|
51
51
|
expect(Object.values(value).length).toBe(2);
|
52
52
|
}),
|
53
53
|
),
|
@@ -59,12 +59,12 @@ test("chain as array", () => {
|
|
59
59
|
const two = new SourceOfValue(2);
|
60
60
|
const chain = new GuestChain<[number, number]>();
|
61
61
|
|
62
|
-
one.receiving(new
|
63
|
-
two.receiving(new
|
62
|
+
one.receiving(new Patron(chain.receiveKey("0")));
|
63
|
+
two.receiving(new Patron(chain.receiveKey("1")));
|
64
64
|
|
65
65
|
chain.resultArray(
|
66
|
-
new
|
67
|
-
new
|
66
|
+
new Patron(
|
67
|
+
new Guest((value) => {
|
68
68
|
expect(JSON.stringify(value)).toBe("[1, 2]");
|
69
69
|
}),
|
70
70
|
),
|
package/src/Guest/GuestChain.ts
CHANGED
@@ -1,12 +1,12 @@
|
|
1
|
-
import {
|
1
|
+
import { Guest, GuestObjectType } from "./Guest";
|
2
2
|
import { GuestPool } from "./GuestPool";
|
3
3
|
import { GuestInTheMiddle } from "./GuestInTheMiddle";
|
4
4
|
import { SourceOfValue } from "../Source/SourceOfValue";
|
5
5
|
|
6
6
|
export interface ChainType<T = unknown> {
|
7
|
-
result(guest:
|
8
|
-
resultArray(guest:
|
9
|
-
receiveKey<R>(key: string):
|
7
|
+
result(guest: GuestObjectType<T>): this;
|
8
|
+
resultArray(guest: GuestObjectType<T>): this;
|
9
|
+
receiveKey<R>(key: string): GuestObjectType<R>;
|
10
10
|
}
|
11
11
|
|
12
12
|
export class GuestChain<T> implements ChainType<T> {
|
@@ -22,7 +22,7 @@ export class GuestChain<T> implements ChainType<T> {
|
|
22
22
|
this.theChain = new SourceOfValue<Record<string, unknown>>({});
|
23
23
|
}
|
24
24
|
|
25
|
-
public resultArray(guest:
|
25
|
+
public resultArray(guest: GuestObjectType<T>) {
|
26
26
|
this.filledChainPool.add(
|
27
27
|
new GuestInTheMiddle(guest, (value: Record<string, unknown>) =>
|
28
28
|
Object.values(value),
|
@@ -30,7 +30,7 @@ export class GuestChain<T> implements ChainType<T> {
|
|
30
30
|
);
|
31
31
|
if (this.isChainFilled()) {
|
32
32
|
this.theChain.receiving(
|
33
|
-
new
|
33
|
+
new Guest((chain: Record<string, unknown>) => {
|
34
34
|
this.filledChainPool.receive(Object.values(chain));
|
35
35
|
}),
|
36
36
|
);
|
@@ -39,11 +39,11 @@ export class GuestChain<T> implements ChainType<T> {
|
|
39
39
|
return this;
|
40
40
|
}
|
41
41
|
|
42
|
-
public result(guest:
|
42
|
+
public result(guest: GuestObjectType<T>) {
|
43
43
|
if (this.isChainFilled()) {
|
44
44
|
this.filledChainPool.add(guest);
|
45
45
|
this.theChain.receiving(
|
46
|
-
new
|
46
|
+
new Guest((chain) => {
|
47
47
|
this.filledChainPool.receive(chain);
|
48
48
|
}),
|
49
49
|
);
|
@@ -53,13 +53,13 @@ export class GuestChain<T> implements ChainType<T> {
|
|
53
53
|
return this;
|
54
54
|
}
|
55
55
|
|
56
|
-
public receiveKey<R>(key: string):
|
56
|
+
public receiveKey<R>(key: string): GuestObjectType<R> {
|
57
57
|
this.keysKnown.add(key);
|
58
|
-
return new
|
58
|
+
return new Guest((value) => {
|
59
59
|
// Обернул в очередь чтобы можно было синхронно наполнить очередь известных ключей
|
60
60
|
queueMicrotask(() => {
|
61
61
|
this.theChain.receiving(
|
62
|
-
new
|
62
|
+
new Guest((chain: Record<string, unknown>) => {
|
63
63
|
this.keysFilled.add(key);
|
64
64
|
const lastChain = {
|
65
65
|
...chain,
|
@@ -1,15 +1,15 @@
|
|
1
1
|
import { expect, test } from "vitest";
|
2
|
-
import {
|
2
|
+
import { Guest } from "./Guest";
|
3
3
|
import { GuestInTheMiddle } from "./GuestInTheMiddle";
|
4
4
|
import { GuestChain } from "./GuestChain";
|
5
5
|
import { SourceOfValue } from "../Source/SourceOfValue";
|
6
|
-
import {
|
6
|
+
import { Patron } from "../Patron/Patron";
|
7
7
|
|
8
8
|
test("test guest in the middle", () => {
|
9
9
|
const one = new SourceOfValue(1);
|
10
10
|
|
11
11
|
let accumValue = 0;
|
12
|
-
const guest = new
|
12
|
+
const guest = new Guest((value: number) => {
|
13
13
|
accumValue += value;
|
14
14
|
});
|
15
15
|
one.receiving(
|
@@ -25,8 +25,8 @@ test("test patron in the middle", () => {
|
|
25
25
|
const one = new SourceOfValue(1);
|
26
26
|
|
27
27
|
let accumValue = 0;
|
28
|
-
const guest = new
|
29
|
-
new
|
28
|
+
const guest = new Patron(
|
29
|
+
new Guest((value: number) => {
|
30
30
|
accumValue += value;
|
31
31
|
}),
|
32
32
|
);
|
@@ -51,14 +51,14 @@ test("test chain in the middle", () => {
|
|
51
51
|
const two = new SourceOfValue(2);
|
52
52
|
const chain = new GuestChain<{ one: number; two: number }>();
|
53
53
|
|
54
|
-
one.receiving(new
|
55
|
-
two.receiving(new
|
54
|
+
one.receiving(new Patron(chain.receiveKey("one")));
|
55
|
+
two.receiving(new Patron(chain.receiveKey("two")));
|
56
56
|
|
57
57
|
one.receive(3);
|
58
58
|
one.receive(4);
|
59
59
|
|
60
|
-
const guest = new
|
61
|
-
new
|
60
|
+
const guest = new Patron(
|
61
|
+
new Guest((value: { one: number; two: number; three: number }) => {
|
62
62
|
expect(Object.values(value).length).toBe(3);
|
63
63
|
}),
|
64
64
|
);
|
@@ -1,8 +1,8 @@
|
|
1
|
-
import {
|
1
|
+
import { GuestObjectType, ReceiveOptions } from "./Guest";
|
2
2
|
|
3
|
-
export class GuestInTheMiddle<T> implements
|
3
|
+
export class GuestInTheMiddle<T> implements GuestObjectType<T> {
|
4
4
|
public constructor(
|
5
|
-
private baseGuest:
|
5
|
+
private baseGuest: GuestObjectType<unknown>,
|
6
6
|
private middleFn: (value: T, options?: ReceiveOptions) => void,
|
7
7
|
) {}
|
8
8
|
|
@@ -1,34 +1,27 @@
|
|
1
1
|
import { expect, test } from "vitest";
|
2
|
-
import { GuestCallback } from "./GuestCallback";
|
3
2
|
import { GuestPool } from "./GuestPool";
|
4
|
-
import {
|
3
|
+
import { Patron } from "../Patron/Patron";
|
5
4
|
|
6
5
|
test("patron pool with guests", () => {
|
7
|
-
const pool = new GuestPool(null);
|
6
|
+
const pool = new GuestPool<number>(null);
|
8
7
|
let receivedCount = 0;
|
9
8
|
|
10
9
|
// 2 + 2
|
11
10
|
pool.add(
|
12
|
-
new
|
13
|
-
|
14
|
-
|
15
|
-
}),
|
16
|
-
),
|
11
|
+
new Patron((value) => {
|
12
|
+
receivedCount += value;
|
13
|
+
}),
|
17
14
|
);
|
18
15
|
// 2 + 2
|
19
16
|
pool.add(
|
20
|
-
new
|
21
|
-
new GuestCallback<number>((value) => {
|
22
|
-
receivedCount += value;
|
23
|
-
}),
|
24
|
-
),
|
25
|
-
);
|
26
|
-
// 2
|
27
|
-
pool.add(
|
28
|
-
new GuestCallback<number>((value) => {
|
17
|
+
new Patron((value) => {
|
29
18
|
receivedCount += value;
|
30
19
|
}),
|
31
20
|
);
|
21
|
+
// 2
|
22
|
+
pool.add((value) => {
|
23
|
+
receivedCount += value;
|
24
|
+
});
|
32
25
|
pool.receive(2);
|
33
26
|
|
34
27
|
setTimeout(() => {
|
package/src/Guest/GuestPool.ts
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
import { PatronPool } from "../Patron/PatronPool";
|
2
2
|
import { PoolType } from "../Patron/PatronPool";
|
3
|
-
import { GuestType, ReceiveOptions } from "./
|
3
|
+
import { give, GuestObjectType, GuestType, ReceiveOptions } from "./Guest";
|
4
4
|
|
5
|
-
export class GuestPool<T> implements
|
5
|
+
export class GuestPool<T> implements GuestObjectType<T>, PoolType<T> {
|
6
6
|
private guests = new Set<GuestType<T>>();
|
7
7
|
|
8
8
|
private patronPool: PatronPool<T>;
|
@@ -18,20 +18,24 @@ export class GuestPool<T> implements GuestType<T>, PoolType<T> {
|
|
18
18
|
}
|
19
19
|
|
20
20
|
public add(guest: GuestType<T>): this {
|
21
|
-
if (
|
21
|
+
if (
|
22
|
+
typeof guest === "function" ||
|
23
|
+
!guest.introduction ||
|
24
|
+
guest.introduction() === "guest"
|
25
|
+
) {
|
22
26
|
this.guests.add(guest);
|
23
27
|
}
|
24
28
|
this.patronPool.add(guest);
|
25
29
|
return this;
|
26
30
|
}
|
27
31
|
|
28
|
-
public remove(patron:
|
32
|
+
public remove(patron: GuestObjectType<T>): this {
|
29
33
|
this.guests.delete(patron);
|
30
34
|
this.patronPool.remove(patron);
|
31
35
|
return this;
|
32
36
|
}
|
33
37
|
|
34
|
-
public distribute(receiving: T, possiblePatron:
|
38
|
+
public distribute(receiving: T, possiblePatron: GuestObjectType<T>): this {
|
35
39
|
this.add(possiblePatron);
|
36
40
|
this.receive(receiving);
|
37
41
|
return this;
|
@@ -39,7 +43,7 @@ export class GuestPool<T> implements GuestType<T>, PoolType<T> {
|
|
39
43
|
|
40
44
|
private deliverToGuests(value: T, options?: ReceiveOptions) {
|
41
45
|
this.guests.forEach((target) => {
|
42
|
-
|
46
|
+
give(value, target, options);
|
43
47
|
});
|
44
48
|
this.guests.clear();
|
45
49
|
}
|
@@ -1,11 +1,11 @@
|
|
1
1
|
import { expect, test } from "vitest";
|
2
2
|
import { GuestSync } from "./GuestSync";
|
3
3
|
import { SourceOfValue } from "../Source/SourceOfValue";
|
4
|
-
import {
|
4
|
+
import { Guest } from "./Guest";
|
5
5
|
|
6
6
|
test("guest sync", () => {
|
7
7
|
const source = new SourceOfValue(123);
|
8
|
-
const syncGuest = new GuestSync(
|
8
|
+
const syncGuest = new GuestSync(111);
|
9
9
|
syncGuest.receive(222);
|
10
10
|
expect(syncGuest.value()).toBe(222);
|
11
11
|
source.receiving(syncGuest);
|
package/src/Guest/GuestSync.ts
CHANGED
@@ -1,16 +1,13 @@
|
|
1
1
|
import { expect, test } from "vitest";
|
2
|
-
import {
|
2
|
+
import { Patron } from "./Patron";
|
3
3
|
import { SourceOfValue } from "../Source/SourceOfValue";
|
4
|
-
import { GuestCallback } from "../Guest/GuestCallback";
|
5
4
|
|
6
5
|
test("patron always guest", () => {
|
7
6
|
const one = new SourceOfValue(1);
|
8
7
|
let patronCalledTimes = 0;
|
9
|
-
const patron = new
|
10
|
-
|
11
|
-
|
12
|
-
}),
|
13
|
-
);
|
8
|
+
const patron = new Patron(() => {
|
9
|
+
patronCalledTimes += 1;
|
10
|
+
});
|
14
11
|
one.receiving(patron);
|
15
12
|
one.receive(2);
|
16
13
|
|
package/src/Patron/Patron.ts
CHANGED
@@ -1,18 +1,22 @@
|
|
1
|
-
import {
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
import {
|
2
|
+
give,
|
3
|
+
GuestObjectType,
|
4
|
+
GuestType,
|
5
|
+
ReceiveOptions,
|
6
|
+
} from "../Guest/Guest";
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
/**
|
9
|
+
* Патрон - это постоянный посетитель
|
10
|
+
*/
|
11
|
+
export class Patron<T> implements GuestObjectType<T> {
|
12
|
+
public constructor(private willBePatron: GuestType<T>) {}
|
10
13
|
|
11
|
-
public
|
12
|
-
return
|
14
|
+
public introduction() {
|
15
|
+
return "patron" as const;
|
13
16
|
}
|
14
17
|
|
15
|
-
public
|
16
|
-
|
18
|
+
public receive(value: T, options?: ReceiveOptions): this {
|
19
|
+
give(value, this.willBePatron, options);
|
20
|
+
return this;
|
17
21
|
}
|
18
22
|
}
|
@@ -1,16 +1,13 @@
|
|
1
1
|
import { expect, test } from "vitest";
|
2
2
|
import { PatronOnce } from "./PatronOnce";
|
3
|
-
import { GuestCallback } from "../Guest/GuestCallback";
|
4
3
|
import { SourceOfValue } from "../Source/SourceOfValue";
|
5
4
|
|
6
5
|
test("patron once", () => {
|
7
6
|
const source = new SourceOfValue(42);
|
8
7
|
let calls = 0;
|
9
|
-
const patron = new PatronOnce(
|
10
|
-
|
11
|
-
|
12
|
-
}),
|
13
|
-
);
|
8
|
+
const patron = new PatronOnce(() => {
|
9
|
+
calls += 1;
|
10
|
+
});
|
14
11
|
source.receiving(patron);
|
15
12
|
source.receive(22);
|
16
13
|
|
package/src/Patron/PatronOnce.ts
CHANGED
@@ -1,11 +1,16 @@
|
|
1
1
|
import { PoolType } from "./PatronPool";
|
2
|
-
import {
|
2
|
+
import {
|
3
|
+
give,
|
4
|
+
GuestObjectType,
|
5
|
+
GuestType,
|
6
|
+
ReceiveOptions,
|
7
|
+
} from "../Guest/Guest";
|
3
8
|
|
4
9
|
type PoolAware = {
|
5
10
|
pool?: PoolType;
|
6
11
|
};
|
7
12
|
|
8
|
-
export class PatronOnce<T> implements
|
13
|
+
export class PatronOnce<T> implements GuestObjectType<T> {
|
9
14
|
private received = false;
|
10
15
|
|
11
16
|
public constructor(private baseGuest: GuestType<T>) {}
|
@@ -16,7 +21,7 @@ export class PatronOnce<T> implements GuestType<T> {
|
|
16
21
|
|
17
22
|
public receive(value: T, options?: ReceiveOptions): this {
|
18
23
|
if (!this.received) {
|
19
|
-
this.baseGuest
|
24
|
+
give(value, this.baseGuest, options);
|
20
25
|
}
|
21
26
|
|
22
27
|
const data = options?.data as PoolAware;
|
@@ -1,26 +1,21 @@
|
|
1
1
|
import { expect, test } from "vitest";
|
2
2
|
import { PatronPool } from "./PatronPool";
|
3
|
-
import {
|
4
|
-
import { GuestCallback } from "../Guest/GuestCallback";
|
3
|
+
import { Patron } from "./Patron";
|
5
4
|
|
6
5
|
test("patron pool", () => {
|
7
|
-
const pool = new PatronPool(null);
|
6
|
+
const pool = new PatronPool<number>(null);
|
8
7
|
let receivedCount = 0;
|
9
8
|
|
10
9
|
pool.add(
|
11
|
-
new
|
12
|
-
|
13
|
-
|
14
|
-
}),
|
15
|
-
),
|
10
|
+
new Patron((value) => {
|
11
|
+
receivedCount += value;
|
12
|
+
}),
|
16
13
|
);
|
17
14
|
pool.add(
|
18
|
-
new
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
}),
|
23
|
-
),
|
15
|
+
new Patron((value) => {
|
16
|
+
receivedCount += value;
|
17
|
+
expect(receivedCount).toBe(4);
|
18
|
+
}),
|
24
19
|
);
|
25
20
|
pool.receive(2);
|
26
21
|
});
|
package/src/Patron/PatronPool.ts
CHANGED
@@ -1,24 +1,29 @@
|
|
1
|
-
import {
|
1
|
+
import {
|
2
|
+
give,
|
3
|
+
GuestObjectType,
|
4
|
+
GuestType,
|
5
|
+
ReceiveOptions,
|
6
|
+
} from "../Guest/Guest";
|
2
7
|
|
3
|
-
const poolSets = new Map<PoolType, Set<
|
8
|
+
const poolSets = new Map<PoolType, Set<GuestObjectType>>();
|
4
9
|
|
5
10
|
/**
|
6
11
|
* Удалить патрон из всех пулов
|
7
12
|
*/
|
8
|
-
export const removePatronFromPools = (patron:
|
13
|
+
export const removePatronFromPools = (patron: GuestObjectType) => {
|
9
14
|
poolSets.forEach((pool) => {
|
10
15
|
pool.delete(patron);
|
11
16
|
});
|
12
17
|
};
|
13
18
|
|
14
|
-
export interface PoolType<T = unknown> extends
|
15
|
-
add(guest:
|
16
|
-
distribute(receiving: T, possiblePatron:
|
17
|
-
remove(patron:
|
19
|
+
export interface PoolType<T = unknown> extends GuestObjectType<T> {
|
20
|
+
add(guest: GuestObjectType<T>): this;
|
21
|
+
distribute(receiving: T, possiblePatron: GuestObjectType<T>): this;
|
22
|
+
remove(patron: GuestObjectType<T>): this;
|
18
23
|
}
|
19
24
|
|
20
25
|
export class PatronPool<T> implements PoolType<T> {
|
21
|
-
private patrons = new Set<
|
26
|
+
private patrons = new Set<GuestObjectType<T>>();
|
22
27
|
|
23
28
|
public receive: (value: T, options?: ReceiveOptions) => this;
|
24
29
|
|
@@ -45,6 +50,7 @@ export class PatronPool<T> implements PoolType<T> {
|
|
45
50
|
|
46
51
|
public add(shouldBePatron: GuestType<T>) {
|
47
52
|
if (
|
53
|
+
typeof shouldBePatron !== "function" &&
|
48
54
|
shouldBePatron.introduction &&
|
49
55
|
shouldBePatron.introduction() === "patron"
|
50
56
|
) {
|
@@ -53,7 +59,7 @@ export class PatronPool<T> implements PoolType<T> {
|
|
53
59
|
return this;
|
54
60
|
}
|
55
61
|
|
56
|
-
public remove(patron:
|
62
|
+
public remove(patron: GuestObjectType<T>) {
|
57
63
|
this.patrons.delete(patron);
|
58
64
|
return this;
|
59
65
|
}
|
@@ -69,7 +75,7 @@ export class PatronPool<T> implements PoolType<T> {
|
|
69
75
|
guest: GuestType<T>,
|
70
76
|
options?: ReceiveOptions,
|
71
77
|
) {
|
72
|
-
|
78
|
+
give(value, guest, {
|
73
79
|
...options,
|
74
80
|
data: {
|
75
81
|
...((options?.data as Record<string, unknown>) ?? {}),
|
@@ -1,13 +1,10 @@
|
|
1
1
|
import { expect, test } from "vitest";
|
2
2
|
import { SourceOfValue } from "./SourceOfValue";
|
3
|
-
import { GuestCallback } from "../Guest/GuestCallback";
|
4
3
|
|
5
4
|
test("source", () => {
|
6
5
|
const source = new SourceOfValue(42);
|
7
6
|
|
8
|
-
source.receiving(
|
9
|
-
|
10
|
-
|
11
|
-
}),
|
12
|
-
);
|
7
|
+
source.receiving((value) => {
|
8
|
+
expect(value).toBe(42);
|
9
|
+
});
|
13
10
|
});
|
@@ -1,8 +1,8 @@
|
|
1
1
|
import { GuestAwareType } from "../Guest/GuestAware";
|
2
|
-
import { GuestType } from "../Guest/
|
2
|
+
import { Guest, GuestObjectType, GuestType } from "../Guest/Guest";
|
3
3
|
import { PatronPool } from "../Patron/PatronPool";
|
4
4
|
|
5
|
-
export type SourceType<T = unknown> = GuestAwareType<T> &
|
5
|
+
export type SourceType<T = unknown> = GuestAwareType<T> & GuestObjectType<T>;
|
6
6
|
|
7
7
|
export class SourceOfValue<T> implements SourceType<T> {
|
8
8
|
private pool = new PatronPool(this);
|
@@ -16,7 +16,11 @@ export class SourceOfValue<T> implements SourceType<T> {
|
|
16
16
|
}
|
17
17
|
|
18
18
|
public receiving(guest: GuestType<T>): this {
|
19
|
-
|
19
|
+
if (typeof guest === "function") {
|
20
|
+
this.pool.distribute(this.sourceDocument, new Guest(guest));
|
21
|
+
} else {
|
22
|
+
this.pool.distribute(this.sourceDocument, guest);
|
23
|
+
}
|
20
24
|
return this;
|
21
25
|
}
|
22
26
|
}
|