@gi-tcg/gts-runtime 0.4.0 → 0.4.2

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
@@ -139,11 +139,15 @@ type BlockDefinitionRewriteMeta<BlockDef extends AttributeBlockDefinition, NewMe
139
139
  interface IViewModel<ModelT, BlockDef extends AttributeBlockDefinition, CtorArgs extends any[]> {
140
140
  parse(view: View<BlockDefinitionRewriteMeta<BlockDef, unknown>>, ...args: CtorArgs): ModelT;
141
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.
142
+ * Creates a new ViewModel with the same actions and binders, but with
143
+ * - a different constructor that accepts the specified arguments, and/or
144
+ * - a different initial Meta.
145
+ *
146
+ * This is useful for creating binding ViewModels directly that forwards
147
+ * the binding logic to inner ViewModels.
145
148
  */
146
- bind(...args: CtorArgs): ViewModel<ModelT, BlockDef, []>;
149
+ bind<NewMeta = BlockDef[Meta]>(): ViewModel<ModelT, BlockDefinitionRewriteMeta<BlockDef, NewMeta>, CtorArgs>;
150
+ bind<NewMeta = BlockDef[Meta]>(...args: CtorArgs): ViewModel<ModelT, BlockDefinitionRewriteMeta<BlockDef, NewMeta>, []>;
147
151
  "~namedDefinition": BlockDef;
148
152
  }
149
153
  type LazyAttributeActionOrBinder<ModelT> = (model: ModelT, positionals: () => unknown[], named: View<any>) => unknown;
@@ -153,7 +157,8 @@ declare class ViewModel<ModelT, BlockDef extends AttributeBlockDefinition, CtorA
153
157
  constructor(Ctor: new (...args: CtorArgs) => ModelT);
154
158
  "~setActionOrBinder"(context: "action" | "binder", name: PropertyKey, action: LazyAttributeActionOrBinder<ModelT>): void;
155
159
  parse(view: View<BlockDefinitionRewriteMeta<BlockDef, unknown>>, ...args: CtorArgs): ModelT;
156
- bind(...args: CtorArgs): ViewModel<ModelT, BlockDef, []>;
160
+ bind<NewMeta = BlockDef[Meta]>(): ViewModel<ModelT, BlockDefinitionRewriteMeta<BlockDef, NewMeta>, CtorArgs>;
161
+ bind<NewMeta = BlockDef[Meta]>(...args: CtorArgs): ViewModel<ModelT, BlockDefinitionRewriteMeta<BlockDef, NewMeta>, []>;
157
162
  }
