@proto-kit/protocol 0.1.1-develop.1352 → 0.1.1-develop.1366

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.
@@ -1,5 +1,3 @@
1
- import { Reference } from "@proto-kit/common";
2
- import { State } from "../State";
3
1
  import { StateServiceProvider } from "../StateServiceProvider";
4
2
  export interface StatefulModule {
5
3
  name?: string;
@@ -7,10 +5,6 @@ export interface StatefulModule {
7
5
  stateServiceProvider: StateServiceProvider;
8
6
  };
9
7
  }
10
- export declare function createStateGetter<TargetModule extends StatefulModule>(target: TargetModule, propertyKey: string, valueReference: Reference<State<unknown> | undefined>, prefix: string, debugInfo: {
11
- parentName: string;
12
- baseModuleNames: string;
13
- }): () => State<unknown> | undefined;
14
8
  /**
15
9
  * Decorates a runtime module property as state, passing down some
16
10
  * underlying values to improve developer experience.
@@ -1 +1 @@
1
- {"version":3,"file":"ProtocolState.d.ts","sourceRoot":"","sources":["../../../src/state/protocol/ProtocolState.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAE/D,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAEjC,OAAO,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAkB/D,MAAM,WAAW,cAAc;IAC7B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE;QACP,oBAAoB,EAAE,oBAAoB,CAAC;KAC5C,CAAC;CACH;AAED,wBAAgB,iBAAiB,CAAC,YAAY,SAAS,cAAc,EACnE,MAAM,EAAE,YAAY,EACpB,WAAW,EAAE,MAAM,EACnB,cAAc,EAAE,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,SAAS,CAAC,EACrD,MAAM,EAAE,MAAM,EACd,SAAS,EAAE;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,eAAe,EAAE,MAAM,CAAA;CAAE,oCA4B3D;AAED;;;GAGG;AACH,wBAAgB,KAAK,uGAGJ,MAAM,UA8BtB"}
1
+ {"version":3,"file":"ProtocolState.d.ts","sourceRoot":"","sources":["../../../src/state/protocol/ProtocolState.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAkB/D,MAAM,WAAW,cAAc;IAC7B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE;QACP,oBAAoB,EAAE,oBAAoB,CAAC;KAC5C,CAAC;CACH;AAED;;;GAGG;AACH,wBAAgB,KAAK,uGAGJ,MAAM,UA6DtB"}
@@ -8,46 +8,50 @@ const errors = {
8
8
  missingParent: (className, type, moduleType) => new Error(`Unable to provide parent '${type}' for state, ${className} is missing a name.
9
9
  Did you forget to extend your module with 'extends ${moduleType}'?`),
10
10
  };
11
- export function createStateGetter(target, propertyKey, valueReference, prefix, debugInfo) {
12
- return () => {
13
- const { value } = valueReference;
14
- // Short-circuit this to return the state in case its already initialized
15
- if (value !== undefined && value.path !== undefined) {
16
- return value;
17
- }
18
- if (target.name === undefined) {
19
- throw errors.missingName(target.constructor.name);
20
- }
21
- if (!target.parent) {
22
- throw errors.missingParent(target.constructor.name, debugInfo.parentName, debugInfo.baseModuleNames);
23
- }
24
- const path = Path.fromProperty(target.name, propertyKey, prefix);
25
- if (value) {
26
- value.path = path;
27
- value.stateServiceProvider = target.parent.stateServiceProvider;
28
- }
29
- return value;
30
- };
31
- }
32
11
  /**
33
12
  * Decorates a runtime module property as state, passing down some
34
13
  * underlying values to improve developer experience.
35
14
  */
36
15
  export function state() {
37
16
  return (target, propertyKey) => {
38
- const stateReference = createReference(undefined);
39
- const isProtocol = target instanceof TransitioningProtocolModule;
40
- const statePrefix = isProtocol
41
- ? PROTOKIT_PREFIXES.STATE_PROTOCOL
42
- : PROTOKIT_PREFIXES.STATE_RUNTIME;
43
- const debugInfo = isProtocol
44
- ? { parentName: "protocol", baseModuleNames: "...Hook" }
45
- : { parentName: "runtime", baseModuleNames: "RuntimeModule" };
46
17
  Object.defineProperty(target, propertyKey, {
47
18
  enumerable: true,
48
- get: createStateGetter(target, propertyKey, stateReference, statePrefix, debugInfo),
49
- set: (newValue) => {
50
- stateReference.value = newValue;
19
+ get: function get() {
20
+ // The reason for why we store the state value in this weird way is that
21
+ // in the decorator on the prototype of the class. This means that if there
22
+ // are multiple instances of this class, any closure that this getter shares
23
+ // will be the same for all instances.
24
+ // Therefore, we need to somehow save the set instance on the instance itself
25
+ // eslint-disable-next-line max-len
26
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions,@typescript-eslint/no-unsafe-assignment
27
+ const reference = this[`protokit_state_cache_${propertyKey}`];
28
+ // Short-circuit this to return the state in case its already initialized
29
+ if (reference !== undefined && reference.value.path !== undefined) {
30
+ return reference.value;
31
+ }
32
+ if (this.name === undefined) {
33
+ throw errors.missingName(this.constructor.name);
34
+ }
35
+ const isProtocol = target instanceof TransitioningProtocolModule;
36
+ if (!this.parent) {
37
+ const debugInfo = isProtocol
38
+ ? { parentName: "protocol", baseModuleNames: "...Hook" }
39
+ : { parentName: "runtime", baseModuleNames: "RuntimeModule" };
40
+ throw errors.missingParent(this.constructor.name, debugInfo.parentName, debugInfo.baseModuleNames);
41
+ }
42
+ const statePrefix = isProtocol
43
+ ? PROTOKIT_PREFIXES.STATE_PROTOCOL
44
+ : PROTOKIT_PREFIXES.STATE_RUNTIME;
45
+ const path = Path.fromProperty(this.name, propertyKey, statePrefix);
46
+ if (reference) {
47
+ const { value } = reference;
48
+ value.path = path;
49
+ value.stateServiceProvider = this.parent.stateServiceProvider;
50
+ }
51
+ return reference?.value;
52
+ },
53
+ set: function set(newValue) {
54
+ this[`protokit_state_cache_${propertyKey}`] = createReference(newValue);
51
55
  },
52
56
  });
53
57
  };
@@ -1 +1 @@
1
- {"version":3,"file":"ProtocolState.js","sourceRoot":"","sources":["../../../src/state/protocol/ProtocolState.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAa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AI,EACrB,SAAS,CAAC,UAAU,EACpB,SAAS,CAAC,eAAe,CAC1B,CAAC;iBACH;gBAED,MAAM,WAAW,GAAG,UAAU;oBAC5B,CAAC,CAAC,iBAAiB,CAAC,cAAc;oBAClC,CAAC,CAAC,iBAAiB,CAAC,aAAa,CAAC;gBACpC,MAAM,IAAI,GAAG,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC;gBACpE,IAAI,SAAS,EAAE;oBACb,MAAM,EAAE,KAAK,EAAE,GAAG,SAAS,CAAC;oBAC5B,KAAK,CAAC,IAAI,GAAG,IAAI,CAAC;oBAClB,KAAK,CAAC,oBAAoB,GAAG,IAAI,CAAC,MAAM,CAAC,oBAAoB,CAAC;iBAC/D;gBACD,OAAO,SAAS,EAAE,KAAK,CAAC;YAC1B,CAAC;YAED,GAAG,EAAE,SAAS,GAAG,CAEf,QAAwB;gBAExB,IAAI,CAAC,wBAAwB,WAAW,EAAE,CAAC,GAAG,eAAe,CAAC,QAAQ,CAAC,CAAC;YAC1E,CAAC;SACF,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "license": "MIT",
4
4
  "private": false,
5
5
  "type": "module",
6
- "version": "0.1.1-develop.1352+31ba89b0",
6
+ "version": "0.1.1-develop.1366+b677d53c",
7
7
  "scripts": {
8
8
  "build": "tsc -p tsconfig.json",
9
9
  "dev": "tsc -p tsconfig.json --watch",
@@ -32,5 +32,5 @@
32
32
  "@jest/globals": "^29.5.0",
33
33
  "@types/lodash": "^4.14.194"
34
34
  },
35
- "gitHead": "31ba89b035ae5a298c8d55f12ea7a88cb00fdad5"
35
+ "gitHead": "b677d53c2c4df7f09a8f595201d2456b2fa44c05"
36
36
  }
@@ -27,41 +27,6 @@ export interface StatefulModule {
27
27
  };
28
28
  }
29
29
 
30
- export function createStateGetter<TargetModule extends StatefulModule>(
31
- target: TargetModule,
32
- propertyKey: string,
33
- valueReference: Reference<State<unknown> | undefined>,
34
- prefix: string,
35
- debugInfo: { parentName: string; baseModuleNames: string }
36
- ) {
37
- return () => {
38
- const { value } = valueReference;
39
- // Short-circuit this to return the state in case its already initialized
40
- if (value !== undefined && value.path !== undefined) {
41
- return value;
42
- }
43
-
44
- if (target.name === undefined) {
45
- throw errors.missingName(target.constructor.name);
46
- }
47
-
48
- if (!target.parent) {
49
- throw errors.missingParent(
50
- target.constructor.name,
51
- debugInfo.parentName,
52
- debugInfo.baseModuleNames
53
- );
54
- }
55
-
56
- const path = Path.fromProperty(target.name, propertyKey, prefix);
57
- if (value) {
58
- value.path = path;
59
- value.stateServiceProvider = target.parent.stateServiceProvider;
60
- }
61
- return value;
62
- };
63
- }
64
-
65
30
  /**
66
31
  * Decorates a runtime module property as state, passing down some
67
32
  * underlying values to improve developer experience.
@@ -71,31 +36,62 @@ export function state() {
71
36
  target: TargetTransitioningModule,
72
37
  propertyKey: string
73
38
  ) => {
74
- const stateReference = createReference<State<unknown> | undefined>(
75
- undefined
76
- );
77
-
78
- const isProtocol = target instanceof TransitioningProtocolModule;
79
- const statePrefix = isProtocol
80
- ? PROTOKIT_PREFIXES.STATE_PROTOCOL
81
- : PROTOKIT_PREFIXES.STATE_RUNTIME;
82
- const debugInfo = isProtocol
83
- ? { parentName: "protocol", baseModuleNames: "...Hook" }
84
- : { parentName: "runtime", baseModuleNames: "RuntimeModule" };
85
-
86
39
  Object.defineProperty(target, propertyKey, {
87
40
  enumerable: true,
88
41
 
89
- get: createStateGetter(
90
- target,
91
- propertyKey,
92
- stateReference,
93
- statePrefix,
94
- debugInfo
95
- ),
42
+ get: function get(this: TargetTransitioningModule) {
43
+ // The reason for why we store the state value in this weird way is that
44
+ // in the decorator on the prototype of the class. This means that if there
45
+ // are multiple instances of this class, any closure that this getter shares
46
+ // will be the same for all instances.
47
+ // Therefore, we need to somehow save the set instance on the instance itself
48
+
49
+ // eslint-disable-next-line max-len
50
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions,@typescript-eslint/no-unsafe-assignment
51
+ const reference: Reference<State<unknown>> | undefined = (this as any)[
52
+ `protokit_state_cache_${propertyKey}`
53
+ ];
54
+
55
+ // Short-circuit this to return the state in case its already initialized
56
+ if (reference !== undefined && reference.value.path !== undefined) {
57
+ return reference.value;
58
+ }
59
+
60
+ if (this.name === undefined) {
61
+ throw errors.missingName(this.constructor.name);
62
+ }
63
+
64
+ const isProtocol = target instanceof TransitioningProtocolModule;
65
+
66
+ if (!this.parent) {
67
+ const debugInfo = isProtocol
68
+ ? { parentName: "protocol", baseModuleNames: "...Hook" }
69
+ : { parentName: "runtime", baseModuleNames: "RuntimeModule" };
70
+
71
+ throw errors.missingParent(
72
+ this.constructor.name,
73
+ debugInfo.parentName,
74
+ debugInfo.baseModuleNames
75
+ );
76
+ }
77
+
78
+ const statePrefix = isProtocol
79
+ ? PROTOKIT_PREFIXES.STATE_PROTOCOL
80
+ : PROTOKIT_PREFIXES.STATE_RUNTIME;
81
+ const path = Path.fromProperty(this.name, propertyKey, statePrefix);
82
+ if (reference) {
83
+ const { value } = reference;
84
+ value.path = path;
85
+ value.stateServiceProvider = this.parent.stateServiceProvider;
86
+ }
87
+ return reference?.value;
88
+ },
96
89
 
97
- set: (newValue: State<unknown>) => {
98
- stateReference.value = newValue;
90
+ set: function set(
91
+ this: TargetTransitioningModule & any,
92
+ newValue: State<unknown>
93
+ ) {
94
+ this[`protokit_state_cache_${propertyKey}`] = createReference(newValue);
99
95
  },
100
96
  });
101
97
  };