Part 1 of the ongoing API factory changes for #107
Decided to start breaking these up to make the PRs easier to digest, and start getting the ball rolling faster.
Changes made
Added StateSnapshotManager class, which makes it easy to sync external state with React's useSyncExternalStore hook.
This will be used for both CoderClient and CoderTokenAuth
Notes
As you'll see with some of the later PRs, this definitely isn't a perfect solution. I'd say there's three main issues:
You can only replace a snapshot with a fully-formed, new snapshot. You can't pass in partial updates
Any other class that uses this one likely has to have some duplicated state, and manually has to make sure the mutable class state and the immutable snapshot state stay in sync, which can make other code more fragile
It's not easy to base one snapshot off of another, where if Snapshot A wants to be aware of a property change in Snapshot B, you have to do some slightly funky subscription juggling
Once the PR is done, I'll start tinkering with things like Proxy objects to start figuring out if there's a way to make the class less error-prone. My hope is that I can make it so that the manager exposes a "mutable snapshot proxy", and when you mutate a property on it, that will automatically run logic to determine if the snapshot has meaningfully changed, and also notify subscriptions
My thinking is that we get the API code working (even if it's less than ideal), get it verified with tests, and then use the tests to guide refactoring.
Part 1 of the ongoing API factory changes for #107 Decided to start breaking these up to make the PRs easier to digest, and start getting the ball rolling faster.
Changes made
StateSnapshotManager
class, which makes it easy to sync external state with React'suseSyncExternalStore
hook.CoderClient
andCoderTokenAuth
Notes
Once the PR is done, I'll start tinkering with things like Proxy objects to start figuring out if there's a way to make the class less error-prone. My hope is that I can make it so that the manager exposes a "mutable snapshot proxy", and when you mutate a property on it, that will automatically run logic to determine if the snapshot has meaningfully changed, and also notify subscriptions
My thinking is that we get the API code working (even if it's less than ideal), get it verified with tests, and then use the tests to guide refactoring.