import { AccountSubscriber } from './types' ; import { Program } from '@project-serum/anchor' ; import { PublicKey } from '@solana/web3.js' ; export class WebSocketAccountSubscriber < T > implements AccountSubscriber < T > { data ? : T ; accountName : string ; program : Program ; accountPublicKey : PublicKey ; onChange : ( data : T ) => void ; public constructor ( accountName , program , accountPublicKey ) { this . accountName = accountName ; this . program = program ; this . accountPublicKey = accountPublicKey ; } async subscribe ( onChange ) { this . onChange = onChange ; await this . fetch ( ) ; this . program . account [ this . accountName ] . subscribe ( this . accountPublicKey , this . program . provider . opts . commitment ) . on ( 'change' , async ( data ) => { this . data = data ; this . onChange ( data ) ; } ) ; } async fetch ( ) { const newData = ( await this . program . account [ this . accountName ] . fetch ( this . accountPublicKey ) ) as T ; if ( JSON . stringify ( newData ) !== JSON . stringify ( this . data ) ) { this . data = newData ; this . onChange ( this . data ) ; } } unsubscribe ( ) { return this . program . account [ this . accountName ] . unsubscribe ( this . accountPublicKey ) ; } }