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.
- package/CHANGELOG.md +20 -0
- package/dist/patron.cjs +359 -268
- package/dist/patron.cjs.map +1 -1
- package/dist/patron.d.ts +215 -150
- package/dist/patron.js +343 -261
- package/dist/patron.js.map +1 -1
- package/dist/patron.min.js +1 -1
- package/dist/patron.min.mjs +1 -1
- package/dist/patron.min.mjs.map +1 -1
- package/dist/patron.mjs +343 -261
- package/dist/patron.mjs.map +1 -1
- package/docs/README.md +3 -0
- package/docs/assets/css/base.css +42 -0
- package/docs/assets/favicon/android-icon-144x144.png +0 -0
- package/docs/assets/favicon/android-icon-192x192.png +0 -0
- package/docs/assets/favicon/android-icon-36x36.png +0 -0
- package/docs/assets/favicon/android-icon-48x48.png +0 -0
- package/docs/assets/favicon/android-icon-72x72.png +0 -0
- package/docs/assets/favicon/android-icon-96x96.png +0 -0
- package/docs/assets/favicon/apple-icon-114x114.png +0 -0
- package/docs/assets/favicon/apple-icon-120x120.png +0 -0
- package/docs/assets/favicon/apple-icon-144x144.png +0 -0
- package/docs/assets/favicon/apple-icon-152x152.png +0 -0
- package/docs/assets/favicon/apple-icon-180x180.png +0 -0
- package/docs/assets/favicon/apple-icon-57x57.png +0 -0
- package/docs/assets/favicon/apple-icon-60x60.png +0 -0
- package/docs/assets/favicon/apple-icon-72x72.png +0 -0
- package/docs/assets/favicon/apple-icon-76x76.png +0 -0
- package/docs/assets/favicon/apple-icon-precomposed.png +0 -0
- package/docs/assets/favicon/apple-icon.png +0 -0
- package/docs/assets/favicon/browserconfig.xml +2 -0
- package/docs/assets/favicon/favicon-16x16.png +0 -0
- package/docs/assets/favicon/favicon-32x32.png +0 -0
- package/docs/assets/favicon/favicon-96x96.png +0 -0
- package/docs/assets/favicon/favicon.ico +0 -0
- package/docs/assets/favicon/manifest.json +41 -0
- package/docs/assets/favicon/ms-icon-144x144.png +0 -0
- package/docs/assets/favicon/ms-icon-150x150.png +0 -0
- package/docs/assets/favicon/ms-icon-310x310.png +0 -0
- package/docs/assets/favicon/ms-icon-70x70.png +0 -0
- package/docs/assets/img/404.jpg +0 -0
- package/docs/assets/img/angular_16.jpg +0 -0
- package/docs/assets/img/angular_32.jpg +0 -0
- package/docs/assets/img/eo_16.jpg +0 -0
- package/docs/assets/img/eo_big.png +0 -0
- package/docs/assets/img/github_16.jpg +0 -0
- package/docs/assets/img/logo.jpg +0 -0
- package/docs/assets/img/philosofy/observable.jpg +0 -0
- package/docs/assets/img/philosofy/responsible.jpg +0 -0
- package/docs/assets/img/philosofy/result.jpg +0 -0
- package/docs/assets/img/philosofy/tell.jpg +0 -0
- package/docs/assets/img/react_16.jpg +0 -0
- package/docs/assets/img/react_32.jpg +0 -0
- package/docs/assets/img/vue_16.jpg +0 -0
- package/docs/assets/img/vue_32.jpg +0 -0
- package/docs/assets/js/components/linkDynamic.mjs +11 -0
- package/docs/assets/js/components/linkReloadable.mjs +13 -0
- package/docs/assets/js/entrypoints/compatibility.mjs +31 -0
- package/docs/assets/js/entrypoints/components.mjs +2 -0
- package/docs/assets/js/entrypoints/examples.mjs +25 -0
- package/docs/assets/js/entrypoints/factory.mjs +20 -0
- package/docs/assets/js/entrypoints/guest.mjs +61 -0
- package/docs/assets/js/entrypoints/index.mjs +110 -0
- package/docs/assets/js/entrypoints/integrations.mjs +36 -0
- package/docs/assets/js/entrypoints/patron.mjs +46 -0
- package/docs/assets/js/entrypoints/source.mjs +76 -0
- package/docs/assets/js/entrypoints/terminology.mjs +51 -0
- package/docs/assets/js/entrypoints/utils.mjs +61 -0
- package/docs/assets/js/lib/StyleFetched.mjs +14 -0
- package/docs/assets/js/routing.mjs +110 -0
- package/docs/assets/schemes/dip.html +28 -0
- package/docs/favicon.ico +0 -0
- package/docs/index-dev.html +346 -0
- package/docs/index.html +346 -0
- package/docs/manifest.json +41 -0
- package/docs/pages/404.html +9 -0
- package/docs/pages/common/layout.html +17 -0
- package/docs/pages/compatibility/dip.html +82 -0
- package/docs/pages/compatibility/elegant-objects.html +373 -0
- package/docs/pages/compatibility/layout.html +17 -0
- package/docs/pages/examples/errors.html +167 -0
- package/docs/pages/examples/index.html +10 -0
- package/docs/pages/examples.html +41 -0
- package/docs/pages/factory/index.html +18 -0
- package/docs/pages/factory.html +36 -0
- package/docs/pages/guest/guest-applied.html +51 -0
- package/docs/pages/guest/guest-cast.html +219 -0
- package/docs/pages/guest/guest-disposable.html +147 -0
- package/docs/pages/guest/guest-executor-applied.html +65 -0
- package/docs/pages/guest/guest-object.html +101 -0
- package/docs/pages/guest/guest-pool.html +107 -0
- package/docs/pages/guest/guest-sync.html +66 -0
- package/docs/pages/guest/index.html +115 -0
- package/docs/pages/guest.html +59 -0
- package/docs/pages/index.html +72 -0
- package/docs/pages/integrations/angular.html +5 -0
- package/docs/pages/integrations/layout.html +17 -0
- package/docs/pages/integrations/react.html +7 -0
- package/docs/pages/integrations/vue.html +102 -0
- package/docs/pages/patron/index.html +140 -0
- package/docs/pages/patron/patron-applied.html +66 -0
- package/docs/pages/patron/patron-executor-applied.html +94 -0
- package/docs/pages/patron/patron-once.html +110 -0
- package/docs/pages/patron/patron-pool.html +99 -0
- package/docs/pages/patron.html +50 -0
- package/docs/pages/philosofy.html +210 -0
- package/docs/pages/source/index.html +167 -0
- package/docs/pages/source/source-active.html +113 -0
- package/docs/pages/source/source-all.html +129 -0
- package/docs/pages/source/source-applied.html +73 -0
- package/docs/pages/source/source-dynamic.html +93 -0
- package/docs/pages/source/source-executor-applied.html +64 -0
- package/docs/pages/source/source-map.html +192 -0
- package/docs/pages/source/source-once.html +73 -0
- package/docs/pages/source/source-race.html +171 -0
- package/docs/pages/source/source-sequence.html +160 -0
- package/docs/pages/source/source-with-pool.html +102 -0
- package/docs/pages/source/source.html +167 -0
- package/docs/pages/source.html +68 -0
- package/docs/pages/terminology/guest.html +14 -0
- package/docs/pages/terminology/index.html +6 -0
- package/docs/pages/terminology/introduction.html +25 -0
- package/docs/pages/terminology/patron.html +10 -0
- package/docs/pages/terminology/source.html +55 -0
- package/docs/pages/terminology/visitor.html +19 -0
- package/docs/pages/terminology.html +53 -0
- package/docs/pages/utils/give.html +47 -0
- package/docs/pages/utils/index.html +24 -0
- package/docs/pages/utils/is-guest-aware.html +22 -0
- package/docs/pages/utils/is-guest.html +21 -0
- package/docs/pages/utils/is-patron-in-pools.html +53 -0
- package/docs/pages/utils/private.html +44 -0
- package/docs/pages/utils/remove-patron-from-pools.html +57 -0
- package/docs/pages/utils/value.html +47 -0
- package/docs/pages/utils.html +61 -0
- package/eslint.config.mjs +6 -0
- package/package.json +1 -1
- package/src/Guest/Guest.test.ts +11 -6
- package/src/Guest/Guest.ts +7 -14
- package/src/Guest/GuestApplied.test.ts +13 -0
- package/src/Guest/GuestApplied.ts +16 -0
- package/src/Guest/GuestCast.test.ts +4 -5
- package/src/Guest/GuestCast.ts +3 -10
- package/src/Guest/GuestDisposable.test.ts +13 -17
- package/src/Guest/GuestDisposable.ts +3 -3
- package/src/Guest/GuestExecutorApplied.test.ts +27 -0
- package/src/Guest/GuestExecutorApplied.ts +23 -0
- package/src/Guest/GuestObject.test.ts +6 -6
- package/src/Guest/GuestObject.ts +3 -3
- package/src/Guest/GuestPool.test.ts +5 -22
- package/src/Guest/GuestPool.ts +7 -8
- package/src/Guest/GuestSync.test.ts +2 -2
- package/src/Patron/Patron.test.ts +4 -5
- package/src/Patron/Patron.ts +11 -3
- package/src/Patron/PatronApplied.ts +22 -0
- package/src/Patron/PatronExecutorApplied.ts +25 -0
- package/src/Patron/PatronOnce.sourceEmpty.test.ts +2 -2
- package/src/Patron/PatronOnce.test.ts +2 -2
- package/src/Patron/PatronOnce.ts +3 -9
- package/src/Patron/PatronPool.test.ts +2 -1
- package/src/Patron/PatronPool.ts +22 -27
- package/src/Private/PrivateClass.modules.test.ts +11 -8
- package/src/Private/PrivateClass.test.ts +7 -6
- package/src/Source/Source.test.ts +9 -5
- package/src/Source/Source.ts +46 -30
- package/src/Source/SourceActive.test.ts +13 -0
- package/src/Source/SourceActive.ts +41 -0
- package/src/Source/SourceAll._asArray.test.ts +22 -0
- package/src/Source/SourceAll._twoValuesAfter.test.ts +19 -0
- package/src/Source/SourceAll._twoValuesBefore.test.ts +19 -0
- package/src/Source/SourceAll._withPatron.test.ts +25 -0
- package/src/{Guest/GuestAwareAll.ts → Source/SourceAll.ts} +11 -11
- package/src/Source/SourceApplied.test.ts +14 -0
- package/src/Source/SourceApplied.ts +23 -0
- package/src/Source/SourceDynamic.ofSource.test.ts +12 -12
- package/src/Source/SourceDynamic.test.ts +9 -9
- package/src/Source/SourceDynamic.ts +7 -7
- package/src/Source/SourceExecutorApplied.test.ts +30 -0
- package/src/Source/SourceExecutorApplied.ts +22 -0
- package/src/Source/SourceMap.defered.test.ts +46 -0
- package/src/Source/SourceMap.fn.test.ts +27 -0
- package/src/Source/SourceMap.test.ts +32 -0
- package/src/Source/SourceMap.ts +48 -0
- package/src/Source/SourceOnce.notcalled.test.ts +4 -4
- package/src/Source/SourceOnce.test.ts +4 -5
- package/src/Source/SourceOnce.ts +7 -13
- package/src/Source/SourceRace.test.ts +46 -0
- package/src/Source/SourceRace.ts +30 -0
- package/src/Source/SourceSequence.defered.test.ts +53 -0
- package/src/Source/SourceSequence.test.ts +30 -0
- package/src/{Guest/GuestAwareSequence.ts → Source/SourceSequence.ts} +14 -19
- package/src/Source/SourceWithPool.empty.test.ts +14 -0
- package/src/Source/SourceWithPool.test.ts +10 -0
- package/src/Source/SourceWithPool.ts +58 -0
- package/src/index.ts +13 -7
- package/test-utils/debounce.ts +11 -0
- package/test-utils/id.ts +1 -0
- package/test-utils/wait.ts +6 -3
- package/src/Guest/GuestAware.test.ts +0 -13
- package/src/Guest/GuestAware.ts +0 -59
- package/src/Guest/GuestAwareActive.test.ts +0 -12
- package/src/Guest/GuestAwareActive.ts +0 -42
- package/src/Guest/GuestAwareAll._asArray.test.ts +0 -19
- package/src/Guest/GuestAwareAll._twoValuesAfter.test.ts +0 -16
- package/src/Guest/GuestAwareAll._twoValuesBefore.test.ts +0 -16
- package/src/Guest/GuestAwareAll._withPatron.test.ts +0 -22
- package/src/Guest/GuestAwareMap.defered.test.ts +0 -46
- package/src/Guest/GuestAwareMap.fn.test.ts +0 -27
- package/src/Guest/GuestAwareMap.test.ts +0 -29
- package/src/Guest/GuestAwareMap.ts +0 -48
- package/src/Guest/GuestAwareRace.test.ts +0 -46
- package/src/Guest/GuestAwareRace.ts +0 -33
- package/src/Guest/GuestAwareSequence.defered.test.ts +0 -58
- package/src/Guest/GuestAwareSequence.test.ts +0 -30
- package/src/Source/SourceEmpty.test.ts +0 -20
- package/src/Source/SourceEmpty.ts +0 -31
@@ -0,0 +1,13 @@
|
|
1
|
+
import { GuestApplied } from "../Guest/GuestApplied";
|
2
|
+
import { expect, test, vitest } from "vitest";
|
3
|
+
import { SourceWithPool } from "../Source/SourceWithPool";
|
4
|
+
|
5
|
+
test("GuestApplied.test", () => {
|
6
|
+
const one = new SourceWithPool(1);
|
7
|
+
const guest = vitest.fn();
|
8
|
+
|
9
|
+
one.value(new GuestApplied(guest, (v) => v * 2));
|
10
|
+
|
11
|
+
expect(guest).toBeCalled();
|
12
|
+
expect(guest).toBeCalledWith(2);
|
13
|
+
});
|
@@ -0,0 +1,16 @@
|
|
1
|
+
import { give, GuestObjectType, GuestType } from "../Guest/Guest";
|
2
|
+
|
3
|
+
/**
|
4
|
+
* @url https://kosukhin.github.io/patron.site/#/guest/guest-applied
|
5
|
+
*/
|
6
|
+
export class GuestApplied<T, R> implements GuestObjectType<T> {
|
7
|
+
public constructor(
|
8
|
+
private baseGuest: GuestType<R>,
|
9
|
+
private applier: (value: T) => R,
|
10
|
+
) {}
|
11
|
+
|
12
|
+
public give(value: T): this {
|
13
|
+
give(this.applier(value), this.baseGuest);
|
14
|
+
return this;
|
15
|
+
}
|
16
|
+
}
|
@@ -1,17 +1,18 @@
|
|
1
|
-
import { SourceEmpty } from "../Source/SourceEmpty";
|
2
1
|
import { expect, test } from "vitest";
|
3
2
|
import { Patron } from "../Patron/Patron";
|
4
3
|
import { give, Guest } from "./Guest";
|
5
4
|
import { GuestCast } from "./GuestCast";
|
5
|
+
import { SourceWithPool } from "../Source/SourceWithPool";
|
6
6
|
|
7
7
|
test("GuestCast.test", () => {
|
8
|
-
const source = new
|
8
|
+
const source = new SourceWithPool();
|
9
9
|
let acc = 0;
|
10
10
|
const mainGuest = new Patron(
|
11
11
|
new Guest((value: number) => {
|
12
12
|
acc += value;
|
13
13
|
}),
|
14
14
|
);
|
15
|
+
|
15
16
|
// Становится патроном тоже, тк наследует это сойство от mainGuest
|
16
17
|
const secondGuest = new GuestCast(
|
17
18
|
mainGuest,
|
@@ -25,7 +26,5 @@ test("GuestCast.test", () => {
|
|
25
26
|
|
26
27
|
give(2, source);
|
27
28
|
|
28
|
-
|
29
|
-
expect(acc).toBe(4);
|
30
|
-
});
|
29
|
+
expect(acc).toBe(4);
|
31
30
|
});
|
package/src/Guest/GuestCast.ts
CHANGED
@@ -1,6 +1,5 @@
|
|
1
|
+
import { give, GuestType } from "./Guest";
|
1
2
|
import { GuestDisposableType, MaybeDisposableType } from "./GuestDisposable";
|
2
|
-
import { give, GiveOptions, GuestType } from "./Guest";
|
3
|
-
import { PoolAwareOptions } from "../Patron/PatronOnce";
|
4
3
|
|
5
4
|
/**
|
6
5
|
* @url https://kosukhin.github.io/patron.site/#/guest/guest-cast
|
@@ -28,14 +27,8 @@ export class GuestCast<T> implements GuestDisposableType<T> {
|
|
28
27
|
return this.sourceGuest.introduction();
|
29
28
|
}
|
30
29
|
|
31
|
-
public give(value: T
|
32
|
-
give(value, this.targetGuest
|
33
|
-
...options,
|
34
|
-
data: {
|
35
|
-
...(options?.data ?? {}),
|
36
|
-
castedGuest: (options?.data as PoolAwareOptions)?.castedGuest ?? this,
|
37
|
-
},
|
38
|
-
});
|
30
|
+
public give(value: T): this {
|
31
|
+
give(value, this.targetGuest);
|
39
32
|
return this;
|
40
33
|
}
|
41
34
|
|
@@ -1,30 +1,26 @@
|
|
1
1
|
import { GuestDisposable } from "./GuestDisposable";
|
2
2
|
import { Patron } from "../Patron/Patron";
|
3
|
-
import {
|
4
|
-
import { expect, test } from "vitest";
|
3
|
+
import { SourceWithPool } from "../Source/SourceWithPool";
|
4
|
+
import { expect, test, vitest } from "vitest";
|
5
5
|
|
6
6
|
test("GuestDisposable.test", () => {
|
7
|
-
const source = new
|
7
|
+
const source = new SourceWithPool(1);
|
8
|
+
|
9
|
+
const guest = vitest.fn();
|
8
10
|
|
9
11
|
// Работает проверка один раз, потом патром себя удаляет
|
10
12
|
source.value(
|
11
13
|
new Patron(
|
12
|
-
new GuestDisposable(
|
13
|
-
|
14
|
-
|
15
|
-
},
|
16
|
-
(value) => {
|
17
|
-
return value !== null && value > 1;
|
18
|
-
},
|
19
|
-
),
|
14
|
+
new GuestDisposable(guest, (value) => {
|
15
|
+
return value !== null && value > 1;
|
16
|
+
}),
|
20
17
|
),
|
21
18
|
);
|
22
19
|
|
23
20
|
// Эти выражения не вызывает expect
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
});
|
21
|
+
source.give(2);
|
22
|
+
source.give(3);
|
23
|
+
|
24
|
+
expect(guest).toBeCalledTimes(1);
|
25
|
+
expect(guest).toBeCalledWith(1);
|
30
26
|
});
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { give,
|
1
|
+
import { give, GuestObjectType, GuestType } from "./Guest";
|
2
2
|
|
3
3
|
export interface GuestDisposableType<T = any> extends GuestObjectType<T> {
|
4
4
|
disposed(value: T | null): boolean;
|
@@ -26,8 +26,8 @@ export class GuestDisposable<T> implements GuestDisposableType<T> {
|
|
26
26
|
return this.disposeCheck(value);
|
27
27
|
}
|
28
28
|
|
29
|
-
public give(value: T
|
30
|
-
give(value, this.guest
|
29
|
+
public give(value: T): this {
|
30
|
+
give(value, this.guest);
|
31
31
|
return this;
|
32
32
|
}
|
33
33
|
}
|
@@ -0,0 +1,27 @@
|
|
1
|
+
import { Patron } from "../Patron/Patron";
|
2
|
+
import { expect, test, vi } from "vitest";
|
3
|
+
import { SourceWithPool } from "../Source/SourceWithPool";
|
4
|
+
import { GuestExecutorApplied } from "../Guest/GuestExecutorApplied";
|
5
|
+
import { debounce } from "../../test-utils/debounce";
|
6
|
+
|
7
|
+
test("GuestExecutorApplied.test", () => {
|
8
|
+
vi.useFakeTimers({ shouldAdvanceTime: true });
|
9
|
+
let counter = 0;
|
10
|
+
const guest = (v: number) => {
|
11
|
+
counter += v;
|
12
|
+
};
|
13
|
+
|
14
|
+
const source = new SourceWithPool();
|
15
|
+
source.value(
|
16
|
+
new Patron(new GuestExecutorApplied(guest, debounce.bind(null, 100))),
|
17
|
+
);
|
18
|
+
|
19
|
+
source.give(1);
|
20
|
+
source.give(1);
|
21
|
+
source.give(1);
|
22
|
+
|
23
|
+
vi.runOnlyPendingTimers();
|
24
|
+
|
25
|
+
expect(counter).toBe(1);
|
26
|
+
vi.useRealTimers();
|
27
|
+
});
|
@@ -0,0 +1,23 @@
|
|
1
|
+
import {
|
2
|
+
give,
|
3
|
+
GuestExecutorType,
|
4
|
+
GuestObjectType,
|
5
|
+
GuestType,
|
6
|
+
} from "../Guest/Guest";
|
7
|
+
|
8
|
+
/**
|
9
|
+
* @url https://kosukhin.github.io/patron.site/#/guest/guest-executor-applied
|
10
|
+
*/
|
11
|
+
export class GuestExecutorApplied<T> implements GuestObjectType<T> {
|
12
|
+
public give: GuestExecutorType<T, this>;
|
13
|
+
|
14
|
+
public constructor(
|
15
|
+
baseGuest: GuestType<T>,
|
16
|
+
applier: (executor: GuestExecutorType) => GuestExecutorType,
|
17
|
+
) {
|
18
|
+
this.give = applier((v) => give(v, baseGuest)) as GuestExecutorType<
|
19
|
+
T,
|
20
|
+
this
|
21
|
+
>;
|
22
|
+
}
|
23
|
+
}
|
@@ -1,11 +1,11 @@
|
|
1
|
-
import { expect, test } from "vitest";
|
2
|
-
import {
|
1
|
+
import { expect, test, vitest } from "vitest";
|
2
|
+
import { SourceWithPool } from "../Source/SourceWithPool";
|
3
3
|
import { GuestObject } from "./GuestObject";
|
4
4
|
|
5
5
|
test("GuestObject.test", () => {
|
6
|
-
const source = new
|
7
|
-
const fnGuest = (
|
8
|
-
expect(value).toBe(1);
|
9
|
-
};
|
6
|
+
const source = new SourceWithPool(1);
|
7
|
+
const fnGuest = vitest.fn();
|
10
8
|
source.value(new GuestObject(fnGuest));
|
9
|
+
expect(fnGuest).toBeCalled();
|
10
|
+
expect(fnGuest).toBeCalledWith(1);
|
11
11
|
});
|
package/src/Guest/GuestObject.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
+
import { Guest, GuestType } from "./Guest";
|
1
2
|
import { GuestDisposableType, MaybeDisposableType } from "./GuestDisposable";
|
2
|
-
import { GiveOptions, Guest, GuestType } from "./Guest";
|
3
3
|
|
4
4
|
/**
|
5
5
|
* @url https://kosukhin.github.io/patron.site/#/guest/guest-object
|
@@ -11,12 +11,12 @@ export class GuestObject<T> implements GuestDisposableType<T> {
|
|
11
11
|
}
|
12
12
|
}
|
13
13
|
|
14
|
-
public give(value: T
|
14
|
+
public give(value: T): this {
|
15
15
|
let guest = this.baseGuest;
|
16
16
|
if (typeof guest === "function") {
|
17
17
|
guest = new Guest(guest);
|
18
18
|
}
|
19
|
-
guest.give(value
|
19
|
+
guest.give(value);
|
20
20
|
return this;
|
21
21
|
}
|
22
22
|
|
@@ -1,16 +1,6 @@
|
|
1
|
-
import { expect, test
|
2
|
-
import { GuestPool } from "./GuestPool";
|
1
|
+
import { expect, test } from "vitest";
|
3
2
|
import { Patron } from "../Patron/Patron";
|
4
|
-
import {
|
5
|
-
|
6
|
-
beforeEach(() => {
|
7
|
-
vi.useFakeTimers({ shouldAdvanceTime: true });
|
8
|
-
});
|
9
|
-
|
10
|
-
afterEach(() => {
|
11
|
-
vi.runOnlyPendingTimers();
|
12
|
-
vi.useRealTimers();
|
13
|
-
});
|
3
|
+
import { GuestPool } from "./GuestPool";
|
14
4
|
|
15
5
|
test("GuestPool.test", async () => {
|
16
6
|
const pool = new GuestPool<number>(null);
|
@@ -32,16 +22,9 @@ test("GuestPool.test", async () => {
|
|
32
22
|
pool.add((value) => {
|
33
23
|
receivedCount += value;
|
34
24
|
});
|
35
|
-
pool.give(2);
|
36
|
-
|
37
|
-
setTimeout(() => {
|
38
|
-
pool.give(2);
|
39
|
-
});
|
40
25
|
|
41
|
-
|
26
|
+
pool.give(2);
|
27
|
+
pool.give(2);
|
42
28
|
|
43
|
-
|
44
|
-
pool.give(2);
|
45
|
-
expect(receivedCount).toBe(10);
|
46
|
-
}, 10);
|
29
|
+
expect(receivedCount).toBe(10);
|
47
30
|
});
|
package/src/Guest/GuestPool.ts
CHANGED
@@ -1,6 +1,5 @@
|
|
1
|
-
import { PatronPool } from "../Patron/PatronPool";
|
2
|
-
import {
|
3
|
-
import { give, GuestObjectType, GuestType, GiveOptions } from "./Guest";
|
1
|
+
import { PatronPool, PoolType } from "../Patron/PatronPool";
|
2
|
+
import { give, GuestObjectType, GuestType } from "./Guest";
|
4
3
|
|
5
4
|
/**
|
6
5
|
* @url https://kosukhin.github.io/patron.site/#/guest/guest-pool
|
@@ -14,9 +13,9 @@ export class GuestPool<T> implements GuestObjectType<T>, PoolType<T> {
|
|
14
13
|
this.patronPool = new PatronPool(initiator);
|
15
14
|
}
|
16
15
|
|
17
|
-
public give(value: T
|
18
|
-
this.deliverToGuests(value
|
19
|
-
this.patronPool.give(value
|
16
|
+
public give(value: T): this {
|
17
|
+
this.deliverToGuests(value);
|
18
|
+
this.patronPool.give(value);
|
20
19
|
return this;
|
21
20
|
}
|
22
21
|
|
@@ -48,9 +47,9 @@ export class GuestPool<T> implements GuestObjectType<T>, PoolType<T> {
|
|
48
47
|
return this.patronPool.size() + this.guests.size;
|
49
48
|
}
|
50
49
|
|
51
|
-
private deliverToGuests(value: T
|
50
|
+
private deliverToGuests(value: T) {
|
52
51
|
this.guests.forEach((target) => {
|
53
|
-
give(value, target
|
52
|
+
give(value, target);
|
54
53
|
});
|
55
54
|
this.guests.clear();
|
56
55
|
}
|
@@ -1,9 +1,9 @@
|
|
1
1
|
import { expect, test } from "vitest";
|
2
2
|
import { GuestSync } from "./GuestSync";
|
3
|
-
import {
|
3
|
+
import { SourceWithPool } from "../Source/SourceWithPool";
|
4
4
|
|
5
5
|
test("GuestSync.test", () => {
|
6
|
-
const source = new
|
6
|
+
const source = new SourceWithPool(123);
|
7
7
|
const syncGuest = new GuestSync(111);
|
8
8
|
syncGuest.give(222);
|
9
9
|
expect(syncGuest.value()).toBe(222);
|
@@ -1,17 +1,16 @@
|
|
1
1
|
import { expect, test } from "vitest";
|
2
2
|
import { Patron } from "./Patron";
|
3
|
-
import {
|
3
|
+
import { SourceWithPool } from "../Source/SourceWithPool";
|
4
4
|
|
5
5
|
test("Patron.test", () => {
|
6
|
-
const one = new
|
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
|
-
|
15
|
-
expect(patronCalledTimes).toBe(2);
|
16
|
-
});
|
15
|
+
expect(patronCalledTimes).toBe(2);
|
17
16
|
});
|
package/src/Patron/Patron.ts
CHANGED
@@ -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
|
19
|
-
give(value, this.willBePatron
|
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";
|
@@ -0,0 +1,22 @@
|
|
1
|
+
import { GuestObjectType, GuestType } from "../Guest/Guest";
|
2
|
+
import { GuestApplied } from "../Guest/GuestApplied";
|
3
|
+
|
4
|
+
/**
|
5
|
+
* @url https://kosukhin.github.io/patron.site/#/patron/patron-applied
|
6
|
+
*/
|
7
|
+
export class PatronApplied<T, R> implements GuestObjectType<T> {
|
8
|
+
private guestApplied: GuestApplied<T, R>;
|
9
|
+
|
10
|
+
public constructor(baseGuest: GuestType<R>, applier: (value: T) => R) {
|
11
|
+
this.guestApplied = new GuestApplied(baseGuest, applier);
|
12
|
+
}
|
13
|
+
|
14
|
+
public give(value: T): this {
|
15
|
+
this.guestApplied.give(value);
|
16
|
+
return this;
|
17
|
+
}
|
18
|
+
|
19
|
+
public introduction(): "guest" | "patron" {
|
20
|
+
return "patron";
|
21
|
+
}
|
22
|
+
}
|
@@ -0,0 +1,25 @@
|
|
1
|
+
import { GuestExecutorType, GuestObjectType, GuestType } from "../Guest/Guest";
|
2
|
+
import { GuestExecutorApplied } from "../Guest/GuestExecutorApplied";
|
3
|
+
|
4
|
+
/**
|
5
|
+
* @url https://kosukhin.github.io/patron.site/#/patron/patron-executor-applied
|
6
|
+
*/
|
7
|
+
export class PatronExecutorApplied<T> implements GuestObjectType<T> {
|
8
|
+
private guestApplied: GuestExecutorApplied<T>;
|
9
|
+
|
10
|
+
public constructor(
|
11
|
+
baseGuest: GuestType<T>,
|
12
|
+
applier: (executor: GuestExecutorType) => GuestExecutorType,
|
13
|
+
) {
|
14
|
+
this.guestApplied = new GuestExecutorApplied(baseGuest, applier);
|
15
|
+
}
|
16
|
+
|
17
|
+
public give(value: T): this {
|
18
|
+
this.guestApplied.give(value);
|
19
|
+
return this;
|
20
|
+
}
|
21
|
+
|
22
|
+
public introduction(): "guest" | "patron" {
|
23
|
+
return "patron";
|
24
|
+
}
|
25
|
+
}
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { afterEach, beforeEach, expect, test, vi } from "vitest";
|
2
2
|
import { PatronOnce } from "./PatronOnce";
|
3
|
-
import {
|
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
|
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 {
|
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
|
16
|
+
const source = new SourceWithPool(12);
|
17
17
|
let calls = 0;
|
18
18
|
const patron = new PatronOnce(() => {
|
19
19
|
calls += 1;
|
package/src/Patron/PatronOnce.ts
CHANGED
@@ -1,15 +1,9 @@
|
|
1
|
-
import {
|
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
|
23
|
+
public give(value: T): this {
|
30
24
|
if (!this.received) {
|
31
25
|
this.received = true;
|
32
|
-
give(value, this.baseGuest
|
26
|
+
give(value, this.baseGuest);
|
33
27
|
}
|
34
28
|
return this;
|
35
29
|
}
|
package/src/Patron/PatronPool.ts
CHANGED
@@ -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
|
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
|
-
|
53
|
-
const doReceive = (value: T, options?: GiveOptions) => {
|
65
|
+
const doReceive = (value: T) => {
|
54
66
|
this.patrons.forEach((target) => {
|
55
|
-
this.sendValueToGuest(value, target
|
67
|
+
this.sendValueToGuest(value, target);
|
56
68
|
});
|
57
69
|
};
|
58
|
-
this.give = (value: T
|
59
|
-
|
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 {
|
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:
|
8
|
+
private source: SourceWithPoolType;
|
9
9
|
|
10
10
|
public constructor(
|
11
11
|
baseNum: number,
|
12
|
-
modules: { main: PrivateType<
|
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(
|
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
|
-
|
31
|
-
|
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 {
|
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(
|
6
|
+
const sourcePrivate = new PrivateClass(SourceWithPool);
|
7
7
|
const source = sourcePrivate.get(42);
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
const guest = vitest.fn();
|
10
|
+
source.value(guest);
|
11
|
+
|
12
|
+
expect(guest).toBeCalledWith(42);
|
12
13
|
});
|