Skip to content

Commit

Permalink
Only check for ambiguous classes in development (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skn0tt authored Feb 2, 2021
1 parent efbacad commit 8e91154
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ test('class registry', () => {

expect(() => registry.register(Car)).not.toThrow();

expect(() => registry.register(class Car {})).toThrow(
'Ambiguous class, provide a unique identifier.'
const warnSpy = jest.spyOn(console, 'warn');

registry.register(class Car {});
expect(warnSpy).toHaveBeenCalledWith(
'Ambiguous class "Car", provide a unique identifier.'
);

registry.register(class Car {}, 'car2');
Expand Down
9 changes: 7 additions & 2 deletions src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ export class Registry<T> {
identifier = this.generateIdentifier(value);
}

if (this.kv.getByKey(identifier)) {
throw new Error('Ambiguous class, provide a unique identifier.');
if (process.env.NODE_ENV !== 'production') {
const alreadyRegistered = this.kv.getByKey(identifier);
if (alreadyRegistered && alreadyRegistered !== value) {
console.warn(
`Ambiguous class "${identifier}", provide a unique identifier.`
);
}
}

this.kv.set(identifier, value);
Expand Down

0 comments on commit 8e91154

Please sign in to comment.