1
0
Fork 0
mirror of https://code.forgejo.org/actions/cache.git synced 2025-03-14 03:51:28 +01:00
cache/src/stateProvider.ts

47 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-12-06 18:26:58 +00:00
import * as core from "@actions/core";
2022-12-12 07:28:12 +00:00
import { Outputs, State } from "./constants";
2022-12-06 18:26:58 +00:00
export interface IStateProvider {
2022-12-11 13:33:36 +00:00
setState(key: string, value: string): void;
2022-12-06 18:26:58 +00:00
getState(key: string): string;
getCacheState(): string | undefined;
}
class StateProviderBase implements IStateProvider {
getCacheState(): string | undefined {
const cacheKey = this.getState(State.CacheMatchedKey);
if (cacheKey) {
core.debug(`Cache state/key: ${cacheKey}`);
return cacheKey;
}
return undefined;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function
2022-12-11 13:33:36 +00:00
setState = (key: string, value: string) => {};
2022-12-06 18:26:58 +00:00
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getState = (key: string) => "";
}
export class StateProvider extends StateProviderBase {
setState = core.saveState;
getState = core.getState;
}
export class NullStateProvider extends StateProviderBase {
2022-12-12 07:28:12 +00:00
stateToOutputMap = new Map<string, string>([
2022-12-14 09:17:49 +00:00
[State.CacheMatchedKey, Outputs.CacheMatchedKey],
2022-12-12 07:28:12 +00:00
[State.CachePrimaryKey, Outputs.CachePrimaryKey]
]);
2022-12-11 13:33:36 +00:00
setState = (key: string, value: string) => {
2022-12-12 07:28:12 +00:00
core.setOutput(this.stateToOutputMap.get(key) as string, value);
2022-12-09 13:56:33 +00:00
};
2022-12-06 18:26:58 +00:00
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getState = (key: string) => "";
}