fix: revalidate user token with no refresh page (#1842)

This commit is contained in:
Himself65
2023-04-07 17:51:51 -05:00
committed by GitHub
parent e50bf9fbfe
commit 20e56cc474
6 changed files with 83 additions and 41 deletions
+15 -3
View File
@@ -1,14 +1,19 @@
import { DebugLogger } from '@affine/debug';
import {
workspaceDetailSchema,
workspaceSchema,
} from '@affine/workspace/affine/api';
import { WebsocketClient } from '@affine/workspace/affine/channel';
import { storageChangeSlot } from '@affine/workspace/affine/login';
import { jotaiStore, jotaiWorkspacesAtom } from '@affine/workspace/atom';
import type { WorkspaceCRUD } from '@affine/workspace/type';
import type { WorkspaceFlavour } from '@affine/workspace/type';
import { assertExists } from '@blocksuite/global/utils';
import type { Disposable } from '@blocksuite/store';
import { z } from 'zod';
const logger = new DebugLogger('affine-sync');
const channelMessageSchema = z.object({
ws_list: z.array(workspaceSchema),
ws_details: z.record(workspaceDetailSchema),
@@ -28,7 +33,7 @@ export function createAffineGlobalChannel(
let client: WebsocketClient | null;
async function handleMessage(channelMessage: ChannelMessage) {
console.log('channelMessage', channelMessage);
logger.debug('channelMessage', channelMessage);
const parseResult = channelMessageSchema.safeParse(channelMessage);
if (!parseResult.success) {
console.error(
@@ -53,8 +58,8 @@ export function createAffineGlobalChannel(
}
}
}
return {
let dispose: Disposable | undefined = undefined;
const apis = {
connect: () => {
client = new WebsocketClient(
`${window.location.protocol === 'https:' ? 'wss' : 'ws'}://${
@@ -62,11 +67,18 @@ export function createAffineGlobalChannel(
}/api/global/sync`
);
client.connect(handleMessage);
dispose = storageChangeSlot.on(() => {
apis.disconnect();
apis.connect();
});
},
disconnect: () => {
assertExists(client, 'client is null');
client.disconnect();
dispose?.dispose();
client = null;
},
};
return apis;
}