Created
August 7, 2019 13:59
-
-
Save savagematt/1ce198dd0f3f836276d20cc1f194111c to your computer and use it in GitHub Desktop.
scenario vars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface Vars<T> { | |
/** | |
* Returns a step that assigns the result of step to key k. | |
* | |
* Always returns void, so if the step returned a promise set() won't block. | |
* | |
* You can: | |
* | |
* // will NOT block on stepThatCompletesEventually return value | |
* set('value',stepThatCompletesEventually), | |
* doSomeOtherStuffThatCausesThePromiseToResolve(), | |
* // will block on stepThatCompletesEventually return value | |
* get('value'), | |
* | |
* Values of keys are immutable. | |
*/ | |
set<C, K extends keyof T>(k: K, step: Step<C, T[K]>): Step<C, void>; | |
/** | |
* Returns a step that gets the value of key k, waiting for it to resolve | |
* if it is a promise. | |
* | |
* Throws exception if k has not been set. | |
*/ | |
get<C, K extends keyof T>(k: K): Step<C, Promise<T[K]>>; | |
} | |
export function vars<T, C = any>(): Vars<T> { | |
const values: Partial<T> = {}; | |
return { | |
set: <C, K extends keyof T>(k: K, step: Step<C, T[K]>): Step<C, void> => { | |
return (c: C): void => { | |
if (k in values) | |
throw new Error(`'${k} already set to ${JSON.stringify(values[k])}'`); | |
values[k] = execute(c, step); | |
return | |
} | |
}, | |
get: <C, K extends keyof T>(k: K): Step<C, Promise<T[K]>> => { | |
return async (c: C): Promise<T[K]> => { | |
if (!(k in values)) | |
throw new Error(`'${k} has not been set`); | |
return await values[k] as T[K]; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also good for storing values that aren't known at runtime: