I want to be sure that a block of code doesn't run concurrently. The async-Mutex library doesn't seem to be working for me though. Here's a minimal replication:
/* eslint-disable */
import React, { useState, useEffect } from "react";
var Mutex = require("async-mutex").Mutex;
const Timer = () => {
const [isActive, setIsActive] = useState(false);
const mutex = new Mutex();
useEffect(() => {
mutex.runExclusive(async function () {
console.log("starting", isActive);
await new Promise((resolve) => setTimeout(resolve, 1000));
console.log("ending", isActive);
});
}, [isActive]);
return (
<div className="app">
<button onClick={() => setIsActive((prev) => !prev)} type="button">
{isActive ? "Active" : "Inactive"}
</button>
</div>
);
};
export { Timer as default };
If you click the button twice quickly, you can see that it will print
starting
true
starting
false
starting
true
ending
true
ending
false
You can see that these are interleaved.
How can I force this to not run concurrently?