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
@@ -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;
@@ -1,3 +1,6 @@
1
- export const wait = (ms: number) => new Promise(resolve => {
2
- setTimeout(() => { resolve(1) }, ms);
3
- })
1
+ export const wait = (ms: number) =>
2
+ new Promise((resolve) => {
3
+ setTimeout(() => {
4
+ resolve(1);
5
+ }, ms);
6
+ });
@@ -1,13 +0,0 @@
1
- import { expect, test } from "vitest";
2
- import { GuestAware } from "./GuestAware";
3
- import { give } from "./Guest";
4
-
5
- test("GuestAware.test", () => {
6
- const aware = new GuestAware((guest) => {
7
- give(111, guest);
8
- });
9
-
10
- aware.value((value) => {
11
- expect(value).toBe(111);
12
- });
13
- });
@@ -1,59 +0,0 @@
1
- import { GuestType } from "./Guest";
2
-
3
- export type GuestAwareExecutorType<T> = (guest: GuestType<T>) => unknown;
4
-
5
- export interface GuestAwareObjectType<T> {
6
- value: GuestAwareExecutorType<T>;
7
- }
8
-
9
- export type GuestAwareType<T = any> =
10
- | GuestAwareExecutorType<T>
11
- | GuestAwareObjectType<T>;
12
-
13
- /**
14
- * @url https://kosukhin.github.io/patron.site/#/utils/give
15
- */
16
- export function value<T>(guestAware: GuestAwareType<T>, guest: GuestType<T>) {
17
- if (guestAware === undefined) {
18
- throw new Error("value didnt receive guestAware argument");
19
- }
20
- if (guest === undefined) {
21
- throw new Error("value didnt receive guest argument");
22
- }
23
- if (typeof guestAware === "function") {
24
- return guestAware(guest);
25
- } else {
26
- return guestAware.value(guest);
27
- }
28
- }
29
-
30
- /**
31
- * @url https://kosukhin.github.io/patron.site/#/utils/is-guest-aware
32
- */
33
- export function isGuestAware(
34
- mbGuestAware: any,
35
- ): mbGuestAware is GuestAwareType {
36
- if (mbGuestAware === undefined) {
37
- throw new Error("isGuestAware didnt receive mbGuestAware argument");
38
- }
39
- return (
40
- typeof mbGuestAware === "function" ||
41
- typeof mbGuestAware?.value === "function"
42
- );
43
- }
44
-
45
- /**
46
- * @url https://kosukhin.github.io/patron.site/#/guest/guest-aware
47
- */
48
- export class GuestAware<T = any> implements GuestAwareObjectType<T> {
49
- public constructor(private guestAware: GuestAwareType<T>) {
50
- if (guestAware === undefined) {
51
- throw new Error("GuestAware constructor didnt receive executor function");
52
- }
53
- }
54
-
55
- public value(guest: GuestType<T>): GuestType<T> {
56
- value(this.guestAware, guest);
57
- return guest;
58
- }
59
- }
@@ -1,12 +0,0 @@
1
- import { GuestAwareActive } from "./GuestAwareActive";
2
- import { expect, test } from "vitest";
3
-
4
- test("GuestAwareActive.test", () => {
5
- const active = new GuestAwareActive<number, number>((config, source) => {
6
- source.give(config * 3);
7
- });
8
-
9
- active.do(4).value((v) => {
10
- expect(v).toBe(12);
11
- });
12
- });
@@ -1,42 +0,0 @@
1
- import { SourceType } from "../Source/Source";
2
- import { SourceEmpty } from "../Source/SourceEmpty";
3
- import { GuestType } from "./Guest";
4
- import { GuestAwareObjectType } from "./GuestAware";
5
-
6
- /**
7
- * @url https://kosukhin.github.io/patron.site/#/utils/action-type
8
- */
9
- export interface ActionType<P = any> {
10
- do(config: P): this;
11
- }
12
-
13
- export interface GuestAwareAcitveType<R = unknown, T = unknown>
14
- extends GuestAwareObjectType<T>,
15
- ActionType<R> {}
16
-
17
- /**
18
- * @url https://kosukhin.github.io/patron.site/#/guest/guest-aware-active
19
- */
20
- export class GuestAwareActive<R, T> implements GuestAwareAcitveType<R, T> {
21
- private source = new SourceEmpty<T>();
22
-
23
- public constructor(
24
- private configExecutor: (config: R, source: SourceType<T>) => void,
25
- ) {
26
- if (configExecutor === undefined) {
27
- throw new Error(
28
- "GuestAwareActive constructor didnt receive executor function",
29
- );
30
- }
31
- }
32
-
33
- public do(config: R): this {
34
- this.configExecutor(config, this.source);
35
- return this;
36
- }
37
-
38
- public value(guest: GuestType<T>): this {
39
- this.source.value(guest);
40
- return this;
41
- }
42
- }
@@ -1,19 +0,0 @@
1
- import { expect, test } from "vitest";
2
- import { GuestAwareAll } from "./GuestAwareAll";
3
- import { Source } from "../Source/Source";
4
- import { Patron } from "../Patron/Patron";
5
-
6
- test("GuestAwareAll._asArray.test", () => {
7
- const one = new Source(1);
8
- const two = new Source(2);
9
- const all = new GuestAwareAll<[number, number]>();
10
-
11
- one.value(new Patron(all.guestKey("0")));
12
- two.value(new Patron(all.guestKey("1")));
13
-
14
- all.valueArray(
15
- new Patron((value) => {
16
- expect(JSON.stringify(value)).toBe("[1,2]");
17
- }),
18
- );
19
- });
@@ -1,16 +0,0 @@
1
- import { expect, test } from "vitest";
2
- import { Source } from "../Source/Source";
3
- import { GuestAwareAll } from "./GuestAwareAll";
4
-
5
- test("GuestAwareAll._twoValuesAfter.test", () => {
6
- const one = new Source(1);
7
- const two = new Source(2);
8
- const all = new GuestAwareAll<{ one: number; two: number }>(["one", "two"]);
9
-
10
- all.value((value) => {
11
- expect(Object.values(value).join()).toBe("1,2");
12
- });
13
-
14
- one.value(all.guestKey("one"));
15
- two.value(all.guestKey("two"));
16
- });
@@ -1,16 +0,0 @@
1
- import { expect, test } from "vitest";
2
- import { Source } from "../Source/Source";
3
- import { GuestAwareAll } from "./GuestAwareAll";
4
-
5
- test("GuestAwareAll._twoValuesBefore.test", () => {
6
- const one = new Source(1);
7
- const two = new Source(2);
8
- const all = new GuestAwareAll<{ one: number; two: number }>();
9
-
10
- one.value(all.guestKey("one"));
11
- two.value(all.guestKey("two"));
12
-
13
- all.value((value) => {
14
- expect(Object.values(value).join()).toBe("1,2");
15
- });
16
- });
@@ -1,22 +0,0 @@
1
- import { expect, test } from "vitest";
2
- import { GuestAwareAll } from "./GuestAwareAll";
3
- import { Source } from "../Source/Source";
4
- import { Patron } from "../Patron/Patron";
5
-
6
- test("GuestAwareAll._withPatron.test", () => {
7
- const one = new Source(1);
8
- const two = new Source(2);
9
- const all = new GuestAwareAll<{ 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
- all.value(
18
- new Patron((value: Record<string, unknown>) => {
19
- expect(Object.values(value).length).toBe(2);
20
- }),
21
- );
22
- });
@@ -1,46 +0,0 @@
1
- import { wait } from "./../../test-utils/wait";
2
- import { Private } from "../Private/Private";
3
- import { afterEach, beforeEach, expect, test, vi } from "vitest";
4
- import { Source } from "../Source/Source";
5
- import { give, GuestType } from "./Guest";
6
- import { GuestAware, GuestAwareType, value } from "./GuestAware";
7
- import { GuestAwareMap } from "./GuestAwareMap";
8
- import { GuestCast } from "./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: GuestAwareType<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("GuestAwareMap.defered.test", async () => {
32
- const guestAwareOf = (val: number) =>
33
- new GuestAware(async (guest) => {
34
- await wait(5);
35
- give(val, guest);
36
- });
37
- const source = new Source([1, 2, 3, 9].map(guestAwareOf));
38
- const guestMapped = new GuestAwareMap(source, new Private(x2));
39
- const callFn = vi.fn();
40
- guestMapped.value((v) => {
41
- expect(v.join()).toBe("2,4,6,18");
42
- callFn();
43
- });
44
- await wait(50);
45
- expect(callFn).toBeCalled();
46
- });
@@ -1,27 +0,0 @@
1
- import { Private } from "../Private/Private";
2
- import { expect, test } from "vitest";
3
- import { Source } from "../Source/Source";
4
- import { give, GuestType } from "./Guest";
5
- import { GuestAwareType, value } from "./GuestAware";
6
- import { GuestAwareMap } from "./GuestAwareMap";
7
- import { GuestCast } from "./GuestCast";
8
-
9
- function x2(baseNumber: GuestAwareType<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("GuestAwareMap.test", () => {
22
- const source = new Source([1, 2, 3, 9]);
23
- const guestMapped = new GuestAwareMap(source, new Private(x2));
24
- guestMapped.value((v) => {
25
- expect(v.join()).toBe("2,4,6,18");
26
- });
27
- });
@@ -1,29 +0,0 @@
1
- import { expect, test } from "vitest";
2
- import { give, GuestType } from "./Guest";
3
- import { GuestAwareObjectType, GuestAwareType, value } from "./GuestAware";
4
- import { GuestAwareMap } from "./GuestAwareMap";
5
- import { GuestCast } from "./GuestCast";
6
- import { Source } from "../Source/Source";
7
- import { PrivateClass } from "../Private/PrivateClass";
8
-
9
- class X2 implements GuestAwareObjectType<number> {
10
- public constructor(private baseNumber: GuestAwareType<number>) {}
11
-
12
- public value(guest: GuestType<number>) {
13
- value(
14
- this.baseNumber,
15
- new GuestCast(<GuestType>guest, (v) => {
16
- give(v * 2, guest);
17
- }),
18
- );
19
- return this;
20
- }
21
- }
22
-
23
- test("GuestAwareMap.test", () => {
24
- const source = new Source([1, 2, 3, 9]);
25
- const guestMapped = new GuestAwareMap(source, new PrivateClass(X2));
26
- guestMapped.value((v) => {
27
- expect(v.join()).toBe("2,4,6,18");
28
- });
29
- });
@@ -1,48 +0,0 @@
1
- import { PrivateType } from "../Private/Private";
2
- import { give, GuestType } from "./Guest";
3
- import {
4
- GuestAware,
5
- GuestAwareObjectType,
6
- GuestAwareType,
7
- isGuestAware,
8
- value,
9
- } from "./GuestAware";
10
- import { GuestAwareAll } from "./GuestAwareAll";
11
- import { GuestCast } from "./GuestCast";
12
-
13
- /**
14
- * @url https://kosukhin.github.io/patron.site/#/guest/guest-aware-map
15
- */
16
- export class GuestAwareMap<T, TG> implements GuestAwareObjectType<TG[]> {
17
- public constructor(
18
- private baseSource: GuestAwareType<T[]>,
19
- private targetSource: PrivateType<GuestAwareType<TG>>,
20
- ) {
21
- if (baseSource === undefined) {
22
- throw new Error("GuestAwareMap didnt receive baseSource argument");
23
- }
24
- if (targetSource === undefined) {
25
- throw new Error("GuestAwareMap didnt receive targetSource argument");
26
- }
27
- }
28
-
29
- public value(guest: GuestType<TG[]>) {
30
- const all = new GuestAwareAll();
31
- value(
32
- this.baseSource,
33
- new GuestCast(<GuestType>guest, (theValue) => {
34
- theValue.forEach((val, index) => {
35
- const valueSource = isGuestAware(val)
36
- ? val
37
- : new GuestAware((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
- }
@@ -1,46 +0,0 @@
1
- import { GuestAwareRace } from "./GuestAwareRace";
2
- import { afterEach, beforeEach, expect, test, vi } from "vitest";
3
- import { SourceEmpty } from "../Source/SourceEmpty";
4
-
5
- beforeEach(() => {
6
- vi.useFakeTimers();
7
- });
8
-
9
- afterEach(() => {
10
- vi.useRealTimers();
11
- });
12
-
13
- test("GuestAwareRace.test", async () => {
14
- const sBuild = (result: number, delay: number) => {
15
- const s = new SourceEmpty();
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 GuestAwareRace([s1, s2]);
28
-
29
- await vi.advanceTimersByTime(201);
30
-
31
- sAny.value((v) => {
32
- expect(v).toBe(1);
33
- });
34
-
35
- setTimeout(() => {
36
- s1.give(3);
37
- s2.give(4);
38
- }, 300);
39
-
40
- await vi.advanceTimersByTime(301);
41
-
42
- sAny.value((v) => {
43
- // ignores second value
44
- expect(v).toBe(3);
45
- });
46
- });
@@ -1,33 +0,0 @@
1
- import { give, GuestType } from "./Guest";
2
- import { GuestAwareObjectType, GuestAwareType, value } from "./GuestAware";
3
- import { GuestCast } from "./GuestCast";
4
-
5
- /**
6
- * @url https://kosukhin.github.io/patron.site/#/guest/guest-aware-race
7
- */
8
- export class GuestAwareRace<T> implements GuestAwareObjectType<T> {
9
- public constructor(private guestAwares: GuestAwareType<T>[]) {
10
- if (guestAwares === undefined) {
11
- throw new Error("GuestAwareRace didnt receive guestAwares argument");
12
- }
13
- }
14
-
15
- public value(guest: GuestType<T>): this {
16
- let connectedWithGuestAware: GuestAwareType | null = null;
17
- this.guestAwares.forEach((guestAware) => {
18
- value(
19
- guestAware,
20
- new GuestCast(<GuestType>guest, (value) => {
21
- if (
22
- !connectedWithGuestAware ||
23
- connectedWithGuestAware === guestAware
24
- ) {
25
- give(value as T, guest);
26
- connectedWithGuestAware = guestAware;
27
- }
28
- }),
29
- );
30
- });
31
- return this;
32
- }
33
- }
@@ -1,58 +0,0 @@
1
- import { GuestAwareSequence } from "./GuestAwareSequence";
2
- import { give } from "./Guest";
3
- import {
4
- GuestAware,
5
- GuestAwareObjectType,
6
- GuestAwareType,
7
- value,
8
- } from "./GuestAware";
9
- import { GuestCast } from "./GuestCast";
10
- import { GuestType } from "./Guest";
11
- import { afterEach, beforeEach, expect, test, vi } from "vitest";
12
- import { Source } from "../Source/Source";
13
- import { PrivateClass } from "../Private/PrivateClass";
14
- import { wait } from "../../test-utils/wait";
15
-
16
- beforeEach(() => {
17
- vi.useFakeTimers({ shouldAdvanceTime: true });
18
- });
19
-
20
- afterEach(() => {
21
- vi.runOnlyPendingTimers();
22
- vi.useRealTimers();
23
- });
24
-
25
- class X2 implements GuestAwareObjectType<number> {
26
- public constructor(private baseNumber: GuestAwareType<number>) {}
27
-
28
- public value(guest: GuestType<number>) {
29
- value(
30
- this.baseNumber,
31
- new GuestCast(<GuestType>guest, (v) => {
32
- give(v * 2, guest);
33
- }),
34
- );
35
- return this;
36
- }
37
- }
38
-
39
- test("GuestAwareSequence.defered.test", async () => {
40
- const guestAwareOf = (val: number) =>
41
- new GuestAware((guest) => {
42
- setTimeout(() => {
43
- give(val, guest);
44
- }, 10);
45
- });
46
- const source = new Source([1, 2, 3, 9].map(guestAwareOf));
47
-
48
- const sequence = new GuestAwareSequence(source, new PrivateClass(X2));
49
-
50
- const callFn = vi.fn();
51
- sequence.value((v) => {
52
- expect(v.join()).toBe("2,4,6,18");
53
- callFn();
54
- });
55
-
56
- await wait(51);
57
- expect(callFn).toBeCalled();
58
- });
@@ -1,30 +0,0 @@
1
- import { GuestAwareSequence } from "./GuestAwareSequence";
2
- import { give } from "./Guest";
3
- import { GuestAwareObjectType, GuestAwareType, value } from "./GuestAware";
4
- import { GuestCast } from "./GuestCast";
5
- import { GuestType } from "./Guest";
6
- import { expect, test } from "vitest";
7
- import { Source } from "../Source/Source";
8
- import { PrivateClass } from "../Private/PrivateClass";
9
-
10
- class X2 implements GuestAwareObjectType<number> {
11
- public constructor(private baseNumber: GuestAwareType<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("GuestAwareSequence.test", () => {
25
- const source = new Source([1, 2, 3, 9]);
26
- const guestMapped = new GuestAwareSequence(source, new PrivateClass(X2));
27
- guestMapped.value((v) => {
28
- expect(v.join()).toBe("2,4,6,18");
29
- });
30
- });
@@ -1,20 +0,0 @@
1
- import { SourceEmpty } from "./SourceEmpty";
2
- import { expect, test } from "vitest";
3
-
4
- test("SourceEmpty.test", () => {
5
- const source = new SourceEmpty<number>();
6
- let accumulator = 0;
7
-
8
- // Не вызывается потому что нет значения
9
- source.value(() => {
10
- accumulator += 100;
11
- });
12
-
13
- // Вызывается после прихода значения
14
- source.give(200);
15
- source.value((value) => {
16
- accumulator += value;
17
- });
18
-
19
- expect(accumulator).toBe(200);
20
- });
@@ -1,31 +0,0 @@
1
- import { PatronPool } from "../Patron/PatronPool";
2
- import { GuestCast } from "../Guest/GuestCast";
3
- import { give, GuestType } from "./../Guest/Guest";
4
- import { Source, SourceType } from "./Source";
5
-
6
- /**
7
- * @url https://kosukhin.github.io/patron.site/#/source/source-empty
8
- */
9
- export class SourceEmpty<T> implements SourceType<T> {
10
- private baseSource = new Source<T | null>(null);
11
-
12
- public value(guest: GuestType<T>) {
13
- this.baseSource.value(
14
- new GuestCast(guest as GuestType, (value, options) => {
15
- if (value !== null) {
16
- give(value, guest, options);
17
- }
18
- }),
19
- );
20
- return this;
21
- }
22
-
23
- public give(value: T): this {
24
- this.baseSource.give(value);
25
- return this;
26
- }
27
-
28
- public pool(): PatronPool<T> {
29
- return this.baseSource.pool();
30
- }
31
- }