• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

excaliburjs / Excalibur / 13619769950

02 Mar 2025 10:08PM UTC coverage: 89.297% (-0.02%) from 89.318%
13619769950

Pull #3380

github

web-flow
Merge 7be4993fb into 6c9fca699
Pull Request #3380: reat: support any, all and not component/tag filters on Query

6446 of 8361 branches covered (77.1%)

59 of 63 new or added lines in 3 files covered. (93.65%)

4 existing lines in 1 file now uncovered.

13874 of 15537 relevant lines covered (89.3%)

25263.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.42
/src/engine/EntityComponentSystem/Entity.ts
1
import { Component, ComponentCtor, isComponentCtor } from './Component';
2

3
import { Observable, Message } from '../Util/Observable';
4
import { OnInitialize, OnPreUpdate, OnPostUpdate, OnAdd, OnRemove } from '../Interfaces/LifecycleEvents';
5
import { Engine } from '../Engine';
6
import { InitializeEvent, PreUpdateEvent, PostUpdateEvent, AddEvent, RemoveEvent } from '../Events';
7
import { KillEvent } from '../Events';
8
import { EventEmitter, EventKey, Handler, Subscription } from '../EventEmitter';
9
import { Scene } from '../Scene';
10
import { removeItemFromArray } from '../Util/Util';
11
import { MaybeKnownComponent } from './Types';
12
import { Logger } from '../Util/Log';
13

14
/**
15
 * Interface holding an entity component pair
16
 */
17
export interface EntityComponent {
18
  component: Component;
19
  entity: Entity;
20
}
21

22
/**
23
 * AddedComponent message
24
 */
25
export class AddedComponent implements Message<EntityComponent> {
26
  readonly type: 'Component Added' = 'Component Added';
×
27
  constructor(public data: EntityComponent) {}
×
28
}
29

30
/**
31
 * Type guard to know if message is f an Added Component
32
 */
33
export function isAddedComponent(x: Message<EntityComponent>): x is AddedComponent {
34
  return !!x && x.type === 'Component Added';
×
35
}
36

37
/**
38
 * RemovedComponent message
39
 */
40
export class RemovedComponent implements Message<EntityComponent> {
41
  readonly type: 'Component Removed' = 'Component Removed';
×
42
  constructor(public data: EntityComponent) {}
×
43
}
44

45
/**
46
 * Type guard to know if message is for a Removed Component
47
 */
48
export function isRemovedComponent(x: Message<EntityComponent>): x is RemovedComponent {
49
  return !!x && x.type === 'Component Removed';
×
50
}
51

52
/**
53
 * Built in events supported by all entities
54
 */
55
export type EntityEvents = {
56
  initialize: InitializeEvent;
57
  //@ts-ignore
58
  add: AddEvent;
59
  //@ts-ignore
60
  remove: RemoveEvent;
61
  preupdate: PreUpdateEvent;
62
  postupdate: PostUpdateEvent;
63
  kill: KillEvent;
64
};
65

66
export const EntityEvents = {
1✔
67
  Add: 'add',
68
  Remove: 'remove',
69
  Initialize: 'initialize',
70
  PreUpdate: 'preupdate',
71
  PostUpdate: 'postupdate',
72
  Kill: 'kill'
73
} as const;
74

75
export interface EntityOptions<TComponents extends Component> {
76
  name?: string;
77
  components?: TComponents[];
78
  silenceWarnings?: boolean;
79
}
80

81
/**
82
 * An Entity is the base type of anything that can have behavior in Excalibur, they are part of the built in entity component system
83
 *
84
 * Entities can be strongly typed with the components they contain
85
 *
86
 * ```typescript
87
 * const entity = new Entity<ComponentA | ComponentB>();
88
 * entity.components.a; // Type ComponentA
89
 * entity.components.b; // Type ComponentB
90
 * ```
91
 */
