@gi-tcg/gts-runtime 0.3.0 → 0.3.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/LICENSE +201 -0
- package/dist/index.d.ts +165 -119
- package/dist/index.js +115 -141
- package/package.json +6 -3
- package/src/attribute_return.ts +3 -3
- package/src/index.ts +4 -4
- package/src/view.ts +11 -6
- package/src/view_model.ts +33 -20
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,149 +1,195 @@
|
|
|
1
|
+
//#region src/attribute_return.d.ts
|
|
1
2
|
declare namespace AttributeReturn {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
rewriteMeta: NewMeta;
|
|
25
|
-
};
|
|
26
|
-
type WithRewriteMeta<
|
|
27
|
-
VM extends IViewModel<any, any>,
|
|
28
|
-
NewMeta
|
|
29
|
-
> = {
|
|
30
|
-
namedDefinition: VM["~namedDefinition"];
|
|
31
|
-
rewriteMeta: NewMeta;
|
|
32
|
-
};
|
|
3
|
+
type This<TMeta> = {
|
|
4
|
+
"~meta": TMeta;
|
|
5
|
+
};
|
|
6
|
+
type EnableIf<Condition, T = void> = Condition extends true ? T : never;
|
|
7
|
+
type Done = {
|
|
8
|
+
namedDefinition: {
|
|
9
|
+
"~meta": void;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
type With<VM extends IViewModel<any, any, any>, TMeta = VM["~namedDefinition"]["~meta"]> = {
|
|
13
|
+
namedDefinition: BlockDefinitionRewriteMeta<VM["~namedDefinition"], TMeta>;
|
|
14
|
+
};
|
|
15
|
+
type DoneRewriteMeta<NewMeta> = {
|
|
16
|
+
namedDefinition: {
|
|
17
|
+
"~meta": void;
|
|
18
|
+
};
|
|
19
|
+
rewriteMeta: NewMeta;
|
|
20
|
+
};
|
|
21
|
+
type WithRewriteMeta<VM extends IViewModel<any, any, any>, NewMeta> = {
|
|
22
|
+
namedDefinition: VM["~namedDefinition"];
|
|
23
|
+
rewriteMeta: NewMeta;
|
|
24
|
+
};
|
|
33
25
|
}
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/symbols.d.ts
|
|
34
28
|
type Meta = "~meta";
|
|
35
29
|
type Action = "~action";
|
|
36
30
|
type NamedDefinition = "~namedDefinition";
|
|
37
31
|
type Prelude = "~prelude";
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/view.d.ts
|
|
38
34
|
type AttributeName = string | symbol;
|
|
39
35
|
interface SingleAttributeNode {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
36
|
+
name: AttributeName;
|
|
37
|
+
/** Lazy evaluation: only evaluate positional expressions when traversing */
|
|
38
|
+
positionals: () => any[];
|
|
39
|
+
named: NamedAttributesNode | null;
|
|
40
|
+
/** Marks this attribute as a binding export candidate */
|
|
41
|
+
binding?: "public" | "private";
|
|
46
42
|
}
|
|
47
43
|
interface NamedAttributesNode {
|
|
48
|
-
|
|
44
|
+
attributes: SingleAttributeNode[];
|
|
49
45
|
}
|
|
50
46
|
declare class View<BlockDef extends AttributeBlockDefinition> {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
47
|
+
#private;
|
|
48
|
+
_node: NamedAttributesNode;
|
|
49
|
+
_bindingCtx?: BindingContext;
|
|
50
|
+
constructor(_node: NamedAttributesNode, _bindingCtx?: BindingContext | undefined);
|
|
55
51
|
}
|
|
56
52
|
declare class BindingContext {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
53
|
+
#private;
|
|
54
|
+
addBinding(value: unknown): void;
|
|
55
|
+
getBindings(): unknown[];
|
|
60
56
|
}
|
|
61
|
-
declare function createDefine(rootVM: IViewModel<any, any>, node: SingleAttributeNode): void;
|
|
62
|
-
declare function createBinding(rootVM: IViewModel<any, any>, node: SingleAttributeNode): unknown[];
|
|
63
|
-
|
|
57
|
+
declare function createDefine(rootVM: IViewModel<any, any, any>, node: SingleAttributeNode): void;
|
|
58
|
+
declare function createBinding(rootVM: IViewModel<any, any, any>, node: SingleAttributeNode): unknown[];
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region ../../node_modules/@standard-schema/spec/dist/index.d.ts
|
|
61
|
+
/** The Standard Typed interface. This is a base type extended by other specs. */
|
|
62
|
+
interface StandardTypedV1<Input = unknown, Output = Input> {
|
|
63
|
+
/** The Standard properties. */
|
|
64
|
+
readonly "~standard": StandardTypedV1.Props<Input, Output>;
|
|
65
|
+
}
|
|
66
|
+
declare namespace StandardTypedV1 {
|
|
67
|
+
/** The Standard Typed properties interface. */
|
|
68
|
+
interface Props<Input = unknown, Output = Input> {
|
|
69
|
+
/** The version number of the standard. */
|
|
70
|
+
readonly version: 1;
|
|
71
|
+
/** The vendor name of the schema library. */
|
|
72
|
+
readonly vendor: string;
|
|
73
|
+
/** Inferred types associated with the schema. */
|
|
74
|
+
readonly types?: Types<Input, Output> | undefined;
|
|
75
|
+
}
|
|
76
|
+
/** The Standard Typed types interface. */
|
|
77
|
+
interface Types<Input = unknown, Output = Input> {
|
|
78
|
+
/** The input type of the schema. */
|
|
79
|
+
readonly input: Input;
|
|
80
|
+
/** The output type of the schema. */
|
|
81
|
+
readonly output: Output;
|
|
82
|
+
}
|
|
83
|
+
/** Infers the input type of a Standard Typed. */
|
|
84
|
+
type InferInput<Schema extends StandardTypedV1> = NonNullable<Schema["~standard"]["types"]>["input"];
|
|
85
|
+
/** Infers the output type of a Standard Typed. */
|
|
86
|
+
type InferOutput<Schema extends StandardTypedV1> = NonNullable<Schema["~standard"]["types"]>["output"];
|
|
87
|
+
}
|
|
88
|
+
/** The Standard Schema interface. */
|
|
89
|
+
/** The Standard JSON Schema interface. */
|
|
90
|
+
interface StandardJSONSchemaV1<Input = unknown, Output = Input> {
|
|
91
|
+
/** The Standard JSON Schema properties. */
|
|
92
|
+
readonly "~standard": StandardJSONSchemaV1.Props<Input, Output>;
|
|
93
|
+
}
|
|
94
|
+
declare namespace StandardJSONSchemaV1 {
|
|
95
|
+
/** The Standard JSON Schema properties interface. */
|
|
96
|
+
interface Props<Input = unknown, Output = Input> extends StandardTypedV1.Props<Input, Output> {
|
|
97
|
+
/** Methods for generating the input/output JSON Schema. */
|
|
98
|
+
readonly jsonSchema: StandardJSONSchemaV1.Converter;
|
|
99
|
+
}
|
|
100
|
+
/** The Standard JSON Schema converter interface. */
|
|
101
|
+
interface Converter {
|
|
102
|
+
/** Converts the input type to JSON Schema. May throw if conversion is not supported. */
|
|
103
|
+
readonly input: (options: StandardJSONSchemaV1.Options) => Record<string, unknown>;
|
|
104
|
+
/** Converts the output type to JSON Schema. May throw if conversion is not supported. */
|
|
105
|
+
readonly output: (options: StandardJSONSchemaV1.Options) => Record<string, unknown>;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* The target version of the generated JSON Schema.
|
|
109
|
+
*
|
|
110
|
+
* It is *strongly recommended* that implementers support `"draft-2020-12"` and `"draft-07"`, as they are both in wide use. All other targets can be implemented on a best-effort basis. Libraries should throw if they don't support a specified target.
|
|
111
|
+
*
|
|
112
|
+
* The `"openapi-3.0"` target is intended as a standardized specifier for OpenAPI 3.0 which is a superset of JSON Schema `"draft-04"`.
|
|
113
|
+
*/
|
|
114
|
+
type Target = "draft-2020-12" | "draft-07" | "openapi-3.0" | ({} & string);
|
|
115
|
+
/** The options for the input/output methods. */
|
|
116
|
+
interface Options {
|
|
117
|
+
/** Specifies the target version of the generated JSON Schema. Support for all versions is on a best-effort basis. If a given version is not supported, the library should throw. */
|
|
118
|
+
readonly target: Target;
|
|
119
|
+
/** Explicit support for additional vendor-specific parameters, if needed. */
|
|
120
|
+
readonly libraryOptions?: Record<string, unknown> | undefined;
|
|
121
|
+
}
|
|
122
|
+
/** The Standard types interface. */
|
|
123
|
+
interface Types<Input = unknown, Output = Input> extends StandardTypedV1.Types<Input, Output> {}
|
|
124
|
+
/** Infers the input type of a Standard. */
|
|
125
|
+
type InferInput<Schema extends StandardTypedV1> = StandardTypedV1.InferInput<Schema>;
|
|
126
|
+
/** Infers the output type of a Standard. */
|
|
127
|
+
type InferOutput<Schema extends StandardTypedV1> = StandardTypedV1.InferOutput<Schema>;
|
|
128
|
+
}
|
|
129
|
+
//#endregion
|
|
130
|
+
//#region src/view_model.d.ts
|
|
64
131
|
interface AttributeBlockDefinition {
|
|
65
|
-
|
|
66
|
-
|
|
132
|
+
"~action"?: AttributeDefinition | undefined;
|
|
133
|
+
"~meta": any;
|
|
67
134
|
}
|
|
68
|
-
type Computed<T> = T extends infer U extends { [K in keyof T]
|
|
69
|
-
type BlockDefinitionRewriteMeta<
|
|
70
|
-
|
|
71
|
-
NewMeta
|
|
72
|
-
> = Computed<Omit<BlockDef, Meta> & {
|
|
73
|
-
"~meta": NewMeta;
|
|
135
|
+
type Computed<T> = T extends infer U extends { [K in keyof T]: unknown } ? U : never;
|
|
136
|
+
type BlockDefinitionRewriteMeta<BlockDef extends AttributeBlockDefinition, NewMeta> = Computed<Omit<BlockDef, Meta> & {
|
|
137
|
+
"~meta": NewMeta;
|
|
74
138
|
}> extends infer R extends AttributeBlockDefinition ? R : never;
|
|
75
|
-
interface IViewModel<
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
> {
|
|
79
|
-
parse(view: View<BlockDefinitionRewriteMeta<BlockDef, unknown>>): ModelT;
|
|
80
|
-
"~namedDefinition": BlockDef;
|
|
139
|
+
interface IViewModel<ModelT, BlockDef extends AttributeBlockDefinition, CtorArgs extends any[]> {
|
|
140
|
+
parse(view: View<BlockDefinitionRewriteMeta<BlockDef, unknown>>, ...args: CtorArgs): ModelT;
|
|
141
|
+
"~namedDefinition": BlockDef;
|
|
81
142
|
}
|
|
82
143
|
type LazyAttributeActionOrBinder<ModelT> = (model: ModelT, positionals: () => unknown[], named: View<any>) => unknown;
|
|
83
|
-
declare class ViewModel<
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
constructor(Ctor: new () => ModelT);
|
|
90
|
-
"~setActionOrBinder"(context: "action" | "binder", name: PropertyKey, action: LazyAttributeActionOrBinder<ModelT>): void;
|
|
91
|
-
parse(view: View<BlockDefinitionRewriteMeta<BlockDef, unknown>>): ModelT;
|
|
144
|
+
declare class ViewModel<ModelT, BlockDef extends AttributeBlockDefinition, CtorArgs extends any[]> implements IViewModel<ModelT, BlockDef, CtorArgs> {
|
|
145
|
+
#private;
|
|
146
|
+
"~namedDefinition": BlockDef;
|
|
147
|
+
constructor(Ctor: new (...args: CtorArgs) => ModelT);
|
|
148
|
+
"~setActionOrBinder"(context: "action" | "binder", name: PropertyKey, action: LazyAttributeActionOrBinder<ModelT>): void;
|
|
149
|
+
parse(view: View<BlockDefinitionRewriteMeta<BlockDef, unknown>>, ...args: CtorArgs): ModelT;
|
|
92
150
|
}
|
|
93
151
|
declare class AttributeDefHelper<ModelT> {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
(...args: Args): AttributeReturn.Done;
|
|
109
|
-
as?(): U;
|
|
110
|
-
};
|
|
152
|
+
#private;
|
|
153
|
+
constructor(viewModel: ViewModel<ModelT, any, any>);
|
|
154
|
+
"~assignActions"(defResult: Partial<Record<string, unknown>>): void;
|
|
155
|
+
attribute<T extends AttributeDefinition & {
|
|
156
|
+
as?: undefined;
|
|
157
|
+
}>(action: AttributeAction<ModelT, T>, binder?: AttributeBinder<ModelT, T> | IViewModel<any, ReturnType<T>["namedDefinition"], []>): T;
|
|
158
|
+
attribute<T extends AttributeDefinition>(action: AttributeAction<ModelT, T>, binder: AttributeBinder<ModelT, T>): T;
|
|
159
|
+
simpleAttribute<Args extends any[]>(action: (this: ModelT, ...args: Args) => void): {
|
|
160
|
+
(...args: Args): AttributeReturn.Done;
|
|
161
|
+
};
|
|
162
|
+
simpleAttribute<Args extends any[], U>(action: (this: ModelT, ...args: Args) => void, binder: (this: ModelT, ...args: Args) => U): {
|
|
163
|
+
(...args: Args): AttributeReturn.Done;
|
|
164
|
+
as?(): U;
|
|
165
|
+
};
|
|
111
166
|
}
|
|
112
|
-
declare function defineViewModel<
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
type SimpleViewModel<T> = IViewModel<T, { [K in keyof T]-? : {
|
|
120
|
-
(value: T[K]): AttributeReturn.Done;
|
|
121
|
-
uniqueKey(): K;
|
|
122
|
-
required(): {} extends Pick<T, K> ? false : true;
|
|
167
|
+
declare function defineViewModel<T, const BlockDef extends Partial<Record<string | Action, AttributeDefinition>>, CtorArgs extends any[] = [], InitMeta = void>(Ctor: new (...args: CtorArgs) => T, modelDefFn: (helper: AttributeDefHelper<T>) => BlockDef, initMeta?: InitMeta): IViewModel<T, BlockDef & {
|
|
168
|
+
"~meta": InitMeta;
|
|
169
|
+
}, CtorArgs>;
|
|
170
|
+
type SimpleViewModel<T> = IViewModel<T, { [K in keyof T]-?: {
|
|
171
|
+
(value: T[K]): AttributeReturn.Done;
|
|
172
|
+
uniqueKey(): K;
|
|
173
|
+
required(): {} extends Pick<T, K> ? false : true;
|
|
123
174
|
} } & {
|
|
124
|
-
|
|
125
|
-
}>;
|
|
175
|
+
"~meta": undefined;
|
|
176
|
+
}, []>;
|
|
126
177
|
declare function defineSimpleViewModel<const T extends StandardJSONSchemaV1>(schema: T): SimpleViewModel<StandardJSONSchemaV1.InferInput<T>>;
|
|
127
178
|
interface AttributeDefinition {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
179
|
+
(...args: any[]): AttributePositionalReturnBase;
|
|
180
|
+
as?(): any;
|
|
181
|
+
required?(): boolean;
|
|
182
|
+
uniqueKey?(): string;
|
|
132
183
|
}
|
|
133
184
|
interface AttributePositionalReturnBase {
|
|
134
|
-
|
|
135
|
-
|
|
185
|
+
rewriteMeta?: any;
|
|
186
|
+
namedDefinition: AttributeBlockDefinition;
|
|
136
187
|
}
|
|
137
|
-
type AttributeAction<
|
|
138
|
-
|
|
139
|
-
T extends AttributeDefinition
|
|
140
|
-
> = (model: Model, positional: Parameters<T>, named: View<ReturnType<T>["namedDefinition"] extends AttributeBlockDefinition ? ReturnType<T>["namedDefinition"] : {
|
|
141
|
-
"~meta": void;
|
|
188
|
+
type AttributeAction<Model, T extends AttributeDefinition> = (model: Model, positional: Parameters<T>, named: View<ReturnType<T>["namedDefinition"] extends AttributeBlockDefinition ? ReturnType<T>["namedDefinition"] : {
|
|
189
|
+
"~meta": void;
|
|
142
190
|
}>) => void;
|
|
143
|
-
type AttributeBinder<
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}>) => T["as"] extends () => infer U ? U : void;
|
|
149
|
-
export { defineViewModel, defineSimpleViewModel, createDefine, createBinding, View, SingleAttributeNode, SimpleViewModel, Prelude, NamedDefinition, NamedAttributesNode, Meta, IViewModel, AttributeReturn, AttributeDefinition, Action, AttributeReturn as AR };
|
|
191
|
+
type AttributeBinder<Model, T extends AttributeDefinition> = (model: Model, positional: Parameters<T>, named: View<ReturnType<T>["namedDefinition"] extends AttributeBlockDefinition ? ReturnType<T>["namedDefinition"] : {
|
|
192
|
+
"~meta": void;
|
|
193
|
+
}>) => T["as"] extends (() => infer U) ? U : void;
|
|
194
|
+
//#endregion
|
|
195
|
+
export { type AttributeReturn as AR, type AttributeReturn, type Action, type AttributeDefinition, type IViewModel, type Meta, type NamedAttributesNode, type NamedDefinition, type Prelude, type SimpleViewModel, type SingleAttributeNode, type View, createBinding, createDefine, defineSimpleViewModel, defineViewModel };
|
package/dist/index.js
CHANGED
|
@@ -1,149 +1,123 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
1
|
+
//#region src/view.ts
|
|
2
|
+
var View = class {
|
|
3
|
+
#phantom;
|
|
4
|
+
_node;
|
|
5
|
+
_bindingCtx;
|
|
6
|
+
constructor(_node, _bindingCtx) {
|
|
7
|
+
this._node = _node;
|
|
8
|
+
this._bindingCtx = _bindingCtx;
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
var BindingContext = class {
|
|
12
|
+
#bindings = [];
|
|
13
|
+
addBinding(value) {
|
|
14
|
+
this.#bindings.push(value);
|
|
15
|
+
}
|
|
16
|
+
getBindings() {
|
|
17
|
+
return this.#bindings;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
21
20
|
function createDefine(rootVM, node) {
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
const view = new View({ attributes: [node] });
|
|
22
|
+
rootVM.parse(view);
|
|
24
23
|
}
|
|
25
24
|
function createBinding(rootVM, node) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// packages/runtime/src/view_model.ts
|
|
33
|
-
class ViewModel {
|
|
34
|
-
#registeredActions = new Map;
|
|
35
|
-
#registeredBinders = new Map;
|
|
36
|
-
#Ctor;
|
|
37
|
-
constructor(Ctor) {
|
|
38
|
-
this.#Ctor = Ctor;
|
|
39
|
-
}
|
|
40
|
-
"~setActionOrBinder"(context, name, action) {
|
|
41
|
-
if (context === "action") {
|
|
42
|
-
this.#registeredActions.set(name, action);
|
|
43
|
-
} else {
|
|
44
|
-
this.#registeredBinders.set(name, action);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
parse(view) {
|
|
48
|
-
const model = new this.#Ctor;
|
|
49
|
-
for (const attrNode of view._node.attributes) {
|
|
50
|
-
let { name, positionals, named, binding } = attrNode;
|
|
51
|
-
const insideBindingCtx = !!view._bindingCtx;
|
|
52
|
-
let fn = (insideBindingCtx ? this.#registeredBinders : this.#registeredActions).get(name);
|
|
53
|
-
if (!insideBindingCtx && !fn) {
|
|
54
|
-
const modelName = this.#Ctor.name;
|
|
55
|
-
throw new Error(`No action registered for attribute "${String(name)}" on model "${modelName}"`);
|
|
56
|
-
}
|
|
57
|
-
fn ??= () => {};
|
|
58
|
-
named ??= { attributes: [] };
|
|
59
|
-
const value = fn(model, positionals, new View(named, view._bindingCtx));
|
|
60
|
-
if (binding && view._bindingCtx) {
|
|
61
|
-
view._bindingCtx.addBinding(value);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
return model;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
class AttributeDefHelper {
|
|
69
|
-
#viewModel;
|
|
70
|
-
constructor(viewModel) {
|
|
71
|
-
this.#viewModel = viewModel;
|
|
72
|
-
}
|
|
73
|
-
static #lazyActionSlot = Symbol("actionSlot");
|
|
74
|
-
static #lazyBinderSlot = Symbol("binderSlot");
|
|
75
|
-
"~assignActions"(defResult) {
|
|
76
|
-
for (const [name, value] of Object.entries(defResult)) {
|
|
77
|
-
const actionDescriptor = Object.getOwnPropertyDescriptor(value, AttributeDefHelper.#lazyActionSlot);
|
|
78
|
-
if (actionDescriptor) {
|
|
79
|
-
this.#viewModel["~setActionOrBinder"]("action", name, actionDescriptor.value);
|
|
80
|
-
}
|
|
81
|
-
const binderDescriptor = Object.getOwnPropertyDescriptor(value, AttributeDefHelper.#lazyBinderSlot);
|
|
82
|
-
if (binderDescriptor) {
|
|
83
|
-
this.#viewModel["~setActionOrBinder"]("binder", name, binderDescriptor.value);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
attribute(action, binder) {
|
|
88
|
-
const returnValue = {};
|
|
89
|
-
const lazyAction = (model, positionals, named) => action(model, positionals(), named);
|
|
90
|
-
Object.defineProperty(returnValue, AttributeDefHelper.#lazyActionSlot, {
|
|
91
|
-
value: lazyAction,
|
|
92
|
-
enumerable: true
|
|
93
|
-
});
|
|
94
|
-
let lazyBinder;
|
|
95
|
-
if (typeof binder === "function") {
|
|
96
|
-
lazyBinder = (model, positionals, named) => binder(model, positionals(), named);
|
|
97
|
-
} else if (binder) {
|
|
98
|
-
const vm = binder;
|
|
99
|
-
lazyBinder = (model, positionals, named) => {
|
|
100
|
-
return vm.parse(named);
|
|
101
|
-
};
|
|
102
|
-
} else {
|
|
103
|
-
lazyBinder = () => {};
|
|
104
|
-
}
|
|
105
|
-
Object.defineProperty(returnValue, AttributeDefHelper.#lazyBinderSlot, {
|
|
106
|
-
value: lazyBinder,
|
|
107
|
-
enumerable: true
|
|
108
|
-
});
|
|
109
|
-
return returnValue;
|
|
110
|
-
}
|
|
111
|
-
simpleAttribute(action, binder) {
|
|
112
|
-
const action2 = (model, positionals) => action.apply(model, positionals);
|
|
113
|
-
let binder2;
|
|
114
|
-
if (binder) {
|
|
115
|
-
binder2 = (model, positionals) => binder.apply(model, positionals);
|
|
116
|
-
}
|
|
117
|
-
return this.attribute(action2, binder2);
|
|
118
|
-
}
|
|
25
|
+
const bindingCtx = new BindingContext();
|
|
26
|
+
const view = new View({ attributes: [node] }, bindingCtx);
|
|
27
|
+
rootVM.parse(view);
|
|
28
|
+
return bindingCtx.getBindings();
|
|
119
29
|
}
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/view_model.ts
|
|
32
|
+
var ViewModel = class {
|
|
33
|
+
#registeredActions = /* @__PURE__ */ new Map();
|
|
34
|
+
#registeredBinders = /* @__PURE__ */ new Map();
|
|
35
|
+
#Ctor;
|
|
36
|
+
constructor(Ctor) {
|
|
37
|
+
this.#Ctor = Ctor;
|
|
38
|
+
}
|
|
39
|
+
"~setActionOrBinder"(context, name, action) {
|
|
40
|
+
if (context === "action") this.#registeredActions.set(name, action);
|
|
41
|
+
else this.#registeredBinders.set(name, action);
|
|
42
|
+
}
|
|
43
|
+
parse(view, ...args) {
|
|
44
|
+
const model = new this.#Ctor(...args);
|
|
45
|
+
for (const attrNode of view._node.attributes) {
|
|
46
|
+
let { name, positionals, named, binding } = attrNode;
|
|
47
|
+
const insideBindingCtx = !!view._bindingCtx;
|
|
48
|
+
let fn = (insideBindingCtx ? this.#registeredBinders : this.#registeredActions).get(name);
|
|
49
|
+
if (!insideBindingCtx && !fn) {
|
|
50
|
+
const modelName = this.#Ctor.name;
|
|
51
|
+
throw new Error(`No action registered for attribute "${String(name)}" on model "${modelName}"`);
|
|
52
|
+
}
|
|
53
|
+
fn ??= () => {};
|
|
54
|
+
named ??= { attributes: [] };
|
|
55
|
+
const value = fn(model, positionals, new View(named, view._bindingCtx));
|
|
56
|
+
if (binding && view._bindingCtx) view._bindingCtx.addBinding(value);
|
|
57
|
+
}
|
|
58
|
+
return model;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
var AttributeDefHelper = class AttributeDefHelper {
|
|
62
|
+
#viewModel;
|
|
63
|
+
constructor(viewModel) {
|
|
64
|
+
this.#viewModel = viewModel;
|
|
65
|
+
}
|
|
66
|
+
static #lazyActionSlot = Symbol("actionSlot");
|
|
67
|
+
static #lazyBinderSlot = Symbol("binderSlot");
|
|
68
|
+
"~assignActions"(defResult) {
|
|
69
|
+
for (const [name, value] of Object.entries(defResult)) {
|
|
70
|
+
const actionDescriptor = Object.getOwnPropertyDescriptor(value, AttributeDefHelper.#lazyActionSlot);
|
|
71
|
+
if (actionDescriptor) this.#viewModel["~setActionOrBinder"]("action", name, actionDescriptor.value);
|
|
72
|
+
const binderDescriptor = Object.getOwnPropertyDescriptor(value, AttributeDefHelper.#lazyBinderSlot);
|
|
73
|
+
if (binderDescriptor) this.#viewModel["~setActionOrBinder"]("binder", name, binderDescriptor.value);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
attribute(action, binder) {
|
|
77
|
+
const returnValue = {};
|
|
78
|
+
const lazyAction = (model, positionals, named) => action(model, positionals(), named);
|
|
79
|
+
Object.defineProperty(returnValue, AttributeDefHelper.#lazyActionSlot, {
|
|
80
|
+
value: lazyAction,
|
|
81
|
+
enumerable: true
|
|
82
|
+
});
|
|
83
|
+
let lazyBinder;
|
|
84
|
+
if (typeof binder === "function") lazyBinder = (model, positionals, named) => binder(model, positionals(), named);
|
|
85
|
+
else if (binder) {
|
|
86
|
+
const vm = binder;
|
|
87
|
+
lazyBinder = (model, positionals, named) => {
|
|
88
|
+
return vm.parse(named);
|
|
89
|
+
};
|
|
90
|
+
} else lazyBinder = () => {};
|
|
91
|
+
Object.defineProperty(returnValue, AttributeDefHelper.#lazyBinderSlot, {
|
|
92
|
+
value: lazyBinder,
|
|
93
|
+
enumerable: true
|
|
94
|
+
});
|
|
95
|
+
return returnValue;
|
|
96
|
+
}
|
|
97
|
+
simpleAttribute(action, binder) {
|
|
98
|
+
const action2 = (model, positionals) => action.apply(model, positionals);
|
|
99
|
+
let binder2;
|
|
100
|
+
if (binder) binder2 = (model, positionals) => binder.apply(model, positionals);
|
|
101
|
+
return this.attribute(action2, binder2);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
120
104
|
function defineViewModel(Ctor, modelDefFn, initMeta) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
105
|
+
const vm = new ViewModel(Ctor);
|
|
106
|
+
const helper = new AttributeDefHelper(vm);
|
|
107
|
+
const defResult = modelDefFn(helper);
|
|
108
|
+
helper["~assignActions"](defResult);
|
|
109
|
+
return vm;
|
|
126
110
|
}
|
|
127
111
|
function defineSimpleViewModel(schema) {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
defResult[key] = helper.simpleAttribute(function(value) {
|
|
138
|
-
this[key] = value;
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
helper["~assignActions"](defResult);
|
|
142
|
-
return vm;
|
|
112
|
+
const jsonSchema = schema["~standard"].jsonSchema.input({ target: "draft-2020-12" });
|
|
113
|
+
const vm = new ViewModel(class SimpleViewModel {});
|
|
114
|
+
const helper = new AttributeDefHelper(vm);
|
|
115
|
+
const defResult = {};
|
|
116
|
+
for (const key of Object.keys(jsonSchema.properties ?? {})) defResult[key] = helper.simpleAttribute(function(value) {
|
|
117
|
+
this[key] = value;
|
|
118
|
+
});
|
|
119
|
+
helper["~assignActions"](defResult);
|
|
120
|
+
return vm;
|
|
143
121
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
defineSimpleViewModel,
|
|
147
|
-
createDefine,
|
|
148
|
-
createBinding
|
|
149
|
-
};
|
|
122
|
+
//#endregion
|
|
123
|
+
export { createBinding, createDefine, defineSimpleViewModel, defineViewModel };
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gi-tcg/gts-runtime",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"repository": "https://github.com/piovium/gts.git",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": {
|
|
9
9
|
"types": "./dist/index.d.ts",
|
|
10
|
-
"
|
|
10
|
+
"development": "./src/index.ts",
|
|
11
11
|
"default": "./dist/index.js"
|
|
12
12
|
}
|
|
13
13
|
},
|
|
@@ -19,5 +19,8 @@
|
|
|
19
19
|
"devDependencies": {
|
|
20
20
|
"@standard-schema/spec": "^1.1.0",
|
|
21
21
|
"json-schema-typed": "^8.0.2"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsdown --platform neutral"
|
|
22
25
|
}
|
|
23
|
-
}
|
|
26
|
+
}
|
package/src/attribute_return.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { BlockDefinitionRewriteMeta, IViewModel } from "./view_model";
|
|
1
|
+
import type { BlockDefinitionRewriteMeta, IViewModel } from "./view_model.ts";
|
|
2
2
|
|
|
3
3
|
export namespace AttributeReturn {
|
|
4
4
|
export type This<TMeta> = {
|
|
@@ -14,7 +14,7 @@ export namespace AttributeReturn {
|
|
|
14
14
|
};
|
|
15
15
|
|
|
16
16
|
export type With<
|
|
17
|
-
VM extends IViewModel<any, any>,
|
|
17
|
+
VM extends IViewModel<any, any, any>,
|
|
18
18
|
TMeta = VM["~namedDefinition"]["~meta"],
|
|
19
19
|
> = {
|
|
20
20
|
namedDefinition: BlockDefinitionRewriteMeta<VM["~namedDefinition"], TMeta>;
|
|
@@ -25,7 +25,7 @@ export namespace AttributeReturn {
|
|
|
25
25
|
rewriteMeta: NewMeta;
|
|
26
26
|
};
|
|
27
27
|
|
|
28
|
-
export type WithRewriteMeta<VM extends IViewModel<any, any>, NewMeta> = {
|
|
28
|
+
export type WithRewriteMeta<VM extends IViewModel<any, any, any>, NewMeta> = {
|
|
29
29
|
namedDefinition: VM["~namedDefinition"];
|
|
30
30
|
rewriteMeta: NewMeta;
|
|
31
31
|
};
|
package/src/index.ts
CHANGED
|
@@ -4,9 +4,9 @@ export {
|
|
|
4
4
|
type AttributeDefinition,
|
|
5
5
|
type IViewModel,
|
|
6
6
|
type SimpleViewModel,
|
|
7
|
-
} from "./view_model";
|
|
8
|
-
export type { AttributeReturn, AR } from "./attribute_return";
|
|
9
|
-
export type { Action, Prelude, Meta, NamedDefinition } from "./symbols";
|
|
7
|
+
} from "./view_model.ts";
|
|
8
|
+
export type { AttributeReturn, AR } from "./attribute_return.ts";
|
|
9
|
+
export type { Action, Prelude, Meta, NamedDefinition } from "./symbols.ts";
|
|
10
10
|
|
|
11
11
|
export {
|
|
12
12
|
createBinding,
|
|
@@ -14,4 +14,4 @@ export {
|
|
|
14
14
|
type SingleAttributeNode,
|
|
15
15
|
type NamedAttributesNode,
|
|
16
16
|
type View,
|
|
17
|
-
} from "./view";
|
|
17
|
+
} from "./view.ts";
|
package/src/view.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AttributeBlockDefinition, IViewModel } from "./view_model";
|
|
1
|
+
import type { AttributeBlockDefinition, IViewModel } from "./view_model.ts";
|
|
2
2
|
|
|
3
3
|
export type AttributeName = string | symbol;
|
|
4
4
|
|
|
@@ -17,11 +17,16 @@ export interface NamedAttributesNode {
|
|
|
17
17
|
|
|
18
18
|
export class View<BlockDef extends AttributeBlockDefinition> {
|
|
19
19
|
#phantom!: BlockDef;
|
|
20
|
+
_node: NamedAttributesNode;
|
|
21
|
+
_bindingCtx?: BindingContext;
|
|
20
22
|
|
|
21
23
|
constructor(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
) {
|
|
24
|
+
_node: NamedAttributesNode,
|
|
25
|
+
_bindingCtx?: BindingContext | undefined,
|
|
26
|
+
) {
|
|
27
|
+
this._node = _node;
|
|
28
|
+
this._bindingCtx = _bindingCtx;
|
|
29
|
+
}
|
|
25
30
|
}
|
|
26
31
|
|
|
27
32
|
export class BindingContext {
|
|
@@ -35,7 +40,7 @@ export class BindingContext {
|
|
|
35
40
|
}
|
|
36
41
|
|
|
37
42
|
export function createDefine(
|
|
38
|
-
rootVM: IViewModel<any, any>,
|
|
43
|
+
rootVM: IViewModel<any, any, any>,
|
|
39
44
|
node: SingleAttributeNode,
|
|
40
45
|
): void {
|
|
41
46
|
const view = new View<any>({ attributes: [node] });
|
|
@@ -43,7 +48,7 @@ export function createDefine(
|
|
|
43
48
|
}
|
|
44
49
|
|
|
45
50
|
export function createBinding(
|
|
46
|
-
rootVM: IViewModel<any, any>,
|
|
51
|
+
rootVM: IViewModel<any, any, any>,
|
|
47
52
|
node: SingleAttributeNode,
|
|
48
53
|
): unknown[] {
|
|
49
54
|
const bindingCtx = new BindingContext();
|
package/src/view_model.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { AttributeReturn } from "./attribute_return";
|
|
2
|
-
import { Action, Meta, NamedDefinition } from "./symbols";
|
|
3
|
-
import { View } from "./view";
|
|
4
|
-
import { StandardJSONSchemaV1 } from "@standard-schema/spec";
|
|
1
|
+
import type { AttributeReturn } from "./attribute_return.ts";
|
|
2
|
+
import type { Action, Meta, NamedDefinition } from "./symbols.ts";
|
|
3
|
+
import { View } from "./view.ts";
|
|
4
|
+
import type { StandardJSONSchemaV1 } from "@standard-schema/spec";
|
|
5
5
|
|
|
6
6
|
export interface AttributeBlockDefinition {
|
|
7
7
|
"~action"?: AttributeDefinition | undefined;
|
|
@@ -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 ?? {})) {
|