158
163
  interface SimpleAttributeOptions {
159
164
  required?: true;
@@ -212,10 +217,23 @@ interface AttributePositionalReturnBase {
212
217
  rewriteMeta?: any;
213
218
  namedDefinition: AttributeBlockDefinition;
214
219
  }
215
- type AttributeAction<Model, T extends AttributeDefinition> = (model: Model, positional: Parameters<T>, named: View<ReturnType<T>["namedDefinition"] extends AttributeBlockDefinition ? ReturnType<T>["namedDefinition"] : {
220
+ type OverloadedParameters<T extends (...args: any[]) => any> = T extends {
221
+ (...args: infer A1): any;
222
+ (...args: infer A2): any;
223
+ (...args: infer A3): any;
224
+ (...args: infer A4): any;
225
+ } ? A1 | A2 | A3 | A4 : T extends {
226
+ (...args: infer A1): any;
227
+ (...args: infer A2): any;
228
+ (...args: infer A3): any;
229
+ } ? A1 | A2 | A3 : T extends {
230
+ (...args: infer A1): any;
231
+ (...args: infer A2): any;
232
+ } ? A1 | A2 : T extends ((...args: infer A) => any) ? A : never;
233
+ type AttributeAction<Model, T extends AttributeDefinition> = (model: Model, positional: OverloadedParameters<T>, named: View<ReturnType<T>["namedDefinition"] extends AttributeBlockDefinition ? ReturnType<T>["namedDefinition"] : {
216
234
  "~meta": void;
217
235
  }>) => void;
218
- type AttributeBinder<Model, T extends AttributeDefinition> = (model: Model, positional: Parameters<T>, named: View<ReturnType<T>["namedDefinition"] extends AttributeBlockDefinition ? ReturnType<T>["namedDefinition"] : {
236
+ type AttributeBinder<Model, T extends AttributeDefinition> = (model: Model, positional: OverloadedParameters<T>, named: View<ReturnType<T>["namedDefinition"] extends AttributeBlockDefinition ? ReturnType<T>["namedDefinition"] : {
219
237
  "~meta": void;
220
238
  }>) => T["as"] extends (() => infer U) ? U : void;
221
239
  //#endregion
package/dist/index.js CHANGED
@@ -64,6 +64,7 @@ var ViewModel = class ViewModel {
64
64
  return newVM;
65
65
  }
66
66
  bind(...args) {
67
+ if (args.length === 0) return this;
67
68
  const newModel = this.#clone();
68
69
  newModel.#Ctor = class extends this.#Ctor {
69
70
  constructor() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gi-tcg/gts-runtime",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/piovium/gts.git"
package/src/view_model.ts CHANGED
@@ -31,11 +31,21 @@ export interface IViewModel<
31
31
  ...args: CtorArgs
32
32
  ): ModelT;
33
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.
34
+ * Creates a new ViewModel with the same actions and binders, but with
35
+ * - a different constructor that accepts the specified arguments, and/or
36
+ * - a different initial Meta.
37
+ *
38
+ * This is useful for creating binding ViewModels directly that forwards
39
+ * the binding logic to inner ViewModels.
37
40
  */
38
- bind(...args: CtorArgs): ViewModel<ModelT, BlockDef, []>;
41
+ bind<NewMeta = BlockDef[Meta]>(): ViewModel<
42
+ ModelT,
43
+ BlockDefinitionRewriteMeta<BlockDef, NewMeta>,
44
+ CtorArgs
45
+ >;
46
+ bind<NewMeta = BlockDef[Meta]>(
47
+ ...args: CtorArgs
48
+ ): ViewModel<ModelT, BlockDefinitionRewriteMeta<BlockDef, NewMeta>, []>;
39
49
  "~namedDefinition": BlockDef;
40
50
  }
41
51
 
@@ -109,11 +119,22 @@ class ViewModel<
109
119
  return newVM;
110
120
  }
111
121
 
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
- ) {
122
+ bind<NewMeta = BlockDef[Meta]>(): ViewModel<
123
+ ModelT,
124
+ BlockDefinitionRewriteMeta<BlockDef, NewMeta>,
125
+ CtorArgs
126
+ >;
127
+ bind<NewMeta = BlockDef[Meta]>(
128
+ ...args: CtorArgs
129
+ ): ViewModel<ModelT, BlockDefinitionRewriteMeta<BlockDef, NewMeta>, []>;
130
+ bind<NewMeta = BlockDef[Meta]>(
131
+ ...args: any[]
132
+ ): ViewModel<ModelT, BlockDefinitionRewriteMeta<BlockDef, NewMeta>, any> {
133
+ if (args.length === 0) {
134
+ return this as any;
135
+ }
136
+ const newModel = this.#clone() as ViewModel<any, any, any>;
137
+ newModel.#Ctor = class extends (this.#Ctor as new (...args: any[]) => any) {
117
138
  constructor() {
118
139
  super(...args);
119
140
  }
@@ -303,7 +324,7 @@ export function defineSimpleViewModel<const T extends StandardJSONSchemaV1>(
303
324
  });
304
325
  }
305
326
  helper["~assignActions"](defResult);
306
- return vm;
327
+ return vm as any;
307
328
  }
308
329
 
309
330
  export interface AttributeDefinition {
@@ -318,9 +339,28 @@ interface AttributePositionalReturnBase {
318
339
  namedDefinition: AttributeBlockDefinition;
319
340
  }
320
341
 
342
+ type OverloadedParameters<T extends (...args: any[]) => any> = T extends {
343
+ (...args: infer A1): any;
344
+ (...args: infer A2): any;
345
+ (...args: infer A3): any;
346
+ (...args: infer A4): any;
347
+ }
348
+ ? A1 | A2 | A3 | A4
349
+ : T extends {
350
+ (...args: infer A1): any;
351
+ (...args: infer A2): any;
352
+ (...args: infer A3): any;
353
+ }
354
+ ? A1 | A2 | A3
355
+ : T extends { (...args: infer A1): any; (...args: infer A2): any }
356
+ ? A1 | A2
357
+ : T extends (...args: infer A) => any
358
+ ? A
359
+ : never;
360
+
321
361
  export type AttributeAction<Model, T extends AttributeDefinition> = (
322
362
  model: Model,
323
- positional: Parameters<T>,
363
+ positional: OverloadedParameters<T>,
324
364
  named: View<
325
365
  ReturnType<T>["namedDefinition"] extends AttributeBlockDefinition
326
366
  ? ReturnType<T>["namedDefinition"]
@@ -330,7 +370,7 @@ export type AttributeAction<Model, T extends AttributeDefinition> = (
330
370
 
331
371
  export type AttributeBinder<Model, T extends AttributeDefinition> = (
332
372
  model: Model,
333
- positional: Parameters<T>,
373
+ positional: OverloadedParameters<T>,
334
374
  named: View<
335
375
  ReturnType<T>["namedDefinition"] extends AttributeBlockDefinition
336
376
  ? ReturnType<T>["namedDefinition"]