@gi-tcg/gts-runtime 0.3.0 → 0.3.1
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 +14 -11
- package/dist/index.js +2 -2
- package/package.json +1 -1
- package/src/view_model.ts +29 -16
package/dist/index.d.ts
CHANGED
|
@@ -74,29 +74,31 @@ type BlockDefinitionRewriteMeta<
|
|
|
74
74
|
}> extends infer R extends AttributeBlockDefinition ? R : never;
|
|
75
75
|
interface IViewModel<
|
|
76
76
|
ModelT,
|
|
77
|
-
BlockDef extends AttributeBlockDefinition
|
|
77
|
+
BlockDef extends AttributeBlockDefinition,
|
|
78
|
+
CtorArgs extends any[]
|
|
78
79
|
> {
|
|
79
|
-
parse(view: View<BlockDefinitionRewriteMeta<BlockDef, unknown
|
|
80
|
+
parse(view: View<BlockDefinitionRewriteMeta<BlockDef, unknown>>, ...args: CtorArgs): ModelT;
|
|
80
81
|
"~namedDefinition": BlockDef;
|
|
81
82
|
}
|
|
82
83
|
type LazyAttributeActionOrBinder<ModelT> = (model: ModelT, positionals: () => unknown[], named: View<any>) => unknown;
|
|
83
84
|
declare class ViewModel<
|
|
84
85
|
ModelT,
|
|
85
|
-
BlockDef extends AttributeBlockDefinition
|
|
86
|
-
|
|
86
|
+
BlockDef extends AttributeBlockDefinition,
|
|
87
|
+
CtorArgs extends any[]
|
|
88
|
+
> implements IViewModel<ModelT, BlockDef, CtorArgs> {
|
|
87
89
|
private;
|
|
88
90
|
"~namedDefinition": BlockDef;
|
|
89
|
-
constructor(Ctor: new () => ModelT);
|
|
91
|
+
constructor(Ctor: new (...args: CtorArgs) => ModelT);
|
|
90
92
|
"~setActionOrBinder"(context: "action" | "binder", name: PropertyKey, action: LazyAttributeActionOrBinder<ModelT>): void;
|
|
91
|
-
parse(view: View<BlockDefinitionRewriteMeta<BlockDef, unknown
|
|
93
|
+
parse(view: View<BlockDefinitionRewriteMeta<BlockDef, unknown>>, ...args: CtorArgs): ModelT;
|
|
92
94
|
}
|
|
93
95
|
declare class AttributeDefHelper<ModelT> {
|
|
94
96
|
private;
|
|
95
|
-
constructor(viewModel: ViewModel<ModelT, any>);
|
|
97
|
+
constructor(viewModel: ViewModel<ModelT, any, any>);
|
|
96
98
|
"~assignActions"(defResult: Partial<Record<string, unknown>>): void;
|
|
97
99
|
attribute<T extends AttributeDefinition & {
|
|
98
100
|
as?: undefined;
|
|
99
|
-
}>(action: AttributeAction<ModelT, T>, binder?: AttributeBinder<ModelT, T> | IViewModel<any, ReturnType<T>["namedDefinition"]>): T;
|
|
101
|
+
}>(action: AttributeAction<ModelT, T>, binder?: AttributeBinder<ModelT, T> | IViewModel<any, ReturnType<T>["namedDefinition"], []>): T;
|
|
100
102
|
attribute<T extends AttributeDefinition>(action: AttributeAction<ModelT, T>, binder: AttributeBinder<ModelT, T>): T;
|
|
101
103
|
simpleAttribute<Args extends any[]>(action: (this: ModelT, ...args: Args) => void): {
|
|
102
104
|
(...args: Args): AttributeReturn.Done;
|
|
@@ -112,17 +114,18 @@ declare class AttributeDefHelper<ModelT> {
|
|
|
112
114
|
declare function defineViewModel<
|
|
113
115
|
T,
|
|
114
116
|
const BlockDef extends Partial<Record<string | Action, AttributeDefinition>>,
|
|
117
|
+
CtorArgs extends any[] = [],
|
|
115
118
|
InitMeta = void
|
|
116
|
-
>(Ctor: new () => T, modelDefFn: (helper: AttributeDefHelper<T>) => BlockDef, initMeta?: InitMeta): IViewModel<T, BlockDef & {
|
|
119
|
+
>(Ctor: new (...args: CtorArgs) => T, modelDefFn: (helper: AttributeDefHelper<T>) => BlockDef, initMeta?: InitMeta): IViewModel<T, BlockDef & {
|
|
117
120
|
"~meta": InitMeta;
|
|
118
|
-
}>;
|
|
121
|
+
}, CtorArgs>;
|
|
119
122
|
type SimpleViewModel<T> = IViewModel<T, { [K in keyof T]-? : {
|
|
120
123
|
(value: T[K]): AttributeReturn.Done;
|
|
121
124
|
uniqueKey(): K;
|
|
122
125
|
required(): {} extends Pick<T, K> ? false : true;
|
|
123
126
|
} } & {
|
|
124
127
|
"~meta": undefined;
|
|
125
|
-
}>;
|
|
128
|
+
}, []>;
|
|
126
129
|
declare function defineSimpleViewModel<const T extends StandardJSONSchemaV1>(schema: T): SimpleViewModel<StandardJSONSchemaV1.InferInput<T>>;
|
|
127
130
|
interface AttributeDefinition {
|
|
128
131
|
(...args: any[]): AttributePositionalReturnBase;
|
package/dist/index.js
CHANGED
|
@@ -44,8 +44,8 @@ class ViewModel {
|
|
|
44
44
|
this.#registeredBinders.set(name, action);
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
|
-
parse(view) {
|
|
48
|
-
const model = new this.#Ctor;
|
|
47
|
+
parse(view, ...args) {
|
|
48
|
+
const model = new this.#Ctor(...args);
|
|
49
49
|
for (const attrNode of view._node.attributes) {
|
|
50
50
|
let { name, positionals, named, binding } = attrNode;
|
|
51
51
|
const insideBindingCtx = !!view._bindingCtx;
|
package/package.json
CHANGED
package/src/view_model.ts
CHANGED
|
@@ -21,8 +21,15 @@ export type BlockDefinitionRewriteMeta<
|
|
|
21
21
|
? R
|
|
22
22
|
: never;
|
|
23
23
|
|
|
24
|
-
export interface IViewModel<
|
|
25
|
-
|
|
24
|
+
export interface IViewModel<
|
|
25
|
+
ModelT,
|
|
26
|
+
BlockDef extends AttributeBlockDefinition,
|
|
27
|
+
CtorArgs extends any[],
|
|
28
|
+
> {
|
|
29
|
+
parse(
|
|
30
|
+
view: View<BlockDefinitionRewriteMeta<BlockDef, unknown>>,
|
|
31
|
+
...args: CtorArgs
|
|
32
|
+
): ModelT;
|
|
26
33
|
"~namedDefinition": BlockDef;
|
|
27
34
|
}
|
|
28
35
|
|
|
@@ -35,7 +42,8 @@ type LazyAttributeActionOrBinder<ModelT> = (
|
|
|
35
42
|
class ViewModel<
|
|
36
43
|
ModelT,
|
|
37
44
|
BlockDef extends AttributeBlockDefinition,
|
|
38
|
-
|
|
45
|
+
CtorArgs extends any[],
|
|
46
|
+
> implements IViewModel<ModelT, BlockDef, CtorArgs> {
|
|
39
47
|
declare "~namedDefinition": BlockDef;
|
|
40
48
|
|
|
41
49
|
#registeredActions: Map<PropertyKey, LazyAttributeActionOrBinder<ModelT>> =
|
|
@@ -43,9 +51,9 @@ class ViewModel<
|
|
|
43
51
|
#registeredBinders: Map<PropertyKey, LazyAttributeActionOrBinder<ModelT>> =
|
|
44
52
|
new Map();
|
|
45
53
|
|
|
46
|
-
#Ctor: new () => ModelT;
|
|
54
|
+
#Ctor: new (...args: CtorArgs) => ModelT;
|
|
47
55
|
|
|
48
|
-
constructor(Ctor: new () => ModelT) {
|
|
56
|
+
constructor(Ctor: new (...args: CtorArgs) => ModelT) {
|
|
49
57
|
this.#Ctor = Ctor;
|
|
50
58
|
}
|
|
51
59
|
|
|
@@ -61,8 +69,11 @@ class ViewModel<
|
|
|
61
69
|
}
|
|
62
70
|
}
|
|
63
71
|
|
|
64
|
-
parse(
|
|
65
|
-
|
|
72
|
+
parse(
|
|
73
|
+
view: View<BlockDefinitionRewriteMeta<BlockDef, unknown>>,
|
|
74
|
+
...args: CtorArgs
|
|
75
|
+
): ModelT {
|
|
76
|
+
const model = new this.#Ctor(...args);
|
|
66
77
|
for (const attrNode of view._node.attributes) {
|
|
67
78
|
let { name, positionals, named, binding } = attrNode;
|
|
68
79
|
const insideBindingCtx = !!view._bindingCtx;
|
|
@@ -87,8 +98,8 @@ class ViewModel<
|
|
|
87
98
|
}
|
|
88
99
|
|
|
89
100
|
class AttributeDefHelper<ModelT> {
|
|
90
|
-
#viewModel: ViewModel<ModelT, any>;
|
|
91
|
-
constructor(viewModel: ViewModel<ModelT, any>) {
|
|
101
|
+
#viewModel: ViewModel<ModelT, any, any>;
|
|
102
|
+
constructor(viewModel: ViewModel<ModelT, any, any>) {
|
|
92
103
|
this.#viewModel = viewModel;
|
|
93
104
|
}
|
|
94
105
|
|
|
@@ -126,7 +137,7 @@ class AttributeDefHelper<ModelT> {
|
|
|
126
137
|
action: AttributeAction<ModelT, T>,
|
|
127
138
|
binder?:
|
|
128
139
|
| AttributeBinder<ModelT, T>
|
|
129
|
-
| IViewModel<any, ReturnType<T>["namedDefinition"]>,
|
|
140
|
+
| IViewModel<any, ReturnType<T>["namedDefinition"], []>,
|
|
130
141
|
): T;
|
|
131
142
|
attribute<T extends AttributeDefinition>(
|
|
132
143
|
action: AttributeAction<ModelT, T>,
|
|
@@ -134,7 +145,7 @@ class AttributeDefHelper<ModelT> {
|
|
|
134
145
|
): T;
|
|
135
146
|
attribute(
|
|
136
147
|
action: any,
|
|
137
|
-
binder?: AttributeBinder<any, any> | IViewModel<any, any>,
|
|
148
|
+
binder?: AttributeBinder<any, any> | IViewModel<any, any, any>,
|
|
138
149
|
) {
|
|
139
150
|
const returnValue = {};
|
|
140
151
|
const lazyAction: LazyAttributeActionOrBinder<ModelT> = (
|
|
@@ -195,13 +206,14 @@ class AttributeDefHelper<ModelT> {
|
|
|
195
206
|
export function defineViewModel<
|
|
196
207
|
T,
|
|
197
208
|
const BlockDef extends Partial<Record<string | Action, AttributeDefinition>>,
|
|
209
|
+
CtorArgs extends any[] = [],
|
|
198
210
|
InitMeta = void,
|
|
199
211
|
>(
|
|
200
|
-
Ctor: new () => T,
|
|
212
|
+
Ctor: new (...args: CtorArgs) => T,
|
|
201
213
|
modelDefFn: (helper: AttributeDefHelper<T>) => BlockDef,
|
|
202
214
|
initMeta?: InitMeta,
|
|
203
|
-
): IViewModel<T, BlockDef & { "~meta": InitMeta }> {
|
|
204
|
-
const vm = new ViewModel<T, BlockDef & { "~meta": InitMeta }>(Ctor);
|
|
215
|
+
): IViewModel<T, BlockDef & { "~meta": InitMeta }, CtorArgs> {
|
|
216
|
+
const vm = new ViewModel<T, BlockDef & { "~meta": InitMeta }, CtorArgs>(Ctor);
|
|
205
217
|
const helper = new AttributeDefHelper(vm);
|
|
206
218
|
const defResult = modelDefFn(helper);
|
|
207
219
|
helper["~assignActions"](defResult);
|
|
@@ -216,7 +228,8 @@ export type SimpleViewModel<T> = IViewModel<
|
|
|
216
228
|
uniqueKey(): K;
|
|
217
229
|
required(): {} extends Pick<T, K> ? false : true;
|
|
218
230
|
};
|
|
219
|
-
} & { "~meta": undefined }
|
|
231
|
+
} & { "~meta": undefined },
|
|
232
|
+
[]
|
|
220
233
|
>;
|
|
221
234
|
|
|
222
235
|
export function defineSimpleViewModel<const T extends StandardJSONSchemaV1>(
|
|
@@ -226,7 +239,7 @@ export function defineSimpleViewModel<const T extends StandardJSONSchemaV1>(
|
|
|
226
239
|
target: "draft-2020-12",
|
|
227
240
|
});
|
|
228
241
|
const Ctor = class SimpleViewModel {};
|
|
229
|
-
const vm = new ViewModel<any, any>(Ctor);
|
|
242
|
+
const vm = new ViewModel<any, any, []>(Ctor);
|
|
230
243
|
const helper = new AttributeDefHelper(vm);
|
|
231
244
|
const defResult: Record<string, any> = {};
|
|
232
245
|
for (const key of Object.keys(jsonSchema.properties ?? {})) {
|