92
export class Entity<TKnownComponents extends Component = any> implements OnInitialize, OnPreUpdate, OnPostUpdate, OnAdd, OnRemove {
93
  private static _ID = 0;
1✔
94
  /**
95
   * The unique identifier for the entity
96
   */
97
  public id: number = Entity._ID++;
7,532✔
98

99
  public name = `Entity#${this.id}`;
7,532✔
100

101
  /**
102
   * Listen to or emit events for an entity
103
   */
104
  public events = new EventEmitter<EntityEvents>();
7,532✔
105
  private _tags = new Set<string>();
7,532✔
106
  public componentAdded$ = new Observable<Component>();
7,532✔
107
  public componentRemoved$ = new Observable<Component>();
7,532✔
108
  public tagAdded$ = new Observable<string>();
7,532✔
109
  public tagRemoved$ = new Observable<string>();
7,532✔
110
  /**
111
   * Current components on the entity
112
   *
113
   * **Do not modify**
114
   *
115
   * Use addComponent/removeComponent otherwise the ECS will not be notified of changes.
116
   */
117
  public readonly components = new Map<Function, Component>();
7,532✔
118
  public componentValues: Component[] = [];
7,532✔
119
  private _componentsToRemove: ComponentCtor[] = [];
7,532✔
120

121
  constructor(options: EntityOptions<TKnownComponents>);
122
  constructor(components?: TKnownComponents[], name?: string);
123
  constructor(componentsOrOptions?: TKnownComponents[] | EntityOptions<TKnownComponents>, name?: string) {
124
    let componentsToAdd!: TKnownComponents[];
125
    let nameToAdd: string | undefined;
126
    let silence = false;
7,532✔
127
    if (Array.isArray(componentsOrOptions)) {
7,532✔
128
      componentsToAdd = componentsOrOptions;
2,934✔
129
      nameToAdd = name;
2,934✔
130
    } else if (componentsOrOptions && typeof componentsOrOptions === 'object') {
4,598✔
131
      const { components, name, silenceWarnings } = componentsOrOptions;
3,500✔
132
      componentsToAdd = components ?? [];
3,500!
133
      nameToAdd = name;
3,500✔
134
      silence = !!silenceWarnings;
3,500✔
135
    }
136
    if (nameToAdd) {
7,532✔
137
      this.name = nameToAdd;
10✔
138
    }
139
    if (componentsToAdd) {
7,532✔
140
      for (const component of componentsToAdd) {
6,434✔
141
        this.addComponent(component);
8,656✔
142
      }
143
    }
144

145
    if (process.env.NODE_ENV === 'development') {
7,532!
146
      if (!silence) {
7,532✔
147
        setTimeout(() => {
4,032✔
148
          if (!this.scene && !this.isInitialized) {
3,885✔
149
            Logger.getInstance().warn(`Entity "${this.name || this.id}" was not added to a scene.`);
2,090!
150
          }
151
        }, 5000);
152
      }
153
    }
154
  }
155

156
  /**
157
   * The current scene that the entity is in, if any
158
   */
159
  public scene: Scene | null = null;
7,532✔
160

161
  /**
162
   * Whether this entity is active, if set to false it will be reclaimed
163
   * @deprecated use isActive
164
   */
165
  public get active(): boolean {
166
    return this.isActive;
1✔
167
  }
168

169
  /**
170
   * Whether this entity is active, if set to false it will be reclaimed
171
   * @deprecated use isActive
172
   */
173
  public set active(val: boolean) {
174
    this.isActive = val;
1✔
175
  }
176

177
  /**
178
   * Whether this entity is active, if set to false it will be reclaimed
179
   */
180
  public isActive: boolean = true;
7,532✔
181

182
  /**
183
   * Kill the entity, means it will no longer be updated. Kills are deferred to the end of the update.
184
   * If parented it will be removed from the parent when killed.
185
   */
186
  public kill() {
187
    if (this.isActive) {
30!
188
      this.isActive = false;
30✔
189
      this.unparent();
30✔
190
    }
191
    this.emit('kill', new KillEvent(this));
30✔
192
  }
193

194
  public isKilled() {
195
    return !this.isActive;
2✔
196
  }
197

198
  /**
199
   * Specifically get the tags on the entity from {@apilink TagsComponent}
200
   */
201
  public get tags(): Set<string> {
202
    return this._tags;
30✔
203
  }
204

205
  /**
206
   * Check if a tag exists on the entity
207
   * @param tag name to check for
208
   */
209
  public hasTag(tag: string): boolean {
210
    return this._tags.has(tag);
4,950✔
211
  }
212

213
  /**
214
   * Adds a tag to an entity
215
   * @param tag
216
   */
217
  public addTag(tag: string): Entity<TKnownComponents> {
218
    this._tags.add(tag);
52✔
219
    this.tagAdded$.notifyAll(tag);
52✔
220
    return this;
52✔
221
  }
222

223
  /**
224
   * Removes a tag on the entity
225
   *
226
   * Removals are deferred until the end of update
227
   * @param tag
228
   */
229
  public removeTag(tag: string): Entity<TKnownComponents> {
230
    this._tags.delete(tag);
4✔
231
    this.tagRemoved$.notifyAll(tag);
4✔
232
    return this;
4✔
233
  }
234

235
  /**
236
   * The types of the components on the Entity
237
   */
238
  public get types(): ComponentCtor[] {
239
    return Array.from(this.components.keys()) as ComponentCtor[];
24✔
240
  }
241

242
  /**
243
   * Returns all component instances on entity
244
   */
245
  public getComponents(): Component[] {
246
    return Array.from(this.components.values());
8✔
247
  }
248

249
  /**
250
   * Verifies that an entity has all the required types
251
   * @param requiredTypes
252
   */
253
  hasAll<TComponent extends Component>(requiredTypes: ComponentCtor<TComponent>[]): boolean {
UNCOV
254
    for (let i = 0; i < requiredTypes.length; i++) {
×
UNCOV
255
      if (!this.components.has(requiredTypes[i])) {
×
UNCOV
256
        return false;
×
257
      }
258
    }
UNCOV
259
    return true;
×
260
  }
261

262
  /**
263
   * Verifies that an entity has all the required tags
264
   * @param requiredTags
265
   */
266
  hasAllTags(requiredTags: string[]): boolean {
267
    for (let i = 0; i < requiredTags.length; i++) {
13✔
268
      if (!this.tags.has(requiredTags[i])) {
26✔
269
        return false;
2✔
270
      }
271
    }
272
    return true;
11✔
273
  }
274

275
  get<TComponent extends Component>(type: ComponentCtor<TComponent>): MaybeKnownComponent<TComponent, TKnownComponents> {
276
    return this.components.get(type) as MaybeKnownComponent<TComponent, TKnownComponents>;
67,945✔
277
  }
278

279
  private _parent: Entity | null = null;
7,532✔
280
  public get parent(): Entity | null {
281
    return this._parent;
15,138✔
282
  }
283

284
  public childrenAdded$ = new Observable<Entity>();
7,532✔
285
  public childrenRemoved$ = new Observable<Entity>();
7,532✔
286

287
  private _children: Entity[] = [];
7,532✔
288
  /**
289
   * Get the direct children of this entity
290
   */
291
  public get children(): readonly Entity[] {
292
    return this._children;
19,909✔
293
  }
294

295
  /**
296
   * Unparents this entity, if there is a parent. Otherwise it does nothing.
297
   */
298
  public unparent() {
299
    if (this._parent) {
31✔
300
      this._parent.removeChild(this);
7✔
301
      this._parent = null;
7✔
302
    }
303
  }
304

305
  /**
306
   * Adds an entity to be a child of this entity
307
   * @param entity
308
   */
309
  public addChild(entity: Entity): Entity {
310
    if (entity.parent === null) {
2,969✔
311
      if (this.getAncestors().includes(entity)) {
2,968✔
312
        throw new Error('Cycle detected, cannot add entity');
1✔
313
      }
314
      this._children.push(entity);
2,967✔
315
      entity._parent = this;
2,967✔
316
      this.childrenAdded$.notifyAll(entity);
2,967✔
317
    } else {
318
      throw new Error('Entity already has a parent, cannot add without unparenting');
1✔
319
    }
320
    return this;
2,967✔
321
  }
322

323
  /**
324
   * Remove an entity from children if it exists
325
   * @param entity
326
   */
327
  public removeChild(entity: Entity): Entity {
328
    if (entity.parent === this) {
22!
329
      removeItemFromArray(entity, this._children);
22✔
330
      entity._parent = null;
22✔
331
      this.childrenRemoved$.notifyAll(entity);
22✔
332
    }
333
    return this;
22✔
334
  }
335

336
  /**
337
   * Removes all children from this entity
338
   */
339
  public removeAllChildren(): Entity {
340
    // Avoid modifying the array issue by walking backwards
341
    for (let i = this.children.length - 1; i >= 0; i--) {
1✔
342
      this.removeChild(this.children[i]);
6✔
343
    }
344
    return this;
1✔
345
  }
346

347
  /**
348
   * Returns a list of parent entities starting with the topmost parent. Includes the current entity.
349
   */
350
  public getAncestors(): Entity[] {
351
    const result: Entity[] = [this];
7,756✔
352
    let current = this.parent;
7,756✔
353
    while (current) {
7,756✔
354
      result.push(current);
2,052✔
355
      current = current.parent;
2,052✔
356
    }
357
    return result.reverse();
7,756✔
358
  }
359

360
  /**
361
   * Returns a list of all the entities that descend from this entity. Includes the current entity.
362
   */
363
  public getDescendants(): Entity[] {
364
    let result: Entity[] = [this];
1✔
365
    let queue: Entity[] = [this];
1✔
366
    while (queue.length > 0) {
1✔
367
      const curr = queue.pop();
3✔
368
      if (curr) {
3!
369
        queue = queue.concat(curr.children);
3✔
370
        result = result.concat(curr.children);
3✔
371
      }
372
    }
373
    return result;
1✔
374
  }
375

376
  /**
377
   * Creates a deep copy of the entity and a copy of all its components
378
   */
379
  public clone(): Entity {
380
    const newEntity = new Entity();
10✔
381
    for (const c of this.types) {
10✔
382
      const componentInstance = this.get(c);
14✔
383
      if (componentInstance) {
14!
384
        newEntity.addComponent(componentInstance.clone());
14✔
385
      }
386
    }
387
    for (const child of this.children) {
10✔
388
      newEntity.addChild(child.clone());
2✔
389
    }
390
    return newEntity;
10✔
391
  }
392

393
  /**
394
   * Adds a copy of all the components from another template entity as a "prefab"
395
   * @param templateEntity Entity to use as a template
396
   * @param force Force component replacement if it already exists on the target entity
397
   */
398
  public addTemplate(templateEntity: Entity, force: boolean = false): Entity {
3✔
399
    for (const c of templateEntity.getComponents()) {
3✔
400
      this.addComponent(c.clone(), force);
5✔
401
    }
402
    for (const child of templateEntity.children) {
3✔
403
      this.addChild(child.clone().addTemplate(child));
2✔
404
    }
405
    return this;
3✔
406
  }
407

408
  private _getClassHierarchyRoot(componentType: ComponentCtor): ComponentCtor {
409
    let current = componentType;
25,852✔
410
    let parent = Object.getPrototypeOf(current.prototype)?.constructor;
25,852!
411

412
    while (parent && parent !== Object && parent !== Component) {
25,852✔
413
      current = parent;
2✔
414
      parent = Object.getPrototypeOf(current.prototype)?.constructor;
2!
415
    }
416
    return current;
25,852✔
417
  }
418

419
  /**
420
   * Adds a component to the entity
421
   * @param component Component or Entity to add copy of components from
422
   * @param force Optionally overwrite any existing components of the same type
423
   */
424
  public addComponent<TComponent extends Component>(component: TComponent, force: boolean = false): Entity<TKnownComponents | TComponent> {
29,411✔
425
    // if component already exists, skip if not forced
426
    if (this.has(component.constructor as ComponentCtor)) {
29,436✔
427
      if (force) {
3,622✔
428
        // Remove existing component type if exists when forced
429
        this.removeComponent(component.constructor as ComponentCtor, true);
7✔
430
      } else {
431
        // early exit component exits
432
        return this as Entity<TKnownComponents | TComponent>;
3,615✔
433
      }
434
    }
435

436
    // TODO circular dependencies will be a problem
437
    if (component.dependencies && component.dependencies.length) {
25,821✔
438
      for (const ctor of component.dependencies) {
1,815✔
439
        this.addComponent(new ctor());
3,629✔
440
      }
441
    }
442

443
    component.owner = this;
25,821✔
444
    const rootComponent = this._getClassHierarchyRoot(component.constructor as ComponentCtor);
25,821✔
445
    this.components.set(rootComponent, component);
25,821✔
446
    this.components.set(component.constructor, component);
25,821✔
447
    this.componentValues.push(component);
25,821✔
448
    if (component.onAdd) {
25,821✔
449
      component.onAdd(this);
10,134✔
450
    }
451

452
    this.componentAdded$.notifyAll(component);
25,821✔
453
    return this as Entity<TKnownComponents | TComponent>;
25,821✔
454
  }
455

456
  /**
457
   * Removes a component from the entity, by default removals are deferred to the end of entity update to avoid consistency issues
458
   *
459
   * Components can be force removed with the `force` flag, the removal is not deferred and happens immediately
460
   * @param typeOrInstance
461
   * @param force
462
   */
463
  public removeComponent<TComponent extends Component>(
464
    typeOrInstance: ComponentCtor<TComponent> | TComponent,
465
    force = false
20✔
466
  ): Entity<Exclude<TKnownComponents, TComponent>> {
467
    let type: ComponentCtor<TComponent>;
468
    if (isComponentCtor(typeOrInstance)) {
51✔
469
      type = typeOrInstance;
50✔
470
    } else {
471
      type = typeOrInstance.constructor as ComponentCtor<TComponent>;
1✔
472
    }
473

474
    if (force) {
51✔
475
      const componentToRemove = this.components.get(type);
31✔
476
      if (componentToRemove) {
31!
477
        this.componentRemoved$.notifyAll(componentToRemove);
31✔
478
        componentToRemove.owner = undefined;
31✔
479
        if (componentToRemove.onRemove) {
31✔
480
          componentToRemove.onRemove(this);
8✔
481
        }
482
        const componentIndex = this.componentValues.indexOf(componentToRemove);
31✔
483
        if (componentIndex > -1) {
31!
484
          this.componentValues.splice(componentIndex, 1);
31✔
485
        }
486
      }
487

488
      const rootComponent = this._getClassHierarchyRoot(type);
31✔
489
      this.components.delete(rootComponent);
31✔
490
      this.components.delete(type); // remove after the notify to preserve typing
31✔
491
    } else {
492
      this._componentsToRemove.push(type);
20✔
493
    }
494

495
    return this as any;
51✔
496
  }
497

498
  public clearComponents() {
499
    const components = this.types;
2✔
500
    for (const c of components) {
2✔
501
      this.removeComponent(c);
14✔
502
    }
503
  }
504

505
  /**
506
   * @hidden
507
   * @internal
508
   */
509
  public processComponentRemoval() {
510
    for (const type of this._componentsToRemove) {
5,564✔
511
      this.removeComponent(type, true);
18✔
512
    }
513
    this._componentsToRemove.length = 0;
5,564✔
514
  }
515

516
  /**
517
   * Check if a component type exists
518
   * @param type
519
   */
520
  public has<TComponent extends Component>(type: ComponentCtor<TComponent>): boolean {
521
    return this.components.has(type);
53,247✔
522
  }
523

524
  private _isInitialized = false;
7,532✔
525
  private _isAdded = false;
7,532✔
526

527
  /**
528
   * Gets whether the actor is Initialized
529
   */
530
  public get isInitialized(): boolean {
531
    return this._isInitialized;
5,496✔
532
  }
533

534
  public get isAdded(): boolean {
535
    return this._isAdded;
6,134✔
536
  }
537

538
  /**
539
   * Initializes this entity, meant to be called by the Scene before first update not by users of Excalibur.
540
   *
541
   * It is not recommended that internal excalibur methods be overridden, do so at your own risk.
542
   * @internal
543
   */
544
  public _initialize(engine: Engine) {
545
    if (!this.isInitialized) {
3,323✔
546
      this.onInitialize(engine);
1,308✔
547
      this.events.emit('initialize', new InitializeEvent(engine, this));
1,308✔
548
      this._isInitialized = true;
1,308✔
549
    }
550
  }
551

552
  /**
553
   * Adds this Actor, meant to be called by the Scene when Actor is added.
554
   *
555
   * It is not recommended that internal excalibur methods be overridden, do so at your own risk.
556
   * @internal
557
   */
558
  public _add(engine: Engine) {
559
    if (!this.isAdded && this.isActive) {
3,067✔
560
      this.onAdd(engine);
1,269✔
561
      this.events.emit('add', new AddEvent(engine, this));
1,269✔
562
      this._isAdded = true;
1,269✔
563
    }
564
  }
565

566
  /**
567
   * Removes Actor, meant to be called by the Scene when Actor is added.
568
   *
569
   * It is not recommended that internal excalibur methods be overridden, do so at your own risk.
570
   * @internal
571
   */
572
  public _remove(engine: Engine) {
573
    if (this.isAdded && !this.isActive) {
3,067✔
574
      this.onRemove(engine);
5✔
575
      this.events.emit('remove', new RemoveEvent(engine, this));
5✔
576
      this._isAdded = false;
5✔
577
    }
578
  }
579

580
  /**
581
   * It is not recommended that internal excalibur methods be overridden, do so at your own risk.
582
   *
583
   * Internal _preupdate handler for {@apilink onPreUpdate} lifecycle event
584
   * @internal
585
   */
586
  public _preupdate(engine: Engine, elapsed: number): void {
587
    this.events.emit('preupdate', new PreUpdateEvent(engine, elapsed, this));
922✔
588
    this.onPreUpdate(engine, elapsed);
922✔
589
  }
590

591
  /**
592
   * It is not recommended that internal excalibur methods be overridden, do so at your own risk.
593
   *
594
   * Internal _preupdate handler for {@apilink onPostUpdate} lifecycle event
595
   * @internal
596
   */
597
  public _postupdate(engine: Engine, elapsed: number): void {
598
    this.events.emit('postupdate', new PostUpdateEvent(engine, elapsed, this));
922✔
599
    this.onPostUpdate(engine, elapsed);
922✔
600
  }
601

602
  /**
603
   * `onInitialize` is called before the first update of the entity. This method is meant to be
604
   * overridden.
605
   *
606
   * Synonymous with the event handler `.on('initialize', (evt) => {...})`
607
   */
608
  public onInitialize(engine: Engine): void {
609
    // Override me
610
  }
611

612
  /**
613
   * `onAdd` is called when Actor is added to scene. This method is meant to be
614
   * overridden.
615
   *
616
   * Synonymous with the event handler `.on('add', (evt) => {...})`
617
   */
618
  public onAdd(engine: Engine): void {
619
    // Override me
620
  }
621

622
  /**
623
   * `onRemove` is called when Actor is added to scene. This method is meant to be
624
   * overridden.
625
   *
626
   * Synonymous with the event handler `.on('remove', (evt) => {...})`
627
   */
628
  public onRemove(engine: Engine): void {
629
    // Override me
630
  }
631

632
  /**
633
   * Safe to override onPreUpdate lifecycle event handler. Synonymous with `.on('preupdate', (evt) =>{...})`
634
   *
635
   * `onPreUpdate` is called directly before an entity is updated.
636
   */
637
  public onPreUpdate(engine: Engine, elapsed: number): void {
638
    // Override me
639
  }
640

641
  /**
642
   * Safe to override onPostUpdate lifecycle event handler. Synonymous with `.on('postupdate', (evt) =>{...})`
643
   *
644
   * `onPostUpdate` is called directly after an entity is updated.
645
   */
646
  public onPostUpdate(engine: Engine, elapsed: number): void {
647
    // Override me
648
  }
649

650
  /**
651
   *
652
   * Entity update lifecycle, called internally
653
   * @internal
654
   * @param engine
655
   * @param elapsed
656
   */
657
  public update(engine: Engine, elapsed: number): void {
658
    this._initialize(engine);
920✔
659
    this._add(engine);
920✔
660
    this._preupdate(engine, elapsed);
920✔
661
    for (const child of this.children) {
920✔
662
      child.update(engine, elapsed);
×
663
    }
664
    this._postupdate(engine, elapsed);
920✔
665
    this._remove(engine);
920✔
666
  }
667

668
  public emit<TEventName extends EventKey<EntityEvents>>(eventName: TEventName, event: EntityEvents[TEventName]): void;
669
  public emit(eventName: string, event?: any): void;
670
  public emit<TEventName extends EventKey<EntityEvents> | string>(eventName: TEventName, event?: any): void {
671
    this.events.emit(eventName, event);
8✔
672
  }
673

674
  public on<TEventName extends EventKey<EntityEvents>>(eventName: TEventName, handler: Handler<EntityEvents[TEventName]>): Subscription;
675
  public on(eventName: string, handler: Handler<unknown>): Subscription;
676
  public on<TEventName extends EventKey<EntityEvents> | string>(eventName: TEventName, handler: Handler<any>): Subscription {
677
    return this.events.on(eventName, handler);
9✔
678
  }
679

680
  public once<TEventName extends EventKey<EntityEvents>>(eventName: TEventName, handler: Handler<EntityEvents[TEventName]>): Subscription;
681
  public once(eventName: string, handler: Handler<unknown>): Subscription;
682
  public once<TEventName extends EventKey<EntityEvents> | string>(eventName: TEventName, handler: Handler<any>): Subscription {
683
    return this.events.once(eventName, handler);
×
684
  }
685

686
  public off<TEventName extends EventKey<EntityEvents>>(eventName: TEventName, handler: Handler<EntityEvents[TEventName]>): void;
687
  public off(eventName: string, handler: Handler<unknown>): void;
688
  public off(eventName: string): void;
689
  public off<TEventName extends EventKey<EntityEvents> | string>(eventName: TEventName, handler?: Handler<any>): void {
690
    if (handler) {
×
691
      this.events.off(eventName, handler);
×
692
    } else {
693
      this.events.off(eventName);
×
694
    }
695
  }
696
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc