-
Notifications
You must be signed in to change notification settings - Fork 425
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support rtc connection between mobile and browser
- Loading branch information
1 parent
fb61b2e
commit 230f0e0
Showing
8 changed files
with
385 additions
and
261 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
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,39 @@ | ||
import { isFunction } from "@osmosis-labs/utils"; | ||
import { Dispatch, SetStateAction, useCallback, useRef, useState } from "react"; | ||
|
||
type ReadOnlyRefObject<T> = { | ||
readonly current: T; | ||
}; | ||
|
||
type UseStateRef = { | ||
<S>(initialState: S | (() => S)): [ | ||
S, | ||
Dispatch<SetStateAction<S>>, | ||
ReadOnlyRefObject<S> | ||
]; | ||
<S = undefined>(): [ | ||
S | undefined, | ||
Dispatch<SetStateAction<S | undefined>>, | ||
ReadOnlyRefObject<S | undefined> | ||
]; | ||
}; | ||
|
||
/** | ||
* useState and useRef together. This is useful to get the state value | ||
* inside an async callback instead of that value at the time the | ||
* callback was created from. | ||
*/ | ||
export const useStateRef: UseStateRef = <S>(initialState?: S | (() => S)) => { | ||
const [state, setState] = useState(initialState); | ||
const ref = useRef(state); | ||
|
||
const dispatch: typeof setState = useCallback((setStateAction) => { | ||
ref.current = isFunction(setStateAction) | ||
? setStateAction(ref.current) | ||
: setStateAction; | ||
|
||
setState(ref.current); | ||
}, []); | ||
|
||
return [state, dispatch, ref]; | ||
}; |
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
Oops, something went wrong.