@praxisjs/decorators 0.7.5 → 0.8.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.
Files changed (64) hide show
  1. package/CHANGELOG.md +29 -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 +29 -0
  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/component/virtual.d.ts.map +1 -1
  13. package/dist/component/virtual.js +12 -6
  14. package/dist/component/virtual.js.map +1 -1
  15. package/dist/create-field-decorator.d.ts +1 -1
  16. package/dist/create-field-decorator.d.ts.map +1 -1
  17. package/dist/create-field-decorator.js.map +1 -1
  18. package/dist/index.d.ts +2 -1
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +1 -1
  21. package/dist/index.js.map +1 -1
  22. package/dist/properties/compose.d.ts +1 -1
  23. package/dist/properties/compose.d.ts.map +1 -1
  24. package/dist/properties/deep-state.d.ts +3 -0
  25. package/dist/properties/deep-state.d.ts.map +1 -0
  26. package/dist/properties/deep-state.js +55 -0
  27. package/dist/properties/deep-state.js.map +1 -0
  28. package/dist/properties/history.d.ts +1 -1
  29. package/dist/properties/history.d.ts.map +1 -1
  30. package/dist/properties/index.d.ts +2 -0
  31. package/dist/properties/index.d.ts.map +1 -1
  32. package/dist/properties/index.js +2 -0
  33. package/dist/properties/index.js.map +1 -1
  34. package/dist/properties/persisted.d.ts +1 -1
  35. package/dist/properties/persisted.d.ts.map +1 -1
  36. package/dist/properties/prop.d.ts +1 -1
  37. package/dist/properties/prop.d.ts.map +1 -1
  38. package/dist/properties/resource.d.ts +1 -1
  39. package/dist/properties/resource.d.ts.map +1 -1
  40. package/dist/properties/slot.d.ts +1 -1
  41. package/dist/properties/slot.d.ts.map +1 -1
  42. package/dist/properties/state.d.ts +2 -1
  43. package/dist/properties/state.d.ts.map +1 -1
  44. package/dist/properties/state.js.map +1 -1
  45. package/dist/properties/synced.d.ts +2 -0
  46. package/dist/properties/synced.d.ts.map +1 -0
  47. package/dist/properties/synced.js +26 -0
  48. package/dist/properties/synced.js.map +1 -0
  49. package/dist/reactive-host.d.ts +4 -0
  50. package/dist/reactive-host.d.ts.map +1 -0
  51. package/dist/reactive-host.js +2 -0
  52. package/dist/reactive-host.js.map +1 -0
  53. package/package.json +2 -2
  54. package/src/__tests__/deep-state.test.ts +185 -0
  55. package/src/__tests__/properties.test.ts +33 -0
  56. package/src/__tests__/synced.test.ts +172 -0
  57. package/src/component/virtual.tsx +36 -12
  58. package/src/create-field-decorator.ts +3 -3
  59. package/src/index.ts +3 -0
  60. package/src/properties/deep-state.ts +62 -0
  61. package/src/properties/index.ts +2 -0
  62. package/src/properties/state.ts +3 -1
  63. package/src/properties/synced.ts +28 -0
  64. package/src/reactive-host.ts +3 -0
@@ -0,0 +1,172 @@
1
+ // @vitest-environment jsdom
2
+ import { describe, it, expect, vi, beforeEach } from "vitest";
3
+
4
+ import { StatefulComponent } from "@praxisjs/core";
5
+ import { effect } from "@praxisjs/core/internal";
6
+
7
+ import { Synced } from "../properties/synced";
8
+
9
+ // BroadcastChannel mock (same as core test)
10
+ class MockBroadcastChannel {
11
+ static instances: MockBroadcastChannel[] = [];
12
+ static byChannel: Map<string, MockBroadcastChannel[]> = new Map();
13
+
14
+ onmessage: ((event: MessageEvent) => void) | null = null;
15
+ readonly name: string;
16
+ closed = false;
17
+
18
+ constructor(name: string) {
19
+ this.name = name;
20
+ MockBroadcastChannel.instances.push(this);
21
+ if (!MockBroadcastChannel.byChannel.has(name)) {
22
+ MockBroadcastChannel.byChannel.set(name, []);
23
+ }
24
+ MockBroadcastChannel.byChannel.get(name)!.push(this);
25
+ }
26
+
27
+ postMessage(data: unknown) {
28
+ MockBroadcastChannel.byChannel.get(this.name)?.forEach((ch) => {
29
+ if (ch !== this && ch.onmessage) {
30
+ ch.onmessage(new MessageEvent("message", { data }));
31
+ }
32
+ });
33
+ }
34
+
35
+ close() {
36
+ this.closed = true;
37
+ const list = MockBroadcastChannel.byChannel.get(this.name);
38
+ if (list) {
39
+ const idx = list.indexOf(this);
40
+ if (idx !== -1) list.splice(idx, 1);
41
+ }
42
+ MockBroadcastChannel.instances.splice(
43
+ MockBroadcastChannel.instances.indexOf(this),
44
+ 1,
45
+ );
46
+ }
47
+ }
48
+
49
+ beforeEach(() => {
50
+ MockBroadcastChannel.instances = [];
51
+ MockBroadcastChannel.byChannel = new Map();
52
+ vi.stubGlobal("BroadcastChannel", MockBroadcastChannel);
53
+ });
54
+
55
+ class TestComponent extends StatefulComponent {
56
+ render() { return null; }
57
+ }
58
+
59
+ function fieldCtx(name: string) {
60
+ const initializers: Array<(this: unknown) => void> = [];
61
+ return {
62
+ ctx: {
63
+ name,
64
+ kind: "field" as const,
65
+ addInitializer(fn: (this: unknown) => void) { initializers.push(fn); },
66
+ } as ClassFieldDecoratorContext,
67
+ run(instance: unknown) {
68
+ initializers.forEach((fn) => { fn.call(instance); });
69
+ },
70
+ };
71
+ }
72
+
73
+ describe("@Synced decorator", () => {
74
+ it("reads and writes the field reactively", () => {
75
+ const { ctx, run } = fieldCtx("count");
76
+ Synced()(undefined, ctx);
77
+ const instance = new TestComponent();
78
+ (instance as unknown as Record<string, unknown>).count = 0;
79
+ run(instance);
80
+
81
+ const inst = instance as unknown as { count: number };
82
+ expect(inst.count).toBe(0);
83
+ inst.count = 5;
84
+ expect(inst.count).toBe(5);
85
+ });
86
+
87
+ it("uses field name as channel name by default", () => {
88
+ const { ctx, run } = fieldCtx("cart");
89
+ Synced()(undefined, ctx);
90
+ const instance = new TestComponent();
91
+ (instance as unknown as Record<string, unknown>).cart = [];
92
+ run(instance);
93
+
94
+ expect(MockBroadcastChannel.instances[0].name).toBe("cart");
95
+ });
96
+
97
+ it("uses custom channel name when provided", () => {
98
+ const { ctx, run } = fieldCtx("items");
99
+ Synced("my-channel")(undefined, ctx);
100
+ const instance = new TestComponent();
101
+ (instance as unknown as Record<string, unknown>).items = [];
102
+ run(instance);
103
+
104
+ expect(MockBroadcastChannel.instances[0].name).toBe("my-channel");
105
+ });
106
+
107
+ it("triggers reactive effects on write", () => {
108
+ const { ctx, run } = fieldCtx("value");
109
+ Synced()(undefined, ctx);
110
+ const instance = new TestComponent();
111
+ (instance as unknown as Record<string, unknown>).value = 0;
112
+ run(instance);
113
+
114
+ const inst = instance as unknown as { value: number };
115
+ const log: number[] = [];
116
+ effect(() => { log.push(inst.value); });
117
+
118
+ inst.value = 10;
119
+ expect(log).toEqual([0, 10]);
120
+ });
121
+
122
+ it("updates field when a remote message arrives", () => {
123
+ const { ctx, run } = fieldCtx("score");
124
+ Synced()(undefined, ctx);
125
+ const instance = new TestComponent();
126
+ (instance as unknown as Record<string, unknown>).score = 0;
127
+ run(instance);
128
+
129
+ const inst = instance as unknown as { score: number };
130
+ const channel = MockBroadcastChannel.instances[0];
131
+ channel.onmessage!(new MessageEvent("message", { data: JSON.stringify(42) }));
132
+
133
+ expect(inst.score).toBe(42);
134
+ });
135
+
136
+ it("two @Synced fields on the same instance share the signalMap entry", () => {
137
+ const { ctx: ctx1, run: run1 } = fieldCtx("a");
138
+ const { ctx: ctx2, run: run2 } = fieldCtx("b");
139
+ Synced()(undefined, ctx1);
140
+ Synced()(undefined, ctx2);
141
+
142
+ const instance = new TestComponent();
143
+ (instance as unknown as Record<string, unknown>).a = 1;
144
+ (instance as unknown as Record<string, unknown>).b = 2;
145
+ run1(instance);
146
+ run2(instance);
147
+
148
+ const inst = instance as unknown as { a: number; b: number };
149
+ inst.a = 10;
150
+ inst.b = 20;
151
+ expect(inst.a).toBe(10);
152
+ expect(inst.b).toBe(20);
153
+
154
+ // Both fields have their own BroadcastChannel
155
+ expect(MockBroadcastChannel.instances.length).toBe(2);
156
+ instance.onUnmount?.();
157
+ expect(MockBroadcastChannel.instances.every((ch) => ch.closed)).toBe(true);
158
+ });
159
+
160
+ it("onUnmount closes the BroadcastChannel", () => {
161
+ const { ctx, run } = fieldCtx("data");
162
+ Synced()(undefined, ctx);
163
+ const instance = new TestComponent();
164
+ (instance as unknown as Record<string, unknown>).data = null;
165
+ run(instance);
166
+
167
+ const channel = MockBroadcastChannel.instances[0];
168
+ expect(channel.closed).toBe(false);
169
+ instance.onUnmount?.();
170
+ expect(channel.closed).toBe(true);
171
+ });
172
+ });
@@ -1,6 +1,15 @@
1
- import { computed, effect, type RootComponent, signal } from "@praxisjs/core/internal";
2
-
3
- import { ClassBehavior, createClassDecorator, type ClassEnhancement } from "../create-class-decorator";
1
+ import {
2
+ computed,
3
+ effect,
4
+ type RootComponent,
5
+ signal,
6
+ } from "@praxisjs/core/internal";
7
+
8
+ import {
9
+ ClassBehavior,
10
+ createClassDecorator,
11
+ type ClassEnhancement,
12
+ } from "../create-class-decorator";
4
13
 
