2021-02-17 21:39:10 +00:00
|
|
|
import {MessageCommandErrorResult} from "./Messages";
|
|
|
|
import {clientServiceLogger} from "./Logging";
|
|
|
|
|
|
|
|
export type ActionResult<T> = {
|
|
|
|
unwrap() : T;
|
|
|
|
} & ({
|
|
|
|
status: "success",
|
|
|
|
result: T
|
|
|
|
} | {
|
|
|
|
status: "error",
|
|
|
|
result: MessageCommandErrorResult
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
export function createErrorResult<T>(result: MessageCommandErrorResult) : ActionResult<T> {
|
|
|
|
return {
|
|
|
|
status: "error",
|
|
|
|
result: result,
|
|
|
|
unwrap(): T {
|
|
|
|
clientServiceLogger.logError("Tried to unwrap an action which failed: %o", result);
|
|
|
|
throw "action failed with " + result.type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-20 16:46:16 +00:00
|
|
|
export function createResult() : ActionResult<void>;
|
|
|
|
export function createResult<T>(result: T) : ActionResult<T>;
|
|
|
|
|
|
|
|
export function createResult(result?) : ActionResult<any> {
|
2021-02-17 21:39:10 +00:00
|
|
|
return {
|
|
|
|
status: "success",
|
|
|
|
result: result,
|
2021-02-20 16:46:16 +00:00
|
|
|
unwrap() {
|
2021-02-17 21:39:10 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|