@praxisjs/decorators 0.7.5 → 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 (39) hide show
  1. package/CHANGELOG.md +17 -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/index.d.ts +1 -1
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +1 -1
  18. package/dist/index.js.map +1 -1
  19. package/dist/properties/deep-state.d.ts +2 -0
  20. package/dist/properties/deep-state.d.ts.map +1 -0
  21. package/dist/properties/deep-state.js +55 -0
  22. package/dist/properties/deep-state.js.map +1 -0
  23. package/dist/properties/index.d.ts +2 -0
  24. package/dist/properties/index.d.ts.map +1 -1
  25. package/dist/properties/index.js +2 -0
  26. package/dist/properties/index.js.map +1 -1
  27. package/dist/properties/synced.d.ts +2 -0
  28. package/dist/properties/synced.d.ts.map +1 -0
  29. package/dist/properties/synced.js +26 -0
  30. package/dist/properties/synced.js.map +1 -0
  31. package/package.json +2 -2
  32. package/src/__tests__/deep-state.test.ts +185 -0
  33. package/src/__tests__/properties.test.ts +33 -0
  34. package/src/__tests__/synced.test.ts +172 -0
  35. package/src/component/virtual.tsx +36 -12
  36. package/src/index.ts +2 -0
  37. package/src/properties/deep-state.ts +60 -0
  38. package/src/properties/index.ts +2 -0
  39. package/src/properties/synced.ts +28 -0
@@ -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
 
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 {
@@ -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
+ }