patron-oop 1.43.0 → 1.45.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 (89) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/dist/patron.cjs +295 -275
  3. package/dist/patron.cjs.map +1 -1
  4. package/dist/patron.d.ts +181 -155
  5. package/dist/patron.js +283 -268
  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 +283 -268
  11. package/dist/patron.mjs.map +1 -1
  12. package/package.json +1 -1
  13. package/src/Guest/Guest.test.ts +11 -6
  14. package/src/Guest/Guest.ts +7 -14
  15. package/src/Guest/GuestApplied.test.ts +13 -0
  16. package/src/Guest/GuestApplied.ts +16 -0
  17. package/src/Guest/GuestCast.test.ts +4 -5
  18. package/src/Guest/GuestCast.ts +3 -10
  19. package/src/Guest/GuestDisposable.test.ts +13 -17
  20. package/src/Guest/GuestDisposable.ts +3 -3
  21. package/src/Guest/GuestExecutorApplied.test.ts +27 -0
  22. package/src/Guest/GuestExecutorApplied.ts +23 -0
  23. package/src/Guest/GuestObject.test.ts +6 -6
  24. package/src/Guest/GuestObject.ts +3 -3
  25. package/src/Guest/GuestPool.test.ts +5 -22
  26. package/src/Guest/GuestPool.ts +7 -8
  27. package/src/Guest/GuestSync.test.ts +2 -2
  28. package/src/Patron/Patron.test.ts +4 -5
  29. package/src/Patron/Patron.ts +11 -3
  30. package/src/Patron/PatronOnce.sourceEmpty.test.ts +2 -2
  31. package/src/Patron/PatronOnce.test.ts +2 -2
  32. package/src/Patron/PatronOnce.ts +3 -9
  33. package/src/Patron/PatronPool.test.ts +2 -1
  34. package/src/Patron/PatronPool.ts +22 -27
  35. package/src/Private/PrivateClass.modules.test.ts +11 -8
  36. package/src/Private/PrivateClass.test.ts +7 -6
  37. package/src/Source/Source.test.ts +9 -5
  38. package/src/Source/Source.ts +46 -30
  39. package/src/Source/SourceActive.test.ts +13 -0
  40. package/src/Source/SourceActive.ts +41 -0
  41. package/src/Source/SourceAll._asArray.test.ts +22 -0
  42. package/src/Source/SourceAll._twoValuesAfter.test.ts +19 -0
  43. package/src/Source/SourceAll._twoValuesBefore.test.ts +19 -0
  44. package/src/Source/SourceAll._withPatron.test.ts +25 -0
  45. package/src/{Guest/GuestAwareAll.ts → Source/SourceAll.ts} +11 -11
  46. package/src/Source/SourceApplied.test.ts +14 -0
  47. package/src/Source/SourceApplied.ts +23 -0
  48. package/src/Source/SourceDynamic.ofSource.test.ts +12 -12
  49. package/src/Source/SourceDynamic.test.ts +9 -9
  50. package/src/Source/SourceDynamic.ts +7 -7
  51. package/src/Source/SourceExecutorApplied.test.ts +30 -0
  52. package/src/Source/SourceExecutorApplied.ts +22 -0
  53. package/src/Source/SourceMap.defered.test.ts +46 -0
  54. package/src/Source/SourceMap.fn.test.ts +27 -0
  55. package/src/Source/SourceMap.test.ts +32 -0
  56. package/src/Source/SourceMap.ts +48 -0
  57. package/src/Source/SourceOnce.notcalled.test.ts +13 -0
  58. package/src/Source/SourceOnce.test.ts +10 -0
  59. package/src/Source/SourceOnce.ts +31 -0
  60. package/src/Source/SourceRace.test.ts +46 -0
  61. package/src/Source/SourceRace.ts +30 -0
  62. package/src/Source/SourceSequence.defered.test.ts +53 -0
  63. package/src/Source/SourceSequence.test.ts +30 -0
  64. package/src/{Guest/GuestAwareSequence.ts → Source/SourceSequence.ts} +14 -19
  65. package/src/Source/SourceWithPool.empty.test.ts +14 -0
  66. package/src/Source/SourceWithPool.test.ts +10 -0
  67. package/src/Source/SourceWithPool.ts +58 -0
  68. package/src/index.ts +9 -7
  69. package/test-utils/debounce.ts +11 -0
  70. package/test-utils/id.ts +1 -0
  71. package/test-utils/wait.ts +6 -3
  72. package/src/Guest/GuestAware.test.ts +0 -13
  73. package/src/Guest/GuestAware.ts +0 -59
  74. package/src/Guest/GuestAwareActive.test.ts +0 -12
  75. package/src/Guest/GuestAwareActive.ts +0 -42
  76. package/src/Guest/GuestAwareAll._asArray.test.ts +0 -19
  77. package/src/Guest/GuestAwareAll._twoValuesAfter.test.ts +0 -16
  78. package/src/Guest/GuestAwareAll._twoValuesBefore.test.ts +0 -16
  79. package/src/Guest/GuestAwareAll._withPatron.test.ts +0 -22
  80. package/src/Guest/GuestAwareMap.defered.test.ts +0 -46
  81. package/src/Guest/GuestAwareMap.fn.test.ts +0 -27
  82. package/src/Guest/GuestAwareMap.test.ts +0 -29
  83. package/src/Guest/GuestAwareMap.ts +0 -48
  84. package/src/Guest/GuestAwareRace.test.ts +0 -46
  85. package/src/Guest/GuestAwareRace.ts +0 -33
  86. package/src/Guest/GuestAwareSequence.defered.test.ts +0 -58
  87. package/src/Guest/GuestAwareSequence.test.ts +0 -30
  88. package/src/Source/SourceEmpty.test.ts +0 -20
  89. package/src/Source/SourceEmpty.ts +0 -31
