-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
115 additions
and
15 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './use-sync-external-store'; |
60 changes: 60 additions & 0 deletions
60
packages/core/src/use-sync-external-store/use-sync-external-store.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,60 @@ | ||
/** @jsx h */ | ||
import { render } from '@dark-engine/platform-browser'; | ||
import { createComponent } from '../component'; | ||
import { useSyncExternalStore } from './use-sync-external-store'; | ||
|
||
let host: HTMLElement = null; | ||
|
||
jest.useFakeTimers(); | ||
|
||
beforeEach(() => { | ||
host = document.createElement('div'); | ||
}); | ||
|
||
function createStore<T>(initialState: T) { | ||
let state = initialState; | ||
const listeners = new Set<() => void>(); | ||
|
||
const getState = () => state; | ||
|
||
const setState = (fn: (prevState: T) => T) => { | ||
state = fn(state); | ||
listeners.forEach(fn => fn()); | ||
}; | ||
|
||
const subscribe = (listener: () => void) => { | ||
listeners.add(listener); | ||
|
||
return () => listeners.delete(listener); | ||
}; | ||
|
||
return { getState, setState, subscribe }; | ||
} | ||
|
||
describe('[use-sync-external-store]', () => { | ||
test('works correctly', () => { | ||
const store = createStore(0); | ||
let state: number; | ||
|
||
const App = createComponent(() => { | ||
state = useSyncExternalStore(store.subscribe, store.getState); | ||
|
||
return null; | ||
}); | ||
|
||
render(App(), host); | ||
jest.runAllTimers(); | ||
expect(state).toBe(store.getState()); | ||
expect(state).toBe(0); | ||
|
||
store.setState(x => x + 1); | ||
jest.runAllTimers(); | ||
expect(state).toBe(store.getState()); | ||
expect(state).toBe(1); | ||
|
||
store.setState(x => x + 1); | ||
jest.runAllTimers(); | ||
expect(state).toBe(store.getState()); | ||
expect(state).toBe(2); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
packages/core/src/use-sync-external-store/use-sync-external-store.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,21 @@ | ||
import { useState } from '../use-state'; | ||
import { useEffect } from '../use-effect'; | ||
|
||
type Sunscribe = (cb: () => void) => Unsubscribe; | ||
type Unsubscribe = () => void; | ||
|
||
function useSyncExternalStore<T>(subscribe: Sunscribe, getSnapshot: () => T) { | ||
const [state, setState] = useState(getSnapshot()); | ||
|
||
useEffect(() => { | ||
const unsubscribe = subscribe(() => { | ||
setState(getSnapshot()); | ||
}); | ||
|
||
return () => unsubscribe(); | ||
}, [getSnapshot]); | ||
|
||
return state; | ||
} | ||
|
||
export { useSyncExternalStore }; |