-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'canary' into ayush/bumpBS22022024
- Loading branch information
Showing
7 changed files
with
150 additions
and
96 deletions.
There are no files selected for viewing
47 changes: 0 additions & 47 deletions
47
packages/frontend/core/src/hooks/__tests__/use-block-suite-workspace-helper.spec.ts
This file was deleted.
Oops, something went wrong.
68 changes: 68 additions & 0 deletions
68
packages/frontend/core/src/hooks/__tests__/use-block-suite-workspace-helper.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* @vitest-environment happy-dom | ||
*/ | ||
import 'fake-indexeddb/auto'; | ||
|
||
import { configureTestingEnvironment } from '@affine/core/testing'; | ||
import { renderHook } from '@testing-library/react'; | ||
import type { Workspace } from '@toeverything/infra'; | ||
import { initEmptyPage, ServiceProviderContext } from '@toeverything/infra'; | ||
import type { PropsWithChildren } from 'react'; | ||
import { beforeEach, describe, expect, test, vi } from 'vitest'; | ||
|
||
import { useBlockSuitePageMeta } from '../use-block-suite-page-meta'; | ||
import { useBlockSuiteWorkspaceHelper } from '../use-block-suite-workspace-helper'; | ||
|
||
const configureTestingWorkspace = async () => { | ||
const { workspace } = await configureTestingEnvironment(); | ||
const blockSuiteWorkspace = workspace.blockSuiteWorkspace; | ||
|
||
// await initEmptyPage(blockSuiteWorkspace.createPage({ id: 'page-0' })); | ||
initEmptyPage(blockSuiteWorkspace.createPage({ id: 'page1' })); | ||
initEmptyPage(blockSuiteWorkspace.createPage({ id: 'page2' })); | ||
|
||
return workspace; | ||
}; | ||
|
||
beforeEach(async () => { | ||
vi.useFakeTimers({ toFake: ['requestIdleCallback'] }); | ||
}); | ||
|
||
const getWrapper = (workspace: Workspace) => | ||
function Provider({ children }: PropsWithChildren) { | ||
return ( | ||
<ServiceProviderContext.Provider value={workspace.services}> | ||
{children} | ||
</ServiceProviderContext.Provider> | ||
); | ||
}; | ||
|
||
describe('useBlockSuiteWorkspaceHelper', () => { | ||
test('should create page', async () => { | ||
const workspace = await configureTestingWorkspace(); | ||
const blockSuiteWorkspace = workspace.blockSuiteWorkspace; | ||
const Wrapper = getWrapper(workspace); | ||
|
||
expect(blockSuiteWorkspace.meta.pageMetas.length).toBe(3); | ||
const helperHook = renderHook( | ||
() => useBlockSuiteWorkspaceHelper(blockSuiteWorkspace), | ||
{ | ||
wrapper: Wrapper, | ||
} | ||
); | ||
const pageMetaHook = renderHook( | ||
() => useBlockSuitePageMeta(blockSuiteWorkspace), | ||
{ | ||
wrapper: Wrapper, | ||
} | ||
); | ||
await new Promise(resolve => setTimeout(resolve)); | ||
expect(pageMetaHook.result.current.length).toBe(3); | ||
expect(blockSuiteWorkspace.meta.pageMetas.length).toBe(3); | ||
const page = helperHook.result.current.createPage('page4'); | ||
expect(page.id).toBe('page4'); | ||
expect(blockSuiteWorkspace.meta.pageMetas.length).toBe(4); | ||
pageMetaHook.rerender(); | ||
expect(pageMetaHook.result.current.length).toBe(4); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/frontend/core/src/hooks/use-all-block-suite-page-meta.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { PageMeta, Workspace } from '@blocksuite/store'; | ||
import type { Atom } from 'jotai'; | ||
import { atom, useAtomValue } from 'jotai'; | ||
|
||
const weakMap = new WeakMap<Workspace, Atom<PageMeta[]>>(); | ||
|
||
// this hook is extracted from './use-block-suite-page-meta.ts' to avoid circular dependency | ||
export function useAllBlockSuitePageMeta( | ||
blockSuiteWorkspace: Workspace | ||
): PageMeta[] { | ||
if (!weakMap.has(blockSuiteWorkspace)) { | ||
const baseAtom = atom<PageMeta[]>(blockSuiteWorkspace.meta.pageMetas); | ||
weakMap.set(blockSuiteWorkspace, baseAtom); | ||
baseAtom.onMount = set => { | ||
set(blockSuiteWorkspace.meta.pageMetas); | ||
const dispose = blockSuiteWorkspace.meta.pageMetasUpdated.on(() => { | ||
set(blockSuiteWorkspace.meta.pageMetas); | ||
}); | ||
return () => { | ||
dispose.dispose(); | ||
}; | ||
}; | ||
} | ||
return useAtomValue(weakMap.get(blockSuiteWorkspace) as Atom<PageMeta[]>); | ||
} |
37 changes: 17 additions & 20 deletions
37
packages/frontend/core/src/hooks/use-block-suite-page-meta.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters