Skip to content

Commit

Permalink
refactor(cache): Explicit types for different cache revisions (#17362)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Aug 23, 2022
1 parent 8f8f059 commit 9e2c81f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
12 changes: 8 additions & 4 deletions lib/util/cache/repository/common.ts
@@ -1,13 +1,17 @@
import is from '@sindresorhus/is';
import type { RepoCacheData, RepoCacheRecord } from './types';
import type {
RepoCacheRecordV10,
RepoCacheRecordV11,
RepoCacheRecordV12,
} from './types';

// Increment this whenever there could be incompatibilities between old and new cache structure
export const CACHE_REVISION = 12;

export function isValidRev10(
input: unknown,
repo?: string
): input is RepoCacheData & { repository?: string; revision?: number } {
): input is RepoCacheRecordV10 {
return (
is.plainObject(input) &&
is.safeInteger(input.revision) &&
Expand All @@ -20,7 +24,7 @@ export function isValidRev10(
export function isValidRev11(
input: unknown,
repo?: string
): input is { repository: string; revision: number; data: RepoCacheData } {
): input is RepoCacheRecordV11 {
return (
is.plainObject(input) &&
is.safeInteger(input.revision) &&
Expand All @@ -34,7 +38,7 @@ export function isValidRev11(
export function isValidRev12(
input: unknown,
repo?: string
): input is RepoCacheRecord {
): input is RepoCacheRecordV12 {
return (
is.plainObject(input) &&
is.safeInteger(input.revision) &&
Expand Down
15 changes: 14 additions & 1 deletion lib/util/cache/repository/types.ts
Expand Up @@ -47,13 +47,26 @@ export interface RepoCacheData {
prComments?: Record<number, Record<string, string>>;
}

export interface RepoCacheRecord {
export interface RepoCacheRecordV10 extends RepoCacheData {
repository?: string;
revision?: number;
}

export interface RepoCacheRecordV11 {
repository: string;
revision: number;
data: RepoCacheData;
}

export interface RepoCacheRecordV12 {
repository: string;
revision: number;
payload: string;
hash: string;
}

export type RepoCacheRecord = RepoCacheRecordV12;

export interface RepoCache {
load(): Promise<void>;
save(): Promise<void>;
Expand Down

0 comments on commit 9e2c81f

Please sign in to comment.