@@ -1,17 +1,16 @@
1
1
  import { expect, test } from "vitest";
2
2
  import { Patron } from "./Patron";
3
- import { Source } from "../Source/Source";
3
+ import { SourceWithPool } from "../Source/SourceWithPool";
4
4
 
5
5
  test("Patron.test", () => {
6
- const one = new Source(1);
6
+ const one = new SourceWithPool(1);
7
7
  let patronCalledTimes = 0;
8
8
  const patron = new Patron(() => {
9
9
  patronCalledTimes += 1;
10
10
  });
11
+
11
12
  one.value(patron);
12
13
  one.give(2);
13
14
 
14
- queueMicrotask(() => {
15
- expect(patronCalledTimes).toBe(2);
16
- });
15
+ expect(patronCalledTimes).toBe(2);
17
16
  });
@@ -1,5 +1,5 @@
1
+ import { give, GuestType } from "../Guest/Guest";
1
2
  import { GuestDisposableType } from "../Guest/GuestDisposable";
2
- import { give, GiveOptions, GuestType } from "../Guest/Guest";
3
3
 
4
4
  /**
5
5
  * @url https://kosukhin.github.io/patron.site/#/patron
@@ -15,8 +15,8 @@ export class Patron<T> implements GuestDisposableType<T> {
15
15
  return "patron" as const;
16
16
  }
17
17
 
18
- public give(value: T, options?: GiveOptions): this {
19
- give(value, this.willBePatron, options);
18
+ public give(value: T): this {
19
+ give(value, this.willBePatron);
20
20
  return this;
21
21
  }
22
22
 
@@ -25,3 +25,11 @@ export class Patron<T> implements GuestDisposableType<T> {
25
25
  return maybeDisposable?.disposed?.(value) || false;
26
26
  }
27
27
  }
28
+
29
+ /**
30
+ * @url https://kosukhin.github.io/patron.site/#/utils/is-patron
31
+ */
32
+ export const isPatron = (guest: GuestType): guest is Patron<unknown> =>
33
+ typeof guest === "object" &&
34
+ guest !== null &&
35
+ guest?.introduction?.() === "patron";
@@ -1,6 +1,6 @@
1
1
  import { afterEach, beforeEach, expect, test, vi } from "vitest";
