@gi-tcg/gts-runtime 0.5.0 → 0.6.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.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,10 @@
1
1
  //#region src/attribute_return.d.ts
2
+ type Computed<T, Constraint = any> = T extends Constraint ? { [K in keyof T]: T[K] } : never;
3
+ interface AttributePositionalReturnBase {
4
+ key?: string;
5
+ rewriteMeta?: any;
6
+ namedDefinition: AttributeBlockDefinition;
7
+ }
2
8
  declare namespace AttributeReturn {
3
9
  type This<TMeta> = {
4
10
  "~meta": TMeta;
@@ -18,8 +24,8 @@ declare namespace AttributeReturn {
18
24
  };
19
25
  rewriteMeta: NewMeta;
20
26
  };
21
- type WithRewriteMeta<VM extends IViewModel<any, any, any>, NewMeta> = {
22
- namedDefinition: VM["~namedDefinition"];
27
+ type WithRewriteMeta<NewMeta, VM extends IViewModel<any, any, any>, TMeta = VM["~namedDefinition"]["~meta"]> = {
28
+ namedDefinition: BlockDefinitionRewriteMeta<VM["~namedDefinition"], TMeta>;
23
29
  rewriteMeta: NewMeta;
24
30
  };
25
31
  }
@@ -58,13 +64,11 @@ declare function createBinding(rootVM: IViewModel<any, any, any>, node: SingleAt
58
64
  //#endregion
59
65
  //#region src/view_model.d.ts
60
66
  interface AttributeBlockDefinition {
61
- "~action"?: AttributeDefinition | undefined;
62
67
  "~meta": any;
63
68
  }
64
- type Computed<T> = T extends infer U extends { [K in keyof T]: unknown } ? U : never;
65
69
  type BlockDefinitionRewriteMeta<BlockDef extends AttributeBlockDefinition, NewMeta> = Computed<Omit<BlockDef, Meta> & {
66
70
  "~meta": NewMeta;
67
- }> extends infer R extends AttributeBlockDefinition ? R : never;
71
+ }, AttributeBlockDefinition>;
68
72
  interface IViewModel<ModelT, BlockDef extends AttributeBlockDefinition, CtorArgs extends any[]> {
69
73
  parse(view: View<BlockDefinitionRewriteMeta<BlockDef, unknown>>, ...args: CtorArgs): ModelT;
70
74
  /**
@@ -75,8 +79,11 @@ interface IViewModel<ModelT, BlockDef extends AttributeBlockDefinition, CtorArgs
75
79
  * This is useful for creating binding ViewModels directly that forwards
76
80
  * the binding logic to inner ViewModels.
77
81
  */
78
- bind<NewMeta = BlockDef[Meta]>(): ViewModel<ModelT, BlockDefinitionRewriteMeta<BlockDef, NewMeta>, CtorArgs>;
79
- bind<NewMeta = BlockDef[Meta]>(...args: CtorArgs): ViewModel<ModelT, BlockDefinitionRewriteMeta<BlockDef, NewMeta>, []>;
82
+ bind<NewMeta = BlockDef[Meta]>(): IViewModel<ModelT, BlockDefinitionRewriteMeta<BlockDef, NewMeta>, CtorArgs>;
83
+ bind<NewMeta = BlockDef[Meta]>(...args: CtorArgs): IViewModel<ModelT, BlockDefinitionRewriteMeta<BlockDef, NewMeta>, []>;
84
+ extend<ChildT extends ModelT, const ChildBlockDef extends Partial<Record<string | Action, AttributeDefinition>>, ChildCtorArgs extends any[] = []>(Ctor: new (...args: ChildCtorArgs) => ChildT, modelDefFn: (helper: AttributeDefHelper<ChildT>) => ChildBlockDef): IViewModel<ChildT, Computed<Omit<BlockDef, keyof ChildBlockDef> & ChildBlockDef & {
85
+ "~meta": BlockDef[Meta];
86
+ }>, ChildCtorArgs>;
80
87
  "~namedDefinition": BlockDef;
81
88
  }
82
89
  type LazyAttributeActionOrBinder<ModelT> = (model: ModelT, positionals: () => unknown[], named: View<any>) => unknown;
@@ -86,8 +93,11 @@ declare class ViewModel<ModelT, BlockDef extends AttributeBlockDefinition, CtorA
86
93
  constructor(Ctor: new (...args: CtorArgs) => ModelT);
87
94
  "~setActionOrBinder"(context: "action" | "binder", name: PropertyKey, action: LazyAttributeActionOrBinder<ModelT>): void;
88
95
  parse(view: View<BlockDefinitionRewriteMeta<BlockDef, unknown>>, ...args: CtorArgs): ModelT;
89
- bind<NewMeta = BlockDef[Meta]>(): ViewModel<ModelT, BlockDefinitionRewriteMeta<BlockDef, NewMeta>, CtorArgs>;
90
- bind<NewMeta = BlockDef[Meta]>(...args: CtorArgs): ViewModel<ModelT, BlockDefinitionRewriteMeta<BlockDef, NewMeta>, []>;
96
+ bind<NewMeta = BlockDef[Meta]>(): IViewModel<ModelT, BlockDefinitionRewriteMeta<BlockDef, NewMeta>, CtorArgs>;
97
+ bind<NewMeta = BlockDef[Meta]>(...args: CtorArgs): IViewModel<ModelT, BlockDefinitionRewriteMeta<BlockDef, NewMeta>, []>;
98
+ extend<ChildT extends ModelT, const ChildBlockDef extends Partial<Record<string | Action, AttributeDefinition>>, ChildCtorArgs extends any[] = []>(Ctor: new (...args: ChildCtorArgs) => ChildT, modelDefFn: (helper: AttributeDefHelper<ChildT>) => ChildBlockDef): IViewModel<ChildT, Computed<Omit<BlockDef, keyof ChildBlockDef> & ChildBlockDef & {
99
+ "~meta": BlockDef[Meta];
100
+ }>, ChildCtorArgs>;
91
101
  }
