patron-oop 1.6.0 → 1.7.0

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.
Files changed (37) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/dist/patron.d.ts +61 -80
  3. package/dist/patron.js +75 -110
  4. package/dist/patron.js.map +1 -1
  5. package/dist/patron.min.js +1 -1
  6. package/dist/patron.mjs +70 -109
  7. package/dist/patron.mjs.map +1 -1
  8. package/package.json +1 -1
  9. package/src/Guest/Guest.test.ts +10 -0
  10. package/src/Guest/Guest.ts +28 -35
  11. package/src/Guest/GuestAware.test.ts +6 -8
  12. package/src/Guest/GuestAware.ts +1 -1
  13. package/src/Guest/GuestCast.test.ts +6 -6
  14. package/src/Guest/GuestCast.ts +7 -3
  15. package/src/Guest/GuestChain.test.ts +12 -12
  16. package/src/Guest/GuestChain.ts +11 -11
  17. package/src/Guest/GuestInTheMiddle.test.ts +9 -9
  18. package/src/Guest/GuestInTheMiddle.ts +3 -3
  19. package/src/Guest/GuestPool.test.ts +10 -17
  20. package/src/Guest/GuestPool.ts +10 -6
  21. package/src/Guest/GuestSync.test.ts +2 -2
  22. package/src/Guest/GuestSync.ts +2 -2
  23. package/src/Patron/{PatronOfGuest.test.ts → Patron.test.ts} +4 -7
  24. package/src/Patron/Patron.ts +16 -12
  25. package/src/Patron/PatronOnce.test.ts +3 -6
  26. package/src/Patron/PatronOnce.ts +8 -3
  27. package/src/Patron/PatronPool.test.ts +9 -14
  28. package/src/Patron/PatronPool.ts +16 -10
  29. package/src/Source/SourceOfValue.test.ts +3 -6
  30. package/src/Source/SourceOfValue.ts +7 -3
  31. package/src/index.ts +35 -13
  32. package/src/Guest/GuestCallback.test.ts +0 -13
  33. package/src/Guest/GuestCallback.ts +0 -21
  34. package/src/Patron/PatronOfGuest.ts +0 -17
  35. package/src/Source/Source.ts +0 -12
  36. package/src/Source/SourcesApplied.test.ts +0 -26
  37. package/src/Source/SourcesApplied.ts +0 -39
@@ -1,12 +1,12 @@
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
4
  import { SourceOfValue } from "../Source/SourceOfValue";
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> {
@@ -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: 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
5
  import { SourceOfValue } from "../Source/SourceOfValue";
6
- import { PatronOfGuest } from "../Patron/PatronOfGuest";
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 GuestCallback((value: number) => {
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 PatronOfGuest(
29
- new GuestCallback((value: number) => {
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 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
3
  import { SourceOfValue } from "../Source/SourceOfValue";
4
- import { GuestCallback } from "./GuestCallback";
4
+ import { Guest } from "./Guest";
5
5
 
6
6
  test("guest sync", () => {
7
7
  const source = new SourceOfValue(123);
8
- const syncGuest = new GuestSync(new GuestCallback(() => {}));
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
 
@@ -1,16 +1,13 @@
1
1
  import { expect, test } from "vitest";
2
- import { PatronOfGuest } from "./PatronOfGuest";
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 PatronOfGuest(
10
- new GuestCallback(() => {
11
- patronCalledTimes += 1;
12
- }),
13
- );
8
+ const patron = new Patron(() => {
9
+ patronCalledTimes += 1;
10
+ });
14
11
  one.receiving(patron);
15
12
  one.receive(2);
16
13
 
@@ -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
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
- 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>) ?? {}),
@@ -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
- new GuestCallback((value) => {
10
- expect(value).toBe(42);
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/GuestCallback";
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> & GuestType<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
- this.pool.distribute(this.sourceDocument, guest);
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
- export * from "./Guest/GuestCallback";
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
- import { Guest } from "./Guest/Guest";
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 (window) {
16
- window["GUEST_LIBRARY"] = {
17
- guest: new Guest(),
18
- patron: new Patron(),
19
- source: new Source(),
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
- }
@@ -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
- });