silentium 0.0.2

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 (199) hide show
  1. package/.husky/pre-commit +1 -0
  2. package/.husky/pre-push +2 -0
  3. package/.vscode/settings.json +8 -0
  4. package/CHANGELOG.md +12 -0
  5. package/LICENSE.md +7 -0
  6. package/README.md +3 -0
  7. package/commitizen.cjs +50 -0
  8. package/dist/silentium.cjs +771 -0
  9. package/dist/silentium.cjs.map +1 -0
  10. package/dist/silentium.d.ts +365 -0
  11. package/dist/silentium.js +735 -0
  12. package/dist/silentium.js.map +1 -0
  13. package/dist/silentium.min.js +1 -0
  14. package/dist/silentium.min.mjs +2 -0
  15. package/dist/silentium.min.mjs.map +1 -0
  16. package/dist/silentium.mjs +735 -0
  17. package/dist/silentium.mjs.map +1 -0
  18. package/docs/README.md +3 -0
  19. package/docs/assets/css/base.css +42 -0
  20. package/docs/assets/favicon/android-icon-144x144.png +0 -0
  21. package/docs/assets/favicon/android-icon-192x192.png +0 -0
  22. package/docs/assets/favicon/android-icon-36x36.png +0 -0
  23. package/docs/assets/favicon/android-icon-48x48.png +0 -0
  24. package/docs/assets/favicon/android-icon-72x72.png +0 -0
  25. package/docs/assets/favicon/android-icon-96x96.png +0 -0
  26. package/docs/assets/favicon/apple-icon-114x114.png +0 -0
  27. package/docs/assets/favicon/apple-icon-120x120.png +0 -0
  28. package/docs/assets/favicon/apple-icon-144x144.png +0 -0
  29. package/docs/assets/favicon/apple-icon-152x152.png +0 -0
  30. package/docs/assets/favicon/apple-icon-180x180.png +0 -0
  31. package/docs/assets/favicon/apple-icon-57x57.png +0 -0
  32. package/docs/assets/favicon/apple-icon-60x60.png +0 -0
  33. package/docs/assets/favicon/apple-icon-72x72.png +0 -0
  34. package/docs/assets/favicon/apple-icon-76x76.png +0 -0
  35. package/docs/assets/favicon/apple-icon-precomposed.png +0 -0
  36. package/docs/assets/favicon/apple-icon.png +0 -0
  37. package/docs/assets/favicon/browserconfig.xml +2 -0
  38. package/docs/assets/favicon/favicon-16x16.png +0 -0
  39. package/docs/assets/favicon/favicon-32x32.png +0 -0
  40. package/docs/assets/favicon/favicon-96x96.png +0 -0
  41. package/docs/assets/favicon/favicon.ico +0 -0
  42. package/docs/assets/favicon/manifest.json +41 -0
  43. package/docs/assets/favicon/ms-icon-144x144.png +0 -0
  44. package/docs/assets/favicon/ms-icon-150x150.png +0 -0
  45. package/docs/assets/favicon/ms-icon-310x310.png +0 -0
  46. package/docs/assets/favicon/ms-icon-70x70.png +0 -0
  47. package/docs/assets/img/404.jpg +0 -0
  48. package/docs/assets/img/angular_16.jpg +0 -0
  49. package/docs/assets/img/angular_32.jpg +0 -0
  50. package/docs/assets/img/eo_16.jpg +0 -0
  51. package/docs/assets/img/eo_big.png +0 -0
  52. package/docs/assets/img/github_16.jpg +0 -0
  53. package/docs/assets/img/logo.jpg +0 -0
  54. package/docs/assets/img/philosofy/observable.jpg +0 -0
  55. package/docs/assets/img/philosofy/responsible.jpg +0 -0
  56. package/docs/assets/img/philosofy/result.jpg +0 -0
  57. package/docs/assets/img/philosofy/tell.jpg +0 -0
  58. package/docs/assets/img/react_16.jpg +0 -0
  59. package/docs/assets/img/react_32.jpg +0 -0
  60. package/docs/assets/img/vue_16.jpg +0 -0
  61. package/docs/assets/img/vue_32.jpg +0 -0
  62. package/docs/assets/schemes/dip.html +28 -0
  63. package/docs/build.sh +20 -0
  64. package/docs/favicon.ico +0 -0
  65. package/docs/index-dev.html +346 -0
  66. package/docs/index.html +346 -0
  67. package/docs/manifest.json +41 -0
  68. package/docs/pages/404.html +9 -0
  69. package/docs/pages/common/layout.html +17 -0
  70. package/docs/pages/compatibility/dip.html +82 -0
  71. package/docs/pages/compatibility/elegant-objects.html +373 -0
  72. package/docs/pages/compatibility/layout.html +17 -0
  73. package/docs/pages/examples/errors.html +167 -0
  74. package/docs/pages/examples/index.html +10 -0
  75. package/docs/pages/examples.html +41 -0
  76. package/docs/pages/factory/index.html +18 -0
  77. package/docs/pages/factory.html +36 -0
  78. package/docs/pages/guest/guest-applied.html +51 -0
  79. package/docs/pages/guest/guest-cast.html +219 -0
  80. package/docs/pages/guest/guest-disposable.html +147 -0
  81. package/docs/pages/guest/guest-executor-applied.html +65 -0
  82. package/docs/pages/guest/guest-object.html +101 -0
  83. package/docs/pages/guest/guest-pool.html +107 -0
  84. package/docs/pages/guest/guest-sync.html +66 -0
  85. package/docs/pages/guest/index.html +115 -0
  86. package/docs/pages/guest.html +59 -0
  87. package/docs/pages/index.html +72 -0
  88. package/docs/pages/integrations/angular.html +5 -0
  89. package/docs/pages/integrations/layout.html +17 -0
  90. package/docs/pages/integrations/react.html +7 -0
  91. package/docs/pages/integrations/vue.html +102 -0
  92. package/docs/pages/patron/index.html +140 -0
  93. package/docs/pages/patron/patron-applied.html +66 -0
  94. package/docs/pages/patron/patron-executor-applied.html +94 -0
  95. package/docs/pages/patron/patron-once.html +110 -0
  96. package/docs/pages/patron/patron-pool.html +99 -0
  97. package/docs/pages/patron.html +50 -0
  98. package/docs/pages/philosofy.html +210 -0
  99. package/docs/pages/source/index.html +167 -0
  100. package/docs/pages/source/source-active.html +120 -0
  101. package/docs/pages/source/source-all.html +129 -0
  102. package/docs/pages/source/source-applied.html +73 -0
  103. package/docs/pages/source/source-dynamic.html +93 -0
  104. package/docs/pages/source/source-executor-applied.html +64 -0
  105. package/docs/pages/source/source-map.html +192 -0
  106. package/docs/pages/source/source-once.html +73 -0
  107. package/docs/pages/source/source-race.html +171 -0
  108. package/docs/pages/source/source-sequence.html +160 -0
  109. package/docs/pages/source/source-with-pool.html +102 -0
  110. package/docs/pages/source/source.html +167 -0
  111. package/docs/pages/source.html +68 -0
  112. package/docs/pages/terminology/guest.html +14 -0
  113. package/docs/pages/terminology/index.html +6 -0
  114. package/docs/pages/terminology/introduction.html +25 -0
  115. package/docs/pages/terminology/patron.html +10 -0
  116. package/docs/pages/terminology/source.html +55 -0
  117. package/docs/pages/terminology/visitor.html +19 -0
  118. package/docs/pages/terminology.html +53 -0
  119. package/docs/pages/utils/give.html +47 -0
  120. package/docs/pages/utils/index.html +24 -0
  121. package/docs/pages/utils/is-guest-aware.html +22 -0
  122. package/docs/pages/utils/is-guest.html +21 -0
  123. package/docs/pages/utils/is-patron-in-pools.html +53 -0
  124. package/docs/pages/utils/is-source.html +21 -0
  125. package/docs/pages/utils/private.html +44 -0
  126. package/docs/pages/utils/remove-patron-from-pools.html +57 -0
  127. package/docs/pages/utils/source-of.html +20 -0
  128. package/docs/pages/utils/value.html +47 -0
  129. package/docs/pages/utils.html +64 -0
  130. package/docs/template.html +346 -0
  131. package/eslint.config.mjs +47 -0
  132. package/package.json +57 -0
  133. package/rollup.config.js +52 -0
  134. package/src/Guest/Guest.test.ts +15 -0
  135. package/src/Guest/Guest.ts +53 -0
  136. package/src/Guest/GuestApplied.test.ts +13 -0
  137. package/src/Guest/GuestApplied.ts +16 -0
  138. package/src/Guest/GuestCast.test.ts +30 -0
  139. package/src/Guest/GuestCast.ts +39 -0
  140. package/src/Guest/GuestDisposable.test.ts +26 -0
  141. package/src/Guest/GuestDisposable.ts +33 -0
  142. package/src/Guest/GuestExecutorApplied.test.ts +27 -0
  143. package/src/Guest/GuestExecutorApplied.ts +23 -0
  144. package/src/Guest/GuestObject.test.ts +11 -0
  145. package/src/Guest/GuestObject.ts +34 -0
  146. package/src/Guest/GuestPool.test.ts +30 -0
  147. package/src/Guest/GuestPool.ts +56 -0
  148. package/src/Guest/GuestSync.test.ts +12 -0
  149. package/src/Guest/GuestSync.ts +25 -0
  150. package/src/Patron/Patron.test.ts +16 -0
  151. package/src/Patron/Patron.ts +35 -0
  152. package/src/Patron/PatronApplied.ts +22 -0
  153. package/src/Patron/PatronExecutorApplied.ts +25 -0
  154. package/src/Patron/PatronOnce.sourceEmpty.test.ts +38 -0
  155. package/src/Patron/PatronOnce.test.ts +32 -0
  156. package/src/Patron/PatronOnce.ts +38 -0
  157. package/src/Patron/PatronPool.test.ts +22 -0
  158. package/src/Patron/PatronPool.ts +121 -0
  159. package/src/Private/Private.test.ts +12 -0
  160. package/src/Private/Private.ts +20 -0
  161. package/src/Private/PrivateClass.modules.test.ts +36 -0
  162. package/src/Private/PrivateClass.test.ts +13 -0
  163. package/src/Private/PrivateClass.ts +29 -0
  164. package/src/Source/Source.test.ts +14 -0
  165. package/src/Source/Source.ts +59 -0
  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/Source/SourceAll.ts +86 -0
  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 +21 -0
  176. package/src/Source/SourceDynamic.test.ts +26 -0
  177. package/src/Source/SourceDynamic.ts +35 -0
  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 +13 -0
  185. package/src/Source/SourceOnce.test.ts +10 -0
  186. package/src/Source/SourceOnce.ts +31 -0
  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/Source/SourceSequence.ts +72 -0
  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 +26 -0
  196. package/test-utils/debounce.ts +11 -0
  197. package/test-utils/id.ts +1 -0
  198. package/test-utils/wait.ts +6 -0
  199. package/tsconfig.json +26 -0
