Merge pull request #875 from hcengineering/Change_token_new_client

Create new client when token has been changed
This commit is contained in:
Andrey Sobolev 2022-01-26 16:04:39 +07:00 committed by GitHub
commit 51c918f429
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -27,17 +27,23 @@ export { connect }
*/
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export default async () => {
let _token: string | undefined
let client: Client | undefined
return {
function: {
GetClient: async (token: string, endpoint: string): Promise<Client> => {
if (token !== _token && client !== undefined) {
await client.close()
client = undefined
}
if (client === undefined) {
client = await createClient((handler: TxHander) => {
const url = new URL(`/${token}`, endpoint)
console.log('connecting to', url.href)
return connect(url.href, handler)
})
_token = token
// Check if we had dev hook for client.
const hook = getMetadata(clientPlugin.metadata.ClientHook)