Skip to content

Commit

Permalink
Ensure global singleton registries are truly global
Browse files Browse the repository at this point in the history
...even when the decoders bundle is doubly-included in production builds
for some reason.
  • Loading branch information
nvie committed Feb 20, 2025
1 parent c18bd5f commit 1353887
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## [Unreleased]

- Ensure the global singleton registries are truly global, even if somehow the bundler
might have included multiple copies of decoders (CJS and ESM) in the same bundle.
- Use `node16` module resolution setting (recommended setting for libraries that run in
both browsers and Node environments)

Expand Down
4 changes: 3 additions & 1 deletion src/core/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,9 @@ export function define<T>(fn: AcceptanceFn<T>): Decoder<T> {
}

/** @internal */
const _register = new WeakSet<Decoder<unknown>>();
const kDecoderRegistry = Symbol.for('decoders.kDecoderRegistry');
const _register: WeakSet<Decoder<unknown>> = ((globalThis as any)[kDecoderRegistry] ??=
new WeakSet());

/** @internal */
function brand<D extends Decoder<unknown>>(decoder: D): D {
Expand Down
4 changes: 3 additions & 1 deletion src/core/annotate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { isPojo } from '~/lib/utils.js';

const _register = new WeakSet<Annotation>();
const kAnnotationRegistry = Symbol.for('decoders.kAnnotationRegistry');
const _register: WeakSet<Annotation> = ((globalThis as any)[kAnnotationRegistry] ??=
new WeakSet());

export interface ObjectAnnotation {
readonly type: 'object';
Expand Down

0 comments on commit 1353887

Please sign in to comment.