patron-oop 1.15.0 → 1.17.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/patron.cjs +376 -0
  3. package/dist/patron.cjs.map +1 -0
  4. package/dist/patron.d.ts +21 -27
  5. package/dist/patron.js +34 -48
  6. package/dist/patron.js.map +1 -1
  7. package/dist/patron.min.js +1 -1
  8. package/dist/patron.min.mjs +1 -1
  9. package/dist/patron.min.mjs.map +1 -1
  10. package/dist/patron.mjs +33 -30
  11. package/dist/patron.mjs.map +1 -1
  12. package/examples/elegant_objects.html +8 -8
  13. package/examples/reactive.html +6 -6
  14. package/package.json +1 -1
  15. package/rollup.config.js +6 -1
  16. package/src/Factory/Factory.test.ts +28 -26
  17. package/src/Factory/Factory.ts +15 -10
  18. package/src/Guest/Guest.test.ts +1 -1
  19. package/src/Guest/Guest.ts +6 -10
  20. package/src/Guest/GuestAware.test.ts +1 -1
  21. package/src/Guest/GuestAware.ts +2 -2
  22. package/src/Guest/GuestCast.test.ts +2 -2
  23. package/src/Guest/GuestCast.ts +3 -4
  24. package/src/Guest/GuestChain.test.ts +10 -10
  25. package/src/Guest/GuestChain.ts +7 -8
  26. package/src/Guest/GuestMiddle.test.ts +11 -11
  27. package/src/Guest/GuestMiddle.ts +4 -4
  28. package/src/Guest/GuestObject.test.ts +1 -1
  29. package/src/Guest/GuestObject.ts +3 -4
  30. package/src/Guest/GuestPool.test.ts +2 -2
  31. package/src/Guest/GuestPool.ts +5 -5
  32. package/src/Guest/GuestSync.test.ts +2 -2
  33. package/src/Guest/GuestSync.ts +1 -1
  34. package/src/Patron/Patron.test.ts +2 -2
  35. package/src/Patron/Patron.ts +2 -10
  36. package/src/Patron/PatronOnce.test.ts +4 -2
  37. package/src/Patron/PatronOnce.ts +2 -10
  38. package/src/Patron/PatronPool.test.ts +1 -1
  39. package/src/Patron/PatronPool.ts +6 -14
  40. package/src/Source/Source.test.ts +1 -1
  41. package/src/Source/Source.ts +3 -3
  42. package/src/Source/SourceEmpty.test.ts +3 -3
  43. package/src/Source/SourceEmpty.ts +4 -4
