-
Notifications
You must be signed in to change notification settings - Fork 5
chore: add StateSnapshotManager class #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
const snapshotBinding = this.activeSnapshot; | ||
this.subscriptions.forEach(cb => cb(snapshotBinding)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to make sure that we store the starting snapshot in a separate variable, because the value of this.activeSnapshot
is allowed to be replaced at any time
Otherwise, it's possible for the subscriptions to start getting processed, the snapshot to change halfway through, and then the subscriptions start notifying you about the updated snapshot, not the original one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense.
// for these kinds of comparisons, but it itself has an edge case | ||
// with -0 and +0. Still need === to handle that comparison |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JavaScript is wild lol
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I usually call Object.is
the "quadruple equals" operator, but the flipside of that is that sometimes it gets too specific for its own good lol
const snapshotBinding = this.activeSnapshot; | ||
this.subscriptions.forEach(cb => cb(snapshotBinding)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense.
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.