anastasis-webui: return promises in anastasis reducer hook

This commit is contained in:
Florian Dold 2021-11-04 17:43:24 +01:00
parent c2d8dd76c3
commit 4ebeb00243
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B

View File

@ -1,5 +1,12 @@
import { TalerErrorCode } from "@gnu-taler/taler-util"; import { TalerErrorCode } from "@gnu-taler/taler-util";
import { BackupStates, getBackupStartState, getRecoveryStartState, RecoveryStates, reduceAction, ReducerState } from "anastasis-core"; import {
BackupStates,
getBackupStartState,
getRecoveryStartState,
RecoveryStates,
reduceAction,
ReducerState,
} from "anastasis-core";
import { useState } from "preact/hooks"; import { useState } from "preact/hooks";
const reducerBaseUrl = "http://localhost:5000/"; const reducerBaseUrl = "http://localhost:5000/";
@ -98,13 +105,15 @@ export interface AnastasisReducerApi {
startBackup: () => void; startBackup: () => void;
startRecover: () => void; startRecover: () => void;
reset: () => void; reset: () => void;
back: () => void; back: () => Promise<void>;
transition(action: string, args: any): void; transition(action: string, args: any): Promise<void>;
/** /**
* Run multiple reducer steps in a transaction without * Run multiple reducer steps in a transaction without
* affecting the UI-visible transition state in-between. * affecting the UI-visible transition state in-between.
*/ */
runTransaction(f: (h: ReducerTransactionHandle) => Promise<void>): void; runTransaction(
f: (h: ReducerTransactionHandle) => Promise<void>,
): Promise<void>;
} }
function storageGet(key: string): string | null { function storageGet(key: string): string | null {
@ -222,9 +231,9 @@ export function useAnastasisReducer(): AnastasisReducerApi {
} }
}, },
transition(action: string, args: any) { transition(action: string, args: any) {
doTransition(action, args); return doTransition(action, args);
}, },
back() { async back() {
const reducerState = anastasisState.reducerState; const reducerState = anastasisState.reducerState;
if (!reducerState) { if (!reducerState) {
return; return;
@ -239,7 +248,7 @@ export function useAnastasisReducer(): AnastasisReducerApi {
reducerState: undefined, reducerState: undefined,
}); });
} else { } else {
doTransition("back", {}); await doTransition("back", {});
} }
}, },
dismissError() { dismissError() {
@ -252,30 +261,27 @@ export function useAnastasisReducer(): AnastasisReducerApi {
reducerState: undefined, reducerState: undefined,
}); });
}, },
runTransaction(f) { async runTransaction(f) {
async function run() { const txHandle = new ReducerTxImpl(anastasisState.reducerState!);
const txHandle = new ReducerTxImpl(anastasisState.reducerState!); try {
try { await f(txHandle);
await f(txHandle); } catch (e) {
} catch (e) { console.log("exception during reducer transaction", e);
console.log("exception during reducer transaction", e); }
} const s = txHandle.transactionState;
const s = txHandle.transactionState; console.log("transaction finished, new state", s);
console.log("transaction finished, new state", s); if (s.code !== undefined) {
if (s.code !== undefined) { setAnastasisState({
setAnastasisState({ ...anastasisState,
...anastasisState, currentError: txHandle.transactionState,
currentError: txHandle.transactionState, });
}); } else {
} else { setAnastasisState({
setAnastasisState({ ...anastasisState,
...anastasisState, reducerState: txHandle.transactionState,
reducerState: txHandle.transactionState, currentError: undefined,
currentError: undefined, });
});
}
} }
run();
}, },
}; };
} }