Public
Back
Version 18
7/22/2023
let safeCounter = async () => {
// 1. try to acquire a lock
let { lease } = await stevekrouse.dlock();
if (!lease) {
// 2. wait a second and try to acquire it again
await stevekrouse.sleep(1000);
return me.safeCounter();
}
// 3. i have a lock!
// 4. get most up to date state
let { state } = await stevekrouse.runValAPI(
""stevekrouse.threadsafeStateEx"
); // TODO type issue
// 5. i update the state
let newState = state + 1;
await api(stevekrouse.updateThreadsafeStateEx, {
lease,
state: newState,
});
// 6. i release the lock
await stevekrouse.dlock({ release: true });
return newState;
};
Updated: October 23, 2023