patron-oop 1.6.0 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/patron.d.ts +61 -80
  3. package/dist/patron.js +77 -112
  4. package/dist/patron.js.map +1 -1
  5. package/dist/patron.min.js +1 -1
  6. package/dist/patron.mjs +72 -111
  7. package/dist/patron.mjs.map +1 -1
  8. package/examples/reactive.html +52 -62
  9. package/package.json +1 -1
  10. package/src/Guest/Guest.test.ts +10 -0
  11. package/src/Guest/Guest.ts +28 -35
  12. package/src/Guest/GuestAware.test.ts +6 -8
  13. package/src/Guest/GuestAware.ts +1 -1
  14. package/src/Guest/GuestCast.test.ts +8 -8
  15. package/src/Guest/GuestCast.ts +7 -3
  16. package/src/Guest/GuestChain.test.ts +21 -21
  17. package/src/Guest/GuestChain.ts +14 -14
  18. package/src/Guest/GuestInTheMiddle.test.ts +14 -14
  19. package/src/Guest/GuestInTheMiddle.ts +3 -3
  20. package/src/Guest/GuestPool.test.ts +10 -17
  21. package/src/Guest/GuestPool.ts +10 -6
  22. package/src/Guest/GuestSync.test.ts +4 -4
  23. package/src/Guest/GuestSync.ts +2 -2
  24. package/src/Patron/Patron.test.ts +17 -0
  25. package/src/Patron/Patron.ts +16 -12
  26. package/src/Patron/PatronOnce.test.ts +5 -8
  27. package/src/Patron/PatronOnce.ts +8 -3
  28. package/src/Patron/PatronPool.test.ts +9 -14
  29. package/src/Patron/PatronPool.ts +16 -10
  30. package/src/Source/Source.test.ts +10 -0
  31. package/src/Source/Source.ts +21 -7
  32. package/src/index.ts +36 -14
  33. package/src/Guest/GuestCallback.test.ts +0 -13
  34. package/src/Guest/GuestCallback.ts +0 -21
  35. package/src/Patron/PatronOfGuest.test.ts +0 -20
  36. package/src/Patron/PatronOfGuest.ts +0 -17
  37. package/src/Source/SourceOfValue.test.ts +0 -13
  38. package/src/Source/SourceOfValue.ts +0 -22
  39. package/src/Source/SourcesApplied.test.ts +0 -26
  40. package/src/Source/SourcesApplied.ts +0 -39
@@ -0,0 +1,10 @@
1
+ import { expect, test } from "vitest";
2
+ import { Source } from "./Source";
3
+
4
+ test("source", () => {
5
+ const source = new Source(42);
6
+
7
+ source.receiving((value) => {
8
+ expect(value).toBe(42);
9
+ });
10
+ });
@@ -1,12 +1,26 @@
1
- import { SourceOfValue } from "./SourceOfValue";
2
- import { sourcesApplied } from "./SourcesApplied";
1
+ import { GuestAwareType } from "../Guest/GuestAware";
2
+ import { Guest, GuestObjectType, GuestType } from "../Guest/Guest";
3
+ import { PatronPool } from "../Patron/PatronPool";
3
4
 
4
- export class Source {
5
- public ofValue<P>(sourceDocument: P) {
6
- return new SourceOfValue(sourceDocument);
5
+ export type SourceType<T = unknown> = GuestAwareType<T> & GuestObjectType<T>;
6
+
7
+ export class Source<T> implements SourceType<T> {
8
+ private pool = new PatronPool(this);
9
+
10
+ public constructor(private sourceDocument: T) {}
11
+
12
+ public receive(value: T): this {
13
+ this.sourceDocument = value;
14
+ this.pool.receive(this.sourceDocument);
15
+ return this;
7
16
  }
8
17
 
9
- public applySources<P>(target: P, methodsSources: Record<string, unknown[]>) {
10
- return sourcesApplied(target, methodsSources);
18
+ public receiving(guest: GuestType<T>): this {
19
+ if (typeof guest === "function") {
20
+ this.pool.distribute(this.sourceDocument, new Guest(guest));
21
+ } else {
22
+ this.pool.distribute(this.sourceDocument, guest);
23
+ }
24
+ return this;
11
25
  }
12
26
  }
package/src/index.ts CHANGED
@@ -1,21 +1,43 @@
1
- export * from "./Guest/GuestCallback";
2
- export * from "./Guest/GuestChain";
3
- export * from "./Guest/GuestSync";
4
- export * from "./Patron/PatronPool";
5
- export * from "./Source/SourceOfValue";
6
-
7
- import { Guest } from "./Guest/Guest";
1
+ import { give, Guest } from "./Guest/Guest";
2
+ import { PatronPool, removePatronFromPools } from "./Patron/PatronPool";
3
+ import { GuestAware } from "./Guest/GuestAware";
4
+ import { GuestCast } from "./Guest/GuestCast";
5
+ import { GuestChain } from "./Guest/GuestChain";
6
+ import { GuestInTheMiddle } from "./Guest/GuestInTheMiddle";
7
+ import { GuestPool } from "./Guest/GuestPool";
8
+ import { GuestSync } from "./Guest/GuestSync";
8
9
  import { Patron } from "./Patron/Patron";
10
+ import { PatronOnce } from "./Patron/PatronOnce";
9
11
  import { Source } from "./Source/Source";
10
12
 
11
- export { Guest, Patron, Source };
13
+ export * from "./Guest/GuestAware";
14
+ export * from "./Guest/Guest";
15
+ export * from "./Guest/GuestCast";
16
+ export * from "./Guest/GuestChain";
17
+ export * from "./Guest/GuestInTheMiddle";
18
+ export * from "./Guest/GuestPool";
19
+ export * from "./Guest/GuestSync";
20
+ export * from "./Patron/Patron";
21
+ export * from "./Patron/PatronOnce";
22
+ export * from "./Patron/PatronPool";
23
+ export * from "./Source/Source";
12
24
 
13
- declare var window: any;
25
+ declare var globalThis: any;
14
26
 
15
- if (window) {
16
- window["GUEST_LIBRARY"] = {
17
- guest: new Guest(),
18
- patron: new Patron(),
19
- source: new Source(),
27
+ if (globalThis) {
28
+ globalThis["GUEST_LIBRARY"] = {
29
+ give,
30
+ removePatronFromPools,
31
+ GuestAware,
32
+ Guest,
33
+ GuestCast,
34
+ GuestChain,
35
+ GuestInTheMiddle,
36
+ GuestPool,
37
+ GuestSync,
38
+ Patron,
39
+ PatronOnce,
40
+ PatronPool,
41
+ Source,
20
42
  };
21
43
  }
@@ -1,13 +0,0 @@
1
- import { expect, test } from "vitest";
2
- import { GuestCallback } from "./GuestCallback";
3
- import { SourceOfValue } from "../Source/SourceOfValue";
4
-
5
- test("guest dynamic", () => {
6
- const one = new SourceOfValue(1);
7
-
8
- one.receiving(
9
- new GuestCallback((value) => {
10
- expect(value).toBe(1);
11
- }),
12
- );
13
- });
@@ -1,21 +0,0 @@
1
- type GuestIntroduction = "guest" | "patron";
2
-
3
- export interface ReceiveOptions {
4
- data?: unknown;
5
- }
6
-
7
- export type GuestExecutorType<T> = (value: T, options?: ReceiveOptions) => void;
8
-
9
- export interface GuestType<T = unknown> {
10
- receive(value: T, options?: ReceiveOptions): this;
11
- introduction?(): GuestIntroduction;
12
- }
13
-
14
- export class GuestCallback<T> implements GuestType<T> {
15
- public constructor(private receiver: GuestExecutorType<T>) {}
16
-
17
- public receive(value: T, options?: ReceiveOptions) {
18
- this.receiver(value, options);
19
- return this;
20
- }
21
- }
@@ -1,20 +0,0 @@
1
- import { expect, test } from "vitest";
2
- import { PatronOfGuest } from "./PatronOfGuest";
3
- import { SourceOfValue } from "../Source/SourceOfValue";
4
- import { GuestCallback } from "../Guest/GuestCallback";
5
-
6
- test("patron always guest", () => {
7
- const one = new SourceOfValue(1);
8
- let patronCalledTimes = 0;
9
- const patron = new PatronOfGuest(
10
- new GuestCallback(() => {
11
- patronCalledTimes += 1;
12
- }),
13
- );
14
- one.receiving(patron);
15
- one.receive(2);
16
-
17
- queueMicrotask(() => {
18
- expect(patronCalledTimes).toBe(2);
19
- });
20
- });
@@ -1,17 +0,0 @@
1
- import { GuestType, ReceiveOptions } from "../Guest/GuestCallback";
2
-
3
- /**
4
- * Патрон - это постоянный посетитель
5
- */
6
- export class PatronOfGuest<T> implements GuestType<T> {
7
- public constructor(private willBePatron: GuestType<T>) {}
8
-
9
- public introduction() {
10
- return "patron" as const;
11
- }
12
-
13
- public receive(value: T, options?: ReceiveOptions): this {
14
- this.willBePatron.receive(value, options);
15
- return this;
16
- }
17
- }
@@ -1,13 +0,0 @@
1
- import { expect, test } from "vitest";
2
- import { SourceOfValue } from "./SourceOfValue";
3
- import { GuestCallback } from "../Guest/GuestCallback";
4
-
5
- test("source", () => {
6
- const source = new SourceOfValue(42);
7
-
8
- source.receiving(
9
- new GuestCallback((value) => {
10
- expect(value).toBe(42);
11
- }),
12
- );
13
- });
@@ -1,22 +0,0 @@
1
- import { GuestAwareType } from "../Guest/GuestAware";
2
- import { GuestType } from "../Guest/GuestCallback";
3
- import { PatronPool } from "../Patron/PatronPool";
4
-
5
- export type SourceType<T = unknown> = GuestAwareType<T> & GuestType<T>;
6
-
7
- export class SourceOfValue<T> implements SourceType<T> {
8
- private pool = new PatronPool(this);
9
-
10
- public constructor(private sourceDocument: T) {}
11
-
12
- public receive(value: T): this {
13
- this.sourceDocument = value;
14
- this.pool.receive(this.sourceDocument);
15
- return this;
16
- }
17
-
18
- public receiving(guest: GuestType<T>): this {
19
- this.pool.distribute(this.sourceDocument, guest);
20
- return this;
21
- }
22
- }
@@ -1,26 +0,0 @@
1
- import { expect, test } from "vitest";
2
- import { sourcesApplied } from "./SourcesApplied";
3
-
4
- test("sources applied", () => {
5
- const target = {
6
- one(data: { one: string }, after: string) {
7
- return data.one + " " + after;
8
- },
9
- };
10
- const targetApplied = sourcesApplied(target, {
11
- one: [{ one: "hello world" }, "after"],
12
- });
13
- expect(targetApplied.one()).toBe("hello world after");
14
- });
15
-
16
- test("sources applied classes", () => {
17
- class Target {
18
- one(data: { one: string }, after: string) {
19
- return data.one + " " + after;
20
- }
21
- }
22
- const targetApplied = sourcesApplied(new Target(), {
23
- one: [{ one: "good bye" }, "after"],
24
- });
25
- expect(targetApplied.one()).toBe("good bye after");
26
- });
@@ -1,39 +0,0 @@
1
- type TupleSplit<
2
- T,
3
- N extends number,
4
- O extends readonly any[] = readonly [],
5
- > = O["length"] extends N
6
- ? [O, T]
7
- : T extends readonly [infer F, ...infer R]
8
- ? TupleSplit<readonly [...R], N, readonly [...O, F]>
9
- : [O, T];
10
-
11
- type SkipFirst<T extends readonly any[], N extends number> = TupleSplit<
12
- T,
13
- N
14
- >[1];
15
-
16
- export const sourcesApplied = <T>(
17
- target: T,
18
- methodsSources: Record<string, unknown[]>,
19
- ) => {
20
- return new Proxy(target as object, {
21
- get: function (target: any, property) {
22
- const maybeMethod = target[property];
23
-
24
- if (typeof maybeMethod !== "function") {
25
- return maybeMethod;
26
- }
27
-
28
- return (...args: any[]) => {
29
- const appliedArgs = (methodsSources as any)[property];
30
-
31
- if (appliedArgs) {
32
- return maybeMethod(...appliedArgs, ...args);
33
- }
34
-
35
- return maybeMethod(...args);
36
- };
37
- },
38
- });
39
- };