@praxisjs/decorators 0.7.4 → 0.8.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 (56) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/dist/__tests__/deep-state.test.d.ts +2 -0
  3. package/dist/__tests__/deep-state.test.d.ts.map +1 -0
  4. package/dist/__tests__/deep-state.test.js +150 -0
  5. package/dist/__tests__/deep-state.test.js.map +1 -0
  6. package/dist/__tests__/properties.test.js +57 -4
  7. package/dist/__tests__/properties.test.js.map +1 -1
  8. package/dist/__tests__/synced.test.d.ts +2 -0
  9. package/dist/__tests__/synced.test.d.ts.map +1 -0
  10. package/dist/__tests__/synced.test.js +142 -0
  11. package/dist/__tests__/synced.test.js.map +1 -0
  12. package/dist/__tests__/virtual.test.js +4 -2
  13. package/dist/__tests__/virtual.test.js.map +1 -1
  14. package/dist/component/virtual.d.ts.map +1 -1
  15. package/dist/component/virtual.js +12 -6
  16. package/dist/component/virtual.js.map +1 -1
  17. package/dist/create-lifecycle-method-decorator.d.ts +1 -1
  18. package/dist/create-lifecycle-method-decorator.d.ts.map +1 -1
  19. package/dist/create-lifecycle-method-decorator.js +3 -1
  20. package/dist/create-lifecycle-method-decorator.js.map +1 -1
  21. package/dist/events/on-command.d.ts +1 -1
  22. package/dist/events/on-command.d.ts.map +1 -1
  23. package/dist/functions/watch.d.ts +1 -1
  24. package/dist/functions/watch.d.ts.map +1 -1
  25. package/dist/functions/watch.js +15 -3
  26. package/dist/functions/watch.js.map +1 -1
  27. package/dist/functions/when.d.ts +1 -1
  28. package/dist/functions/when.d.ts.map +1 -1
  29. package/dist/index.d.ts +1 -1
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +1 -1
  32. package/dist/index.js.map +1 -1
  33. package/dist/properties/deep-state.d.ts +2 -0
  34. package/dist/properties/deep-state.d.ts.map +1 -0
  35. package/dist/properties/deep-state.js +55 -0
  36. package/dist/properties/deep-state.js.map +1 -0
  37. package/dist/properties/index.d.ts +2 -0
  38. package/dist/properties/index.d.ts.map +1 -1
  39. package/dist/properties/index.js +2 -0
  40. package/dist/properties/index.js.map +1 -1
  41. package/dist/properties/synced.d.ts +2 -0
  42. package/dist/properties/synced.d.ts.map +1 -0
  43. package/dist/properties/synced.js +26 -0
  44. package/dist/properties/synced.js.map +1 -0
  45. package/package.json +2 -2
  46. package/src/__tests__/deep-state.test.ts +185 -0
  47. package/src/__tests__/properties.test.ts +65 -4
  48. package/src/__tests__/synced.test.ts +172 -0
  49. package/src/__tests__/virtual.test.ts +4 -2
  50. package/src/component/virtual.tsx +36 -12
  51. package/src/create-lifecycle-method-decorator.ts +2 -1
  52. package/src/functions/watch.ts +15 -3
  53. package/src/index.ts +2 -0
  54. package/src/properties/deep-state.ts +60 -0
  55. package/src/properties/index.ts +2 -0
  56. package/src/properties/synced.ts +28 -0
@@ -0,0 +1,60 @@
1
+ import { signal } from "@praxisjs/core/internal";
2
+
3
+ import { createFieldDecorator } from "../create-field-decorator";
4
+
5
+ function deepProxy<T extends object>(target: T, notify: () => void): T {
6
+ return new Proxy(target, {
7
+ get(obj, key, receiver) {
8
+ const val: unknown = Reflect.get(obj, key, receiver);
9
+ if (val !== null && typeof val === "object") {
10
+ return deepProxy(val, notify);
11
+ }
12
+ return val;
13
+ },
14
+ set(obj, key, value) {
15
+ const result = Reflect.set(obj, key, value);
16
+ notify();
17
+ return result;
18
+ },
19
+ deleteProperty(obj, key) {
20
+ const result = Reflect.deleteProperty(obj, key);
21
+ notify();
22
+ return result;
23
+ },
24
+ });
25
+ }
26
+
27
+ export function DeepState() {
28
+ return createFieldDecorator({
29
+ bind(instance, _name, initialValue) {
30
+ const version = signal(0);
31
+ let current = initialValue;
32
+ let proxy =
33
+ current !== null && typeof current === "object"
34
+ ? deepProxy(current, notify)
35
+ : current;
36
+
37
+ function notify() {
38
+ instance._stateDirty = true;
39
+ version.update((v) => v + 1);
40
+ }
41
+
42
+ return {
43
+ descriptor: {
44
+ get() {
45
+ version();
46
+ return proxy;
47
+ },
48
+ set(value: unknown) {
49
+ current = value;
50
+ proxy =
51
+ value !== null && typeof value === "object"
52
+ ? deepProxy(value, notify)
53
+ : value;
54
+ notify();
55
+ },
56
+ },
57
+ };
58
+ },
59
+ });
60
+ }
@@ -6,3 +6,5 @@ export { Slot, initSlots } from "./slot";
6
6
  export { History, type HistoryOf } from "./history";
7
7
  export { Compose } from "./compose";
8
8
  export { Resource, type ResourceInstance, type ResourceOptions } from "./resource";
9
+ export { Synced } from "./synced";
10
+ export { DeepState } from "./deep-state";
@@ -0,0 +1,28 @@
1
+ import { syncedSignal, type SyncedSignal } from "@praxisjs/core/internal";
2
+
3
+ import { createFieldDecorator } from "../create-field-decorator";
4
+
5
+ const signalMap = new WeakMap<object, Map<string, SyncedSignal<unknown>>>();
6
+
7
+ export function Synced(channelName?: string) {
8
+ return createFieldDecorator({
9
+ bind(instance, name, initialValue) {
10
+ const key = channelName ?? name;
11
+
12
+ if (!signalMap.has(instance)) signalMap.set(instance, new Map());
13
+ const map = signalMap.get(instance) as Map<string, SyncedSignal<unknown>>;
14
+ if (!map.has(key)) map.set(key, syncedSignal(key, initialValue));
15
+ const sig = map.get(key) as SyncedSignal<unknown>;
16
+
17
+ return {
18
+ descriptor: {
19
+ get: () => sig(),
20
+ set: (value: unknown) => { sig.set(value); },
21
+ },
22
+ onUnmount() {
23
+ sig.close();
24
+ },
25
+ };
26
+ },
27
+ });
28
+ }