@gi-tcg/gts-runtime 0.3.7 → 0.3.9

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
@@ -148,6 +148,15 @@ declare class ViewModel<ModelT, BlockDef extends AttributeBlockDefinition, CtorA
148
148
  "~setActionOrBinder"(context: "action" | "binder", name: PropertyKey, action: LazyAttributeActionOrBinder<ModelT>): void;
149
149
  parse(view: View<BlockDefinitionRewriteMeta<BlockDef, unknown>>, ...args: CtorArgs): ModelT;
150
150
  }
151
+ interface SimpleAttributeOptions {
152
+ required?: true;
153
+ uniqueKey?: string;
154
+ }
155
+ type WithSimpleOptions<Base, Options extends SimpleAttributeOptions> = Base & (Options["required"] extends true ? {
156
+ required(): true;
157
+ } : {}) & (Options["uniqueKey"] extends string ? {
158
+ uniqueKey(): Options["uniqueKey"];
159
+ } : {});
151
160
  declare class AttributeDefHelper<ModelT> {
152
161
  #private;
153
162
  constructor(viewModel: ViewModel<ModelT, any, any>);
@@ -156,12 +165,23 @@ declare class AttributeDefHelper<ModelT> {
156
165
  as?: undefined;
157
166
  }>(action: AttributeAction<ModelT, T>, binder?: AttributeBinder<ModelT, T> | IViewModel<any, ReturnType<T>["namedDefinition"], []>): T;
158
167
  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;
168
+ simpleAttribute(): {
169
+ <Args extends any[]>(action: (this: ModelT, ...args: Args) => void): {
170
+ (...args: Args): AttributeReturn.Done;
171
+ };
172
+ <Args extends any[], U>(action: (this: ModelT, ...args: Args) => void, binder: (this: ModelT, ...args: Args) => U): {
173
+ (...args: Args): AttributeReturn.Done;
174
+ as?(): U;
175
+ };
161
176
  };
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;
177
+ simpleAttribute<const Options extends SimpleAttributeOptions>(options: Options): {
178
+ <Args extends any[]>(action: (this: ModelT, ...args: Args) => void): WithSimpleOptions<{
179
+ (...args: Args): AttributeReturn.Done;
180
+ }, Options>;
181
+ <Args extends any[], U>(action: (this: ModelT, ...args: Args) => void, binder: (this: ModelT, ...args: Args) => U): WithSimpleOptions<{
182
+ (...args: Args): AttributeReturn.Done;
183
+ as?(): U;
184
+ }, Options>;
165
185
  };
166
186
  }
167
187
  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 & {
package/dist/index.js CHANGED
@@ -98,11 +98,13 @@ var AttributeDefHelper = class AttributeDefHelper {
98
98
  });
99
99
  return returnValue;
100
100
  }
101
- simpleAttribute(action, binder) {
102
- const action2 = (model, positionals) => action.apply(model, positionals);
103
- let binder2;
104
- if (binder) binder2 = (model, positionals) => binder.apply(model, positionals);
105
- return this.attribute(action2, binder2);
101
+ simpleAttribute(options) {
102
+ return (action, binder) => {
103
+ const action2 = (model, positionals) => action.apply(model, positionals);
104
+ let binder2;
105
+ if (binder) binder2 = (model, positionals) => binder.apply(model, positionals);
106
+ return this.attribute(action2, binder2);
107
+ };
106
108
  }
107
109
  };
108
110
  function defineViewModel(Ctor, modelDefFn, initMeta) {
@@ -117,7 +119,7 @@ function defineSimpleViewModel(schema) {
117
119
  const vm = new ViewModel(class SimpleViewModel {});
118
120
  const helper = new AttributeDefHelper(vm);
119
121
  const defResult = {};
120
- for (const key of Object.keys(jsonSchema.properties ?? {})) defResult[key] = helper.simpleAttribute(function(value) {
122
+ for (const key of Object.keys(jsonSchema.properties ?? {})) defResult[key] = helper.simpleAttribute()(function(value) {
121
123
  this[key] = value;
122
124
  });
123
125
  helper["~assignActions"](defResult);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gi-tcg/gts-runtime",
3
- "version": "0.3.7",
3
+ "version": "0.3.9",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/piovium/gts.git"
package/src/view_model.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type { AttributeReturn } from "./attribute_return.ts";
2
2
  import type { Action, Meta, NamedDefinition } from "./symbols.ts";
3
3
  import { View } from "./view.ts";
4
- import type { StandardJSONSchemaV1 } from "@standard-schema/spec";
4
+ import type { StandardJSONSchemaV1 } from "@standard-schema/spec";
5
5
 
6
6
  export interface AttributeBlockDefinition {
7
7
  "~action"?: AttributeDefinition | undefined;
@@ -97,6 +97,17 @@ class ViewModel<
97
97
  }
98
98
  }
99
99
 
100
+ interface SimpleAttributeOptions {
101
+ required?: true;
102
+ uniqueKey?: string;
103
+ }
104
+
105
+ type WithSimpleOptions<Base, Options extends SimpleAttributeOptions> = Base &
106
+ (Options["required"] extends true ? { required(): true } : {}) &
107
+ (Options["uniqueKey"] extends string
108
+ ? { uniqueKey(): Options["uniqueKey"] }
109
+ : {});
110
+
100
111
  class AttributeDefHelper<ModelT> {
101
112
  #viewModel: ViewModel<ModelT, any, any>;
102
113
  constructor(viewModel: ViewModel<ModelT, any, any>) {
@@ -109,7 +120,10 @@ class AttributeDefHelper<ModelT> {
109
120
  "~assignActions"(defResult: Partial<Record<string, unknown>>): void {
110
121
  for (const [name, value] of Object.entries(defResult)) {
111
122
  if (!value) {
112
- console?.warn?.(`Attribute "${name}" is assigned a falsy value, which is not a valid attribute definition.`);
123
+ // @ts-expect-error no typing for console
124
+ console?.warn?.(
125
+ `Attribute "${name}" is assigned a falsy value, which is not a valid attribute definition.`,
126
+ );
113
127
  continue;
114
128
  }
115
129
  const actionDescriptor = Object.getOwnPropertyDescriptor(
@@ -180,30 +194,42 @@ class AttributeDefHelper<ModelT> {
180
194
  return returnValue;
181
195
  }
182
196
 
183
- simpleAttribute<Args extends any[]>(
184
- action: (this: ModelT, ...args: Args) => void,
185
- ): {
186
- (...args: Args): AttributeReturn.Done;
197
+ simpleAttribute(): {
198
+ <Args extends any[]>(
199
+ action: (this: ModelT, ...args: Args) => void,
200
+ ): { (...args: Args): AttributeReturn.Done };
201
+ <Args extends any[], U>(
202
+ action: (this: ModelT, ...args: Args) => void,
203
+ binder: (this: ModelT, ...args: Args) => U,
204
+ ): { (...args: Args): AttributeReturn.Done; as?(): U };
187
205
  };
188
- simpleAttribute<Args extends any[], U>(
189
- action: (this: ModelT, ...args: Args) => void,
190
- binder: (this: ModelT, ...args: Args) => U,
206
+ simpleAttribute<const Options extends SimpleAttributeOptions>(
207
+ options: Options,
191
208
  ): {
192
- (...args: Args): AttributeReturn.Done;
193
- as?(): U;
209
+ <Args extends any[]>(
210
+ action: (this: ModelT, ...args: Args) => void,
211
+ ): WithSimpleOptions<{ (...args: Args): AttributeReturn.Done }, Options>;
212
+ <Args extends any[], U>(
213
+ action: (this: ModelT, ...args: Args) => void,
214
+ binder: (this: ModelT, ...args: Args) => U,
215
+ ): WithSimpleOptions<
216
+ { (...args: Args): AttributeReturn.Done; as?(): U },
217
+ Options
218
+ >;
194
219
  };
195
- simpleAttribute<Args extends any[], U>(
196
- action: (this: ModelT, ...args: Args) => void,
197
- binder?: (this: ModelT, ...args: Args) => U,
198
- ) {
199
- const action2: AttributeAction<ModelT, any> = (model, positionals) =>
200
- action.apply(model, positionals as Args);
201
- let binder2: AttributeBinder<ModelT, any> | undefined;
202
- if (binder) {
203
- binder2 = (model, positionals) =>
204
- binder.apply(model, positionals as Args);
205
- }
206
- return this.attribute<any>(action2, binder2);
220
+ simpleAttribute(options?: SimpleAttributeOptions) {
221
+ return (
222
+ action: (this: ModelT, ...args: any[]) => void,
223
+ binder?: (...args: any[]) => any,
224
+ ) => {
225
+ const action2: AttributeAction<ModelT, any> = (model, positionals) =>
226
+ action.apply(model, positionals);
227
+ let binder2: AttributeBinder<ModelT, any> | undefined;
228
+ if (binder) {
229
+ binder2 = (model, positionals) => binder.apply(model, positionals);
230
+ }
231
+ return this.attribute<any>(action2, binder2);
232
+ };
207
233
  }
208
234
  }
209
235
 
@@ -247,7 +273,7 @@ export function defineSimpleViewModel<const T extends StandardJSONSchemaV1>(
247
273
  const helper = new AttributeDefHelper(vm);
248
274
  const defResult: Record<string, any> = {};
249
275
  for (const key of Object.keys(jsonSchema.properties ?? {})) {
250
- defResult[key] = helper.simpleAttribute(function (this: any, value) {
276
+ defResult[key] = helper.simpleAttribute()(function (this: any, value) {
251
277
  this[key] = value;
252
278
  });
253
279
  }