patron-oop 1.4.0 → 1.5.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 +12 -0
- package/dist/patron.d.ts +14 -105
- package/dist/patron.js +134 -130
- package/dist/patron.js.map +1 -1
- package/dist/patron.min.js +1 -1
- package/dist/patron.mjs +132 -118
- package/dist/patron.mjs.map +1 -1
- package/examples/elegant_objects.html +3 -1
- package/package.json +1 -1
- package/src/Guest/Guest.ts +45 -0
- package/src/Guest/GuestAware.test.ts +15 -0
- package/src/{GuestAware.ts → Guest/GuestAware.ts} +1 -1
- package/src/Guest/GuestCallback.test.ts +13 -0
- package/src/{Guest.ts → Guest/GuestCallback.ts} +1 -1
- package/src/Guest/GuestCast.test.ts +31 -0
- package/src/{GuestCast.ts → Guest/GuestCast.ts} +1 -1
- package/src/Guest/GuestChain.test.ts +72 -0
- package/src/{Chain.ts → Guest/GuestChain.ts} +9 -9
- package/src/Guest/GuestInTheMiddle.test.ts +71 -0
- package/src/{GuestInTheMiddle.ts → Guest/GuestInTheMiddle.ts} +1 -1
- package/src/{GuestPool.test.ts → Guest/GuestPool.test.ts} +7 -7
- package/src/{GuestPool.ts → Guest/GuestPool.ts} +3 -3
- package/src/Guest/GuestSync.test.ts +13 -0
- package/src/{GuestSync.ts → Guest/GuestSync.ts} +1 -1
- package/src/Patron/Patron.ts +18 -0
- package/src/{Patron.test.ts → Patron/PatronOfGuest.test.ts} +6 -6
- package/src/{Patron.ts → Patron/PatronOfGuest.ts} +2 -2
- package/src/{PatronOnce.test.ts → Patron/PatronOnce.test.ts} +4 -4
- package/src/{PatronOnce.ts → Patron/PatronOnce.ts} +2 -2
- package/src/{PatronPool.test.ts → Patron/PatronPool.test.ts} +6 -6
- package/src/{PatronPool.ts → Patron/PatronPool.ts} +7 -2
- package/src/Source/Source.ts +12 -0
- package/src/Source/SourceOfValue.test.ts +13 -0
- package/src/{Source.ts → Source/SourceOfValue.ts} +4 -4
- package/src/Source/SourcesApplied.test.ts +14 -0
- package/src/Source/SourcesApplied.ts +40 -0
- package/src/index.ts +17 -15
- package/src/Cache.test.ts +0 -20
- package/src/Cache.ts +0 -33
- package/src/Chain.test.ts +0 -72
- package/src/Factory.test.ts +0 -16
- package/src/Factory.ts +0 -23
- package/src/FactoryDynamic.ts +0 -11
- package/src/FactoryWithFactories.ts +0 -25
- package/src/Guest.test.ts +0 -13
- package/src/GuestInTheMiddle.test.ts +0 -71
- package/src/GuestSync.test.ts +0 -11
- package/src/PoolType.ts +0 -7
- package/src/Source.test.ts +0 -13
package/src/Chain.test.ts
DELETED
@@ -1,72 +0,0 @@
|
|
1
|
-
import { expect, test } from "vitest";
|
2
|
-
import { Chain } from "./Chain";
|
3
|
-
import { Patron } from "./Patron";
|
4
|
-
import { Guest } from "./Guest";
|
5
|
-
import { Source } from "./Source";
|
6
|
-
|
7
|
-
test("chain guest returns 2 values after result guest", () => {
|
8
|
-
const one = new Source(1);
|
9
|
-
const two = new Source(2);
|
10
|
-
const chain = new Chain<{ one: number; two: number }>();
|
11
|
-
|
12
|
-
chain.result(
|
13
|
-
new Guest((value) => {
|
14
|
-
expect(Object.values(value).join()).toBe("1,2");
|
15
|
-
}),
|
16
|
-
);
|
17
|
-
|
18
|
-
one.receiving(chain.receiveKey("one"));
|
19
|
-
two.receiving(chain.receiveKey("two"));
|
20
|
-
});
|
21
|
-
|
22
|
-
test("chain guest returns 2 values before result guest", () => {
|
23
|
-
const one = new Source(1);
|
24
|
-
const two = new Source(2);
|
25
|
-
const chain = new Chain<{ one: number; two: number }>();
|
26
|
-
|
27
|
-
one.receiving(chain.receiveKey("one"));
|
28
|
-
two.receiving(chain.receiveKey("two"));
|
29
|
-
|
30
|
-
chain.result(
|
31
|
-
new Guest((value) => {
|
32
|
-
expect(Object.values(value).join()).toBe("1,2");
|
33
|
-
}),
|
34
|
-
);
|
35
|
-
});
|
36
|
-
|
37
|
-
test("chain with patron", () => {
|
38
|
-
const one = new Source(1);
|
39
|
-
const two = new Source(2);
|
40
|
-
const chain = new Chain<{ one: number; two: number }>();
|
41
|
-
|
42
|
-
one.receiving(new Patron(chain.receiveKey("one")));
|
43
|
-
two.receiving(new Patron(chain.receiveKey("two")));
|
44
|
-
|
45
|
-
one.receive(3);
|
46
|
-
one.receive(4);
|
47
|
-
|
48
|
-
chain.result(
|
49
|
-
new Patron(
|
50
|
-
new Guest((value) => {
|
51
|
-
expect(Object.values(value).length).toBe(2);
|
52
|
-
}),
|
53
|
-
),
|
54
|
-
);
|
55
|
-
});
|
56
|
-
|
57
|
-
test("chain as array", () => {
|
58
|
-
const one = new Source(1);
|
59
|
-
const two = new Source(2);
|
60
|
-
const chain = new Chain<[number, number]>();
|
61
|
-
|
62
|
-
one.receiving(new Patron(chain.receiveKey("0")));
|
63
|
-
two.receiving(new Patron(chain.receiveKey("1")));
|
64
|
-
|
65
|
-
chain.resultArray(
|
66
|
-
new Patron(
|
67
|
-
new Guest((value) => {
|
68
|
-
expect(JSON.stringify(value)).toBe("[1, 2]");
|
69
|
-
}),
|
70
|
-
),
|
71
|
-
);
|
72
|
-
});
|
package/src/Factory.test.ts
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
import { expect, test } from "vitest";
|
2
|
-
import { Factory } from "./Factory";
|
3
|
-
import { Guest } from "./Guest";
|
4
|
-
import { Source } from "./Source";
|
5
|
-
|
6
|
-
test("factory", () => {
|
7
|
-
const sourceFactory = new Factory(Source);
|
8
|
-
|
9
|
-
const source = sourceFactory.create(42);
|
10
|
-
|
11
|
-
source.receiving(
|
12
|
-
new Guest((value) => {
|
13
|
-
expect(value).toBe(42);
|
14
|
-
}),
|
15
|
-
);
|
16
|
-
});
|
package/src/Factory.ts
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
interface Constructable<T> {
|
2
|
-
new (...args: unknown[]): T;
|
3
|
-
}
|
4
|
-
|
5
|
-
interface Prototyped<T> {
|
6
|
-
prototype: T;
|
7
|
-
}
|
8
|
-
|
9
|
-
export interface FactoryType<T> {
|
10
|
-
create<R extends unknown[], CT = null>(...args: R): CT extends null ? T : CT;
|
11
|
-
}
|
12
|
-
|
13
|
-
export class Factory<T> implements FactoryType<T> {
|
14
|
-
public constructor(private constructorFn: Prototyped<T>) {}
|
15
|
-
|
16
|
-
public create<R extends unknown[], CT = null>(
|
17
|
-
...args: R
|
18
|
-
): CT extends null ? T : CT {
|
19
|
-
return new (this.constructorFn as Constructable<T>)(
|
20
|
-
...args,
|
21
|
-
) as CT extends null ? T : CT;
|
22
|
-
}
|
23
|
-
}
|
package/src/FactoryDynamic.ts
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
import { FactoryType } from "./Factory";
|
2
|
-
|
3
|
-
export class FactoryDynamic<T> implements FactoryType<T> {
|
4
|
-
public constructor(private creationFn: (...args: unknown[]) => T) {}
|
5
|
-
|
6
|
-
public create<R extends unknown[], CT = null>(
|
7
|
-
...args: R
|
8
|
-
): CT extends null ? T : CT {
|
9
|
-
return this.creationFn(...args) as CT extends null ? T : CT;
|
10
|
-
}
|
11
|
-
}
|
@@ -1,25 +0,0 @@
|
|
1
|
-
import { FactoryType } from "./Factory";
|
2
|
-
|
3
|
-
interface Constructable<T> {
|
4
|
-
new (...args: unknown[]): T;
|
5
|
-
}
|
6
|
-
|
7
|
-
interface Prototyped<T> {
|
8
|
-
prototype: T;
|
9
|
-
}
|
10
|
-
|
11
|
-
export class FactoryWithFactories<T> implements FactoryType<T> {
|
12
|
-
public constructor(
|
13
|
-
private constructorFn: Prototyped<T>,
|
14
|
-
private factories: Record<string, unknown> = {},
|
15
|
-
) {}
|
16
|
-
|
17
|
-
public create<R extends unknown[], CT = null>(
|
18
|
-
...args: R
|
19
|
-
): CT extends null ? T : CT {
|
20
|
-
return new (this.constructorFn as Constructable<T>)(
|
21
|
-
...args,
|
22
|
-
this.factories,
|
23
|
-
) as CT extends null ? T : CT;
|
24
|
-
}
|
25
|
-
}
|
package/src/Guest.test.ts
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
import { expect, test } from "vitest";
|
2
|
-
import { Guest } from "./Guest";
|
3
|
-
import { Source } from "./Source";
|
4
|
-
|
5
|
-
test("guest dynamic", () => {
|
6
|
-
const one = new Source(1);
|
7
|
-
|
8
|
-
one.receiving(
|
9
|
-
new Guest((value) => {
|
10
|
-
expect(value).toBe(1);
|
11
|
-
}),
|
12
|
-
);
|
13
|
-
});
|
@@ -1,71 +0,0 @@
|
|
1
|
-
import { expect, test } from "vitest";
|
2
|
-
import { Guest } from "./Guest";
|
3
|
-
import { GuestInTheMiddle } from "./GuestInTheMiddle";
|
4
|
-
import { Patron } from "./Patron";
|
5
|
-
import { Chain } from "./Chain";
|
6
|
-
import { Source } from "./Source";
|
7
|
-
|
8
|
-
test("test guest in the middle", () => {
|
9
|
-
const one = new Source(1);
|
10
|
-
|
11
|
-
let accumValue = 0;
|
12
|
-
const guest = new Guest((value: number) => {
|
13
|
-
accumValue += value;
|
14
|
-
});
|
15
|
-
one.receiving(
|
16
|
-
new GuestInTheMiddle(guest, (value) => {
|
17
|
-
guest.receive(value + 3);
|
18
|
-
}),
|
19
|
-
);
|
20
|
-
|
21
|
-
expect(accumValue).toBe(4);
|
22
|
-
});
|
23
|
-
|
24
|
-
test("test patron in the middle", () => {
|
25
|
-
const one = new Source(1);
|
26
|
-
|
27
|
-
let accumValue = 0;
|
28
|
-
const guest = new Patron(
|
29
|
-
new Guest((value: number) => {
|
30
|
-
accumValue += value;
|
31
|
-
}),
|
32
|
-
);
|
33
|
-
one.receiving(
|
34
|
-
new GuestInTheMiddle(guest, (value) => {
|
35
|
-
guest.receive(value + 3);
|
36
|
-
}),
|
37
|
-
);
|
38
|
-
one.receive(3);
|
39
|
-
|
40
|
-
setTimeout(() => {
|
41
|
-
one.receive(3);
|
42
|
-
});
|
43
|
-
|
44
|
-
setTimeout(() => {
|
45
|
-
expect(accumValue).toBe(16);
|
46
|
-
});
|
47
|
-
});
|
48
|
-
|
49
|
-
test("test chain in the middle", () => {
|
50
|
-
const one = new Source(1);
|
51
|
-
const two = new Source(2);
|
52
|
-
const chain = new Chain<{ one: number; two: number }>();
|
53
|
-
|
54
|
-
one.receiving(new Patron(chain.receiveKey("one")));
|
55
|
-
two.receiving(new Patron(chain.receiveKey("two")));
|
56
|
-
|
57
|
-
one.receive(3);
|
58
|
-
one.receive(4);
|
59
|
-
|
60
|
-
const guest = new Patron(
|
61
|
-
new Guest((value: { one: number; two: number; three: number }) => {
|
62
|
-
expect(Object.values(value).length).toBe(3);
|
63
|
-
}),
|
64
|
-
);
|
65
|
-
|
66
|
-
chain.result(
|
67
|
-
new GuestInTheMiddle(guest, (value) => {
|
68
|
-
guest.receive({ ...value, three: 99 });
|
69
|
-
}),
|
70
|
-
);
|
71
|
-
});
|
package/src/GuestSync.test.ts
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
import { expect, test } from "vitest";
|
2
|
-
import { Source } from "./Source";
|
3
|
-
import { GuestSync } from "./GuestSync";
|
4
|
-
|
5
|
-
test("guest sync", () => {
|
6
|
-
const source = new Source(123);
|
7
|
-
const syncGuest = new GuestSync(222);
|
8
|
-
expect(syncGuest.value()).toBe(222);
|
9
|
-
source.receiving(syncGuest);
|
10
|
-
expect(syncGuest.value()).toBe(123);
|
11
|
-
});
|
package/src/PoolType.ts
DELETED
package/src/Source.test.ts
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
import { expect, test } from "vitest";
|
2
|
-
import { Source } from "./Source";
|
3
|
-
import { Guest } from "./Guest";
|
4
|
-
|
5
|
-
test("source", () => {
|
6
|
-
const source = new Source(42);
|
7
|
-
|
8
|
-
source.receiving(
|
9
|
-
new Guest((value) => {
|
10
|
-
expect(value).toBe(42);
|
11
|
-
}),
|
12
|
-
);
|
13
|
-
});
|