diff --git a/src/__tests__/__snapshots__/exports.ts.snap b/src/__tests__/__snapshots__/exports.ts.snap index d41a41b752a..a952fea3d9a 100644 --- a/src/__tests__/__snapshots__/exports.ts.snap +++ b/src/__tests__/__snapshots__/exports.ts.snap @@ -352,6 +352,7 @@ Array [ "isReference", "iterateObserversSafely", "makeReference", + "makeUniqueId", "maybeDeepFreeze", "mergeDeep", "mergeDeepArray", diff --git a/src/core/QueryManager.ts b/src/core/QueryManager.ts index 8c8880b1282..24a3b64d0dc 100644 --- a/src/core/QueryManager.ts +++ b/src/core/QueryManager.ts @@ -19,6 +19,7 @@ import { isNonEmptyArray, Concast, ConcastSourcesIterable, + makeUniqueId, } from '../utilities'; import { ApolloError, isApolloError } from '../errors'; import { @@ -1414,10 +1415,3 @@ function getQueryIdsForQueryDescriptor( } return queryIds; } - -const prefixCounts: Record = Object.create(null); -function makeUniqueId(prefix: string) { - const count = prefixCounts[prefix] || 1; - prefixCounts[prefix] = count + 1; - return `${prefix}:${count}:${Math.random().toString(36).slice(2)}`; -} diff --git a/src/utilities/common/makeUniqueId.ts b/src/utilities/common/makeUniqueId.ts new file mode 100644 index 00000000000..b0e804bd7fb --- /dev/null +++ b/src/utilities/common/makeUniqueId.ts @@ -0,0 +1,9 @@ +const prefixCounts = new Map(); + +// These IDs won't be globally unique, but they will be unique within this +// process, thanks to the counter, and unguessable thanks to the random suffix. +export function makeUniqueId(prefix: string) { + const count = prefixCounts.get(prefix) || 1; + prefixCounts.set(prefix, count + 1); + return `${prefix}:${count}:${Math.random().toString(36).slice(2)}`; +} diff --git a/src/utilities/index.ts b/src/utilities/index.ts index 9657e447ac1..91540d00e2d 100644 --- a/src/utilities/index.ts +++ b/src/utilities/index.ts @@ -86,5 +86,6 @@ export * from './common/arrays'; export * from './common/errorHandling'; export * from './common/canUse'; export * from './common/compact'; +export * from './common/makeUniqueId'; export * from './types/IsStrictlyAny';