mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-17 22:37:04 +08:00
fix: delay setAom on rootWorkspacesMetadataAtom (#2271)
This commit is contained in:
@@ -40,17 +40,24 @@ rootWorkspacesMetadataAtom.onMount = setAtom => {
|
|||||||
}).filter((ids): ids is RootWorkspaceMetadata => !!ids);
|
}).filter((ids): ids is RootWorkspaceMetadata => !!ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
setAtom(metadata => {
|
const abortController = new AbortController();
|
||||||
if (metadata.length === 0) {
|
|
||||||
const newMetadata = createFirst();
|
// next tick to make sure the hydration is correct
|
||||||
logger.info('create first workspace', newMetadata);
|
const id = setTimeout(() => {
|
||||||
return newMetadata;
|
setAtom(metadata => {
|
||||||
}
|
if (abortController.signal.aborted) return metadata;
|
||||||
return metadata;
|
if (metadata.length === 0) {
|
||||||
});
|
const newMetadata = createFirst();
|
||||||
|
logger.info('create first workspace', newMetadata);
|
||||||
|
return newMetadata;
|
||||||
|
}
|
||||||
|
return metadata;
|
||||||
|
});
|
||||||
|
}, 0);
|
||||||
|
|
||||||
if (environment.isDesktop) {
|
if (environment.isDesktop) {
|
||||||
window.apis?.workspace.list().then(workspaceIDs => {
|
window.apis?.workspace.list().then(workspaceIDs => {
|
||||||
|
if (abortController.signal.aborted) return;
|
||||||
const newMetadata = workspaceIDs.map(w => ({
|
const newMetadata = workspaceIDs.map(w => ({
|
||||||
id: w[0],
|
id: w[0],
|
||||||
flavour: WorkspaceFlavour.LOCAL,
|
flavour: WorkspaceFlavour.LOCAL,
|
||||||
@@ -63,6 +70,11 @@ rootWorkspacesMetadataAtom.onMount = setAtom => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
clearTimeout(id);
|
||||||
|
abortController.abort();
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -202,6 +202,8 @@ describe('useWorkspaces', () => {
|
|||||||
const { result } = renderHook(() => useAppHelper(), {
|
const { result } = renderHook(() => useAppHelper(), {
|
||||||
wrapper: ProviderWrapper,
|
wrapper: ProviderWrapper,
|
||||||
});
|
});
|
||||||
|
// next tick
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 100));
|
||||||
{
|
{
|
||||||
const workspaces = await store.get(workspacesAtom);
|
const workspaces = await store.get(workspacesAtom);
|
||||||
expect(workspaces.length).toEqual(1);
|
expect(workspaces.length).toEqual(1);
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ export type RootWorkspaceMetadata = {
|
|||||||
// root primitive atom that stores the necessary data for the whole app
|
// root primitive atom that stores the necessary data for the whole app
|
||||||
// be careful when you use this atom,
|
// be careful when you use this atom,
|
||||||
// it should be used only in the root component
|
// it should be used only in the root component
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* root workspaces atom
|
* root workspaces atom
|
||||||
* this atom stores the metadata of all workspaces,
|
* this atom stores the metadata of all workspaces,
|
||||||
|
|||||||
Reference in New Issue
Block a user