Skip to content

Commit

Permalink
Fix TypeError: domain.enter is not a function
Browse files Browse the repository at this point in the history
An `EventEmitter` subclass must not use the `domain` property. Having a
`domain` present triggers Node's deprecated `domain` error handling
behavior, which, as it's not actually loaded, causes a `TypeError`.

Instead, delegate to a separate `EventEmitter` object, so our
`_OrmScope` can safely keep its `domain` property.

nodejs/node-v0.x-archive#3922
  • Loading branch information
Peeja committed Jul 8, 2024
1 parent 85cdf20 commit 2e4dcc6
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/orm/OrmDomain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export interface OrmUpdating extends ReadLatchable {
}

/** @internal */
export interface OrmScope extends EventEmitter {
export interface OrmScope {
/** The domain to which this scope belongs */
readonly domain: OrmDomain;
/** Destroys this scope including all cached subjects and event listeners */
Expand Down Expand Up @@ -350,19 +350,18 @@ export class OrmDomain implements MeldAppContext {
* Private friend class of OrmDomain for created scopes
* @internal
*/
class _OrmScope extends EventEmitter implements OrmScope {
class _OrmScope implements OrmScope {
_cache = new Map<Iri, OrmSubject | Promise<OrmSubject>>();
eventEmitter = new EventEmitter();

constructor(
readonly domain: OrmDomain
) {
super();
}
) {}

invalidate(): void {
this.removeAllListeners();
this.eventEmitter.removeAllListeners();
this._cache.clear();
this.emit('invalidated');
this.eventEmitter.emit('invalidated');
}

/** emitted when an ORM subject has been removed from the domain */
Expand All @@ -377,18 +376,19 @@ class _OrmScope extends EventEmitter implements OrmScope {
on(eventName: 'invalidated', listener: () => void): this;
/** @override */
on(eventName: string, listener: (...args: any[]) => void): this {
return super.on(eventName, listener);
this.eventEmitter.on(eventName, listener)
return this;
}

emitDeleted = (subject: OrmSubject) =>
this.emit('deleted', subject);
this.eventEmitter.emit('deleted', subject);

get hasCacheMissListeners() {
return this.listenerCount('cacheMiss') > 0;
return this.eventEmitter.listenerCount('cacheMiss') > 0;
}

// Manual emission to return promise
emitCacheMiss = (ref: GraphSubject, orm: OrmUpdating) =>
Promise.all(this.listeners('cacheMiss')
Promise.all(this.eventEmitter.listeners('cacheMiss')
.map((listener: CacheMissListener) => listener(ref, orm)));
}

0 comments on commit 2e4dcc6

Please sign in to comment.