-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed getChainIdDefault() race condition (#1648)
- Loading branch information
Showing
7 changed files
with
91 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@onflow/fcl": patch | ||
--- | ||
|
||
Fixed `setChainIdDefault()` race condition, instead run function on accessNode.api value changes |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {config} from "@onflow/config" | ||
import {setChainIdDefault} from "./get-chain-id" | ||
|
||
/** | ||
* @description | ||
* Watches the config for changes to access node and updates the chain id accordingly | ||
* | ||
* @returns {Function} A function that unsubscribes the listener | ||
* | ||
*/ | ||
export function watchForChainIdChanges() { | ||
return config.subscribe( | ||
function configSubscriber(config) { | ||
const nextAccessNode = config?.["accessNode.api"] | ||
if (this.prevAccessNode !== nextAccessNode) { | ||
setChainIdDefault() | ||
} | ||
this.prevAccessNode = nextAccessNode | ||
}.bind({}) | ||
) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import {watchForChainIdChanges} from "./chain-id-watcher" | ||
import {config} from "@onflow/config" | ||
import * as chainIdUtils from "./get-chain-id" | ||
|
||
describe("chain-id-watcher", () => { | ||
let unsubscribe | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks() | ||
unsubscribe && unsubscribe() | ||
}) | ||
|
||
test("flow.network.default is correctly set on first call", async () => { | ||
await config.overload( | ||
{"accessNode.api": "https://example.com"}, | ||
async () => { | ||
// Mock the setChainIdDefault function | ||
const spy = jest.spyOn(chainIdUtils, "setChainIdDefault") | ||
spy.mockImplementation(() => {}) | ||
|
||
// Start watching for changes | ||
unsubscribe = watchForChainIdChanges() | ||
|
||
// Wait for microtask queue to flush | ||
await new Promise(resolve => setTimeout(resolve, 0)) | ||
|
||
// Expect only one call at initial setup | ||
expect(chainIdUtils.setChainIdDefault).toHaveBeenCalledTimes(1) | ||
} | ||
) | ||
}) | ||
|
||
test("flow.network.default is correctly set when changed later", async () => { | ||
await config.overload({}, async () => { | ||
// Mock the setChainIdDefault function | ||
const spy = jest.spyOn(chainIdUtils, "setChainIdDefault") | ||
spy.mockImplementation(() => {}) | ||
|
||
// Start watching for changes | ||
unsubscribe = watchForChainIdChanges() | ||
|
||
// Wait for microtask queue to flush | ||
await new Promise(resolve => setTimeout(resolve, 0)) | ||
|
||
config.put("accessNode.api", "https://example.com") | ||
|
||
// Wait for microtask queue to flush | ||
await new Promise(resolve => setTimeout(resolve, 0)) | ||
|
||
// Expect two calls since we changed the access node and there is an initial call | ||
expect(chainIdUtils.setChainIdDefault).toHaveBeenCalledTimes(1) | ||
}) | ||
}) | ||
}) |
This file contains 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
2 changes: 1 addition & 1 deletion
2
packages/fcl/src/utils/getChainId.test.js → packages/fcl/src/utils/get-chain-id.test.js
This file contains 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
This file contains 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