@@ -1,4 +1,4 @@
1
- import { give, GuestObjectType, GuestType, ReceiveOptions } from "./Guest";
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
- receive(value: T, options?: ReceiveOptions): this {
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.receiving(chain.receiveKey("one"));
16
- two.receiving(chain.receiveKey("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.receiving(chain.receiveKey("one"));
25
- two.receiving(chain.receiveKey("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.receiving(new Patron(chain.receiveKey("one")));
38
- two.receiving(new Patron(chain.receiveKey("two")));
37
+ one.value(new Patron(chain.receiveKey("one")));
38
+ two.value(new Patron(chain.receiveKey("two")));
39
39
 
40
- one.receive(3);
41
- one.receive(4);
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.receiving(new Patron(chain.receiveKey("0")));
56
- two.receiving(new Patron(chain.receiveKey("1")));
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) => {
@@ -31,13 +31,12 @@ export class GuestChain<T> implements ChainType<T> {
31
31
  ),
32
32
  );
33
33
  if (this.isChainFilled()) {
34
- this.theChain.receiving(
34
+ this.theChain.value(
35
35
  new Guest((chain: Record<string, unknown>) => {
36
- this.filledChainPool.receive(Object.values(chain));
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.receiving(
47
+ this.theChain.value(
49
48
  new Guest((chain) => {
50
- this.filledChainPool.receive(chain);
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.receiving(
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.receive(lastChain);
70
+ this.theChain.give(lastChain);
72
71
  if (this.isChainFilled()) {
73
- this.filledChainPool.receive(lastChain);
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.receiving(
15
+ one.value(
16
16
  new GuestMiddle(guest, (value: number) => {
17
- guest.receive(value + 3);
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.receiving(
33
+ one.value(
34
34
  new GuestMiddle(guest, (value: number) => {
35
- guest.receive(value + 3);
35
+ guest.give(value + 3);
36
36
  }),
37
37
  );
38
- one.receive(3);
38
+ one.give(3);
39
39
 
40
40
  setTimeout(() => {
41
- one.receive(3);
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.receiving(new Patron(chain.receiveKey("one")));
55
- two.receiving(new Patron(chain.receiveKey("two")));
54
+ one.value(new Patron(chain.receiveKey("one")));
55
+ two.value(new Patron(chain.receiveKey("two")));
56
56
 
57
- one.receive(3);
58
- one.receive(4);
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.receive({ ...value, three: 99 });
68
+ guest.give({ ...value, three: 99 });
69
69
  }),
70
70
  );
71
71
  });
@@ -1,19 +1,19 @@
1
- import { GuestObjectType, GuestType, ReceiveOptions } from "./Guest";
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?: ReceiveOptions) => void,
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
- receive(value: T, options?: ReceiveOptions): this {
16
+ public give(value: T, options?: GiveOptions): this {
17
17
  this.middleFn(value, options);
18
18
  return this;
19
19
  }
@@ -7,5 +7,5 @@ test("guest object", () => {
7
7
  const fnGuest = (value: number) => {
8
8
  expect(value).toBe(1);
9
9
  };
10
- source.receiving(new GuestObject(fnGuest));
10
+ source.value(new GuestObject(fnGuest));
11
11
  });
@@ -1,14 +1,14 @@
1
- import { Guest, GuestObjectType, GuestType, ReceiveOptions } from "./Guest";
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 receive(value: T, options?: ReceiveOptions): this {
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.receive(value, options);
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
  }
@@ -22,10 +22,10 @@ test("patron pool with guests", () => {
22
22
  pool.add((value) => {
23
23
  receivedCount += value;
24
24
  });
25
- pool.receive(2);
25
+ pool.give(2);
26
26
 
27
27
  setTimeout(() => {
28
- pool.receive(2);
28
+ pool.give(2);
29
29
  });
30
30
 
31
31
  setTimeout(() => {
@@ -1,6 +1,6 @@
1
1
  import { PatronPool } from "../Patron/PatronPool";
2
2
  import { PoolType } from "../Patron/PatronPool";
3
- import { give, GuestObjectType, GuestType, ReceiveOptions } from "./Guest";
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 receive(value: T, options?: ReceiveOptions): this {
14
+ public give(value: T, options?: GiveOptions): this {
15
15
  this.deliverToGuests(value, options);
16
- this.patronPool.receive(value, options);
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.receive(receiving);
40
+ this.give(receiving);
41
41
  return this;
42
42
  }
43
43
 
44
- private deliverToGuests(value: T, options?: ReceiveOptions) {
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.receive(222);
8
+ syncGuest.give(222);
9
9
  expect(syncGuest.value()).toBe(222);
10
- source.receiving(syncGuest);
10
+ source.value(syncGuest);
11
11
  expect(syncGuest.value()).toBe(123);
12
12
  });
@@ -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 receive(value: T): this {
10
+ public give(value: T): this {
11
11
  this.theValue = value;
12
12
  return this;
13
13
  }
@@ -8,8 +8,8 @@ test("patron always guest", () => {
8
8
  const patron = new Patron(() => {
9
9
  patronCalledTimes += 1;
10
10
  });
11
- one.receiving(patron);
12
- one.receive(2);
11
+ one.value(patron);
12
+ one.give(2);
13
13
 
14
14
  queueMicrotask(() => {
15
15
  expect(patronCalledTimes).toBe(2);
@@ -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 receive(value: T, options?: ReceiveOptions): this {
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.receiving(patron);
12
- source.receive(22);
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
  });
@@ -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 receive(value: T, options?: ReceiveOptions): this {
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
  }
@@ -17,5 +17,5 @@ test("patron pool", () => {
17
17
  expect(receivedCount).toBe(4);
18
18
  }),
19
19
  );
20
- pool.receive(2);
20
+ pool.give(2);
21
21
  });
@@ -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 receive: (value: T, options?: ReceiveOptions) => this;
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?: ReceiveOptions) => {
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.receive = (value: T, options?: ReceiveOptions) => {
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?: ReceiveOptions,
68
+ options?: GiveOptions,
77
69
  ) {
78
70
  give(value, guest, {
79
71
  ...options,
@@ -4,7 +4,7 @@ import { Source } from "./Source";
4
4
  test("source", () => {
5
5
  const source = new Source(42);
6
6
 
7
- source.receiving((value) => {
7
+ source.value((value) => {
8
8
  expect(value).toBe(42);
9
9
  });
10
10
  });
@@ -9,13 +9,13 @@ export class Source<T> implements SourceType<T> {
9
9
 
10
10
  public constructor(private sourceDocument: T) {}
11
11
 
12
- public receive(value: T): this {
12
+ public give(value: T): this {
13
13
  this.sourceDocument = value;
14
- this.pool.receive(this.sourceDocument);
14
+ this.pool.give(this.sourceDocument);
15
15
  return this;
16
16
  }
17
17
 
18
- public receiving(guest: GuestType<T>): this {
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.receiving(() => {
9
+ source.value(() => {
10
10
  accumulator += 100;
11
11
  });
12
12
 
13
13
  // Вызывается после прихода значения
14
- source.receive(200);
15
- source.receiving((value) => {
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 receiving(guest: GuestType<T>) {
9
- this.baseSource.receiving(
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 receive(value: T): this {
20
- this.baseSource.receive(value);
19
+ public give(value: T): this {
20
+ this.baseSource.give(value);
21
21
  return this;
22
22
  }
23
23
  }