@@ -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
+ }
@@ -0,0 +1,13 @@
1
+ import { expect, test, vi, vitest } from "vitest";
2
+ import { SourceOnce } from "./SourceOnce";
3
+
4
+ test("SourceOnce.notcalled.test", () => {
5
+ const source = new SourceOnce();
6
+ const guestNotCalled = vi.fn();
7
+ source.value(guestNotCalled);
8
+ expect(guestNotCalled).not.toHaveBeenCalled();
9
+ source.give(111);
10
+ const g = vitest.fn();
11
+ source.value(g);
12
+ expect(g).toBeCalledWith(111);
13
+ });
@@ -0,0 +1,10 @@
1
+ import { SourceOnce } from "./SourceOnce";
2
+ import { expect, test, vitest } from "vitest";
3
+
4
+ test("SourceOnce.test", () => {
5
+ const source = new SourceOnce(123);
6
+ source.give(321);
7
+ const g = vitest.fn();
8
+ source.value(g);
9
+ expect(g).toBeCalledWith(123);
10
+ });
@@ -0,0 +1,31 @@
1
+ import { GuestType } from "../Guest/Guest";
2
+ import { PatronPool } from "../Patron/PatronPool";
3
+ import { value } from "./Source";
4
+ import { SourceWithPool, SourceWithPoolType } from "./SourceWithPool";
5
+
6
+ /**
7
+ * @url https://kosukhin.github.io/patron.site/#/source/source-once
8
+ */
9
+ export class SourceOnce<T> implements SourceWithPoolType<T> {
10
+ private source: SourceWithPool<T>;
11
+
12
+ public constructor(initialValue?: T) {
13
+ this.source = new SourceWithPool(initialValue);
14
+ }
15
+
16
+ public value(guest: GuestType<T>) {
17
+ value(this.source, guest);
18
+ return this;
19
+ }
20
+
21
+ public give(value: T): this {
22
+ if (!this.source.filled()) {
23
+ this.source.give(value);
24
+ }
25
+ return this;
26
+ }
27
+
28
+ public pool(): PatronPool<T> {
29
+ return this.source.pool();
30
+ }
31
+ }
@@ -0,0 +1,46 @@
1
+ import { SourceWithPool } from "../Source/SourceWithPool";
2
+ import { SourceRace } from "./SourceRace";
3
+ import { afterEach, beforeEach, expect, test, vi, vitest } from "vitest";
4
+
5
+ beforeEach(() => {
6
+ vi.useFakeTimers();
7
+ });
8
+
9
+ afterEach(() => {
10
+ vi.useRealTimers();
11
+ });
12
+
13
+ test("SourceRace.test", async () => {
14
+ const sBuild = (result: number, delay: number) => {
15
+ const s = new SourceWithPool();
16
+
17
+ setTimeout(() => {
18
+ s.give(result);
19
+ }, delay);
20
+
21
+ return s;
22
+ };
23
+
24
+ const s2 = sBuild(2, 100);
25
+ const s1 = sBuild(1, 200);
26
+
27
+ const sAny = new SourceRace([s1, s2]);
28
+
29
+ await vi.advanceTimersByTime(201);
30
+
31
+ const g1 = vitest.fn();
32
+ sAny.value(g1);
33
+ expect(g1).toBeCalledWith(1);
34
+
35
+ setTimeout(() => {
36
+ s1.give(3);
37
+ s2.give(4);
38
+ }, 300);
39
+
40
+ await vi.advanceTimersByTime(301);
41
+
42
+ const g2 = vitest.fn();
43
+ sAny.value(g2);
44
+ // ignores second value
45
+ expect(g2).toBeCalledWith(3);
46
+ });
@@ -0,0 +1,30 @@
1
+ import { give, GuestType } from "../Guest/Guest";
2
+ import { SourceObjectType, SourceType, value } from "./Source";
3
+ import { GuestCast } from "../Guest/GuestCast";
4
+
5
+ /**
6
+ * @url https://kosukhin.github.io/patron.site/#/guest/source-race
7
+ */
8
+ export class SourceRace<T> implements SourceObjectType<T> {
9
+ public constructor(private sources: SourceType<T>[]) {
10
+ if (sources === undefined) {
11
+ throw new Error("SourceRace didnt receive sources argument");
12
+ }
13
+ }
14
+
15
+ public value(guest: GuestType<T>): this {
16
+ let connectedWithSource: SourceType | null = null;
17
+ this.sources.forEach((source) => {
18
+ value(
19
+ source,
20
+ new GuestCast(<GuestType>guest, (value) => {
21
+ if (!connectedWithSource || connectedWithSource === source) {
22
+ give(value as T, guest);
23
+ connectedWithSource = source;
24
+ }
25
+ }),
26
+ );
27
+ });
28
+ return this;
29
+ }
30
+ }
@@ -0,0 +1,53 @@
1
+ import { SourceSequence } from "./SourceSequence";
2
+ import { give } from "../Guest/Guest";
3
+ import { Source, SourceObjectType, SourceType, value } from "./Source";
4
+ import { GuestCast } from "../Guest/GuestCast";
5
+ import { GuestType } from "../Guest/Guest";
6
+ import { afterEach, beforeEach, expect, test, vi } from "vitest";
7
+ import { SourceWithPool } from "./SourceWithPool";
8
+ import { PrivateClass } from "../Private/PrivateClass";
9
+ import { wait } from "../../test-utils/wait";
10
+
11
+ beforeEach(() => {
12
+ vi.useFakeTimers({ shouldAdvanceTime: true });
13
+ });
14
+
15
+ afterEach(() => {
16
+ vi.runOnlyPendingTimers();
17
+ vi.useRealTimers();
18
+ });
19
+
20
+ class X2 implements SourceObjectType<number> {
21
+ public constructor(private baseNumber: SourceType<number>) {}
22
+
23
+ public value(guest: GuestType<number>) {
24
+ value(
25
+ this.baseNumber,
26
+ new GuestCast(<GuestType>guest, (v) => {
27
+ give(v * 2, guest);
28
+ }),
29
+ );
30
+ return this;
31
+ }
32
+ }
33
+
34
+ test("SourceSequence.defered.test", async () => {
35
+ const sourceOf = (val: number) =>
36
+ new Source((guest) => {
37
+ setTimeout(() => {
38
+ give(val, guest);
39
+ }, 10);
40
+ });
41
+ const source = new SourceWithPool([1, 2, 3, 9].map(sourceOf));
42
+
43
+ const sequence = new SourceSequence(source, new PrivateClass(X2));
44
+
45
+ const callFn = vi.fn();
46
+ sequence.value((v) => {
47
+ callFn(v.join());
48
+ });
49
+
50
+ await wait(51);
51
+ expect(callFn).toBeCalled();
52
+ expect(callFn).toBeCalledWith("2,4,6,18");
53
+ });
@@ -0,0 +1,30 @@
1
+ import { SourceSequence } from "./SourceSequence";
2
+ import { give } from "../Guest/Guest";
3
+ import { SourceObjectType, SourceType, value } from "./Source";
4
+ import { GuestCast } from "../Guest/GuestCast";
5
+ import { GuestType } from "../Guest/Guest";
6
+ import { expect, test, vitest } from "vitest";
7
+ import { SourceWithPool } from "./SourceWithPool";
8
+ import { PrivateClass } from "../Private/PrivateClass";
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("SourceSequence.test", () => {
25
+ const source = new SourceWithPool([1, 2, 3, 9]);
26
+ const guestMapped = new SourceSequence(source, new PrivateClass(X2));
27
+ const g = vitest.fn();
28
+ guestMapped.value(g);
29
+ expect(g).toBeCalledWith([2, 4, 6, 18]);
30
+ });
@@ -0,0 +1,72 @@
1
+ import { SourceWithPool } from "../Source/SourceWithPool";
2
+ import { give, GuestType } from "../Guest/Guest";
3
+ import { GuestCast } from "../Guest/GuestCast";
4
+ import { PatronOnce } from "../Patron/PatronOnce";
5
+ import { PrivateType } from "../Private/Private";
6
+ import { isSource, SourceObjectType, SourceType, value } from "./Source";
7
+ import { SourceAll } from "./SourceAll";
8
+
9
+ /**
10
+ * @url https://kosukhin.github.io/patron.site/#/guest/source-sequence
11
+ */
12
+ export class SourceSequence<T, TG> implements SourceObjectType<TG[]> {
13
+ public constructor(
14
+ private baseSource: SourceType<T[]>,
15
+ private targetSource: PrivateType<SourceType<TG>>,
16
+ ) {
17
+ if (baseSource === undefined) {
18
+ throw new Error("SourceSequence didnt receive baseSource argument");
19
+ }
20
+ if (targetSource === undefined) {
21
+ throw new Error("SourceSequence didnt receive targetSource argument");
22
+ }
23
+ }
24
+
25
+ public value(guest: GuestType<TG[]>) {
26
+ const all = new SourceAll<TG[]>();
27
+ const sequenceSource = new SourceWithPool();
28
+ const targetSource = this.targetSource.get(sequenceSource);
29
+
30
+ value(
31
+ this.baseSource,
32
+ new GuestCast(guest, (theValue) => {
33
+ let index = 0;
34
+
35
+ const nextItemHandle = () => {
36
+ if (theValue[index + 1] !== undefined) {
37
+ index = index + 1;
38
+ handle();
39
+ } else {
40
+ all.valueArray(guest);
41
+ }
42
+ };
43
+
44
+ function handle() {
45
+ sequenceSource.give(null);
46
+ const nextValue = theValue[index];
47
+ if (isSource(nextValue)) {
48
+ value(
49
+ nextValue,
50
+ new PatronOnce((theNextValue) => {
51
+ sequenceSource.give(theNextValue);
52
+ value(targetSource, all.guestKey(index.toString()));
53
+ nextItemHandle();
54
+ }),
55
+ );
56
+ } else {
57
+ sequenceSource.give(nextValue);
58
+ value(targetSource, all.guestKey(index.toString()));
59
+ nextItemHandle();
60
+ }
61
+ }
62
+
63
+ if (theValue[index] !== undefined) {
64
+ handle();
65
+ } else {
66
+ give([], guest);
67
+ }
68
+ }),
69
+ );
70
+ return this;
71
+ }
72
+ }
@@ -0,0 +1,14 @@
1
+ import { expect, test, vitest } from "vitest";
2
+ import { SourceWithPool } from "./SourceWithPool";
3
+ import { Patron } from "../Patron/Patron";
4
+
5
+ test("SourceChangeable.test", () => {
6
+ const source = new SourceWithPool();
7
+ const guest = vitest.fn();
8
+
9
+ source.value(new Patron(guest));
10
+ source.give(42);
11
+
12
+ expect(guest).toBeCalled();
13
+ expect(guest).toBeCalledWith(42);
14
+ });
@@ -0,0 +1,10 @@
1
+ import { expect, test, vitest } from "vitest";
2
+ import { SourceWithPool } from "./SourceWithPool";
3
+
4
+ test("SourceChangeable.test", () => {
5
+ const source = new SourceWithPool(42);
6
+
7
+ const g = vitest.fn();
8
+ source.value(g);
9
+ expect(g).toBeCalledWith(42);
10
+ });
@@ -0,0 +1,58 @@
1
+ import { Guest, GuestObjectType, GuestType } from "../Guest/Guest";
2
+ import { SourceObjectType } from "./Source";
3
+ import { PatronPool } from "../Patron/PatronPool";
4
+ import { isPatron } from "../Patron/Patron";
5
+
6
+ export interface PoolAwareType<T = any> {
7
+ pool(): PatronPool<T>;
8
+ }
9
+
10
+ /**
11
+ * @url https://kosukhin.github.io/patron.site/#/source-with-pool
12
+ */
13
+ export type SourceWithPoolType<T = any> = SourceObjectType<T> &
14
+ GuestObjectType<T> &
15
+ PoolAwareType<T>;
16
+
17
+ export class SourceWithPool<T> implements SourceWithPoolType<T> {
18
+ private thePool = new PatronPool(this);
19
+ private theEmptyPool = new PatronPool(this);
20
+ private isEmpty: boolean;
21
+
22
+ public constructor(private sourceDocument?: T) {
23
+ this.isEmpty = sourceDocument === undefined;
24
+ }
25
+
26
+ public pool() {
27
+ return this.thePool;
28
+ }
29
+
30
+ public give(value: T): this {
31
+ this.isEmpty = false;
32
+ this.sourceDocument = value;
33
+ this.thePool.give(this.sourceDocument);
34
+ this.theEmptyPool.give(this.sourceDocument);
35
+ return this;
36
+ }
37
+
38
+ public value(guest: GuestType<T>): this {
39
+ if (this.isEmpty) {
40
+ if (isPatron(guest)) {
41
+ this.theEmptyPool.add(guest);
42
+ }
43
+ return this;
44
+ }
45
+
46
+ if (typeof guest === "function") {
47
+ this.thePool.distribute(this.sourceDocument, new Guest(guest));
48
+ } else {
49
+ this.thePool.distribute(this.sourceDocument, guest);
50
+ }
51
+
52
+ return this;
53
+ }
54
+
55
+ public filled() {
56
+ return !this.isEmpty;
57
+ }
58
+ }
package/src/index.ts ADDED
@@ -0,0 +1,26 @@
1
+ export * from "./Guest/Guest";
2
+ export * from "./Guest/GuestCast";
3
+ export * from "./Guest/GuestPool";
4
+ export * from "./Guest/GuestSync";
5
+ export * from "./Guest/GuestObject";
6
+ export * from "./Guest/GuestDisposable";
7
+ export * from "./Guest/GuestApplied";
8
+ export * from "./Guest/GuestExecutorApplied";
9
+ export * from "./Patron/Patron";
10
+ export * from "./Patron/PatronOnce";
11
+ export * from "./Patron/PatronPool";
12
+ export * from "./Patron/PatronApplied";
13
+ export * from "./Patron/PatronExecutorApplied";
14
+ export * from "./Source/SourceAll";
15
+ export * from "./Source/Source";
16
+ export * from "./Source/SourceSequence";
17
+ export * from "./Source/SourceMap";
18
+ export * from "./Source/SourceRace";
19
+ export * from "./Source/SourceActive";
20
+ export * from "./Source/SourceWithPool";
21
+ export * from "./Source/SourceDynamic";
22
+ export * from "./Source/SourceApplied";
23
+ export * from "./Source/SourceExecutorApplied";
24
+ export * from "./Source/SourceOnce";
25
+ export * from "./Private/PrivateClass";
26
+ export * from "./Private/Private";
@@ -0,0 +1,11 @@
1
+ export function debounce(wait: number, func: any) {
2
+ let timeout: number;
3
+
4
+ return function (...args: any[]) {
5
+ clearTimeout(timeout);
6
+
7
+ timeout = setTimeout(() => {
8
+ func(...args);
9
+ }, wait);
10
+ };
11
+ }
@@ -0,0 +1 @@
1
+ export const id = (v: unknown) => v;
@@ -0,0 +1,6 @@
1
+ export const wait = (ms: number) =>
2
+ new Promise((resolve) => {
3
+ setTimeout(() => {
4
+ resolve(1);
5
+ }, ms);
6
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "esnext",
4
+ "module": "esnext",
5
+ "strict": true,
6
+ "moduleResolution": "node",
7
+ "skipLibCheck": true,
8
+ "esModuleInterop": true,
9
+ "allowSyntheticDefaultImports": true,
10
+ "forceConsistentCasingInFileNames": true,
11
+ "useDefineForClassFields": true,
12
+ "sourceMap": true,
13
+ "baseUrl": ".",
14
+ "resolveJsonModule": true,
15
+ "lib": [
16
+ "es2022",
17
+ "dom"
18
+ ]
19
+ },
20
+ "include": [
21
+ "src/**/*.ts",
22
+ ],
23
+ "exclude": [
24
+ "node_modules"
25
+ ]
26
+ }