patron-oop 1.42.1 → 1.44.1
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 +16 -0
- package/dist/patron.cjs +16 -17
- package/dist/patron.cjs.map +1 -1
- package/dist/patron.d.ts +1 -1
- package/dist/patron.js +16 -17
- 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 +16 -17
- package/dist/patron.mjs.map +1 -1
- package/package.json +1 -1
- package/src/Guest/GuestAwareAll._twoValuesAfter.test.ts +1 -1
- package/src/Guest/GuestAwareAll.ts +16 -18
- package/src/Source/SourceOnce.notcalled.test.ts +13 -0
- package/src/Source/SourceOnce.test.ts +11 -0
- package/src/Source/SourceOnce.ts +37 -0
@@ -5,7 +5,7 @@ import { GuestAwareAll } from "./GuestAwareAll";
|
|
5
5
|
test("GuestAwareAll._twoValuesAfter.test", () => {
|
6
6
|
const one = new Source(1);
|
7
7
|
const two = new Source(2);
|
8
|
-
const all = new GuestAwareAll<{ one: number; two: number }>();
|
8
|
+
const all = new GuestAwareAll<{ one: number; two: number }>(["one", "two"]);
|
9
9
|
|
10
10
|
all.value((value) => {
|
11
11
|
expect(Object.values(value).join()).toBe("1,2");
|
@@ -16,14 +16,15 @@ export interface GuestAwareAllType<T = any> extends GuestAwareObjectType<T> {
|
|
16
16
|
export class GuestAwareAll<T> implements GuestAwareAllType<T> {
|
17
17
|
private theAll: Source<Record<string, unknown>>;
|
18
18
|
|
19
|
-
private keysKnown
|
19
|
+
private keysKnown: Set<string>;
|
20
20
|
|
21
21
|
private keysFilled = new Set();
|
22
22
|
|
23
23
|
private filledAllPool = new GuestPool(this);
|
24
24
|
|
25
|
-
public constructor() {
|
25
|
+
public constructor(initialKnownKeys: string[] = []) {
|
26
26
|
this.theAll = new Source<Record<string, unknown>>({});
|
27
|
+
this.keysKnown = new Set(initialKnownKeys);
|
27
28
|
}
|
28
29
|
|
29
30
|
public valueArray(guest: GuestType<T>) {
|
@@ -61,22 +62,19 @@ export class GuestAwareAll<T> implements GuestAwareAllType<T> {
|
|
61
62
|
public guestKey<R>(key: string): GuestObjectType<R> {
|
62
63
|
this.keysKnown.add(key);
|
63
64
|
return new Guest((value) => {
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
this.
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
}),
|
78
|
-
);
|
79
|
-
});
|
65
|
+
this.theAll.value(
|
66
|
+
new Guest((all: Record<string, unknown>) => {
|
67
|
+
this.keysFilled.add(key);
|
68
|
+
const lastAll = {
|
69
|
+
...all,
|
70
|
+
[key]: value,
|
71
|
+
};
|
72
|
+
this.theAll.give(lastAll);
|
73
|
+
if (this.isAllFilled()) {
|
74
|
+
this.filledAllPool.give(lastAll);
|
75
|
+
}
|
76
|
+
}),
|
77
|
+
);
|
80
78
|
});
|
81
79
|
}
|
82
80
|
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import { expect, test, vi } 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
|
+
source.value((v) => {
|
11
|
+
expect(v).toBe(111);
|
12
|
+
});
|
13
|
+
});
|
@@ -0,0 +1,37 @@
|
|
1
|
+
import { PatronPool } from "../Patron/PatronPool";
|
2
|
+
import { GuestType } from "../Guest/Guest";
|
3
|
+
import { value } from "../Guest/GuestAware";
|
4
|
+
import { SourceType } from "../Source/Source";
|
5
|
+
import { SourceEmpty } from "../Source/SourceEmpty";
|
6
|
+
|
7
|
+
/**
|
8
|
+
* @url https://kosukhin.github.io/patron.site/#/source/source-once
|
9
|
+
*/
|
10
|
+
export class SourceOnce<T> implements SourceType<T> {
|
11
|
+
private source = new SourceEmpty<T>();
|
12
|
+
private isFilled = false;
|
13
|
+
|
14
|
+
public constructor(initialValue?: T) {
|
15
|
+
if (initialValue !== undefined) {
|
16
|
+
this.isFilled = true;
|
17
|
+
this.source.give(initialValue);
|
18
|
+
}
|
19
|
+
}
|
20
|
+
|
21
|
+
public value(guest: GuestType<T>) {
|
22
|
+
value(this.source, guest);
|
23
|
+
return this;
|
24
|
+
}
|
25
|
+
|
26
|
+
public give(value: T): this {
|
27
|
+
if (!this.isFilled) {
|
28
|
+
this.isFilled = true;
|
29
|
+
this.source.give(value);
|
30
|
+
}
|
31
|
+
return this;
|
32
|
+
}
|
33
|
+
|
34
|
+
public pool(): PatronPool<T> {
|
35
|
+
return this.source.pool();
|
36
|
+
}
|
37
|
+
}
|