Files
languageservices/actions-languageservice/src/context-providers/config.ts
T
2022-11-15 16:26:20 -08:00

27 lines
838 B
TypeScript

import {data} from "@github/actions-expressions";
import {Dictionary} from "@github/actions-expressions/data/dictionary";
import {ExpressionData, Pair} from "@github/actions-expressions/data/expressiondata";
export type ContextProviderConfig = {
getContext: (name: string) => Promise<data.Dictionary | undefined>;
};
/**
* DynamicDictionary is a dictionary that returns an empty DynamicDictionary (or other given type)
* for any key that is not present.
*/
export class DynamicDictionary<T extends ExpressionData = Dictionary> extends data.Dictionary {
constructor(pairs: Pair[], private creator: () => T = () => new data.Dictionary() as T) {
super(...pairs);
}
get(key: string): ExpressionData | undefined {
const value = super.get(key);
if (value) {
return value;
}
return this.creator();
}
}