Skip to content

Commit

Permalink
Move makeUniqueId from QueryManager.ts into @apollo/client/utilities.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed May 14, 2021
1 parent faa6ac6 commit 318039c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/exports.ts.snap
Expand Up @@ -352,6 +352,7 @@ Array [
"isReference",
"iterateObserversSafely",
"makeReference",
"makeUniqueId",
"maybeDeepFreeze",
"mergeDeep",
"mergeDeepArray",
Expand Down
8 changes: 1 addition & 7 deletions src/core/QueryManager.ts
Expand Up @@ -19,6 +19,7 @@ import {
isNonEmptyArray,
Concast,
ConcastSourcesIterable,
makeUniqueId,
} from '../utilities';
import { ApolloError, isApolloError } from '../errors';
import {
Expand Down Expand Up @@ -1414,10 +1415,3 @@ function getQueryIdsForQueryDescriptor<TStore>(
}
return queryIds;
}

const prefixCounts: Record<string, number> = 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)}`;
}
9 changes: 9 additions & 0 deletions src/utilities/common/makeUniqueId.ts
@@ -0,0 +1,9 @@
const prefixCounts = new Map<string, number>();

// 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)}`;
}
1 change: 1 addition & 0 deletions src/utilities/index.ts
Expand Up @@ -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';

0 comments on commit 318039c

Please sign in to comment.