Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions packages/appkit/src/client/appkit-base-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2415,6 +2415,7 @@ export abstract class AppKitBaseClient {
callback: (newState: UseAppKitAccountReturn) => void,
namespace?: ChainNamespace
) {
const unsubArr: (() => void)[] = []
const updateVal = () => {
const account = this.getAccount(namespace)

Expand All @@ -2426,11 +2427,18 @@ export abstract class AppKitBaseClient {
}

if (namespace) {
ChainController.subscribeChainProp('accountState', updateVal, namespace)
const unsub = ChainController.subscribeChainProp('accountState', updateVal, namespace)
unsubArr.push(unsub)
} else {
ChainController.subscribe(updateVal)
const unsub = ChainController.subscribe(updateVal)
unsubArr.push(unsub)
}
const unsub = ConnectorController.subscribe(updateVal)
unsubArr.push(unsub)

return () => {
unsubArr.forEach(fn => fn())
}
ConnectorController.subscribe(updateVal)
}

public subscribeNetwork(
Expand Down Expand Up @@ -2463,13 +2471,13 @@ export abstract class AppKitBaseClient {
}

public subscribeShouldUpdateToAddress(callback: (newState?: string) => void) {
ChainController.subscribeChainProp('accountState', accountState =>
return ChainController.subscribeChainProp('accountState', accountState =>
callback(accountState?.shouldUpdateToAddress)
)
}

public subscribeCaipNetworkChange(callback: (newState?: CaipNetwork) => void) {
ChainController.subscribeKey('activeCaipNetwork', callback)
return ChainController.subscribeKey('activeCaipNetwork', callback)
}

public getState() {
Expand Down
31 changes: 31 additions & 0 deletions packages/appkit/tests/client/appkit-core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,35 @@ describe('AppKitCore', () => {
expect(blockchainApiSpy).not.toHaveBeenCalled()
})
})

describe('Subscribe methods return unsubscribe functions', () => {
beforeEach(() => {
appKit = new AppKit(mockOptions)
})

it('should return unsubscribe functions', () => {
expect(typeof appKit.subscribeProviders(() => {})).toBe('function')
expect(typeof appKit.subscribeTheme(() => {})).toBe('function')
expect(typeof appKit.subscribeConnections(() => {})).toBe('function')
expect(typeof appKit.subscribeAccount(() => {})).toBe('function')
expect(typeof appKit.subscribeNetwork(() => {})).toBe('function')
expect(typeof appKit.subscribeWalletInfo(() => {})).toBe('function')
expect(typeof appKit.subscribeShouldUpdateToAddress(() => {})).toBe('function')
expect(typeof appKit.subscribeCaipNetworkChange(() => {})).toBe('function')
expect(typeof appKit.subscribeState(() => {})).toBe('function')
expect(typeof appKit.subscribeRemoteFeatures(() => {})).toBe('function')
expect(typeof appKit.subscribeEvents(() => {})).toBe('function')
})

it('should call all unsubscribe functions in subscribeAccount', () => {
const mockUnsub = vi.fn()
vi.spyOn(ChainController, 'subscribe').mockReturnValue(mockUnsub)
vi.spyOn(ConnectorController, 'subscribe').mockReturnValue(mockUnsub)

const unsubscribe = appKit.subscribeAccount(() => {})
unsubscribe()

expect(mockUnsub).toHaveBeenCalledTimes(2)
})
})
})
Loading