mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-26 10:45:57 +08:00
refactor(core): refactor atom to use di (#5831)
To support multiple instances, this PR removes some atoms and implements them using the new DI system. removed atom - `pageSettingsAtom` - `currentPageIdAtom` - `currentModeAtom`
This commit is contained in:
@@ -3,13 +3,29 @@ import { useSyncExternalStore } from 'react';
|
||||
|
||||
import type { LiveData } from './index';
|
||||
|
||||
function noopSubscribe() {
|
||||
return () => {};
|
||||
}
|
||||
|
||||
function noopGetSnapshot() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* subscribe LiveData and return the value.
|
||||
*/
|
||||
export function useLiveData<T>(liveData: LiveData<T>): T {
|
||||
export function useLiveData<Input extends LiveData<any> | null | undefined>(
|
||||
liveData: Input
|
||||
): NonNullable<Input> extends LiveData<infer T>
|
||||
? Input extends undefined
|
||||
? T | undefined
|
||||
: Input extends null
|
||||
? T | null
|
||||
: T
|
||||
: never {
|
||||
return useSyncExternalStore(
|
||||
liveData.reactSubscribe,
|
||||
liveData.reactGetSnapshot
|
||||
liveData ? liveData.reactSubscribe : noopSubscribe,
|
||||
liveData ? liveData.reactGetSnapshot : noopGetSnapshot
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user