patron-oop 1.6.0 → 1.8.0

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