Skip to content

watchConnections

Action for watching changes to the Starknet wallet connections.

Import

import { watchConnections } from "starkweb/core";

Usage

index.ts
import { watchConnections } from "starkweb/core";
import { config } from "./config";
 
const unwatch = watchConnections(config, {
  onChange(connections, prevConnections) { 
    console.log('Connections changed:', connections); 
    console.log('Previous connections:', prevConnections); 
  }, 
});

Parameters

import { type WatchConnectionsParameters } from "starkweb/core";

onChange

(connections: GetConnectionsReturnType, prevConnections: GetConnectionsReturnType) => void

Callback function that is called when the wallet connections change. Receives the new connections and previous connections as arguments.

index.ts
import { watchConnections } from "starkweb/core";
import { config } from "./config";
 
const unwatch = watchConnections(config, {
  onChange(connections, prevConnections) { 
    console.log('Connections changed:', connections); 
    console.log('Previous connections:', prevConnections); 
  }, 
});

Return Type

import { type WatchConnectionsReturnType } from "starkweb/core";

() => void

Function to stop watching for connection changes.

Example

example.ts
import { watchConnections } from "starkweb/core";
import { config } from "./config";
 
// Start watching connection changes
const unwatch = watchConnections(config, {
  onChange(connections, prevConnections) { 
    const newConnections = connections.filter(
      connection => !prevConnections.find(prev => prev.id === connection.id)
    );
    if (newConnections.length > 0) {
      console.log('New connections:', newConnections);
    }
  }, 
});
 
// Later: stop watching
unwatch();