Skip to content

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

Merged
merged 1 commit into from
Apr 24, 2024
Merged

Conversation

Parkreiner
Copy link
Member

@Parkreiner Parkreiner commented Apr 23, 2024

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.

@Parkreiner Parkreiner requested a review from code-asher April 23, 2024 13:32
@Parkreiner Parkreiner self-assigned this Apr 23, 2024
Comment on lines +136 to +137
const snapshotBinding = this.activeSnapshot;
this.subscriptions.forEach(cb => cb(snapshotBinding));
Copy link
Member Author

@Parkreiner Parkreiner Apr 23, 2024

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

Comment on lines +45 to +46
// for these kinds of comparisons, but it itself has an edge case
// with -0 and +0. Still need === to handle that comparison
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JavaScript is wild lol

Copy link
Member Author

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

Comment on lines +136 to +137
const snapshotBinding = this.activeSnapshot;
this.subscriptions.forEach(cb => cb(snapshotBinding));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

@Parkreiner Parkreiner merged commit fc86b8c into main Apr 24, 2024
2 checks passed
@Parkreiner Parkreiner deleted the mes/api-factories-01 branch April 24, 2024 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants