patron-oop 1.44.1 → 1.46.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 (216) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/dist/patron.cjs +359 -268
  3. package/dist/patron.cjs.map +1 -1
  4. package/dist/patron.d.ts +215 -150
  5. package/dist/patron.js +343 -261
  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 +343 -261
  11. package/dist/patron.mjs.map +1 -1
  12. package/docs/README.md +3 -0
  13. package/docs/assets/css/base.css +42 -0
  14. package/docs/assets/favicon/android-icon-144x144.png +0 -0
  15. package/docs/assets/favicon/android-icon-192x192.png +0 -0
  16. package/docs/assets/favicon/android-icon-36x36.png +0 -0
  17. package/docs/assets/favicon/android-icon-48x48.png +0 -0
  18. package/docs/assets/favicon/android-icon-72x72.png +0 -0
  19. package/docs/assets/favicon/android-icon-96x96.png +0 -0
  20. package/docs/assets/favicon/apple-icon-114x114.png +0 -0
  21. package/docs/assets/favicon/apple-icon-120x120.png +0 -0
  22. package/docs/assets/favicon/apple-icon-144x144.png +0 -0
  23. package/docs/assets/favicon/apple-icon-152x152.png +0 -0
  24. package/docs/assets/favicon/apple-icon-180x180.png +0 -0
  25. package/docs/assets/favicon/apple-icon-57x57.png +0 -0
  26. package/docs/assets/favicon/apple-icon-60x60.png +0 -0
  27. package/docs/assets/favicon/apple-icon-72x72.png +0 -0
  28. package/docs/assets/favicon/apple-icon-76x76.png +0 -0
  29. package/docs/assets/favicon/apple-icon-precomposed.png +0 -0
  30. package/docs/assets/favicon/apple-icon.png +0 -0
  31. package/docs/assets/favicon/browserconfig.xml +2 -0
  32. package/docs/assets/favicon/favicon-16x16.png +0 -0
  33. package/docs/assets/favicon/favicon-32x32.png +0 -0
  34. package/docs/assets/favicon/favicon-96x96.png +0 -0
  35. package/docs/assets/favicon/favicon.ico +0 -0
  36. package/docs/assets/favicon/manifest.json +41 -0
  37. package/docs/assets/favicon/ms-icon-144x144.png +0 -0
  38. package/docs/assets/favicon/ms-icon-150x150.png +0 -0
  39. package/docs/assets/favicon/ms-icon-310x310.png +0 -0
  40. package/docs/assets/favicon/ms-icon-70x70.png +0 -0
  41. package/docs/assets/img/404.jpg +0 -0
  42. package/docs/assets/img/angular_16.jpg +0 -0
  43. package/docs/assets/img/angular_32.jpg +0 -0
  44. package/docs/assets/img/eo_16.jpg +0 -0
  45. package/docs/assets/img/eo_big.png +0 -0
  46. package/docs/assets/img/github_16.jpg +0 -0
  47. package/docs/assets/img/logo.jpg +0 -0
  48. package/docs/assets/img/philosofy/observable.jpg +0 -0
  49. package/docs/assets/img/philosofy/responsible.jpg +0 -0
  50. package/docs/assets/img/philosofy/result.jpg +0 -0
  51. package/docs/assets/img/philosofy/tell.jpg +0 -0
  52. package/docs/assets/img/react_16.jpg +0 -0
  53. package/docs/assets/img/react_32.jpg +0 -0
  54. package/docs/assets/img/vue_16.jpg +0 -0
  55. package/docs/assets/img/vue_32.jpg +0 -0
  56. package/docs/assets/js/components/linkDynamic.mjs +11 -0
  57. package/docs/assets/js/components/linkReloadable.mjs +13 -0
  58. package/docs/assets/js/entrypoints/compatibility.mjs +31 -0
  59. package/docs/assets/js/entrypoints/components.mjs +2 -0
  60. package/docs/assets/js/entrypoints/examples.mjs +25 -0
  61. package/docs/assets/js/entrypoints/factory.mjs +20 -0
  62. package/docs/assets/js/entrypoints/guest.mjs +61 -0
  63. package/docs/assets/js/entrypoints/index.mjs +110 -0
  64. package/docs/assets/js/entrypoints/integrations.mjs +36 -0
  65. package/docs/assets/js/entrypoints/patron.mjs +46 -0
  66. package/docs/assets/js/entrypoints/source.mjs +76 -0
  67. package/docs/assets/js/entrypoints/terminology.mjs +51 -0
  68. package/docs/assets/js/entrypoints/utils.mjs +61 -0
  69. package/docs/assets/js/lib/StyleFetched.mjs +14 -0
  70. package/docs/assets/js/routing.mjs +110 -0
  71. package/docs/assets/schemes/dip.html +28 -0
  72. package/docs/favicon.ico +0 -0
  73. package/docs/index-dev.html +346 -0
  74. package/docs/index.html +346 -0
  75. package/docs/manifest.json +41 -0
  76. package/docs/pages/404.html +9 -0
  77. package/docs/pages/common/layout.html +17 -0
  78. package/docs/pages/compatibility/dip.html +82 -0
  79. package/docs/pages/compatibility/elegant-objects.html +373 -0
  80. package/docs/pages/compatibility/layout.html +17 -0
  81. package/docs/pages/examples/errors.html +167 -0
  82. package/docs/pages/examples/index.html +10 -0
  83. package/docs/pages/examples.html +41 -0
  84. package/docs/pages/factory/index.html +18 -0
  85. package/docs/pages/factory.html +36 -0
  86. package/docs/pages/guest/guest-applied.html +51 -0
  87. package/docs/pages/guest/guest-cast.html +219 -0
  88. package/docs/pages/guest/guest-disposable.html +147 -0
  89. package/docs/pages/guest/guest-executor-applied.html +65 -0
  90. package/docs/pages/guest/guest-object.html +101 -0
  91. package/docs/pages/guest/guest-pool.html +107 -0
  92. package/docs/pages/guest/guest-sync.html +66 -0
  93. package/docs/pages/guest/index.html +115 -0
  94. package/docs/pages/guest.html +59 -0
  95. package/docs/pages/index.html +72 -0
  96. package/docs/pages/integrations/angular.html +5 -0
  97. package/docs/pages/integrations/layout.html +17 -0
  98. package/docs/pages/integrations/react.html +7 -0
  99. package/docs/pages/integrations/vue.html +102 -0
  100. package/docs/pages/patron/index.html +140 -0
  101. package/docs/pages/patron/patron-applied.html +66 -0
  102. package/docs/pages/patron/patron-executor-applied.html +94 -0
  103. package/docs/pages/patron/patron-once.html +110 -0
  104. package/docs/pages/patron/patron-pool.html +99 -0
  105. package/docs/pages/patron.html +50 -0
  106. package/docs/pages/philosofy.html +210 -0
  107. package/docs/pages/source/index.html +167 -0
  108. package/docs/pages/source/source-active.html +113 -0
  109. package/docs/pages/source/source-all.html +129 -0
  110. package/docs/pages/source/source-applied.html +73 -0
  111. package/docs/pages/source/source-dynamic.html +93 -0
  112. package/docs/pages/source/source-executor-applied.html +64 -0
  113. package/docs/pages/source/source-map.html +192 -0
  114. package/docs/pages/source/source-once.html +73 -0
  115. package/docs/pages/source/source-race.html +171 -0
  116. package/docs/pages/source/source-sequence.html +160 -0
  117. package/docs/pages/source/source-with-pool.html +102 -0
  118. package/docs/pages/source/source.html +167 -0
  119. package/docs/pages/source.html +68 -0
  120. package/docs/pages/terminology/guest.html +14 -0
  121. package/docs/pages/terminology/index.html +6 -0
  122. package/docs/pages/terminology/introduction.html +25 -0
  123. package/docs/pages/terminology/patron.html +10 -0
  124. package/docs/pages/terminology/source.html +55 -0
  125. package/docs/pages/terminology/visitor.html +19 -0
  126. package/docs/pages/terminology.html +53 -0
  127. package/docs/pages/utils/give.html +47 -0
  128. package/docs/pages/utils/index.html +24 -0
  129. package/docs/pages/utils/is-guest-aware.html +22 -0
  130. package/docs/pages/utils/is-guest.html +21 -0
  131. package/docs/pages/utils/is-patron-in-pools.html +53 -0
  132. package/docs/pages/utils/private.html +44 -0
  133. package/docs/pages/utils/remove-patron-from-pools.html +57 -0
  134. package/docs/pages/utils/value.html +47 -0
  135. package/docs/pages/utils.html +61 -0
  136. package/eslint.config.mjs +6 -0
  137. package/package.json +1 -1
  138. package/src/Guest/Guest.test.ts +11 -6
  139. package/src/Guest/Guest.ts +7 -14
  140. package/src/Guest/GuestApplied.test.ts +13 -0
  141. package/src/Guest/GuestApplied.ts +16 -0
  142. package/src/Guest/GuestCast.test.ts +4 -5
  143. package/src/Guest/GuestCast.ts +3 -10
  144. package/src/Guest/GuestDisposable.test.ts +13 -17
  145. package/src/Guest/GuestDisposable.ts +3 -3
  146. package/src/Guest/GuestExecutorApplied.test.ts +27 -0
  147. package/src/Guest/GuestExecutorApplied.ts +23 -0
  148. package/src/Guest/GuestObject.test.ts +6 -6
  149. package/src/Guest/GuestObject.ts +3 -3
  150. package/src/Guest/GuestPool.test.ts +5 -22
  151. package/src/Guest/GuestPool.ts +7 -8
  152. package/src/Guest/GuestSync.test.ts +2 -2
  153. package/src/Patron/Patron.test.ts +4 -5
  154. package/src/Patron/Patron.ts +11 -3
  155. package/src/Patron/PatronApplied.ts +22 -0
  156. package/src/Patron/PatronExecutorApplied.ts +25 -0
  157. package/src/Patron/PatronOnce.sourceEmpty.test.ts +2 -2
  158. package/src/Patron/PatronOnce.test.ts +2 -2
  159. package/src/Patron/PatronOnce.ts +3 -9
  160. package/src/Patron/PatronPool.test.ts +2 -1
  161. package/src/Patron/PatronPool.ts +22 -27
  162. package/src/Private/PrivateClass.modules.test.ts +11 -8
  163. package/src/Private/PrivateClass.test.ts +7 -6
  164. package/src/Source/Source.test.ts +9 -5
  165. package/src/Source/Source.ts +46 -30
  166. package/src/Source/SourceActive.test.ts +13 -0
  167. package/src/Source/SourceActive.ts +41 -0
  168. package/src/Source/SourceAll._asArray.test.ts +22 -0
  169. package/src/Source/SourceAll._twoValuesAfter.test.ts +19 -0
  170. package/src/Source/SourceAll._twoValuesBefore.test.ts +19 -0
  171. package/src/Source/SourceAll._withPatron.test.ts +25 -0
  172. package/src/{Guest/GuestAwareAll.ts → Source/SourceAll.ts} +11 -11
  173. package/src/Source/SourceApplied.test.ts +14 -0
  174. package/src/Source/SourceApplied.ts +23 -0
  175. package/src/Source/SourceDynamic.ofSource.test.ts +12 -12
  176. package/src/Source/SourceDynamic.test.ts +9 -9
  177. package/src/Source/SourceDynamic.ts +7 -7
  178. package/src/Source/SourceExecutorApplied.test.ts +30 -0
  179. package/src/Source/SourceExecutorApplied.ts +22 -0
  180. package/src/Source/SourceMap.defered.test.ts +46 -0
  181. package/src/Source/SourceMap.fn.test.ts +27 -0
  182. package/src/Source/SourceMap.test.ts +32 -0
  183. package/src/Source/SourceMap.ts +48 -0
  184. package/src/Source/SourceOnce.notcalled.test.ts +4 -4
  185. package/src/Source/SourceOnce.test.ts +4 -5
  186. package/src/Source/SourceOnce.ts +7 -13
  187. package/src/Source/SourceRace.test.ts +46 -0
  188. package/src/Source/SourceRace.ts +30 -0
  189. package/src/Source/SourceSequence.defered.test.ts +53 -0
  190. package/src/Source/SourceSequence.test.ts +30 -0
  191. package/src/{Guest/GuestAwareSequence.ts → Source/SourceSequence.ts} +14 -19
  192. package/src/Source/SourceWithPool.empty.test.ts +14 -0
  193. package/src/Source/SourceWithPool.test.ts +10 -0
  194. package/src/Source/SourceWithPool.ts +58 -0
  195. package/src/index.ts +13 -7
  196. package/test-utils/debounce.ts +11 -0
  197. package/test-utils/id.ts +1 -0
  198. package/test-utils/wait.ts +6 -3
  199. package/src/Guest/GuestAware.test.ts +0 -13
  200. package/src/Guest/GuestAware.ts +0 -59
  201. package/src/Guest/GuestAwareActive.test.ts +0 -12
  202. package/src/Guest/GuestAwareActive.ts +0 -42
  203. package/src/Guest/GuestAwareAll._asArray.test.ts +0 -19
  204. package/src/Guest/GuestAwareAll._twoValuesAfter.test.ts +0 -16
  205. package/src/Guest/GuestAwareAll._twoValuesBefore.test.ts +0 -16
  206. package/src/Guest/GuestAwareAll._withPatron.test.ts +0 -22
  207. package/src/Guest/GuestAwareMap.defered.test.ts +0 -46
  208. package/src/Guest/GuestAwareMap.fn.test.ts +0 -27
  209. package/src/Guest/GuestAwareMap.test.ts +0 -29
  210. package/src/Guest/GuestAwareMap.ts +0 -48
  211. package/src/Guest/GuestAwareRace.test.ts +0 -46
  212. package/src/Guest/GuestAwareRace.ts +0 -33
  213. package/src/Guest/GuestAwareSequence.defered.test.ts +0 -58
  214. package/src/Guest/GuestAwareSequence.test.ts +0 -30
  215. package/src/Source/SourceEmpty.test.ts +0 -20
  216. package/src/Source/SourceEmpty.ts +0 -31
