patron-oop 1.3.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +60 -0
  3. package/dist/patron.d.ts +59 -83
  4. package/dist/patron.js +128 -127
  5. package/dist/patron.js.map +1 -1
  6. package/dist/patron.min.js +1 -1
  7. package/dist/patron.mjs +126 -118
  8. package/dist/patron.mjs.map +1 -1
  9. package/package.json +1 -1
  10. package/src/Guest/Guest.ts +45 -0
  11. package/src/Guest/GuestAware.test.ts +15 -0
  12. package/src/{GuestAware.ts → Guest/GuestAware.ts} +5 -2
  13. package/src/Guest/GuestCallback.test.ts +13 -0
  14. package/src/Guest/GuestCallback.ts +21 -0
  15. package/src/Guest/GuestCast.test.ts +31 -0
  16. package/src/{GuestCast.ts → Guest/GuestCast.ts} +1 -1
  17. package/src/Guest/GuestChain.test.ts +72 -0
  18. package/src/{Chain.ts → Guest/GuestChain.ts} +15 -11
  19. package/src/Guest/GuestInTheMiddle.test.ts +71 -0
  20. package/src/{GuestInTheMiddle.ts → Guest/GuestInTheMiddle.ts} +1 -1
  21. package/src/{GuestPool.test.ts → Guest/GuestPool.test.ts} +7 -7
  22. package/src/{GuestPool.ts → Guest/GuestPool.ts} +3 -3
  23. package/src/Guest/GuestSync.test.ts +13 -0
  24. package/src/{GuestSync.ts → Guest/GuestSync.ts} +5 -1
  25. package/src/Patron/Patron.ts +18 -0
  26. package/src/{Patron.test.ts → Patron/PatronOfGuest.test.ts} +6 -6
  27. package/src/{Patron.ts → Patron/PatronOfGuest.ts} +2 -2
  28. package/src/{PatronOnce.test.ts → Patron/PatronOnce.test.ts} +4 -4
  29. package/src/{PatronOnce.ts → Patron/PatronOnce.ts} +3 -3
  30. package/src/{PatronPool.test.ts → Patron/PatronPool.test.ts} +6 -6
  31. package/src/{PatronPool.ts → Patron/PatronPool.ts} +7 -2
  32. package/src/Source/Source.ts +12 -0
  33. package/src/Source/SourceOfValue.test.ts +13 -0
  34. package/src/{Source.ts → Source/SourceOfValue.ts} +6 -4
  35. package/src/Source/SourcesApplied.test.ts +14 -0
  36. package/src/Source/SourcesApplied.ts +40 -0
  37. package/src/index.ts +9 -16
  38. package/src/Cache.test.ts +0 -20
  39. package/src/Cache.ts +0 -31
  40. package/src/CacheType.ts +0 -4
  41. package/src/Chain.test.ts +0 -72
  42. package/src/ChainType.ts +0 -7
  43. package/src/Factory.test.ts +0 -16
  44. package/src/Factory.ts +0 -21
  45. package/src/FactoryDynamic.ts +0 -11
  46. package/src/FactoryType.ts +0 -3
  47. package/src/FactoryWithFactories.ts +0 -25
  48. package/src/Guest.test.ts +0 -13
  49. package/src/Guest.ts +0 -11
  50. package/src/GuestAwareType.ts +0 -5
  51. package/src/GuestExecutorType.ts +0 -3
  52. package/src/GuestInTheMiddle.test.ts +0 -71
  53. package/src/GuestSync.test.ts +0 -11
  54. package/src/GuestType.ts +0 -10
  55. package/src/GuestValueType.ts +0 -5
  56. package/src/PoolType.ts +0 -7
  57. package/src/Source.test.ts +0 -13
  58. package/src/SourceType.ts +0 -4
