@rs-x/vue 2.0.0-next.57 → 2.0.0-next.59

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.
package/dist/index.d.ts CHANGED
@@ -8,4 +8,7 @@ declare function getExpressionManager(): IExpressionManager;
8
8
 
9
9
  declare function useRsxExpression<T>(expression: IExpression<T>): vue.Ref<any, any> extends T | null ? vue.ShallowRef<null, null> | (T extends T & vue.Ref<any, any> ? _vue_shared.IfAny<T, vue.ShallowRef<T, T>, T> : vue.ShallowRef<T, T>) : vue.ShallowRef<T | null, T | null>;
10
10
 
11
- export { getExpressionFactory, getExpressionManager, useRsxExpression };
11
+ type FieldFilter = (model: object, field: string) => boolean;
12
+ declare function useRsxModel<TModel extends object>(model: TModel, mustWatch?: FieldFilter): TModel;
13
+
14
+ export { type FieldFilter, getExpressionFactory, getExpressionManager, useRsxExpression, useRsxModel };
package/dist/index.js CHANGED
@@ -54,8 +54,72 @@ function useRsxExpression(expression) {
54
54
  return value;
55
55
  }
56
56
  __name(useRsxExpression, "useRsxExpression");
57
+
58
+ // src/hooks/use-rsx-model.ts
59
+ import { getCurrentInstance, getCurrentScope as getCurrentScope2, markRaw, onScopeDispose as onScopeDispose2 } from "vue";
60
+ import { truePredicate, Type, UnsupportedException } from "@rs-x/core";
61
+ import { rsx } from "@rs-x/expression-parser";
62
+ function useRsxModel(model, mustWatch) {
63
+ const expressionCache = /* @__PURE__ */ new Map();
64
+ const subscriptions = [];
65
+ const shouldWatch = mustWatch ?? truePredicate;
66
+ const currentInstance = getCurrentInstance();
67
+ const forceUpdate = /* @__PURE__ */ __name(() => {
68
+ currentInstance?.proxy?.$forceUpdate();
69
+ }, "forceUpdate");
70
+ const getOrCreateExpression = /* @__PURE__ */ __name((parent, field) => {
71
+ let fieldMap = expressionCache.get(parent);
72
+ if (!fieldMap) {
73
+ fieldMap = /* @__PURE__ */ new Map();
74
+ expressionCache.set(parent, fieldMap);
75
+ }
76
+ let expression = fieldMap.get(field);
77
+ if (!expression) {
78
+ expression = rsx(field)(parent);
79
+ fieldMap.set(field, expression);
80
+ }
81
+ return expression;
82
+ }, "getOrCreateExpression");
83
+ const disposeExpressions = /* @__PURE__ */ __name(() => {
84
+ for (const subscription of subscriptions) {
85
+ subscription.unsubscribe();
86
+ }
87
+ for (const fieldMap of expressionCache.values()) {
88
+ for (const expression of fieldMap.values()) {
89
+ expression.dispose();
90
+ }
91
+ }
92
+ }, "disposeExpressions");
93
+ if (getCurrentScope2()) {
94
+ onScopeDispose2(() => {
95
+ disposeExpressions();
96
+ });
97
+ }
98
+ const bindObject = /* @__PURE__ */ __name((sourceObject) => {
99
+ for (const [field, value] of Object.entries(sourceObject)) {
100
+ if (!shouldWatch(sourceObject, field) || Type.isMethod(value) || Type.isArrowFunction(value)) {
101
+ continue;
102
+ }
103
+ if (Type.isIterableCollection(value)) {
104
+ throw new UnsupportedException("Collections are not supported by useRsxModel yet.");
105
+ }
106
+ if (Type.isPlainObject(value)) {
107
+ bindObject(value);
108
+ continue;
109
+ }
110
+ const expression = getOrCreateExpression(sourceObject, field);
111
+ subscriptions.push(expression.changed.subscribe(() => {
112
+ forceUpdate();
113
+ }));
114
+ }
115
+ }, "bindObject");
116
+ bindObject(model);
117
+ return markRaw(model);
118
+ }
119
+ __name(useRsxModel, "useRsxModel");
57
120
  export {
58
121
  getExpressionFactory,
59
122
  getExpressionManager,
60
- useRsxExpression
123
+ useRsxExpression,
124
+ useRsxModel
61
125
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rs-x/vue",
3
- "version": "2.0.0-next.57",
3
+ "version": "2.0.0-next.59",
4
4
  "description": "Vue extension for the RS-X framework with composables to bind RS-X expressions to Vue components",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -38,9 +38,9 @@
38
38
  "vue": ">=3.3.0"
39
39
  },
40
40
  "dependencies": {
41
- "@rs-x/core": "2.0.0-next.57",
42
- "@rs-x/expression-parser": "2.0.0-next.57",
43
- "@rs-x/state-manager": "2.0.0-next.57"
41
+ "@rs-x/core": "2.0.0-next.59",
42
+ "@rs-x/expression-parser": "2.0.0-next.59",
43
+ "@rs-x/state-manager": "2.0.0-next.59"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@vitejs/plugin-vue": "^6.0.5",