patron-oop 1.6.0 → 1.7.0
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +9 -0
- package/dist/patron.d.ts +61 -80
- package/dist/patron.js +75 -110
- package/dist/patron.js.map +1 -1
- package/dist/patron.min.js +1 -1
- package/dist/patron.mjs +70 -109
- package/dist/patron.mjs.map +1 -1
- 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 -26
- package/src/Source/SourcesApplied.ts +0 -39
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
|
}
|
package/src/index.ts
CHANGED
@@ -1,21 +1,43 @@
|
|
1
|
-
|
1
|
+
import { give, Guest } from "./Guest/Guest";
|
2
|
+
import { PatronPool, removePatronFromPools } from "./Patron/PatronPool";
|
3
|
+
import { GuestAware } from "./Guest/GuestAware";
|
4
|
+
import { GuestCast } from "./Guest/GuestCast";
|
5
|
+
import { GuestChain } from "./Guest/GuestChain";
|
6
|
+
import { GuestInTheMiddle } from "./Guest/GuestInTheMiddle";
|
7
|
+
import { GuestPool } from "./Guest/GuestPool";
|
8
|
+
import { GuestSync } from "./Guest/GuestSync";
|
9
|
+
import { Patron } from "./Patron/Patron";
|
10
|
+
import { PatronOnce } from "./Patron/PatronOnce";
|
11
|
+
import { SourceOfValue } from "./Source/SourceOfValue";
|
12
|
+
|
13
|
+
export * from "./Guest/GuestAware";
|
14
|
+
export * from "./Guest/Guest";
|
15
|
+
export * from "./Guest/GuestCast";
|
2
16
|
export * from "./Guest/GuestChain";
|
17
|
+
export * from "./Guest/GuestInTheMiddle";
|
18
|
+
export * from "./Guest/GuestPool";
|
3
19
|
export * from "./Guest/GuestSync";
|
20
|
+
export * from "./Patron/Patron";
|
21
|
+
export * from "./Patron/PatronOnce";
|
4
22
|
export * from "./Patron/PatronPool";
|
5
23
|
export * from "./Source/SourceOfValue";
|
6
24
|
|
7
|
-
|
8
|
-
import { Patron } from "./Patron/Patron";
|
9
|
-
import { Source } from "./Source/Source";
|
10
|
-
|
11
|
-
export { Guest, Patron, Source };
|
12
|
-
|
13
|
-
declare var window: any;
|
25
|
+
declare var globalThis: any;
|
14
26
|
|
15
|
-
if (
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
27
|
+
if (globalThis) {
|
28
|
+
globalThis["GUEST_LIBRARY"] = {
|
29
|
+
give,
|
30
|
+
removePatronFromPools,
|
31
|
+
GuestAware,
|
32
|
+
Guest,
|
33
|
+
GuestCast,
|
34
|
+
GuestChain,
|
35
|
+
GuestInTheMiddle,
|
36
|
+
GuestPool,
|
37
|
+
GuestSync,
|
38
|
+
Patron,
|
39
|
+
PatronOnce,
|
40
|
+
PatronPool,
|
41
|
+
SourceOfValue,
|
20
42
|
};
|
21
43
|
}
|
@@ -1,13 +0,0 @@
|
|
1
|
-
import { expect, test } from "vitest";
|
2
|
-
import { GuestCallback } from "./GuestCallback";
|
3
|
-
import { SourceOfValue } from "../Source/SourceOfValue";
|
4
|
-
|
5
|
-
test("guest dynamic", () => {
|
6
|
-
const one = new SourceOfValue(1);
|
7
|
-
|
8
|
-
one.receiving(
|
9
|
-
new GuestCallback((value) => {
|
10
|
-
expect(value).toBe(1);
|
11
|
-
}),
|
12
|
-
);
|
13
|
-
});
|
@@ -1,21 +0,0 @@
|
|
1
|
-
type GuestIntroduction = "guest" | "patron";
|
2
|
-
|
3
|
-
export interface ReceiveOptions {
|
4
|
-
data?: unknown;
|
5
|
-
}
|
6
|
-
|
7
|
-
export type GuestExecutorType<T> = (value: T, options?: ReceiveOptions) => void;
|
8
|
-
|
9
|
-
export interface GuestType<T = unknown> {
|
10
|
-
receive(value: T, options?: ReceiveOptions): this;
|
11
|
-
introduction?(): GuestIntroduction;
|
12
|
-
}
|
13
|
-
|
14
|
-
export class GuestCallback<T> implements GuestType<T> {
|
15
|
-
public constructor(private receiver: GuestExecutorType<T>) {}
|
16
|
-
|
17
|
-
public receive(value: T, options?: ReceiveOptions) {
|
18
|
-
this.receiver(value, options);
|
19
|
-
return this;
|
20
|
-
}
|
21
|
-
}
|
@@ -1,17 +0,0 @@
|
|
1
|
-
import { GuestType, ReceiveOptions } from "../Guest/GuestCallback";
|
2
|
-
|
3
|
-
/**
|
4
|
-
* Патрон - это постоянный посетитель
|
5
|
-
*/
|
6
|
-
export class PatronOfGuest<T> implements GuestType<T> {
|
7
|
-
public constructor(private willBePatron: GuestType<T>) {}
|
8
|
-
|
9
|
-
public introduction() {
|
10
|
-
return "patron" as const;
|
11
|
-
}
|
12
|
-
|
13
|
-
public receive(value: T, options?: ReceiveOptions): this {
|
14
|
-
this.willBePatron.receive(value, options);
|
15
|
-
return this;
|
16
|
-
}
|
17
|
-
}
|
package/src/Source/Source.ts
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
import { SourceOfValue } from "./SourceOfValue";
|
2
|
-
import { sourcesApplied } from "./SourcesApplied";
|
3
|
-
|
4
|
-
export class Source {
|
5
|
-
public ofValue<P>(sourceDocument: P) {
|
6
|
-
return new SourceOfValue(sourceDocument);
|
7
|
-
}
|
8
|
-
|
9
|
-
public applySources<P>(target: P, methodsSources: Record<string, unknown[]>) {
|
10
|
-
return sourcesApplied(target, methodsSources);
|
11
|
-
}
|
12
|
-
}
|
@@ -1,26 +0,0 @@
|
|
1
|
-
import { expect, test } from "vitest";
|
2
|
-
import { sourcesApplied } from "./SourcesApplied";
|
3
|
-
|
4
|
-
test("sources applied", () => {
|
5
|
-
const target = {
|
6
|
-
one(data: { one: string }, after: string) {
|
7
|
-
return data.one + " " + after;
|
8
|
-
},
|
9
|
-
};
|
10
|
-
const targetApplied = sourcesApplied(target, {
|
11
|
-
one: [{ one: "hello world" }, "after"],
|
12
|
-
});
|
13
|
-
expect(targetApplied.one()).toBe("hello world after");
|
14
|
-
});
|
15
|
-
|
16
|
-
test("sources applied classes", () => {
|
17
|
-
class Target {
|
18
|
-
one(data: { one: string }, after: string) {
|
19
|
-
return data.one + " " + after;
|
20
|
-
}
|
21
|
-
}
|
22
|
-
const targetApplied = sourcesApplied(new Target(), {
|
23
|
-
one: [{ one: "good bye" }, "after"],
|
24
|
-
});
|
25
|
-
expect(targetApplied.one()).toBe("good bye after");
|
26
|
-
});
|