package/src/Cache.test.ts DELETED
@@ -1,20 +0,0 @@
1
- import { expect, test } from "vitest";
2
- import { Cache } from "./Cache";
3
- import { Guest } from "./Guest";
4
-
5
- test("value works", () => {
6
- const value = new Cache(null);
7
- value.receive(2);
8
- value.receiving(
9
- new Guest((latestValue: number) => {
10
- expect(latestValue).toBe(2);
11
- }),
12
- );
13
-
14
- value.receive(4);
15
- value.receiving(
16
- new Guest((latestValue: number) => {
17
- expect(latestValue).toBe(4);
18
- }),
19
- );
20
- });
package/src/Cache.ts DELETED
@@ -1,31 +0,0 @@
1
- import { PatronPool } from "./PatronPool";
2
- import { CacheType } from "./CacheType";
3
- import { GuestType, ReceiveOptions } from "./GuestType";
4
-
5
- export class Cache<T> implements CacheType<T> {
6
- private pool: PatronPool<T>;
7
-
8
- public constructor(
9
- initiator: unknown,
10
- private defaultValue: T | null = null,
11
- private theCache: T | null = null,
12
- ) {
13
- this.pool = new PatronPool<T>(initiator);
14
- }
15
-
16
- public receive(value: T, options?: ReceiveOptions): this {
17
- this.theCache = value;
18
- this.pool.receive(value, options);
19
- return this;
20
- }
21
-
22
- public receiving(guest: GuestType<T>): this {
23
- if (this.theCache !== null) {
24
- guest.receive(this.theCache);
25
- } else if (this.defaultValue !== null) {
26
- guest.receive(this.defaultValue);
27
- }
28
- this.pool.add(guest);
29
- return this;
30
- }
31
- }
package/src/CacheType.ts DELETED
@@ -1,4 +0,0 @@
1
- import { GuestAwareType } from "./GuestAwareType";
2
- import { GuestType } from "./GuestType";
3
-
4
- export type CacheType<T = unknown> = GuestType<T> & GuestAwareType<T>;
package/src/Chain.test.ts DELETED
@@ -1,72 +0,0 @@
1
- import { expect, test } from "vitest";
2
- import { Chain } from "./Chain";
3
- import { Patron } from "./Patron";
4
- import { Guest } from "./Guest";
5
- import { Source } from "./Source";
6
-
7
- test("chain guest returns 2 values after result guest", () => {
8
- const one = new Source(1);
9
- const two = new Source(2);
10
- const chain = new Chain<{ one: number; two: number }>();
11
-
12
- chain.result(
13
- new Guest((value) => {
14
- expect(Object.values(value).join()).toBe("1,2");
15
- }),
16
- );
17
-
18
- one.receiving(chain.receiveKey("one"));
19
- two.receiving(chain.receiveKey("two"));
20
- });
21
-
22
- test("chain guest returns 2 values before result guest", () => {
23
- const one = new Source(1);
24
- const two = new Source(2);
25
- const chain = new Chain<{ one: number; two: number }>();
26
-
27
- one.receiving(chain.receiveKey("one"));
28
- two.receiving(chain.receiveKey("two"));
29
-
30
- chain.result(
31
- new Guest((value) => {
32
- expect(Object.values(value).join()).toBe("1,2");
33
- }),
34
- );
35
- });
36
-
37
- test("chain with patron", () => {
38
- const one = new Source(1);
39
- const two = new Source(2);
40
- const chain = new Chain<{ one: number; two: number }>();
41
-
42
- one.receiving(new Patron(chain.receiveKey("one")));
43
- two.receiving(new Patron(chain.receiveKey("two")));
44
-
45
- one.receive(3);
46
- one.receive(4);
47
-
48
- chain.result(
49
- new Patron(
50
- new Guest((value) => {
51
- expect(Object.values(value).length).toBe(2);
52
- }),
53
- ),
54
- );
55
- });
56
-
57
- test("chain as array", () => {
58
- const one = new Source(1);
59
- const two = new Source(2);
60
- const chain = new Chain<[number, number]>();
61
-
62
- one.receiving(new Patron(chain.receiveKey("0")));
63
- two.receiving(new Patron(chain.receiveKey("1")));
64
-
65
- chain.resultArray(
66
- new Patron(
67
- new Guest((value) => {
68
- expect(JSON.stringify(value)).toBe("[1, 2]");
69
- }),
70
- ),
71
- );
72
- });
package/src/ChainType.ts DELETED
@@ -1,7 +0,0 @@
1
- import { GuestType } from "./GuestType";
2
-
3
- export interface ChainType<T = unknown> {
4
- result(guest: GuestType<T>): this;
5
- resultArray(guest: GuestType<T>): this;
6
- receiveKey<R>(key: string): GuestType<R>;
7
- }
@@ -1,16 +0,0 @@
1
- import { expect, test } from "vitest";
2
- import { Factory } from "./Factory";
3
- import { Guest } from "./Guest";
4
- import { Source } from "./Source";
5
-
6
- test("factory", () => {
7
- const sourceFactory = new Factory(Source);
8
-
9
- const source = sourceFactory.create(42);
10
-
11
- source.receiving(
12
- new Guest((value) => {
13
- expect(value).toBe(42);
14
- }),
15
- );
16
- });
package/src/Factory.ts DELETED
@@ -1,21 +0,0 @@
1
- import { FactoryType } from "./FactoryType";
2
-
3
- interface Constructable<T> {
4
- new (...args: unknown[]): T;
5
- }
6
-
7
- interface Prototyped<T> {
8
- prototype: T;
9
- }
10
-
11
- export class Factory<T> implements FactoryType<T> {
12
- public constructor(private constructorFn: Prototyped<T>) {}
13
-
14
- public create<R extends unknown[], CT = null>(
15
- ...args: R
16
- ): CT extends null ? T : CT {
17
- return new (this.constructorFn as Constructable<T>)(
18
- ...args,
19
- ) as CT extends null ? T : CT;
20
- }
21
- }
@@ -1,11 +0,0 @@
1
- import { FactoryType } from "./FactoryType";
2
-
3
- export class FactoryDynamic<T> implements FactoryType<T> {
4
- public constructor(private creationFn: (...args: unknown[]) => T) {}
5
-
6
- public create<R extends unknown[], CT = null>(
7
- ...args: R
8
- ): CT extends null ? T : CT {
9
- return this.creationFn(...args) as CT extends null ? T : CT;
10
- }
11
- }
@@ -1,3 +0,0 @@
1
- export interface FactoryType<T> {
2
- create<R extends unknown[], CT = null>(...args: R): CT extends null ? T : CT;
3
- }
@@ -1,25 +0,0 @@
1
- import { FactoryType } from "./FactoryType";
2
-
3
- interface Constructable<T> {
4
- new (...args: unknown[]): T;
5
- }
6
-
7
- interface Prototyped<T> {
8
- prototype: T;
9
- }
10
-
11
- export class FactoryWithFactories<T> implements FactoryType<T> {
12
- public constructor(
13
- private constructorFn: Prototyped<T>,
14
- private factories: Record<string, unknown> = {},
15
- ) {}
16
-
17
- public create<R extends unknown[], CT = null>(
18
- ...args: R
19
- ): CT extends null ? T : CT {
20
- return new (this.constructorFn as Constructable<T>)(
21
- ...args,
22
- this.factories,
23
- ) as CT extends null ? T : CT;
24
- }
25
- }
package/src/Guest.test.ts DELETED
@@ -1,13 +0,0 @@
1
- import { expect, test } from "vitest";
2
- import { Guest } from "./Guest";
3
- import { Source } from "./Source";
4
-
5
- test("guest dynamic", () => {
6
- const one = new Source(1);
7
-
8
- one.receiving(
9
- new Guest((value) => {
10
- expect(value).toBe(1);
11
- }),
12
- );
13
- });
package/src/Guest.ts DELETED
@@ -1,11 +0,0 @@
1
- import { GuestExecutorType } from "./GuestExecutorType";
2
- import { GuestType, ReceiveOptions } from "./GuestType";
3
-
4
- export class Guest<T> implements GuestType<T> {
5
- public constructor(private receiver: GuestExecutorType<T>) {}
6
-
7
- public receive(value: T, options?: ReceiveOptions) {
8
- this.receiver(value, options);
9
- return this;
10
- }
11
- }
@@ -1,5 +0,0 @@
1
- import { GuestType } from "./GuestType";
2
-
3
- export interface GuestAwareType<T = unknown> {
4
- receiving(guest: GuestType<T>): unknown;
5
- }
@@ -1,3 +0,0 @@
1
- import { ReceiveOptions } from "./GuestType";
2
-
3
- export type GuestExecutorType<T> = (value: T, options?: ReceiveOptions) => void;
@@ -1,71 +0,0 @@
1
- import { expect, test } from "vitest";
2
- import { Guest } from "./Guest";
3
- import { GuestInTheMiddle } from "./GuestInTheMiddle";
4
- import { Patron } from "./Patron";
5
- import { Chain } from "./Chain";
6
- import { Source } from "./Source";
7
-
8
- test("test guest in the middle", () => {
9
- const one = new Source(1);
10
-
11
- let accumValue = 0;
12
- const guest = new Guest((value: number) => {
13
- accumValue += value;
14
- });
15
- one.receiving(
16
- new GuestInTheMiddle(guest, (value) => {
17
- guest.receive(value + 3);
18
- }),
19
- );
20
-
21
- expect(accumValue).toBe(4);
22
- });
23
-
24
- test("test patron in the middle", () => {
25
- const one = new Source(1);
26
-
27
- let accumValue = 0;
28
- const guest = new Patron(
29
- new Guest((value: number) => {
30
- accumValue += value;
31
- }),
32
- );
33
- one.receiving(
34
- new GuestInTheMiddle(guest, (value) => {
35
- guest.receive(value + 3);
36
- }),
37
- );
38
- one.receive(3);
39
-
40
- setTimeout(() => {
41
- one.receive(3);
42
- });
43
-
44
- setTimeout(() => {
45
- expect(accumValue).toBe(16);
46
- });
47
- });
48
-
49
- test("test chain in the middle", () => {
50
- const one = new Source(1);
51
- const two = new Source(2);
52
- const chain = new Chain<{ one: number; two: number }>();
53
-
54
- one.receiving(new Patron(chain.receiveKey("one")));
55
- two.receiving(new Patron(chain.receiveKey("two")));
56
-
57
- one.receive(3);
58
- one.receive(4);
59
-
60
- const guest = new Patron(
61
- new Guest((value: { one: number; two: number; three: number }) => {
62
- expect(Object.values(value).length).toBe(3);
63
- }),
64
- );
65
-
66
- chain.result(
67
- new GuestInTheMiddle(guest, (value) => {
68
- guest.receive({ ...value, three: 99 });
69
- }),
70
- );
71
- });
@@ -1,11 +0,0 @@
1
- import { expect, test } from "vitest";
2
- import { Source } from "./Source";
3
- import { GuestSync } from "./GuestSync";
4
-
5
- test("guest sync", () => {
6
- const source = new Source(123);
7
- const syncGuest = new GuestSync(222);
8
- expect(syncGuest.value()).toBe(222);
9
- source.receiving(syncGuest);
10
- expect(syncGuest.value()).toBe(123);
11
- });
package/src/GuestType.ts DELETED
@@ -1,10 +0,0 @@
1
- type GuestIntroduction = "guest" | "patron";
2
-
3
- export interface ReceiveOptions {
4
- data?: unknown;
5
- }
6
-
7
- export interface GuestType<T = unknown> {
8
- receive(value: T, options?: ReceiveOptions): this;
9
- introduction?(): GuestIntroduction;
10
- }
@@ -1,5 +0,0 @@
1
- import { GuestType } from "./GuestType";
2
-
3
- export interface GuestValueType<T = unknown> extends GuestType<T> {
4
- value(): T;
5
- }
package/src/PoolType.ts DELETED
@@ -1,7 +0,0 @@
1
- import { GuestType } from "./GuestType";
2
-
3
- export interface PoolType<T = unknown> extends GuestType<T> {
4
- add(guest: GuestType<T>): this;
5
- distribute(receiving: T, possiblePatron: GuestType<T>): this;
6
- remove(patron: GuestType<T>): this;
7
- }
@@ -1,13 +0,0 @@
1
- import { expect, test } from "vitest";
2
- import { Source } from "./Source";
3
- import { Guest } from "./Guest";
4
-
5
- test("source", () => {
6
- const source = new Source(42);
7
-
8
- source.receiving(
9
- new Guest((value) => {
10
- expect(value).toBe(42);
11
- }),
12
- );
13
- });
package/src/SourceType.ts DELETED
@@ -1,4 +0,0 @@
1
- import { GuestAwareType } from "./GuestAwareType";
2
- import { GuestType } from "./GuestType";
3
-
4
- export type SourceType<T = unknown> = GuestAwareType<T> & GuestType<T>;