92
102
  interface SimpleAttributeOptions {
93
103
  required?: true;
@@ -133,10 +143,7 @@ interface AttributeDefinition {
133
143
  as?(): any;
134
144
  required?(): boolean;
135
145
  uniqueKey?(): string;
136
- }
137
- interface AttributePositionalReturnBase {
138
- rewriteMeta?: any;
139
- namedDefinition: AttributeBlockDefinition;
146
+ mergeMeta?(meta: any, subMeta: any): any;
140
147
  }
141
148
  type OverloadedParameters<T extends (...args: any[]) => any> = T extends {
142
149
  (...args: infer A1): any;
@@ -255,12 +262,14 @@ type SimpleViewModelAttribute<ValueT, Options extends SimpleViewModelOptions> =
255
262
  } : Options["booleanSwitch"] extends true ? [true] extends [ValueT] ? {
256
263
  (): AttributeReturn.Done;
257
264
  } : {} : {} : {});
258
- type SimpleViewModel<T, Options extends SimpleViewModelOptions = {}> = IViewModel<T, { [K in keyof T]-?: SimpleViewModelAttribute<T[K], Options> & {
265
+ interface SimpleViewModel<T, Options extends SimpleViewModelOptions = {}> extends IViewModel<T, { [K in keyof T]-?: SimpleViewModelAttribute<T[K], Options> & {
259
266
  uniqueKey(): K;
260
267
  required(): {} extends Pick<T, K> ? false : true;
261
268
  } } & {
262
269
  "~meta": undefined;
263
- }, []>;
270
+ }, []> {
271
+ Model: new () => T;
272
+ }
264
273
  declare function defineSimpleViewModel<const T extends StandardJSONSchemaV1, const Options extends SimpleViewModelOptions = {}>(schema: T, options?: Options): SimpleViewModel<StandardJSONSchemaV1.InferInput<T>, Options>;
265
274
  //#endregion
266
- export { type AttributeReturn as AR, type AttributeReturn, type Action, type AttributeDefinition, type IViewModel, type Meta, type NamedAttributesNode, type NamedDefinition, type SimpleViewModel, type SimpleViewModelOptions, type SingleAttributeNode, type View, createBinding, createDefine, defineSimpleViewModel, defineViewModel };
275
+ export { type AttributeReturn as AR, type AttributeReturn, type Action, type AttributeBlockDefinition, type AttributeDefinition, type IViewModel, type Meta, type NamedAttributesNode, type NamedDefinition, type SimpleAttributeOptions, type SimpleViewModel, type SimpleViewModelOptions, type SingleAttributeNode, type View, createBinding, createDefine, defineSimpleViewModel, defineViewModel };
package/dist/index.js CHANGED
@@ -74,6 +74,15 @@ var ViewModel = class ViewModel {
74
74
  };
75
75
  return newModel;
76
76
  }
