import * as React from "react" ; import * as ko from "knockout" ; import { noop } from "lodash" ; export function linkComponentToObservables ( component ) { let observableSubscription = ko . observable ( ) . subscribe ( noop ) ; const oldComponentDidMount = component . componentDidMount || noop ; component . componentDidMount = ( ) => { observableSubscription = ko . computed ( ( ) => component . render ( ) ) . subscribe ( ( ) => component . forceUpdate ( ) ) ; oldComponentDidMount ( ) ; } ; const oldComponentWillUnmount = component . componentWillUnmount || noop ; component . componentWillUnmount = ( ) => { observableSubscription . dispose ( ) ; oldComponentWillUnmount ( ) ; } ; } export function useSubscription < T > ( observable ) { const [ value , setValue ] = React . useState ( { current : observable ( ) } ) ; React . useEffect ( ( ) => { setValue ( { current : observable ( ) } ) ; const subscription = observable . subscribe ( newValue => { setValue ( { current : newValue } ) ; } ) ; return ( ) => subscription . dispose ( ) ; } , [ observable ] ) ; return value . current ; }