use a class for the fake signal instead

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2022-11-29 09:55:19 +01:00
parent de8a975911
commit 6042b95e65
2 changed files with 20 additions and 22 deletions
@@ -18,34 +18,32 @@ import { Context } from './types';
/**
* Since the root context can never abort, and since nobody is ever meant to
* dispatch events through it, we can use a static fake instance for
* efficiency.
* dispatch events through it, we can use a static fake instance for efficiency.
*
* The reason that this was initially made for the root context is that due to
* the way that we always chain contexts off of it, sometimes a huge number of
* listeners want to add themselves to something that effectively never can be
* aborted in the first place. This triggered warnings that the max listeners
* limit was exceeded.
*/
const fakeAbortController = new AbortController();
const fakeAbortSignal: AbortSignal = Object.freeze(
Object.assign(
{
aborted: false,
reason: undefined,
throwIfAborted() {},
onabort() {},
addEventListener() {},
removeEventListener() {},
dispatchEvent() {
return true;
},
},
fakeAbortController.signal,
),
);
class FakeAbortSignal implements AbortSignal {
readonly aborted = false;
readonly reason = undefined;
onabort() {}
throwIfAborted() {}
addEventListener() {}
removeEventListener() {}
dispatchEvent() {
return true;
}
}
/**
* An empty root context.
*/
export class RootContext implements Context {
readonly abortSignal = fakeAbortSignal;
readonly abortSignal = new FakeAbortSignal();
readonly deadline = undefined;
value<T = unknown>(_key: string): T | undefined {
return undefined;
}
@@ -20,7 +20,7 @@ import { delegateAbortController, nowPlus, sleep, validateId } from './util';
class KnexBuilder {
public build(client: string): Knex {
return knexFactory({ client });
return knexFactory({ client, useNullAsDefault: true });
}
}