77
+ extend(Ctor, modelDefFn) {
78
+ const newVM = new ViewModel(Ctor);
79
+ for (const [name, action] of this.#registeredActions) newVM["~setActionOrBinder"]("action", name, action);
80
+ for (const [name, binder] of this.#registeredBinders) newVM["~setActionOrBinder"]("binder", name, binder);
81
+ const helper = new AttributeDefHelper(newVM);
82
+ const defResult = modelDefFn(helper);
83
+ helper["~assignActions"](defResult);
84
+ return newVM;
85
+ }
77
86
  };
78
87
  var AttributeDefHelper = class AttributeDefHelper {
79
88
  #viewModel;
@@ -150,7 +159,8 @@ function extractObjectSchema(propSchema) {
150
159
  return null;
151
160
  }
152
161
  function createSimpleViewModelFromJsonSchema(jsonSchema, options) {
153
- const vm = new ViewModel(class SimpleViewModel {});
162
+ const Ctor = class SimpleViewModel {};
163
+ const vm = new ViewModel(Ctor);
154
164
  const helper = new AttributeDefHelper(vm);
155
165
  const defResult = {};
156
166
  for (const key of Object.keys(jsonSchema.properties ?? {})) {
@@ -159,6 +169,12 @@ function createSimpleViewModelFromJsonSchema(jsonSchema, options) {
159
169
  defResult[key] = registerAttribute(helper, key, propSchema, options);
160
170
  }
161
171
  helper["~assignActions"](defResult);
172
+ Object.defineProperty(vm, "Model", {
173
+ value: Ctor,
174
+ writable: false,
175
+ enumerable: true,
176
+ configurable: true
177
+ });
162
178
  return vm;
163
179
  }
164
180
  function registerAttribute(helper, key, propSchema, options) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gi-tcg/gts-runtime",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/piovium/gts.git"
@@ -1,4 +1,18 @@
1
- import type { BlockDefinitionRewriteMeta, IViewModel } from "./view_model.ts";
1
+ import type {
2
+ AttributeBlockDefinition,
3
+ BlockDefinitionRewriteMeta,
4
+ IViewModel,
5
+ } from "./view_model.ts";
6
+
7
+ export type Computed<T, Constraint = any> = T extends Constraint
8
+ ? { [K in keyof T]: T[K] }
9
+ : never;
10
+
11
+ export interface AttributePositionalReturnBase {
12
+ key?: string;
13
+ rewriteMeta?: any;
14
+ namedDefinition: AttributeBlockDefinition;
15
+ }
2
16
 
3
17
  export namespace AttributeReturn {
4
18
  export type This<TMeta> = {
@@ -25,8 +39,12 @@ export namespace AttributeReturn {
25
39
  rewriteMeta: NewMeta;
26
40
  };
27
41
 
28
- export type WithRewriteMeta<VM extends IViewModel<any, any, any>, NewMeta> = {
29
- namedDefinition: VM["~namedDefinition"];
42
+ export type WithRewriteMeta<
43
+ NewMeta,
44
+ VM extends IViewModel<any, any, any>,
45
+ TMeta = VM["~namedDefinition"]["~meta"],
46
+ > = {
47
+ namedDefinition: BlockDefinitionRewriteMeta<VM["~namedDefinition"], TMeta>;
30
48
  rewriteMeta: NewMeta;
31
49
  };
32
50
  }
package/src/index.ts CHANGED
@@ -2,6 +2,8 @@ export {
2
2
  defineViewModel,
3
3
  type AttributeDefinition,
4
4
  type IViewModel,
5
+ type SimpleAttributeOptions,
6
+ type AttributeBlockDefinition,
5
7
  } from "./view_model.ts";
6
8
  export {
7
9
  defineSimpleViewModel,
@@ -44,10 +44,10 @@ type SimpleViewModelAttribute<
44
44
  : {}
45
45
  : {});
46
46
 
47
- export type SimpleViewModel<
47
+ export interface SimpleViewModel<
48
48
  T,
49
49
  Options extends SimpleViewModelOptions = {},
50
- > = IViewModel<
50
+ > extends IViewModel<
51
51
  T,
52
52
  {
53
53
  [K in keyof T]-?: SimpleViewModelAttribute<T[K], Options> & {
@@ -58,7 +58,9 @@ export type SimpleViewModel<
58
58
  "~meta": undefined;
59
59
  },
60
60
  []
61
- >;
61
+ > {
62
+ Model: new () => T;
63
+ }
62
64
 
63
65
  const ajv = new Ajv2020({ strict: false });
64
66
 
@@ -91,7 +93,7 @@ function extractObjectSchema(
91
93
  function createSimpleViewModelFromJsonSchema(
92
94
  jsonSchema: Record<string, unknown>,
93
95
  options: SimpleViewModelOptions,
94
- ): IViewModel<any, any, []> {
96
+ ): SimpleViewModel<any, any> {
95
97
  const Ctor = class SimpleViewModel {};
96
98
  const vm = new ViewModel<any, any, []>(Ctor);
97
99
  const helper = new AttributeDefHelper(vm);
@@ -109,6 +111,12 @@ function createSimpleViewModelFromJsonSchema(
109
111
  );
110
112
  }
111
113
  helper["~assignActions"](defResult);
114
+ Object.defineProperty(vm, "Model", {
115
+ value: Ctor,
116
+ writable: false,
117
+ enumerable: true,
118
+ configurable: true,
119
+ });
112
120
  return vm as any;
113
121
  }
114
122
 
@@ -120,7 +128,10 @@ function registerAttribute(
120
128
  ): unknown {
121
129
  let objectSchema: Record<string, unknown> | null = null;
122
130
  if (options.recursive && (objectSchema = extractObjectSchema(propSchema))) {
123
- const subVM = createSimpleViewModelFromJsonSchema(objectSchema, options);
131
+ const subVM: IViewModel<any, any, []> = createSimpleViewModelFromJsonSchema(
132
+ objectSchema,
133
+ options,
134
+ );
124
135
  return helper.attribute((model, positionals, named) => {
125
136
  if (positionals.length > 0) {
126
137
  model[key] = positionals[0];
@@ -156,5 +167,8 @@ export function defineSimpleViewModel<
156
167
  const jsonSchema = schema["~standard"].jsonSchema.input({
157
168
  target: "draft-2020-12",
158
169
  });
159
- return createSimpleViewModelFromJsonSchema(jsonSchema, resolvedOptions);
170
+ return createSimpleViewModelFromJsonSchema(
171
+ jsonSchema,
172
+ resolvedOptions,
173
+ ) as SimpleViewModel<StandardJSONSchemaV1.InferInput<T>, Options>;
160
174
  }
package/src/view_model.ts CHANGED
@@ -1,24 +1,22 @@
1
- import type { AttributeReturn } from "./attribute_return.ts";
2
- import type { Action, Meta, NamedDefinition } from "./symbols.ts";
1
+ import type {
2
+ AttributePositionalReturnBase,
3
+ AttributeReturn,
4
+ Computed,
5
+ } from "./attribute_return.ts";
6
+ import type { Action, Meta } from "./symbols.ts";
3
7
  import { View } from "./view.ts";
4
8
 
5
9
  export interface AttributeBlockDefinition {
6
- "~action"?: AttributeDefinition | undefined;
7
10
  "~meta": any;
8
11
  }
9
12
 
10
- type Computed<T> = T extends infer U extends { [K in keyof T]: unknown }
11
- ? U
12
- : never;
13
-
14
13
  export type BlockDefinitionRewriteMeta<
15
14
  BlockDef extends AttributeBlockDefinition,
16
15
  NewMeta,
17
- > =
18
- Computed<Omit<BlockDef, Meta> & { "~meta": NewMeta }> extends infer R extends
19
- AttributeBlockDefinition
20
- ? R
21
- : never;
16
+ > = Computed<
17
+ Omit<BlockDef, Meta> & { "~meta": NewMeta },
18
+ AttributeBlockDefinition
19
+ >;
22
20
 
23
21
  export interface IViewModel<
24
22
  ModelT,
@@ -37,14 +35,31 @@ export interface IViewModel<
37
35
  * This is useful for creating binding ViewModels directly that forwards
38
36
  * the binding logic to inner ViewModels.
39
37
  */
40
- bind<NewMeta = BlockDef[Meta]>(): ViewModel<
38
+ bind<NewMeta = BlockDef[Meta]>(): IViewModel<
41
39
  ModelT,
42
40
  BlockDefinitionRewriteMeta<BlockDef, NewMeta>,
43
41
  CtorArgs
44
42
  >;
45
43
  bind<NewMeta = BlockDef[Meta]>(
46
44
  ...args: CtorArgs
47
- ): ViewModel<ModelT, BlockDefinitionRewriteMeta<BlockDef, NewMeta>, []>;
45
+ ): IViewModel<ModelT, BlockDefinitionRewriteMeta<BlockDef, NewMeta>, []>;
46
+ extend<
47
+ ChildT extends ModelT,
48
+ const ChildBlockDef extends Partial<
49
+ Record<string | Action, AttributeDefinition>
50
+ >,
51
+ ChildCtorArgs extends any[] = [],
52
+ >(
53
+ Ctor: new (...args: ChildCtorArgs) => ChildT,
54
+ modelDefFn: (helper: AttributeDefHelper<ChildT>) => ChildBlockDef,
55
+ ): IViewModel<
56
+ ChildT,
57
+ Computed<
58
+ Omit<BlockDef, keyof ChildBlockDef> &
59
+ ChildBlockDef & { "~meta": BlockDef[Meta] }
60
+ >,
61
+ ChildCtorArgs
62
+ >;
48
63
  "~namedDefinition": BlockDef;
49
64
  }
50
65
 
@@ -118,17 +133,17 @@ export class ViewModel<
118
133
  return newVM;
119
134
  }
120
135
 
121
- bind<NewMeta = BlockDef[Meta]>(): ViewModel<
136
+ bind<NewMeta = BlockDef[Meta]>(): IViewModel<
122
137
  ModelT,
123
138
  BlockDefinitionRewriteMeta<BlockDef, NewMeta>,
124
139
  CtorArgs
125
140
  >;
126
141
  bind<NewMeta = BlockDef[Meta]>(
127
142
  ...args: CtorArgs
128
- ): ViewModel<ModelT, BlockDefinitionRewriteMeta<BlockDef, NewMeta>, []>;
143
+ ): IViewModel<ModelT, BlockDefinitionRewriteMeta<BlockDef, NewMeta>, []>;
129
144
  bind<NewMeta = BlockDef[Meta]>(
130
145
  ...args: any[]
131
- ): ViewModel<ModelT, BlockDefinitionRewriteMeta<BlockDef, NewMeta>, any> {
146
+ ): IViewModel<ModelT, BlockDefinitionRewriteMeta<BlockDef, NewMeta>, any> {
132
147
  if (args.length === 0) {
133
148
  return this as any;
134
149
  }
@@ -138,11 +153,41 @@ export class ViewModel<
138
153
  super(...args);
139
154
  }
140
155
  };
141
- return newModel;
156
+ return newModel as IViewModel<any, any, any>;
157
+ }
158
+
159
+ extend<
160
+ ChildT extends ModelT,
161
+ const ChildBlockDef extends Partial<
162
+ Record<string | Action, AttributeDefinition>
163
+ >,
164
+ ChildCtorArgs extends any[] = [],
165
+ >(
166
+ Ctor: new (...args: ChildCtorArgs) => ChildT,
167
+ modelDefFn: (helper: AttributeDefHelper<ChildT>) => ChildBlockDef,
168
+ ): IViewModel<
169
+ ChildT,
170
+ Computed<
171
+ Omit<BlockDef, keyof ChildBlockDef> &
172
+ ChildBlockDef & { "~meta": BlockDef[Meta] }
173
+ >,
174
+ ChildCtorArgs
175
+ > {
176
+ const newVM = new ViewModel<ChildT, any, ChildCtorArgs>(Ctor);
177
+ for (const [name, action] of this.#registeredActions) {
178
+ newVM["~setActionOrBinder"]("action", name, action as any);
179
+ }
180
+ for (const [name, binder] of this.#registeredBinders) {
181
+ newVM["~setActionOrBinder"]("binder", name, binder as any);
182
+ }
183
+ const helper = new AttributeDefHelper(newVM);
184
+ const defResult = modelDefFn(helper);
185
+ helper["~assignActions"](defResult);
186
+ return newVM as any;
142
187
  }
143
188
  }
144
189
 
145
- interface SimpleAttributeOptions {
190
+ export interface SimpleAttributeOptions {
146
191
  required?: true;
147
192
  uniqueKey?: string;
148
193
  }
@@ -300,11 +345,7 @@ export interface AttributeDefinition {
300
345
  as?(): any;
301
346
  required?(): boolean;
302
347
  uniqueKey?(): string;
303
- }
304
-
305
- interface AttributePositionalReturnBase {
306
- rewriteMeta?: any;
307
- namedDefinition: AttributeBlockDefinition;
348
+ mergeMeta?(meta: any, subMeta: any): any;
308
349
  }
309
350
 
310
351
  type OverloadedParameters<T extends (...args: any[]) => any> = T extends {