Skip to content
Closed
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 @@ -638,7 +638,7 @@
await this.disconnectConnector(params.namespace, params.id)
},
disconnect: async params => {
const { id: connectorIdParam, chainNamespace, initialDisconnect } = params || {}

Check warning on line 641 in packages/appkit/src/client/appkit-base-client.ts

View workflow job for this annotation

GitHub Actions / code_style (lint)

Variable name `initialDisconnect` must have one of the following prefixes: is, has, can, should, will, did

const namespace = chainNamespace || ChainController.state.activeChain
const namespaceConnectorId = ConnectorController.getConnectorId(namespace)
Expand Down Expand Up @@ -955,7 +955,7 @@
if (params.type === UtilConstantsUtil.CONNECTOR_TYPE_AUTH) {
const authNamespaces = ConstantsUtil.AUTH_CONNECTOR_SUPPORTED_CHAINS
const hasConnectedAuthNamespace = authNamespaces.some(
namespace =>

Check warning on line 958 in packages/appkit/src/client/appkit-base-client.ts

View workflow job for this annotation

GitHub Actions / code_style (lint)

'namespace' is already declared in the upper scope on line 950 column 11
ConnectorController.getConnectorId(namespace) === ConstantsUtil.CONNECTOR_ID.AUTH
)

Expand Down Expand Up @@ -1403,7 +1403,7 @@
chainNamespace: ChainNamespace
closeModal?: boolean
}) {
const { chainNamespace, closeModal } = options || {}

Check warning on line 1406 in packages/appkit/src/client/appkit-base-client.ts

View workflow job for this annotation

GitHub Actions / code_style (lint)

Variable name `closeModal` must have one of the following prefixes: is, has, can, should, will, did

ChainController.resetAccount(chainNamespace)
ChainController.resetNetwork(chainNamespace)
Expand Down Expand Up @@ -1871,7 +1871,7 @@
onConnect: accounts => {
const { address } = CoreHelperUtil.getAccount(accounts[0])

for (const namespace of this.chainNamespaces) {

Check warning on line 1874 in packages/appkit/src/client/appkit-base-client.ts

View workflow job for this annotation

GitHub Actions / code_style (lint)

'namespace' is already declared in the upper scope on line 1864 column 36
StorageUtil.removeDisconnectedConnectorId(
ConstantsUtil.CONNECTOR_ID.WALLET_CONNECT,
namespace
Expand Down Expand Up @@ -2452,6 +2452,7 @@
callback: (newState: UseAppKitAccountReturn) => void,
namespace?: ChainNamespace
) {
const unsubArr: (() => void)[] = []
const updateVal = () => {
const account = this.getAccount(namespace)

Expand All @@ -2463,11 +2464,18 @@
}

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 @@ -2500,13 +2508,13 @@
}

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