Promises, promises

This commit is contained in:
Will Scullin 2022-06-04 20:54:48 -07:00
parent 40bc862389
commit a55baa3f25
No known key found for this signature in database
GPG Key ID: 26DCD1042C6638CD
2 changed files with 17 additions and 12 deletions

View File

@ -13,7 +13,7 @@ import { Screen } from './Screen';
import { Drives } from './Drives';
import { Slinky } from './Slinky';
import { ThunderClock } from './ThunderClock';
import { Ready } from './util/promises';
import { noAwait, Ready } from './util/promises';
import styles from './css/Apple2.module.css';
@ -55,17 +55,19 @@ export const Apple2 = (props: Apple2Props) => {
...props,
};
const apple2 = new Apple2Impl(options);
apple2.ready.then(() => {
setApple2(apple2);
const io = apple2.getIO();
const cpu = apple2.getCPU();
setIO(io);
setCPU(cpu);
return drivesReady.promise.then(() => {
noAwait((async () => {
try {
await apple2.ready;
setApple2(apple2);
setIO(apple2.getIO());
setCPU(apple2.getCPU());
await drivesReady.ready;
apple2.reset();
apple2.run();
});
}).catch((e) => setError(e));
} catch (e) {
setError(e);
}
}))();
}
}, [props, drivesReady]);

View File

@ -17,12 +17,15 @@ export function noAwait<F extends (...args: unknown[]) => Promise<unknown>>(f: F
*/
export class Ready {
onError: (value?: unknown) => void;
onReady: (value?: unknown) => void;
promise: Promise<unknown>;
ready: Promise<unknown>;
constructor() {
this.promise = new Promise((resolve, _reject) => {
this.ready = new Promise((resolve, reject) => {
this.onReady = resolve;
this.onError = reject;
}).catch(console.error);
}
}