Created
November 27, 2021 16:14
-
-
Save jtmthf/6b038bad9315ce59a3ac67ce4ba5457d to your computer and use it in GitHub Desktop.
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
import { useRef, useState, MutableRefObject } from 'react'; | |
type ValueCallback<T> = (value: T) => void; | |
export type ObservableRef<T> = MutableRefObject<T> & { | |
subscribe(onNext: ValueCallback<T>): Subscription; | |
}; | |
export interface Subscription { | |
unsubscribe(): void; | |
} | |
export function useObservableRef<T>(initialValue: T): ObservableRef<T> { | |
const ref = useRef(initialValue); | |
const [observable] = useState<ObservableRef<T>>(() => { | |
const callbacks = new Set<ValueCallback<T>>(); | |
return { | |
get current() { | |
return ref.current; | |
}, | |
set current(value) { | |
ref.current = value; | |
callbacks.forEach((callback) => { | |
callback(value); | |
}); | |
}, | |
subscribe(onNext) { | |
callbacks.add(onNext); | |
return { | |
unsubscribe() { | |
callbacks.delete(onNext); | |
}, | |
}; | |
}, | |
}; | |
}); | |
return observable; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment