@zcomponent/core 1.16.0-beta → 1.17.0-beta

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.
@@ -100,7 +100,8 @@ export interface ContextValueEntityPropOverride extends BaseEntityPropOverride {
100
100
  export declare enum FunctionCallType {
101
101
  Entity = "entity",
102
102
  Context = "context",
103
- Import = "import"
103
+ Import = "import",
104
+ EmitComponentEvent = "emitcomponentevent"
104
105
  }
105
106
  export interface BaseFunctionCall {
106
107
  _zfn: true;
@@ -125,4 +126,8 @@ export interface ImportFunctionCall extends BaseFunctionCall {
125
126
  t: FunctionCallType.Import;
126
127
  resolved?: any;
127
128
  }
128
- export type FunctionCall = EntityFunctionCall | ContextFunctionCall | ImportFunctionCall;
129
+ export interface EmitComponentEventFunctionCall extends BaseFunctionCall {
130
+ prop: string;
131
+ t: FunctionCallType.EmitComponentEvent;
132
+ }
133
+ export type FunctionCall = EntityFunctionCall | ContextFunctionCall | ImportFunctionCall | EmitComponentEventFunctionCall;
package/lib/data/core.js CHANGED
@@ -9,4 +9,5 @@ export var FunctionCallType;
9
9
  FunctionCallType["Entity"] = "entity";
10
10
  FunctionCallType["Context"] = "context";
11
11
  FunctionCallType["Import"] = "import";
12
+ FunctionCallType["EmitComponentEvent"] = "emitcomponentevent";
12
13
  })(FunctionCallType || (FunctionCallType = {}));
package/lib/selectors.js CHANGED
@@ -319,7 +319,7 @@ export const typeDefinitionForComponent = (nodes, props, constructorProps, scrip
319
319
  }
320
320
  }
321
321
  }
322
- return `import { ZComponent, ContextManager, Observable, Animation, Layer, LayerClip } from "@zcomponent/core";
322
+ return `import { ZComponent, ContextManager, Observable, Animation, Layer, LayerClip, Event } from "@zcomponent/core";
323
323
 
324
324
  ${importStrings.join('\n')}
325
325
 
@@ -429,6 +429,9 @@ function getJSDocForProp(prop) {
429
429
  * @returns A string representing the TypeScript output for the property.
430
430
  */
431
431
  function typeOutputForProp(prop) {
432
+ if (prop.type.name === 'event')
433
+ return `${getJSDocForProp(prop)}
434
+ public ${prop.name}: ${outputForType(prop.type, true)};`;
432
435
  return `${getJSDocForProp(prop)}
433
436
  public ${prop.name}: Observable<${outputForType(prop.type, true)}>;`;
434
437
  }
package/lib/types.d.ts CHANGED
@@ -124,11 +124,11 @@ export interface FunctionType extends BaseType {
124
124
  }
125
125
  /**
126
126
  * Type definition for an event type.
127
- *
128
- * This interface extends BaseType and is used specifically for defining an event type.
129
127
  */
130
128
  export interface EventType extends BaseType {
131
129
  name: 'event';
130
+ children: Type[];
131
+ names?: string[];
132
132
  }
133
133
  /** Internal use comment is sufficient for EntityType
134
134
  * @internal
package/lib/types.js CHANGED
@@ -350,10 +350,19 @@ export function mergeTypes(a, b) {
350
350
  comments: mergeComments(a.comments ?? [], b.comments ?? []),
351
351
  };
352
352
  case 'event':
353
+ const children = [];
354
+ for (let i = 0; i < a.children.length; i++) {
355
+ const merged = mergeTypes(a.children[i], b.children[i]);
356
+ if (!merged)
357
+ return undefined;
358
+ children.push(merged);
359
+ }
353
360
  return {
354
361
  name: a.name,
355
362
  typeHint,
363
+ children,
356
364
  comments: mergeComments(a.comments ?? [], b.comments ?? []),
365
+ names: mergeTupleNames(a, b),
357
366
  };
358
367
  }
359
368
  }
@@ -451,6 +460,11 @@ export function isValidValueForType(def, t, allowUndefined) {
451
460
  return false;
452
461
  return true;
453
462
  }
463
+ if (def['t'] === FunctionCallType.EmitComponentEvent) {
464
+ if (typeof def['prop'] !== 'string' || def['prop'].length === 0)
465
+ return false;
466
+ return true;
467
+ }
454
468
  return false;
455
469
  }
456
470
  return false;
@@ -494,7 +508,10 @@ function getBasicType(t) {
494
508
  case 'function':
495
509
  return `(${t.args.map(entry => `${entry.name}: ${outputForType(entry.type)}`).join(', ')}) => ${t.ret ? outputForType(t.ret) : 'void'}`;
496
510
  case 'event':
497
- return 'Event';
511
+ if (Array.isArray(t.names) && t.names.length === t.children.length) {
512
+ return `Event<[${t.children.map((c, indx) => `${getSafeKeyName(t.names[indx])}: ${outputForType(c)}`).join(', ')}]>`;
513
+ }
514
+ return `Event<[${t.children.map(c => outputForType(c)).join(', ')}]>`;
498
515
  }
499
516
  }
500
517
  /**
package/lib/zcomponent.js CHANGED
@@ -13,6 +13,7 @@ import { populateAnimationStructureFromData, populateAnimationTracksFromData, up
13
13
  import { EntityPropOverrideType, FunctionCallType } from './data/core';
14
14
  import { Observable } from './observable';
15
15
  import { setCurrentZComponentConstruction } from './zcomponentconstruction';
16
+ import { Event } from './event';
16
17
  export class ZComponentContext extends Context {
17
18
  /**
18
19
  * Creates an instance of ZComponentContext.
@@ -370,6 +371,25 @@ export class ZComponent extends Component {
370
371
  console.error('Unable to call entity function', err);
371
372
  }
372
373
  };
374
+ case FunctionCallType.EmitComponentEvent:
375
+ return () => {
376
+ try {
377
+ if (typeof c.prop !== 'string' || typeof c.args !== 'object')
378
+ throw new Error('Invalid function call data');
379
+ const evt = this[c.prop];
380
+ if (!evt)
381
+ throw new Error('Unable to find component event');
382
+ if (!(evt instanceof Event))
383
+ throw new Error('Component property is not an event');
384
+ const converted = [];
385
+ for (const key in c.args)
386
+ converted[key] = c.args[key];
387
+ evt.emit.call(evt, ...converted);
388
+ }
389
+ catch (err) {
390
+ console.error('Unable to emit component event', err);
391
+ }
392
+ };
373
393
  }
374
394
  }
375
395
  /**
@@ -484,7 +504,10 @@ export class ZComponent extends Component {
484
504
  _initializeComponentProps() {
485
505
  const componentProps = this._opts.data.props ?? {};
486
506
  for (const prop of Object.values(componentProps)) {
487
- this[prop.name] = new Observable(prop.default);
507
+ if (prop.type.name === 'event')
508
+ this[prop.name] = new Event();
509
+ else
510
+ this[prop.name] = new Observable(prop.default);
488
511
  }
489
512
  }
490
513
  _setEntityProp(entity, prop, v) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zcomponent/core",
3
- "version": "1.16.0-beta",
3
+ "version": "1.17.0-beta",
4
4
  "description": "The core component model and built-in functionality for Mattercraft.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",