2
2
  import { PatronOnce } from "./PatronOnce";
3
- import { SourceEmpty } from "../Source/SourceEmpty";
3
+ import { SourceWithPool } from "../Source/SourceWithPool";
4
4
 
5
5
  beforeEach(() => {
6
6
  vi.useFakeTimers({ shouldAdvanceTime: true });
@@ -19,7 +19,7 @@ const wait = (ms: number) =>
19
19
  });
20
20
 
21
21
  test("PatronOnce.sourceEmpty.test", async () => {
22
- const source = new SourceEmpty();
22
+ const source = new SourceWithPool();
23
23
  let calls = 0;
24
24
  const patron = new PatronOnce(() => {
25
25
  calls += 1;
@@ -1,6 +1,6 @@
1
1
  import { wait } from "./../../test-utils/wait";
2
2
  import { afterEach, beforeEach, expect, test, vi } from "vitest";
3
- import { Source } from "../Source/Source";
3
+ import { SourceWithPool } from "../Source/SourceWithPool";
4
4
  import { PatronOnce } from "./PatronOnce";
5
5
 
6
6
  beforeEach(() => {
@@ -13,7 +13,7 @@ afterEach(() => {
13
13
  });
14
14
 
15
15
  test("PatronOnce.test", async () => {
16
- const source = new Source(12);
16
+ const source = new SourceWithPool(12);
17
17
  let calls = 0;
18
18
  const patron = new PatronOnce(() => {
19
19
  calls += 1;
@@ -1,15 +1,9 @@
1
- import { PoolType } from "./PatronPool";
2
- import { give, GuestType, GiveOptions, GuestObjectType } from "../Guest/Guest";
1
+ import { give, GuestType } from "../Guest/Guest";
3
2
  import {
4
3
  GuestDisposableType,
5
4
  MaybeDisposableType,
6
5
  } from "../Guest/GuestDisposable";
7
6
 
8
- export type PoolAwareOptions = {
9
- pool?: PoolType;
10
- castedGuest?: GuestObjectType;
11
- };
12
-
13
7
  /**
14
8
  * @url https://kosukhin.github.io/patron.site/#/patron/patron-once
15
9
  */
@@ -26,10 +20,10 @@ export class PatronOnce<T> implements GuestDisposableType<T> {
26
20
  return "patron" as const;
27
21
  }
28
22
 
29
- public give(value: T, options?: GiveOptions): this {
23
+ public give(value: T): this {
30
24
  if (!this.received) {
31
25
  this.received = true;
32
- give(value, this.baseGuest, options);
26
+ give(value, this.baseGuest);
33
27
  }
34
28
  return this;
35
29
  }
@@ -14,8 +14,9 @@ test("PatronPool.test", () => {
14
14
  pool.add(
15
15
  new Patron((value) => {
16
16
  receivedCount += value;
17
- expect(receivedCount).toBe(4);
18
17
  }),
19
18
  );
20
19
  pool.give(2);
20
+
21
+ expect(receivedCount).toBe(4);
21
22
  });
@@ -1,8 +1,21 @@
1
+ import { give, GuestObjectType, GuestType } from "../Guest/Guest";
1
2
  import { GuestDisposableType } from "../Guest/GuestDisposable";
2
- import { give, GuestObjectType, GuestType, GiveOptions } from "../Guest/Guest";
3
3
 
4
4
  const poolSets = new Map<PoolType, Set<GuestObjectType>>();
5
5
 
6
+ /**
7
+ * @url https://kosukhin.github.io/patron.site/#/utils/patron-pools
8
+ */
9
+ export const patronPools = (patron: GuestObjectType) => {
10
+ const pools: PoolType[] = [];
11
+ poolSets.forEach((pool, poolInstance) => {
12
+ if (pool.has(patron)) {
13
+ pools.push(poolInstance);
14
+ }
15
+ });
16
+ return pools;
17
+ };
18
+
6
19
  /**
7
20
  * @url https://kosukhin.github.io/patron.site/#/utils/remove-patron-from-pools
8
21
  */
@@ -44,25 +57,18 @@ export interface PoolType<T = any> extends GuestObjectType<T> {
44
57
  export class PatronPool<T> implements PoolType<T> {
45
58
  private patrons: Set<GuestObjectType<T>>;
46
59
 
47
- public give: (value: T, options?: GiveOptions) => this;
60
+ public give: (value: T) => this;
48
61
 
49
62
  public constructor(private initiator: unknown) {
50
63
  this.patrons = new Set<GuestObjectType<T>>();
51
64
  poolSets.set(this, this.patrons);
52
- let lastMicrotask: (() => void) | null = null;
53
- const doReceive = (value: T, options?: GiveOptions) => {
65
+ const doReceive = (value: T) => {
54
66
  this.patrons.forEach((target) => {
55
- this.sendValueToGuest(value, target, options);
67
+ this.sendValueToGuest(value, target);
56
68
  });
57
69
  };
58
- this.give = (value: T, options?: GiveOptions) => {
59
- const currentMicroTask = () => {
60
- if (currentMicroTask === lastMicrotask) {
61
- doReceive(value, options);
62
- }
63
- };
64
- lastMicrotask = currentMicroTask;
65
- queueMicrotask(currentMicroTask);
70
+ this.give = (value: T) => {
71
+ doReceive(value);
66
72
  return this;
67
73
  };
68
74
  }
@@ -92,26 +98,15 @@ export class PatronPool<T> implements PoolType<T> {
92
98
 
93
99
  public distribute(receiving: T, possiblePatron: GuestType<T>): this {
94
100
  this.add(possiblePatron);
95
- this.sendValueToGuest(receiving, possiblePatron, {});
101
+ this.sendValueToGuest(receiving, possiblePatron);
96
102
  return this;
97
103
  }
98
104
 
99
- private sendValueToGuest(
100
- value: T,
101
- guest: GuestType<T>,
102
- options?: GiveOptions,
103
- ) {
105
+ private sendValueToGuest(value: T, guest: GuestType<T>) {
104
106
  const isDisposed = this.guestDisposed(value, guest);
105
107
 
106
108
  if (!isDisposed) {
107
- give(value, guest, {
108
- ...options,
109
- data: {
110
- ...((options?.data as Record<string, unknown>) ?? {}),
111
- initiator: this.initiator,
112
- pool: this,
113
- },
114
- });
109
+ give(value, guest);
115
110
  }
116
111
  }
117
112
 
@@ -1,15 +1,15 @@
1
1
  import { PrivateClass } from "./PrivateClass";
2
2
  import { GuestType } from "../Guest/Guest";
3
- import { Source, SourceType } from "../Source/Source";
4
- import { expect, test } from "vitest";
3
+ import { SourceWithPool, SourceWithPoolType } from "../Source/SourceWithPool";
4
+ import { expect, test, vitest } from "vitest";
5
5
  import { PrivateType } from "./Private";
6
6
 
7
7
  class TestClass {
8
- private source: SourceType;
8
+ private source: SourceWithPoolType;
9
9
 
10
10
  public constructor(
11
11
  baseNum: number,
12
- modules: { main: PrivateType<SourceType> },
12
+ modules: { main: PrivateType<SourceWithPoolType> },
13
13
  ) {
14
14
  this.source = modules.main.get(baseNum + 55);
15
15
  }
@@ -21,13 +21,16 @@ class TestClass {
21
21
  }
22
22
 
23
23
  test("PrivateClass.modules.test", () => {
24
- const main = new PrivateClass(Source);
24
+ const main = new PrivateClass(SourceWithPool);
25
25
  const testSource = new PrivateClass(TestClass, {
26
26
  main,
27
27
  });
28
28
 
29
29
  const source = testSource.get(42);
30
- source.value((value) => {
31
- expect(value).toBe(97);
32
- });
30
+
31
+ const guest = vitest.fn();
32
+ source.value(guest);
33
+
34
+ expect(guest).toBeCalled();
35
+ expect(guest).toBeCalledWith(97);
33
36
  });
@@ -1,12 +1,13 @@
1
- import { expect, test } from "vitest";
2
- import { Source } from "../Source/Source";
1
+ import { expect, test, vitest } from "vitest";
2
+ import { SourceWithPool } from "../Source/SourceWithPool";
3
3
  import { PrivateClass } from "./PrivateClass";
4
4
 
5
5
  test("PrivateClass.test", () => {
6
- const sourcePrivate = new PrivateClass(Source);
6
+ const sourcePrivate = new PrivateClass(SourceWithPool);
7
7
  const source = sourcePrivate.get(42);
8
8
 
9
- source.value((value) => {
10
- expect(value).toBe(42);
11
- });
9
+ const guest = vitest.fn();
10
+ source.value(guest);
11
+
12
+ expect(guest).toBeCalledWith(42);
12
13
  });
@@ -1,10 +1,14 @@
1
- import { expect, test } from "vitest";
1
+ import { expect, test, vitest } from "vitest";
2
2
  import { Source } from "./Source";
3
+ import { give } from "../Guest/Guest";
3
4
 
4
5
  test("Source.test", () => {
5
- const source = new Source(42);
6
-
7
- source.value((value) => {
8
- expect(value).toBe(42);
6
+ const aware = new Source((guest) => {
7
+ give(111, guest);
9
8
  });
9
+
10
+ const guest = vitest.fn();
11
+ aware.value(guest);
12
+
13
+ expect(guest).toBeCalledWith(111);
10
14
  });
@@ -1,43 +1,59 @@
1
- import { Guest, GuestObjectType, GuestType } from "../Guest/Guest";
2
- import { GuestAwareObjectType } from "../Guest/GuestAware";
3
- import { PatronPool } from "../Patron/PatronPool";
1
+ import { give, GuestType } from "../Guest/Guest";
4
2
 
5
- export interface PoolAware<T = any> {
6
- pool(): PatronPool<T>;
3
+ export type SourceExecutorType<T> = (guest: GuestType<T>) => unknown;
4
+
5
+ export interface SourceObjectType<T> {
6
+ value: SourceExecutorType<T>;
7
7
  }
8
8
 
9
+ export type SourceType<T = any> = SourceExecutorType<T> | SourceObjectType<T>;
10
+
9
11
  /**
10
- * @url https://kosukhin.github.io/patron.site/#/source
12
+ * @url https://kosukhin.github.io/patron.site/#/utils/value
11
13
  */
12
- export type SourceType<T = any> = GuestAwareObjectType<T> &
13
- GuestObjectType<T> &
14
- PoolAware<T>;
15
-
16
- export class Source<T> implements SourceType<T> {
17
- private thePool = new PatronPool(this);
18
-
19
- public constructor(private sourceDocument: T) {
20
- if (sourceDocument === undefined) {
21
- throw new Error("Source didnt receive sourceDocument argument");
22
- }
14
+ export function value<T>(source: SourceType<T>, guest: GuestType<T>) {
15
+ if (source === undefined) {
16
+ throw new Error("value didnt receive source argument");
23
17
  }
24
-
25
- public pool() {
26
- return this.thePool;
18
+ if (guest === undefined) {
19
+ throw new Error("value didnt receive guest argument");
20
+ }
21
+ if (typeof source === "function") {
22
+ return source(guest);
23
+ } else {
24
+ return source.value(guest);
27
25
  }
26
+ }
28
27
 
29
- public give(value: T): this {
30
- this.sourceDocument = value;
31
- this.thePool.give(this.sourceDocument);
32
- return this;
28
+ /**
29
+ * @url https://kosukhin.github.io/patron.site/#/utils/is-source
30
+ */
31
+ export function isSource(mbSource: any): mbSource is SourceType {
32
+ if (mbSource === undefined) {
33
+ throw new Error("isSource didnt receive mbSource argument");
33
34
  }
35
+ return (
36
+ typeof mbSource === "function" || typeof mbSource?.value === "function"
37
+ );
38
+ }
34
39
 
35
- public value(guest: GuestType<T>): this {
36
- if (typeof guest === "function") {
37
- this.thePool.distribute(this.sourceDocument, new Guest(guest));
38
- } else {
39
- this.thePool.distribute(this.sourceDocument, guest);
40
+ /**
41
+ * @url https://kosukhin.github.io/patron.site/#/guest/source
42
+ */
43
+ export class Source<T = any> implements SourceObjectType<T> {
44
+ public constructor(private source: SourceType<T>) {
45
+ if (source === undefined) {
46
+ throw new Error("Source constructor didnt receive executor function");
40
47
  }
41
- return this;
48
+ }
49
+
50
+ public value(guest: GuestType<T>): GuestType<T> {
51
+ value(this.source, guest);
52
+ return guest;
42
53
  }
43
54
  }
55
+
56
+ /**
57
+ * @url https://kosukhin.github.io/patron.site/#/utils/source-of
58
+ */
59
+ export const sourceOf = <T>(value: T) => new Source<T>((g) => give(value, g));
@@ -0,0 +1,13 @@
1
+ import { SourceActive } from "./SourceActive";
2
+ import { expect, test, vitest } from "vitest";
3
+
4
+ test("SourceActive.test", () => {
5
+ const active = new SourceActive<number, number>((config, source) => {
6
+ source.give(config * 3);
7
+ });
8
+
9
+ const guest = vitest.fn();
10
+ active.do(4).value(guest);
11
+
12
+ expect(guest).toBeCalledWith(12);
13
+ });
@@ -0,0 +1,41 @@
1
+ import { GuestType } from "../Guest/Guest";
2
+ import { SourceObjectType } from "./Source";
3
+ import { SourceWithPool, SourceWithPoolType } from "./SourceWithPool";
4
+
5
+ /**
6
+ * @url https://kosukhin.github.io/patron.site/#/utils/action-type
7
+ */
8
+ export interface ActionType<P = any> {
9
+ do(config: P): this;
10
+ }
11
+
12
+ export interface SourceAcitveType<R = unknown, T = unknown>
13
+ extends SourceObjectType<T>,
14
+ ActionType<R> {}
15
+
16
+ /**
17
+ * @url https://kosukhin.github.io/patron.site/#/guest/source-active
18
+ */
19
+ export class SourceActive<R, T> implements SourceAcitveType<R, T> {
20
+ private source = new SourceWithPool<T>();
21
+
22
+ public constructor(
23
+ private configExecutor: (config: R, source: SourceWithPoolType<T>) => void,
24
+ ) {
25
+ if (configExecutor === undefined) {
26
+ throw new Error(
27
+ "SourceActive constructor didnt receive executor function",
28
+ );
29
+ }
30
+ }
31
+
32
+ public do(config: R): this {
33
+ this.configExecutor(config, this.source);
34
+ return this;
35
+ }
36
+
37
+ public value(guest: GuestType<T>): this {
38
+ this.source.value(guest);
39
+ return this;
40
+ }
41
+ }
@@ -0,0 +1,22 @@
1
+ import { expect, test, vitest } from "vitest";
2
+ import { SourceAll } from "./SourceAll";
3
+ import { SourceWithPool } from "./SourceWithPool";
4
+ import { Patron } from "../Patron/Patron";
5
+
6
+ test("SourceAll._asArray.test", () => {
7
+ const one = new SourceWithPool(1);
8
+ const two = new SourceWithPool(2);
9
+ const all = new SourceAll<[number, number]>();
10
+
11
+ one.value(new Patron(all.guestKey("0")));
12
+ two.value(new Patron(all.guestKey("1")));
13
+
14
+ const guest = vitest.fn();
15
+ all.valueArray(
16
+ new Patron((value) => {
17
+ guest(JSON.stringify(value));
18
+ }),
19
+ );
20
+
21
+ expect(guest).toBeCalledWith("[1,2]");
22
+ });
@@ -0,0 +1,19 @@
1
+ import { expect, test, vitest } from "vitest";
2
+ import { SourceWithPool } from "./SourceWithPool";
3
+ import { SourceAll } from "./SourceAll";
4
+
5
+ test("SourceAll._twoValuesAfter.test", () => {
6
+ const one = new SourceWithPool(1);
7
+ const two = new SourceWithPool(2);
8
+ const all = new SourceAll<{ one: number; two: number }>(["one", "two"]);
9
+
10
+ const g = vitest.fn();
11
+ all.value((value) => {
12
+ g(Object.values(value).join());
13
+ });
14
+
15
+ one.value(all.guestKey("one"));
16
+ two.value(all.guestKey("two"));
17
+
18
+ expect(g).toBeCalledWith("1,2");
19
+ });
@@ -0,0 +1,19 @@
1
+ import { expect, test, vitest } from "vitest";
2
+ import { SourceWithPool } from "./SourceWithPool";
3
+ import { SourceAll } from "./SourceAll";
4
+
5
+ test("SourceAll._twoValuesBefore.test", () => {
6
+ const one = new SourceWithPool(1);
7
+ const two = new SourceWithPool(2);
8
+ const all = new SourceAll<{ one: number; two: number }>();
9
+
10
+ one.value(all.guestKey("one"));
11
+ two.value(all.guestKey("two"));
12
+
13
+ const g = vitest.fn();
14
+ all.value((value) => {
15
+ g(Object.values(value).join());
16
+ });
17
+
18
+ expect(g).toBeCalledWith("1,2");
19
+ });
@@ -0,0 +1,25 @@
1
+ import { expect, test, vitest } from "vitest";
2
+ import { SourceAll } from "./SourceAll";
3
+ import { SourceWithPool } from "./SourceWithPool";
4
+ import { Patron } from "../Patron/Patron";
5
+
6
+ test("SourceAll._withPatron.test", () => {
7
+ const one = new SourceWithPool(1);
8
+ const two = new SourceWithPool(2);
9
+ const all = new SourceAll<{ one: number; two: number }>();
10
+
11
+ one.value(new Patron(all.guestKey("one")));
12
+ two.value(new Patron(all.guestKey("two")));
13
+
14
+ one.give(3);
15
+ one.give(4);
16
+
17
+ const g = vitest.fn();
18
+ all.value(
19
+ new Patron((value: Record<string, unknown>) => {
20
+ g(Object.values(value).length);
21
+ }),
22
+ );
23
+
24
+ expect(g).toBeCalledWith(2);
25
+ });
@@ -1,20 +1,20 @@
1
- import { GuestAwareObjectType } from "./GuestAware";
2
- import { Source } from "../Source/Source";
3
- import { Guest, GuestObjectType, GuestType } from "./Guest";
4
- import { GuestCast } from "./GuestCast";
5
- import { GuestObject } from "./GuestObject";
6
- import { GuestPool } from "./GuestPool";
1
+ import { SourceObjectType } from "./Source";
2
+ import { SourceWithPool } from "./SourceWithPool";
3
+ import { Guest, GuestObjectType, GuestType } from "../Guest/Guest";
4
+ import { GuestCast } from "../Guest/GuestCast";
5
+ import { GuestObject } from "../Guest/GuestObject";
6
+ import { GuestPool } from "../Guest/GuestPool";
7
7
 
8
- export interface GuestAwareAllType<T = any> extends GuestAwareObjectType<T> {
8
+ export interface SourceAllType<T = any> extends SourceObjectType<T> {
9
9
  valueArray(guest: GuestObjectType<T>): this;
10
10
  guestKey<R>(key: string): GuestObjectType<R>;
11
11
  }
12
12
 
13
13
  /**
14
- * @url https://kosukhin.github.io/patron.site/#/guest/guest-aware-all
14
+ * @url https://kosukhin.github.io/patron.site/#/guest/source-all
15
15
  */
16
- export class GuestAwareAll<T> implements GuestAwareAllType<T> {
17
- private theAll: Source<Record<string, unknown>>;
16
+ export class SourceAll<T> implements SourceAllType<T> {
17
+ private theAll: SourceWithPool<Record<string, unknown>>;
18
18
 
19
19
  private keysKnown: Set<string>;
20
20
 
@@ -23,7 +23,7 @@ export class GuestAwareAll<T> implements GuestAwareAllType<T> {
23
23
  private filledAllPool = new GuestPool(this);
24
24
 
25
25
  public constructor(initialKnownKeys: string[] = []) {
26
- this.theAll = new Source<Record<string, unknown>>({});
26
+ this.theAll = new SourceWithPool<Record<string, unknown>>({});
27
27
  this.keysKnown = new Set(initialKnownKeys);
28
28
  }
29
29
 
@@ -0,0 +1,14 @@
1
+ import { GuestSync } from "../Guest/GuestSync";
2
+ import { SourceApplied } from "../Source/SourceApplied";
3
+ import { SourceWithPool } from "../Source/SourceWithPool";
4
+ import { expect, test } from "vitest";
5
+
6
+ test("SourceApplied.test", () => {
7
+ const source = new SourceWithPool(1);
8
+ const sourceDouble = new SourceApplied(source, (x) => x * 2);
9
+ const guest = new GuestSync(0);
10
+
11
+ sourceDouble.value(guest);
12
+
13
+ expect(guest.value()).toBe(2);
14
+ });
@@ -0,0 +1,23 @@
1
+ import { give, GuestType } from "../Guest/Guest";
2
+ import { GuestCast } from "../Guest/GuestCast";
3
+ import { SourceObjectType, SourceType, value } from "../Source/Source";
4
+
5
+ /**
6
+ * @url https://kosukhin.github.io/patron.site/#/source/source-applied
7
+ */
8
+ export class SourceApplied<T, R> implements SourceObjectType<R> {
9
+ public constructor(
10
+ private baseSource: SourceType<T>,
11
+ private applier: (v: T) => R,
12
+ ) {}
13
+
14
+ public value(g: GuestType<R>) {
15
+ value(
16
+ this.baseSource,
17
+ new GuestCast(g, (v) => {
18
+ give(this.applier(v), g);
19
+ }),
20
+ );
21
+ return this;
22
+ }
23
+ }
@@ -1,21 +1,21 @@
1
1
  import { SourceDynamic } from "./SourceDynamic";
2
- import { Source } from "./Source";
3
- import { expect, test } from "vitest";
2
+ import { SourceWithPool } from "./SourceWithPool";
3
+ import { expect, test, vitest } from "vitest";
4
4
 
5
5
  test("SourceDynamic.ofSource.test", () => {
6
- const source = new Source(1);
6
+ const source = new SourceWithPool(1);
7
7
  const sourceDynamic = new SourceDynamic(source, source);
8
8
 
9
- sourceDynamic.value((value) => {
10
- expect(value).toBe(1);
11
- });
9
+ const g1 = vitest.fn();
10
+ sourceDynamic.value(g1);
11
+ expect(g1).toBeCalledWith(1);
12
12
 
13
13
  sourceDynamic.give(2);
14
14
 
15
- sourceDynamic.value((value) => {
16
- expect(value).toBe(2);
17
- });
18
- source.value((value) => {
19
- expect(value).toBe(2);
20
- });
15
+ const g2 = vitest.fn();
16
+ sourceDynamic.value(g2);
17
+ expect(g2).toBeCalledWith(2);
18
+ const g3 = vitest.fn();
19
+ source.value(g3);
20
+ expect(g3).toBeCalledWith(2);
21
21
  });