@@ -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
  });
@@ -1,7 +1,7 @@
1
1
  import { SourceDynamic } from "./SourceDynamic";
2
- import { expect, test } from "vitest";
2
+ import { expect, test, vitest } from "vitest";
3
3
  import { give, Guest } from "../Guest/Guest";
4
- import { GuestAware } from "../Guest/GuestAware";
4
+ import { Source } from "./Source";
5
5
 
6
6
  test("SourceDynamic", () => {
7
7
  let theValue = 1;
@@ -9,18 +9,18 @@ test("SourceDynamic", () => {
9
9
  new Guest((value: number) => {
10
10
  theValue = value;
11
11
  }),
12
- new GuestAware((guest) => {
12
+ new Source((guest) => {
13
13
  give(theValue, guest);
14
14
  }),
15
15
  );
16
16
 
17
- sourceDynamic.value((value) => {
18
- expect(value).toBe(1);
19
- });
17
+ const g1 = vitest.fn();
18
+ sourceDynamic.value(g1);
19
+ expect(g1).toBeCalledWith(1);
20
20
 
21
21
  sourceDynamic.give(2);
22
22
 
23
- sourceDynamic.value((value) => {
24
- expect(value).toBe(2);
25
- });
23
+ const g2 = vitest.fn();
24
+ sourceDynamic.value(g2);
25
+ expect(g2).toBeCalledWith(2);
26
26
  });
@@ -1,26 +1,26 @@
1
1
  import { give, GuestType } from "../Guest/Guest";
2
- import { GuestAwareType, value } from "../Guest/GuestAware";
2
+ import { SourceType, value } from "./Source";
3
3
  import { PatronPool } from "../Patron/PatronPool";
4
- import { SourceType } from "./Source";
4
+ import { SourceWithPoolType } from "./SourceWithPool";
5
5
 
6
6
  /**
7
7
  * @url https://kosukhin.github.io/patron.site/#/source-dynamic
8
8
  */
9
- export class SourceDynamic<T = unknown> implements SourceType<T> {
9
+ export class SourceDynamic<T = unknown> implements SourceWithPoolType<T> {
10
10
  public constructor(
11
11
  private baseGuest: GuestType<T>,
12
- private baseGuestAware: GuestAwareType<T>,
12
+ private baseSource: SourceType<T>,
13
13
  ) {
14
14
  if (baseGuest === undefined) {
15
15
  throw new Error("SourceDynamic didnt receive baseGuest argument");
16
16
  }
17
- if (baseGuestAware === undefined) {
18
- throw new Error("SourceDynamic didnt receive baseGuestAware argument");
17
+ if (baseSource === undefined) {
18
+ throw new Error("SourceDynamic didnt receive baseSource argument");
19
19
  }
20
20
  }
21
21
 
22
22
  public value(guest: GuestType<T>) {
23
- value(this.baseGuestAware, guest);
23
+ value(this.baseSource, guest);
24
24
  return this;
25
25
  }
26
26
 
@@ -0,0 +1,30 @@
1
+ import { SourceExecutorApplied } from "../Source/SourceExecutorApplied";
2
+ import { SourceWithPool } from "../Source/SourceWithPool";
3
+ import { debounce } from "../../test-utils/debounce";
4
+ import { expect, test, vi } from "vitest";
5
+ import { Patron } from "../Patron/Patron";
6
+
7
+ test("SourceExecutorApplied.test", () => {
8
+ vi.useFakeTimers({ shouldAdvanceTime: true });
9
+
10
+ const source = new SourceWithPool<number>();
11
+ const sourceDebounced = new SourceExecutorApplied(
12
+ source,
13
+ debounce.bind(null, 100),
14
+ );
15
+
16
+ let counter = 0;
17
+ sourceDebounced.value(
18
+ new Patron((v) => {
19
+ counter += v;
20
+ }),
21
+ );
22
+
23
+ source.give(1);
24
+ source.give(1);
25
+ source.give(1);
26
+
27
+ vi.runOnlyPendingTimers();
28
+
29
+ expect(counter).toBe(1);
30
+ });
@@ -0,0 +1,22 @@
1
+ import {
2
+ SourceExecutorType,
3
+ SourceObjectType,
4
+ SourceType,
5
+ value,
6
+ } from "../Source/Source";
7
+
8
+ /**
9
+ * @url https://kosukhin.github.io/patron.site/#/source/source-executor-applied
10
+ */
11
+ export class SourceExecutorApplied<T> implements SourceObjectType<T> {
12
+ public value: SourceExecutorType<T>;
13
+
14
+ public constructor(
15
+ source: SourceType<T>,
16
+ applier: (executor: SourceExecutorType<T>) => SourceExecutorType<T>,
17
+ ) {
18
+ this.value = applier((g) => {
19
+ value(source, g);
20
+ });
21
+ }
22
+ }
@@ -0,0 +1,46 @@
1
+ import { wait } from "../../test-utils/wait";
2
+ import { Private } from "../Private/Private";
3
+ import { afterEach, beforeEach, expect, test, vi } from "vitest";
4
+ import { SourceWithPool } from "./SourceWithPool";
5
+ import { give, GuestType } from "../Guest/Guest";
6
+ import { Source, SourceType, value } from "./Source";
7
+ import { SourceMap } from "./SourceMap";
8
+ import { GuestCast } from "../Guest/GuestCast";
9
+
10
+ beforeEach(() => {
11
+ vi.useFakeTimers({ shouldAdvanceTime: true });
12
+ });
13
+
14
+ afterEach(() => {
15
+ vi.runOnlyPendingTimers();
16
+ vi.useRealTimers();
17
+ });
18
+
19
+ function x2(baseNumber: SourceType<number>) {
20
+ return (guest: GuestType<number>) => {
21
+ value(
22
+ baseNumber,
23
+ new GuestCast(guest, (v) => {
24
+ give(v * 2, guest);
25
+ }),
26
+ );
27
+ return guest;
28
+ };
29
+ }
30
+
31
+ test("SourceMap.defered.test", async () => {
32
+ const sourceOf = (val: number) =>
33
+ new Source(async (guest) => {
34
+ await wait(5);
35
+ give(val, guest);
36
+ });
37
+ const source = new SourceWithPool([1, 2, 3, 9].map(sourceOf));
38
+ const guestMapped = new SourceMap(source, new Private(x2));
39
+ const callFn = vi.fn();
40
+ guestMapped.value((v) => {
41
+ callFn(v.join());
42
+ });
43
+ await wait(50);
44
+ expect(callFn).toBeCalled();
45
+ expect(callFn).toBeCalledWith("2,4,6,18");
46
+ });
@@ -0,0 +1,27 @@
1
+ import { Private } from "../Private/Private";
2
+ import { expect, test, vitest } from "vitest";
3
+ import { SourceWithPool } from "./SourceWithPool";
4
+ import { give, GuestType } from "../Guest/Guest";
5
+ import { SourceType, value } from "./Source";
6
+ import { SourceMap } from "./SourceMap";
7
+ import { GuestCast } from "../Guest/GuestCast";
8
+
9
+ function x2(baseNumber: SourceType<number>) {
10
+ return (guest: GuestType<number>) => {
11
+ value(
12
+ baseNumber,
13
+ new GuestCast(<GuestType>guest, (v) => {
14
+ give(v * 2, guest);
15
+ }),
16
+ );
17
+ return guest;
18
+ };
19
+ }
20
+
21
+ test("SourceMap.test", () => {
22
+ const source = new SourceWithPool([1, 2, 3, 9]);
23
+ const guestMapped = new SourceMap(source, new Private(x2));
24
+ const g = vitest.fn();
25
+ guestMapped.value(g);
26
+ expect(g).toBeCalledWith([2, 4, 6, 18]);
27
+ });
@@ -0,0 +1,32 @@
1
+ import { expect, test } from "vitest";
2
+ import { give, GuestType } from "../Guest/Guest";
3
+ import { SourceObjectType, SourceType, value } from "./Source";
4
+ import { SourceMap } from "./SourceMap";
5
+ import { GuestCast } from "../Guest/GuestCast";
6
+ import { SourceWithPool } from "./SourceWithPool";
7
+ import { PrivateClass } from "../Private/PrivateClass";
8
+ import { GuestSync } from "../Guest/GuestSync";
9
+
10
+ class X2 implements SourceObjectType<number> {
11
+ public constructor(private baseNumber: SourceType<number>) {}
12
+
13
+ public value(guest: GuestType<number>) {
14
+ value(
15
+ this.baseNumber,
16
+ new GuestCast(<GuestType>guest, (v) => {
17
+ give(v * 2, guest);
18
+ }),
19
+ );
20
+ return this;
21
+ }
22
+ }
23
+
24
+ test("SourceMap.test", () => {
25
+ const source = new SourceWithPool([1, 2, 3, 9]);
26
+ const guestMapped = new SourceMap(source, new PrivateClass(X2));
27
+ const guest = new GuestSync([]);
28
+
29
+ guestMapped.value(guest);
30
+
31
+ expect(guest.value().join()).toBe("2,4,6,18");
32
+ });
@@ -0,0 +1,48 @@
1
+ import { PrivateType } from "../Private/Private";
2
+ import { give, GuestType } from "../Guest/Guest";
3
+ import {
4
+ Source,
5
+ SourceObjectType,
6
+ SourceType,
7
+ isSource,
8
+ value,
9
+ } from "./Source";
10
+ import { SourceAll } from "./SourceAll";
11
+ import { GuestCast } from "../Guest/GuestCast";
12
+
13
+ /**
14
+ * @url https://kosukhin.github.io/patron.site/#/guest/source-map
15
+ */
16
+ export class SourceMap<T, TG> implements SourceObjectType<TG[]> {
17
+ public constructor(
18
+ private baseSource: SourceType<T[]>,
19
+ private targetSource: PrivateType<SourceType<TG>>,
20
+ ) {
21
+ if (baseSource === undefined) {
22
+ throw new Error("SourceMap didnt receive baseSource argument");
23
+ }
24
+ if (targetSource === undefined) {
25
+ throw new Error("SourceMap didnt receive targetSource argument");
26
+ }
27
+ }
28
+
29
+ public value(guest: GuestType<TG[]>) {
30
+ const all = new SourceAll();
31
+ value(
32
+ this.baseSource,
33
+ new GuestCast(<GuestType>guest, (theValue) => {
34
+ theValue.forEach((val, index) => {
35
+ const valueSource = isSource(val)
36
+ ? val
37
+ : new Source((innerGuest) => {
38
+ give(val, innerGuest);
39
+ });
40
+ const targetSource = this.targetSource.get(valueSource);
41
+ value(targetSource, all.guestKey(index.toString()));
42
+ });
43
+ }),
44
+ );
45
+ all.valueArray(<GuestType>guest);
46
+ return this;
47
+ }
48
+ }