-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
[Try] Async stan #27155
[Try] Async stan #27155
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,25 +4,25 @@ | |
import { createAtomRegistry, createAtom } from '../'; | ||
|
||
describe( 'atoms', () => { | ||
it( 'should allow getting and setting atom values', () => { | ||
it( 'should allow getting and setting atom values', async () => { | ||
const count = createAtom( 1 ); | ||
const registry = createAtomRegistry(); | ||
expect( registry.get( count ) ).toEqual( 1 ); | ||
registry.set( count, 2 ); | ||
expect( registry.get( count ) ).toEqual( 2 ); | ||
expect( await registry.get( count ) ).toEqual( 1 ); | ||
await registry.set( count, 2 ); | ||
expect( await registry.get( count ) ).toEqual( 2 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need all the async/wait for the getters for sync atoms and subscriptions? Note that even if the behavior is sync, JS will make it async and only resolve on the next tick and this breaks the editor (and useSelect) in various ways. It is important that sync selectors stay sync and resolve right away. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, I’ll think some more about this on Monday. It seems like Recoil separates atoms and selectors, and only the latter may be async - maybe that would be a better way to go, our API is already pretty similar. I wonder how it handles errors thrown on the intersection of the two though, like propagation of the atom value to async selectors |
||
} ); | ||
|
||
it( 'should allow subscribing to atom changes', () => { | ||
it( 'should allow subscribing to atom changes', async () => { | ||
const count = createAtom( 1 ); | ||
const registry = createAtomRegistry(); | ||
const listener = jest.fn(); | ||
registry.subscribe( count, listener ); | ||
expect( registry.get( count ) ).toEqual( 1 ); | ||
registry.set( count, 2 ); | ||
await registry.subscribe( count, listener ); | ||
expect( await registry.get( count ) ).toEqual( 1 ); | ||
await registry.set( count, 2 ); | ||
expect( listener ).toHaveBeenCalledTimes( 1 ); | ||
expect( registry.get( count ) ).toEqual( 2 ); | ||
registry.set( count, 3 ); | ||
expect( await registry.get( count ) ).toEqual( 2 ); | ||
await registry.set( count, 3 ); | ||
expect( listener ).toHaveBeenCalledTimes( 2 ); | ||
expect( registry.get( count ) ).toEqual( 3 ); | ||
expect( await registry.get( count ) ).toEqual( 3 ); | ||
} ); | ||
} ); |
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.
Unsubscribe callback should be available right away so maybe this should return synchronously with an array of promise and unsubscribe callback?