-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from cloudflare/rxjs
Rxjs
- Loading branch information
Showing
20 changed files
with
1,236 additions
and
172 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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
import { RemixBrowser } from '@remix-run/react' | ||
import { startTransition } from 'react' | ||
import { hydrateRoot } from 'react-dom/client' | ||
|
||
hydrateRoot(document, <RemixBrowser />) | ||
startTransition(() => { | ||
hydrateRoot(document, <RemixBrowser />) | ||
}) |
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,54 @@ | ||
import { useEffect, useRef, useState } from 'react' | ||
import { BehaviorSubject, Observable } from 'rxjs' | ||
|
||
export function useSubscribedState<T>(observable: Observable<T>): T | undefined | ||
export function useSubscribedState<T>( | ||
observable: Observable<T>, | ||
defaultValue: T | ||
): T | ||
export function useSubscribedState<T>( | ||
observable: Observable<T>, | ||
defaultValue?: T | ||
): T { | ||
const [state, setState] = useState(defaultValue) | ||
useObservableEffect(observable, setState) | ||
return state as any | ||
} | ||
|
||
export function useObservableEffect<T>( | ||
observable: Observable<T>, | ||
fn: (value: T) => void | ||
) { | ||
const fnRef = useRef(fn) | ||
fnRef.current = fn | ||
useEffect(() => { | ||
const subscription = observable.subscribe((v) => fnRef.current(v)) | ||
return () => { | ||
subscription.unsubscribe() | ||
} | ||
}, [observable]) | ||
} | ||
|
||
/** | ||
* Turns a value into a stable observable that will emit new | ||
* values when the value changes, and completes upon unmounting. | ||
*/ | ||
export function useStateObservable<T>(value: T) { | ||
const ref = useRef(new BehaviorSubject(value)) | ||
const observableRef = useRef(ref.current.asObservable()) | ||
const previousValue = useRef<T>() | ||
if (previousValue.current !== value) { | ||
previousValue.current = value | ||
ref.current.next(value) | ||
} | ||
|
||
useEffect(() => { | ||
const { current } = ref | ||
if (!current) return | ||
return () => { | ||
current.complete() | ||
} | ||
}, []) | ||
|
||
return observableRef.current | ||
} |
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,9 @@ | ||
import { useSyncExternalStore } from 'react' | ||
|
||
export function useIsServer() { | ||
return useSyncExternalStore( | ||
() => () => {}, | ||
() => false, | ||
() => true | ||
) | ||
} |
Oops, something went wrong.