Skip to content

Instantly share code, notes, and snippets.

@jtmthf
Created November 27, 2021 16:14
Show Gist options
  • Save jtmthf/6b038bad9315ce59a3ac67ce4ba5457d to your computer and use it in GitHub Desktop.
Save jtmthf/6b038bad9315ce59a3ac67ce4ba5457d to your computer and use it in GitHub Desktop.
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