watchChains
Action for watching changes to the configured Starknet chains.
Import
import { watchChains } from "starkweb/core";Usage
index.ts
import { watchChains } from "starkweb/core";
import { config } from "./config";
const unwatch = watchChains(config, {
onChange(chains, prevChains) {
console.log('Chains changed:', chains);
console.log('Previous chains:', prevChains);
},
});Parameters
import { type WatchChainsParameters } from "starkweb/core";onChange
(chains: GetChainsReturnType, prevChains: GetChainsReturnType) => void
Callback function that is called when the chain configuration changes. Receives the new chains and previous chains as arguments.
index.ts
import { watchChains } from "starkweb/core";
import { config } from "./config";
const unwatch = watchChains(config, {
onChange(chains, prevChains) {
console.log('Chains changed:', chains);
console.log('Previous chains:', prevChains);
},
});Return Type
import { type WatchChainsReturnType } from "starkweb/core";() => void
Function to stop watching for chain configuration changes.
Example
example.ts
import { watchChains } from "starkweb/core";
import { mainnet, testnet } from "starkweb/chains";
import { config } from "./config";
// Start watching chain configuration changes
const unwatch = watchChains(config, {
onChange(chains, prevChains) {
const addedChains = chains.filter(
chain => !prevChains.find(prev => prev.chain_id === chain.chain_id)
);
console.log('Added chains:', addedChains);
},
});
// Later: stop watching
unwatch();