@praxisjs/decorators 0.4.3 → 0.6.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.
Files changed (192) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/dist/__tests__/component.test.js +10 -1
  3. package/dist/__tests__/component.test.js.map +1 -1
  4. package/dist/__tests__/compose.test.d.ts +2 -0
  5. package/dist/__tests__/compose.test.d.ts.map +1 -0
  6. package/dist/__tests__/compose.test.js +171 -0
  7. package/dist/__tests__/compose.test.js.map +1 -0
  8. package/dist/__tests__/events.test.js +26 -0
  9. package/dist/__tests__/events.test.js.map +1 -1
  10. package/dist/__tests__/functions.test.js +205 -47
  11. package/dist/__tests__/functions.test.js.map +1 -1
  12. package/dist/__tests__/lazy.test.js +5 -7
  13. package/dist/__tests__/lazy.test.js.map +1 -1
  14. package/dist/__tests__/memo.test.js +114 -25
  15. package/dist/__tests__/memo.test.js.map +1 -1
  16. package/dist/__tests__/properties.test.js +193 -0
  17. package/dist/__tests__/properties.test.js.map +1 -1
  18. package/dist/__tests__/resource.test.d.ts +2 -0
  19. package/dist/__tests__/resource.test.d.ts.map +1 -0
  20. package/dist/__tests__/resource.test.js +127 -0
  21. package/dist/__tests__/resource.test.js.map +1 -0
  22. package/dist/__tests__/slot.test.js +32 -0
  23. package/dist/__tests__/slot.test.js.map +1 -1
  24. package/dist/__tests__/until.test.d.ts +2 -0
  25. package/dist/__tests__/until.test.d.ts.map +1 -0
  26. package/dist/__tests__/until.test.js +88 -0
  27. package/dist/__tests__/until.test.js.map +1 -0
  28. package/dist/__tests__/virtual.test.js +164 -15
  29. package/dist/__tests__/virtual.test.js.map +1 -1
  30. package/dist/component/component.d.ts +1 -1
  31. package/dist/component/component.d.ts.map +1 -1
  32. package/dist/component/component.js +15 -10
  33. package/dist/component/component.js.map +1 -1
  34. package/dist/component/lazy.d.ts.map +1 -1
  35. package/dist/component/lazy.js +29 -30
  36. package/dist/component/lazy.js.map +1 -1
  37. package/dist/component/virtual.d.ts.map +1 -1
  38. package/dist/component/virtual.js +49 -50
  39. package/dist/component/virtual.js.map +1 -1
  40. package/dist/create-class-decorator.d.ts +16 -0
  41. package/dist/create-class-decorator.d.ts.map +1 -0
  42. package/dist/create-class-decorator.js +35 -0
  43. package/dist/create-class-decorator.js.map +1 -0
  44. package/dist/create-field-decorator.d.ts +14 -0
  45. package/dist/create-field-decorator.d.ts.map +1 -0
  46. package/dist/create-field-decorator.js +37 -0
  47. package/dist/create-field-decorator.js.map +1 -0
  48. package/dist/create-getter-decorator.d.ts +18 -0
  49. package/dist/create-getter-decorator.d.ts.map +1 -0
  50. package/dist/create-getter-decorator.js +15 -0
  51. package/dist/create-getter-decorator.js.map +1 -0
  52. package/dist/create-lifecycle-method-decorator.d.ts +6 -0
  53. package/dist/create-lifecycle-method-decorator.d.ts.map +1 -0
  54. package/dist/create-lifecycle-method-decorator.js +22 -0
  55. package/dist/create-lifecycle-method-decorator.js.map +1 -0
  56. package/dist/create-method-decorator.d.ts +6 -0
  57. package/dist/create-method-decorator.d.ts.map +1 -0
  58. package/dist/create-method-decorator.js +14 -0
  59. package/dist/create-method-decorator.js.map +1 -0
  60. package/dist/events/emit.d.ts +1 -1
  61. package/dist/events/emit.d.ts.map +1 -1
  62. package/dist/events/emit.js +24 -29
  63. package/dist/events/emit.js.map +1 -1
  64. package/dist/events/on-command.d.ts +1 -2
  65. package/dist/events/on-command.d.ts.map +1 -1
  66. package/dist/events/on-command.js +15 -32
  67. package/dist/events/on-command.js.map +1 -1
  68. package/dist/functions/bind.d.ts +1 -2
  69. package/dist/functions/bind.d.ts.map +1 -1
  70. package/dist/functions/bind.js +4 -11
  71. package/dist/functions/bind.js.map +1 -1
  72. package/dist/functions/debounce.d.ts.map +1 -1
  73. package/dist/functions/debounce.js +21 -20
  74. package/dist/functions/debounce.js.map +1 -1
  75. package/dist/functions/index.d.ts +1 -0
  76. package/dist/functions/index.d.ts.map +1 -1
  77. package/dist/functions/index.js +1 -0
  78. package/dist/functions/index.js.map +1 -1
  79. package/dist/functions/log.d.ts +1 -2
  80. package/dist/functions/log.d.ts.map +1 -1
  81. package/dist/functions/log.js +40 -38
  82. package/dist/functions/log.js.map +1 -1
  83. package/dist/functions/memo.d.ts +1 -2
  84. package/dist/functions/memo.d.ts.map +1 -1
  85. package/dist/functions/memo.js +29 -27
  86. package/dist/functions/memo.js.map +1 -1
  87. package/dist/functions/once.d.ts +1 -2
  88. package/dist/functions/once.d.ts.map +1 -1
  89. package/dist/functions/once.js +14 -12
  90. package/dist/functions/once.js.map +1 -1
  91. package/dist/functions/retry.d.ts +1 -2
  92. package/dist/functions/retry.d.ts.map +1 -1
  93. package/dist/functions/retry.js +23 -20
  94. package/dist/functions/retry.js.map +1 -1
  95. package/dist/functions/throttle.d.ts +1 -2
  96. package/dist/functions/throttle.d.ts.map +1 -1
  97. package/dist/functions/throttle.js +11 -17
  98. package/dist/functions/throttle.js.map +1 -1
  99. package/dist/functions/until.d.ts +2 -0
  100. package/dist/functions/until.d.ts.map +1 -0
  101. package/dist/functions/until.js +17 -0
  102. package/dist/functions/until.js.map +1 -0
  103. package/dist/functions/watch.d.ts +1 -1
  104. package/dist/functions/watch.d.ts.map +1 -1
  105. package/dist/functions/watch.js +25 -39
  106. package/dist/functions/watch.js.map +1 -1
  107. package/dist/functions/when.d.ts +1 -2
  108. package/dist/functions/when.d.ts.map +1 -1
  109. package/dist/functions/when.js +10 -26
  110. package/dist/functions/when.js.map +1 -1
  111. package/dist/index.d.ts +7 -2
  112. package/dist/index.d.ts.map +1 -1
  113. package/dist/index.js +7 -2
  114. package/dist/index.js.map +1 -1
  115. package/dist/properties/compose.d.ts +3 -0
  116. package/dist/properties/compose.d.ts.map +1 -0
  117. package/dist/properties/compose.js +40 -0
  118. package/dist/properties/compose.js.map +1 -0
  119. package/dist/properties/computed.d.ts +1 -2
  120. package/dist/properties/computed.d.ts.map +1 -1
  121. package/dist/properties/computed.js +10 -9
  122. package/dist/properties/computed.js.map +1 -1
  123. package/dist/properties/history.d.ts.map +1 -1
  124. package/dist/properties/history.js +37 -32
  125. package/dist/properties/history.js.map +1 -1
  126. package/dist/properties/index.d.ts +2 -0
  127. package/dist/properties/index.d.ts.map +1 -1
  128. package/dist/properties/index.js +2 -0
  129. package/dist/properties/index.js.map +1 -1
  130. package/dist/properties/persisted.d.ts +1 -2
  131. package/dist/properties/persisted.d.ts.map +1 -1
  132. package/dist/properties/persisted.js +13 -29
  133. package/dist/properties/persisted.js.map +1 -1
  134. package/dist/properties/prop.d.ts +1 -2
  135. package/dist/properties/prop.d.ts.map +1 -1
  136. package/dist/properties/prop.js +21 -25
  137. package/dist/properties/prop.js.map +1 -1
  138. package/dist/properties/resource.d.ts +14 -0
  139. package/dist/properties/resource.d.ts.map +1 -0
  140. package/dist/properties/resource.js +27 -0
  141. package/dist/properties/resource.js.map +1 -0
  142. package/dist/properties/slot.d.ts +1 -2
  143. package/dist/properties/slot.d.ts.map +1 -1
  144. package/dist/properties/slot.js +15 -20
  145. package/dist/properties/slot.js.map +1 -1
  146. package/dist/properties/state.d.ts +1 -2
  147. package/dist/properties/state.d.ts.map +1 -1
  148. package/dist/properties/state.js +13 -18
  149. package/dist/properties/state.js.map +1 -1
  150. package/package.json +2 -2
  151. package/src/__tests__/component.test.ts +10 -0
  152. package/src/__tests__/compose.test.ts +202 -0
  153. package/src/__tests__/events.test.ts +32 -0
  154. package/src/__tests__/functions.test.ts +257 -46
  155. package/src/__tests__/lazy.test.ts +3 -5
  156. package/src/__tests__/memo.test.ts +143 -25
  157. package/src/__tests__/properties.test.ts +233 -0
  158. package/src/__tests__/resource.test.ts +141 -0
  159. package/src/__tests__/slot.test.ts +38 -0
  160. package/src/__tests__/until.test.ts +113 -0
  161. package/src/__tests__/virtual.test.ts +135 -7
  162. package/src/component/component.ts +27 -12
  163. package/src/component/lazy.ts +33 -37
  164. package/src/component/virtual.tsx +67 -75
  165. package/src/create-class-decorator.ts +61 -0
  166. package/src/create-field-decorator.ts +66 -0
  167. package/src/create-getter-decorator.ts +52 -0
  168. package/src/create-lifecycle-method-decorator.ts +38 -0
  169. package/src/create-method-decorator.ts +26 -0
  170. package/src/events/emit.ts +22 -33
  171. package/src/events/on-command.ts +22 -47
  172. package/src/functions/bind.ts +4 -15
  173. package/src/functions/debounce.ts +21 -23
  174. package/src/functions/index.ts +1 -0
  175. package/src/functions/log.ts +40 -43
  176. package/src/functions/memo.ts +28 -42
  177. package/src/functions/once.ts +13 -16
  178. package/src/functions/retry.ts +21 -22
  179. package/src/functions/throttle.ts +11 -24
  180. package/src/functions/until.ts +19 -0
  181. package/src/functions/watch.ts +27 -43
  182. package/src/functions/when.ts +11 -36
  183. package/src/index.ts +29 -0
  184. package/src/properties/compose.ts +53 -0
  185. package/src/properties/computed.ts +11 -13
  186. package/src/properties/history.ts +42 -41
  187. package/src/properties/index.ts +2 -0
  188. package/src/properties/persisted.ts +17 -58
  189. package/src/properties/prop.ts +21 -31
  190. package/src/properties/resource.ts +34 -0
  191. package/src/properties/slot.ts +17 -30
  192. package/src/properties/state.ts +14 -24