5
14
  interface VirtualHost {
6
15
  _anchor?: Comment;
@@ -39,8 +48,9 @@ class VirtualBehavior extends ClassBehavior {
39
48
  container.style.position = "relative";
40
49
  viewHeight.set(container.clientHeight || 600);
41
50
 
51
+ const currentContainer = container;
42
52
  scrollHandler = () => {
43
- if (container) scrollTop.set(container.scrollTop);
53
+ scrollTop.set(currentContainer.scrollTop);
44
54
  };
45
55
  container.addEventListener("scroll", scrollHandler);
46
56
  },
@@ -49,7 +59,9 @@ class VirtualBehavior extends ClassBehavior {
49
59
  if (container && scrollHandler) {
50
60
  container.removeEventListener("scroll", scrollHandler);
51
61
  }
52
- cleanups.forEach((c) => { c(); });
62
+ cleanups.forEach((c) => {
63
+ c();
64
+ });
53
65
  cleanups.length = 0;
54
66
  },
55
67
 
@@ -84,10 +96,15 @@ class VirtualBehavior extends ClassBehavior {
84
96
  return result;
85
97
  });
86
98
  const offsetTop = computed(() => startIdx() * itemHeight);
87
- const offsetBottom = computed(() => (total - 1 - endIdx()) * itemHeight);
99
+ const offsetBottom = computed(
100
+ () => (total - 1 - endIdx()) * itemHeight,
101
+ );
88
102
 
89
103
  const outer = document.createElement("div");
90
- outer.setAttribute("style", `height:${String(totalH)}px; position:relative;`);
104
+ outer.setAttribute(
105
+ "style",
106
+ `height:${String(totalH)}px; position:relative;`,
107
+ );
91
108
 
92
109
  const spacerTop = document.createElement("div");
