@gi-tcg/gts-runtime 0.3.9 → 0.3.11

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
@@ -138,6 +138,12 @@ type BlockDefinitionRewriteMeta<BlockDef extends AttributeBlockDefinition, NewMe
138
138
  }> extends infer R extends AttributeBlockDefinition ? R : never;
139
139
  interface IViewModel<ModelT, BlockDef extends AttributeBlockDefinition, CtorArgs extends any[]> {
140
140
  parse(view: View<BlockDefinitionRewriteMeta<BlockDef, unknown>>, ...args: CtorArgs): ModelT;
141
+ /**
142
+ * Creates a new ViewModel with the same actions and binders, but with a different constructor
143
+ * that accepts the specified arguments. This is useful for creating binding ViewModels directly
144
+ * that forwards the binding logic to inner ViewModels.
145
+ */
146
+ bind(...args: CtorArgs): ViewModel<ModelT, BlockDef, []>;
141
147
  "~namedDefinition": BlockDef;
142
148
  }
143
149
  type LazyAttributeActionOrBinder<ModelT> = (model: ModelT, positionals: () => unknown[], named: View<any>) => unknown;
@@ -147,6 +153,7 @@ declare class ViewModel<ModelT, BlockDef extends AttributeBlockDefinition, CtorA
147
153
  constructor(Ctor: new (...args: CtorArgs) => ModelT);
148
154
  "~setActionOrBinder"(context: "action" | "binder", name: PropertyKey, action: LazyAttributeActionOrBinder<ModelT>): void;
149
155
  parse(view: View<BlockDefinitionRewriteMeta<BlockDef, unknown>>, ...args: CtorArgs): ModelT;
156
+ bind(...args: CtorArgs): ViewModel<ModelT, BlockDef, []>;
150
157
  }
151
158
  interface SimpleAttributeOptions {
152
159
  required?: true;
@@ -171,7 +178,7 @@ declare class AttributeDefHelper<ModelT> {
171
178
  };
172
179
  <Args extends any[], U>(action: (this: ModelT, ...args: Args) => void, binder: (this: ModelT, ...args: Args) => U): {
173
180
  (...args: Args): AttributeReturn.Done;
174
- as?(): U;
181
+ as(): U;
175
182
  };
176
183
  };
177
184
  simpleAttribute<const Options extends SimpleAttributeOptions>(options: Options): {
@@ -180,7 +187,7 @@ declare class AttributeDefHelper<ModelT> {
180
187
  }, Options>;
181
188
  <Args extends any[], U>(action: (this: ModelT, ...args: Args) => void, binder: (this: ModelT, ...args: Args) => U): WithSimpleOptions<{
182
189
  (...args: Args): AttributeReturn.Done;
183
- as?(): U;
190
+ as(): U;
184
191
  }, Options>;
185
192
  };
186
193
  }
package/dist/index.js CHANGED
@@ -29,7 +29,7 @@ function createBinding(rootVM, node) {
29
29
  }
30
30
  //#endregion
31
31
  //#region src/view_model.ts
32
- var ViewModel = class {
32
+ var ViewModel = class ViewModel {
33
33
  #registeredActions = /* @__PURE__ */ new Map();
34
34
  #registeredBinders = /* @__PURE__ */ new Map();
35
35
  #Ctor;
@@ -57,6 +57,21 @@ var ViewModel = class {
57
57
  }
58
58
  return model;
59
59
  }
60
+ #clone() {
61
+ const newVM = new ViewModel(this.#Ctor);
62
+ newVM.#registeredActions = new Map(this.#registeredActions);
63
+ newVM.#registeredBinders = new Map(this.#registeredBinders);
64
+ return newVM;
65
+ }
66
+ bind(...args) {
67
+ const newModel = this.#clone();
68
+ newModel.#Ctor = class extends this.#Ctor {
69
+ constructor() {
70
+ super(...args);
71
+ }
72
+ };
73
+ return newModel;
74
+ }
60
75
  };
61
76
  var AttributeDefHelper = class AttributeDefHelper {
62
77
  #viewModel;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gi-tcg/gts-runtime",
3
- "version": "0.3.9",
3
+ "version": "0.3.11",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/piovium/gts.git"
package/src/view_model.ts CHANGED
@@ -30,6 +30,12 @@ export interface IViewModel<
30
30
  view: View<BlockDefinitionRewriteMeta<BlockDef, unknown>>,
31
31
  ...args: CtorArgs
32
32
  ): ModelT;
33
+ /**
34
+ * Creates a new ViewModel with the same actions and binders, but with a different constructor
35
+ * that accepts the specified arguments. This is useful for creating binding ViewModels directly
36
+ * that forwards the binding logic to inner ViewModels.
37
+ */
38
+ bind(...args: CtorArgs): ViewModel<ModelT, BlockDef, []>;
33
39
  "~namedDefinition": BlockDef;
34
40
  }
35
41
 
@@ -95,6 +101,25 @@ class ViewModel<
95
101
  }
96
102
  return model;
97
103
  }
104
+
105
+ #clone() {
106
+ const newVM = new ViewModel(this.#Ctor);
107
+ newVM.#registeredActions = new Map(this.#registeredActions);
108
+ newVM.#registeredBinders = new Map(this.#registeredBinders);
109
+ return newVM;
110
+ }
111
+
112
+ bind(...args: CtorArgs): ViewModel<ModelT, BlockDef, []> {
113
+ const newModel = this.#clone() as ViewModel<any, BlockDef, any>;
114
+ newModel.#Ctor = class extends (
115
+ (this.#Ctor as new (...args: CtorArgs) => any)
116
+ ) {
117
+ constructor() {
118
+ super(...args);
119
+ }
120
+ };
121
+ return newModel;
122
+ }
98
123
  }
99
124
 
100
125
  interface SimpleAttributeOptions {
@@ -201,7 +226,7 @@ class AttributeDefHelper<ModelT> {
201
226
  <Args extends any[], U>(
202
227
  action: (this: ModelT, ...args: Args) => void,
203
228
  binder: (this: ModelT, ...args: Args) => U,
204
- ): { (...args: Args): AttributeReturn.Done; as?(): U };
229
+ ): { (...args: Args): AttributeReturn.Done; as(): U };
205
230
  };
206
231
  simpleAttribute<const Options extends SimpleAttributeOptions>(
207
232
  options: Options,
@@ -213,14 +238,14 @@ class AttributeDefHelper<ModelT> {
213
238
  action: (this: ModelT, ...args: Args) => void,
214
239
  binder: (this: ModelT, ...args: Args) => U,
215
240
  ): WithSimpleOptions<
216
- { (...args: Args): AttributeReturn.Done; as?(): U },
241
+ { (...args: Args): AttributeReturn.Done; as(): U },
217
242
  Options
218
243
  >;
219
244
  };
220
245
  simpleAttribute(options?: SimpleAttributeOptions) {
221
246
  return (
222
247
  action: (this: ModelT, ...args: any[]) => void,
223
- binder?: (...args: any[]) => any,
248
+ binder?: (this: ModelT, ...args: any[]) => any,
224
249
  ) => {
225
250
  const action2: AttributeAction<ModelT, any> = (model, positionals) =>
226
251
  action.apply(model, positionals);