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