Skip to content

Commit

Permalink
fix: optimize useAtomSelector for React 19 (#106)
Browse files Browse the repository at this point in the history
@affects atoms, react
  • Loading branch information
bowheart committed Sep 17, 2024
1 parent 2a6b370 commit dda8fb3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
1 change: 1 addition & 0 deletions packages/atoms/src/classes/Selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ export class Selectors {
_graph.removeDependencies(id)
_graph.removeNode(id)
delete this._items[id]
this._refBaseKeys.delete(cache.selectorRef)
cache.isDestroyed = true
// don't delete the ref from this._refBaseKeys; this selector cache isn't
// necessarily the only one using it (if the selector takes params). Just
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ exports[`selection same-name selectors share the namespace when destroyed and re
{
"1": {
"dependencies": Map {
"@@selector-common-name-0" => true,
"@@selector-common-name-4" => true,
},
"dependents": Map {
"no-4" => {
"no-5" => {
"callback": undefined,
"createdAt": 123456789,
"flags": 3,
Expand All @@ -136,12 +136,12 @@ exports[`selection same-name selectors share the namespace when destroyed and re
"refCount": 1,
"weight": 3,
},
"@@selector-common-name-0": {
"@@selector-common-name-2": {
"dependencies": Map {
"root" => true,
},
"dependents": Map {
"1" => {
"2" => {
"callback": undefined,
"createdAt": 123456789,
"flags": 0,
Expand All @@ -152,12 +152,12 @@ exports[`selection same-name selectors share the namespace when destroyed and re
"refCount": 1,
"weight": 2,
},
"@@selector-common-name-2": {
"@@selector-common-name-4": {
"dependencies": Map {
"root" => true,
},
"dependents": Map {
"2" => {
"1" => {
"callback": undefined,
"createdAt": 123456789,
"flags": 0,
Expand All @@ -177,7 +177,7 @@ exports[`selection same-name selectors share the namespace when destroyed and re
"flags": 0,
"operation": "get",
},
"@@selector-common-name-0" => {
"@@selector-common-name-4" => {
"callback": undefined,
"createdAt": 123456789,
"flags": 0,
Expand All @@ -195,10 +195,10 @@ exports[`selection same-name selectors share the namespace when destroyed and re
{
"1": {
"dependencies": Map {
"@@selector-common-name-0" => true,
"@@selector-common-name-4" => true,
},
"dependents": Map {
"no-4" => {
"no-5" => {
"callback": undefined,
"createdAt": 123456789,
"flags": 3,
Expand All @@ -209,7 +209,7 @@ exports[`selection same-name selectors share the namespace when destroyed and re
"refCount": 1,
"weight": 3,
},
"@@selector-common-name-0": {
"@@selector-common-name-4": {
"dependencies": Map {
"root" => true,
},
Expand All @@ -228,7 +228,7 @@ exports[`selection same-name selectors share the namespace when destroyed and re
"root": {
"dependencies": Map {},
"dependents": Map {
"@@selector-common-name-0" => {
"@@selector-common-name-4" => {
"callback": undefined,
"createdAt": 123456789,
"flags": 0,
Expand Down
12 changes: 5 additions & 7 deletions packages/react/test/units/Selectors.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ describe('the Selectors class', () => {
ecosystem.selectors.destroyCache(cache2b) // destroys only selector2

expect(ecosystem.selectors._items).toEqual({
// id # is still 1 'cause the Selector class's `_refBaseKeys` still holds
// the cached key despite `cache2b`'s destruction above
'@@selector-selector1-1': expect.any(Object),
// id 3 'cause cache1b is the 4th id'd item created
'@@selector-selector1-3': expect.any(Object),
})

cleanup()
Expand Down Expand Up @@ -116,10 +115,9 @@ describe('the Selectors class', () => {
expect(cache1.isDestroyed).toBe(true)
expect(cache2.isDestroyed).toBe(true)
expect(ecosystem.selectors.dehydrate()).toEqual({
// ids 2 & 1 - the refs are still cached in the Selector class's
// `_refBaseKeys`
'@@selector-selector1-2': 'ab',
'@@selector-selector2-1': 'abc',
// 5 ids have been created at this point
'@@selector-selector1-4': 'ab',
'@@selector-selector2-3': 'abc',
'@@selector-selector3-0': 'abcd',
})

Expand Down

0 comments on commit dda8fb3

Please sign in to comment.