@@ -1,35 +1,25 @@
1
- import type { StatefulComponent } from "@praxisjs/core";
1
+ import { createFieldDecorator } from "../create-field-decorator";
2
2
 
3
3
  export function Prop() {
4
- return function (
5
- _value: undefined,
6
- context: ClassFieldDecoratorContext<StatefulComponent>,
7
- ): void {
8
- context.addInitializer(function (this: unknown) {
9
- const instance = this as StatefulComponent;
10
- const name = context.name as string;
11
- const raw = instance as unknown as Record<string, unknown>;
12
- const defaultValue = raw[name];
13
- Reflect.deleteProperty(raw, name);
14
-
15
- instance._defaults[name] = defaultValue;
16
-
17
- Object.defineProperty(instance, name, {
18
- get(this: StatefulComponent) {
19
- const fromParent = this._rawProps[name];
20
- if (fromParent !== undefined) {
21
- return typeof fromParent === "function"
22
- ? (fromParent as () => unknown)()
23
- : fromParent;
24
- }
25
- return this._defaults[name];
26
- },
27
- set(this: StatefulComponent, value: unknown) {
28
- this._defaults[name] = value;
4
+ return createFieldDecorator({
5
+ bind(instance, name, initialValue) {
6
+ instance._defaults[name] = initialValue;
7
+ return {
8
+ descriptor: {
9
+ get() {
10
+ const fromParent = (instance._rawProps)[name];
11
+ if (fromParent !== undefined) {
12
+ return typeof fromParent === "function"
13
+ ? (fromParent as () => unknown)()
14
+ : fromParent;
15
+ }
16
+ return instance._defaults[name];
17
+ },
18
+ set(value: unknown) {
19
+ instance._defaults[name] = value;
20
+ },
29
21
  },
30
- enumerable: true,
31
- configurable: true,
32
- });
33
- });
34
- };
22
+ };
23
+ },
24
+ });
35
25
  }
@@ -0,0 +1,34 @@
1
+ import {
2
+ resource,
3
+ type Resource as ResourceInstance,
4
+ type ResourceOptions,
5
+ } from "@praxisjs/core/internal";
6
+
7
+ import { createFieldDecorator, type FieldBinding } from "../create-field-decorator";
8
+
9
+ export type { ResourceInstance, ResourceOptions };
10
+
11
+ /**
12
+ * Binds an async resource to the field. The field holds a Resource<T> object
13
+ * with reactive data, pending, error, and status signals, plus refetch/cancel/mutate methods.
14
+ *
15
+ * class MyComponent extends StatefulComponent {
16
+ * @Resource(() => api.getUsers())
17
+ * users!: ResourceInstance<User[]>;
18
+ * // access: this.users.data(), this.users.pending(), this.users.refetch()
19
+ * }
20
+ */
21
+ export function Resource<T>(fetcher: () => Promise<T>, options: ResourceOptions<T> = {}) {
22
+ return createFieldDecorator({
23
+ bind(_instance, _name, _initialValue): FieldBinding {
24
+ const r = resource(fetcher, options);
25
+ return {
26
+ descriptor: {
27
+ get(): ResourceInstance<T> { return r; },
28
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
29
+ set(): void {},
30
+ },
31
+ };
32
+ },
33
+ });
34
+ }
@@ -1,4 +1,4 @@
1
- import type { StatefulComponent } from "@praxisjs/core";
1
+ import { createFieldDecorator } from "../create-field-decorator";
2
2
 
3
3
  const slotsMap = new WeakMap<object, Map<string, unknown[]>>();
4
4
 
@@ -14,7 +14,6 @@ function resolveSlots(children: unknown): Map<string, unknown[]> {
14
14
  : [children];
15
15
 
16
16
  for (const child of arr) {
17
- // Named slots via `slot` attribute on DOM elements
18
17
  if (child instanceof Element) {
19
18
  const slotName = child.getAttribute("slot");
20
19
  if (slotName) {
@@ -39,33 +38,21 @@ export function getSlot(instance: object, name: string): unknown[] {
39
38
  }
40
39
 
41
40
  export function Slot(name?: string) {
42
- return function (
43
- _value: undefined,
44
- context: ClassFieldDecoratorContext<StatefulComponent>,
45
- ): void {
46
- context.addInitializer(function (this: unknown) {
47
- const instance = this as StatefulComponent;
48
- const propKey = context.name as string;
49
- const slotName = name ?? (propKey === "default" ? "default" : propKey);
50
-
51
- Reflect.deleteProperty(instance, propKey);
52
-
53
- Object.defineProperty(instance, propKey, {
54
- get(this: StatefulComponent): unknown[] {
55
- return getSlot(this, slotName);
41
+ return createFieldDecorator({
42
+ bind(instance, propName, _initialValue) {
43
+ const slotName = name ?? (propName === "default" ? "default" : propName);
44
+ return {
45
+ descriptor: {
46
+ get: () => getSlot(instance, slotName),
47
+ set: (_value: unknown) => {
48
+ if (process.env.NODE_ENV !== "production") {
49
+ console.warn(
50
+ `[Slot] "${propName}" is a slot and cannot be assigned directly. Slots are filled by the parent component.`,
51
+ );
52
+ }
53
+ },
56
54
  },
57
-
58
- set(_value: unknown): void {
59
- if (process.env.NODE_ENV !== "production") {
60
- console.warn(
61
- `[Slot] "${propKey}" is a slot and cannot be assigned directly. Slots are filled by the parent component.`,
62
- );
63
- }
64
- },
65
-
66
- enumerable: true,
67
- configurable: true,
68
- });
69
- });
70
- };
55
+ };
56
+ },
57
+ });
71
58
  }
@@ -1,30 +1,20 @@
1
- import type { StatefulComponent } from "@praxisjs/core";
2
1
  import { signal } from "@praxisjs/core/internal";
3
2
 
4
- export function State() {
5
- return function (
6
- _value: undefined,
7
- context: ClassFieldDecoratorContext<StatefulComponent>,
8
- ): void {
9
- context.addInitializer(function (this: unknown) {
10
- const instance = this as StatefulComponent & Record<string, unknown>;
11
- const name = context.name as string;
12
- const initialValue = instance[name];
13
- Reflect.deleteProperty(instance, name);
3
+ import { createFieldDecorator } from "../create-field-decorator";
14
4
 
5
+ export function State() {
6
+ return createFieldDecorator({
7
+ bind(instance, _name, initialValue) {
15
8
  const sig = signal(initialValue);
16
-
17
- Object.defineProperty(instance, name, {
18
- get() {
19
- return sig();
20
- },
21
- set(value: unknown) {
22
- instance._stateDirty = true;
23
- sig.set(value);
9
+ return {
10
+ descriptor: {
11
+ get: () => sig(),
12
+ set: (value: unknown) => {
13
+ instance._stateDirty = true;
14
+ sig.set(value);
15
+ },
24
16
  },
25
- enumerable: true,
26
- configurable: true,
27
- });
28
- });
29
- };
17
+ };
18
+ },
19
+ });
30
20
  }