Skip to content

Commit 92a342e

Browse files
authored
fix: Use Symbol.for to register a single instance of all hooks (#110)
1 parent 05bde46 commit 92a342e

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

main/hooks/src/base.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { AsyncMiddleware } from './compose.ts';
22
import { copyProperties } from './utils.ts';
33

4-
export const HOOKS: string = Symbol('@feathersjs/hooks') as any;
4+
export const HOOKS: string = Symbol.for('@feathersjs/hooks') as any;
55

66
export type HookContextData = { [key: string]: any };
77

@@ -46,7 +46,7 @@ export type HookContextConstructor = new (data?: {
4646
export type HookDefaultsInitializer = (
4747
self?: any,
4848
args?: any[],
49-
context?: HookContext,
49+
context?: HookContext
5050
) => HookContextData;
5151

5252
export class HookManager {
@@ -134,9 +134,12 @@ export class HookManager {
134134
getDefaults(
135135
self: any,
136136
args: any[],
137-
context: HookContext,
137+
context: HookContext
138138
): HookContextData | null {
139-
const defaults = typeof this._defaults === 'function' ? this._defaults(self, args, context) : null;
139+
const defaults =
140+
typeof this._defaults === 'function'
141+
? this._defaults(self, args, context)
142+
: null;
140143
const previous = this._parent?.getDefaults(self, args, context);
141144

142145
if (previous && defaults) {
@@ -147,7 +150,7 @@ export class HookManager {
147150
}
148151

149152
getContextClass(
150-
Base: HookContextConstructor = BaseHookContext,
153+
Base: HookContextConstructor = BaseHookContext
151154
): HookContextConstructor {
152155
const ContextClass = class ContextClass extends Base {
153156
constructor(data: any) {
@@ -161,7 +164,7 @@ export class HookManager {
161164
params.forEach((name, index) => {
162165
if (props?.[name] !== undefined) {
163166
throw new Error(
164-
`Hooks can not have a property and param named '${name}'. Use .defaults instead.`,
167+
`Hooks can not have a property and param named '${name}'. Use .defaults instead.`
165168
);
166169
}
167170

@@ -172,7 +175,7 @@ export class HookManager {
172175
},
173176
set(value: any) {
174177
this.arguments[index] = value;
175-
},
178+
}
176179
});
177180
});
178181
}
@@ -185,7 +188,9 @@ export class HookManager {
185188
}
186189

187190
initializeContext(self: any, args: any[], context: HookContext): HookContext {
188-
const ctx = this._parent ? this._parent.initializeContext(self, args, context) : context;
191+
const ctx = this._parent
192+
? this._parent.initializeContext(self, args, context)
193+
: context;
189194
const defaults = this.getDefaults(self, args, ctx);
190195

191196
if (self) {
@@ -213,7 +218,9 @@ export function convertOptions(options: HookOptions = null) {
213218
return new HookManager();
214219
}
215220

216-
return Array.isArray(options) ? new HookManager().middleware(options) : options;
221+
return Array.isArray(options)
222+
? new HookManager().middleware(options)
223+
: options;
217224
}
218225

219226
export function getManager(target: any): HookManager | null {

0 commit comments

Comments
 (0)