Add observables#260
Open
rflamand wants to merge 1 commit into
Open
Conversation
Collaborator
Author
|
@igoroctaviano @fedorov any chance any of you could take a look at this? |
396f539 to
2d6e721
Compare
Member
|
I will defer to @igoroctaviano! Igor, please add this to your queue! |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a new Observable primitive to the codebase to support DOM-independent reactive state, intended as a reusable building block for upcoming work (e.g., observing matchMedia color-gamut changes as discussed in #239).
Changes:
- Added
Observableclass implementing async (microtask) subscriber notifications on value replacement. - Added unit tests covering core behaviors: get/set, async notification, (un)subscribe semantics, and destroy cleanup.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/observable.js | Adds the Observable implementation (value storage, subscribe/unsubscribe, async notification, destroy). |
| src/observable.test.js | Adds Jest tests to validate observable behavior and lifecycle. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+46
to
+58
| setValue(value) { | ||
| const previous = this[_value] | ||
| if (value === previous) { | ||
| return | ||
| } | ||
| this[_value] = value | ||
| const callbacks = this[_subscribers].slice() | ||
| Promise.resolve().then(() => { | ||
| for (const cb of callbacks) { | ||
| cb(value, previous) | ||
| } | ||
| }) | ||
| } |
| expect(() => obs.unsubscribe(() => {})).not.toThrow() | ||
| }) | ||
|
|
||
| it('only removes one instance when the same callback is subscribed once', async () => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR introduces a new Observable class to the codebase, providing a self-contained, DOM-independent reactive value store. The Observable pattern enables efficient reactive programming within the application.
It's implemented as part of #239 , however, it should be usable by other components in the module.
The object is not used yet in this PR.
A follow up PR will use it (as explained in #239) to ensure that the color space gamut is dynamically updated.
Changes
Added: observable.js - A new Observable class that:
Added: observable.test.js - Comprehensive test suite covering:
Key Features
✅ Reactive state management with microtask-based notifications
✅ Strict equality (!==) comparison for change detection
✅ Encapsulated implementation using private symbols
✅ Full test coverage
Testing
All new functionality is covered by unit tests in observable.test.js.