93
110
  cleanups.push(
@@ -99,7 +116,8 @@ class VirtualBehavior extends ClassBehavior {
99
116
  const itemsSlot = document.createElement("div");
100
117
  cleanups.push(
101
118
  effect(() => {
102
- while (itemsSlot.firstChild) itemsSlot.removeChild(itemsSlot.firstChild);
119
+ while (itemsSlot.firstChild)
120
+ itemsSlot.removeChild(itemsSlot.firstChild);
103
121
  visibleItems().forEach(({ item, index }) => {
104
122
  const wrapper = document.createElement("div");
105
123
  wrapper.setAttribute(
@@ -108,9 +126,12 @@ class VirtualBehavior extends ClassBehavior {
108
126
  );
109
127
  const rendered = renderItem(item, index);
110
128
  if (rendered) {
111
- (Array.isArray(rendered) ? rendered.flat() : [rendered]).forEach(
112
- (n) => { wrapper.appendChild(n); },
113
- );
129
+ (Array.isArray(rendered)
130
+ ? rendered.flat()
131
+ : [rendered]
132
+ ).forEach((n) => {
133
+ wrapper.appendChild(n);
134
+ });
114
135
  }
115
136
  itemsSlot.appendChild(wrapper);
116
137
  });
@@ -120,7 +141,10 @@ class VirtualBehavior extends ClassBehavior {
120
141
  const spacerBottom = document.createElement("div");
121
142
  cleanups.push(
122
143
  effect(() => {
123
- spacerBottom.setAttribute("style", `height:${String(offsetBottom())}px;`);
144
+ spacerBottom.setAttribute(
145
+ "style",
146
+ `height:${String(offsetBottom())}px;`,
147
+ );
124
148
  }),
125
149
  );
126
150
 
@@ -18,12 +18,12 @@ export interface FieldBehavior<T = object> {
18
18
  }
19
19
 
20
20
  export function createFieldDecorator<T = StatefulComponent>(behavior: FieldBehavior<T>) {
21
- return function (
21
+ return function <This extends T, Value = unknown>(
22
22
  _value: undefined,
23
- context: ClassFieldDecoratorContext<T>,
23
+ context: ClassFieldDecoratorContext<This, Value>,
24
24
  ): void {
25
25
  context.addInitializer(function (this: unknown) {
26
- const instance = this as T & StatefulComponent & Record<string, unknown>;
26
+ const instance = this as This & StatefulComponent & Record<string, unknown>;
27
27
  const name = context.name as string;
28
28
  const initialValue = instance[name];
29
29
 
package/src/index.ts CHANGED
@@ -26,6 +26,8 @@ export {
26
26
  Resource,
27
27
  type ResourceInstance,
28
28
  type ResourceOptions,
29
+ Synced,
30
+ DeepState,
29
31
  } from "./properties";
30
32
  export { type Command, createCommand, Emit, OnCommand } from "./events";
31
33
  export {
@@ -33,6 +35,7 @@ export {
33
35
  type FieldBehavior,
34
36
  type FieldBinding,
35
37
  } from "./create-field-decorator";
38
+ export { type ReactiveHost } from "./reactive-host";
36
39
  export {
37
40
  createClassDecorator,
38
41
  ClassBehavior,
@@ -0,0 +1,62 @@
1
+ import { signal } from "@praxisjs/core/internal";
2
+
3
+ import { createFieldDecorator } from "../create-field-decorator";
4
+
5
+ import type { ReactiveHost } from "../reactive-host";
6
+
7
+ function deepProxy<T extends object>(target: T, notify: () => void): T {
8
+ return new Proxy(target, {
9
+ get(obj, key, receiver) {
10
+ const val: unknown = Reflect.get(obj, key, receiver);
11
+ if (val !== null && typeof val === "object") {
12
+ return deepProxy(val, notify);
13
+ }
14
+ return val;
15
+ },
16
+ set(obj, key, value) {
17
+ const result = Reflect.set(obj, key, value);
18
+ notify();
19
+ return result;
20
+ },
21
+ deleteProperty(obj, key) {
22
+ const result = Reflect.deleteProperty(obj, key);
23
+ notify();
24
+ return result;
25
+ },
26
+ });
27
+ }
28
+
29
+ export function DeepState() {
30
+ return createFieldDecorator<ReactiveHost>({
31
+ bind(instance, _name, initialValue) {
32
+ const version = signal(0);
33
+ let current = initialValue;
34
+ let proxy =
35
+ current !== null && typeof current === "object"
36
+ ? deepProxy(current, notify)
37
+ : current;
38
+
39
+ function notify() {
40
+ instance._stateDirty = true;
41
+ version.update((v) => v + 1);
42
+ }
43
+
44
+ return {
45
+ descriptor: {
46
+ get() {
47
+ version();
48
+ return proxy;
49
+ },
50
+ set(value: unknown) {
51
+ current = value;
52
+ proxy =
53
+ value !== null && typeof value === "object"
54
+ ? deepProxy(value, notify)
55
+ : value;
56
+ notify();
57
+ },
58
+ },
59
+ };
60
+ },
61
+ });
62
+ }
@@ -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";
@@ -2,8 +2,10 @@ import { signal } from "@praxisjs/core/internal";
2
2
 
3
3
  import { createFieldDecorator } from "../create-field-decorator";
4
4
 
5
+ import type { ReactiveHost } from "../reactive-host";
6
+
5
7
  export function State() {
6
- return createFieldDecorator({
8
+ return createFieldDecorator<ReactiveHost>({
7
9
  bind(instance, _name, initialValue) {
8
10
  const sig = signal(initialValue);
9
11
  return {
@@ -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
+ }
@@ -0,0 +1,3 @@
1
+ export interface ReactiveHost {
2
+ _stateDirty: boolean;
3
+ }