How to use the observable-hooks.useSubscription function in observable-hooks

To help you get started, we’ve selected a few observable-hooks examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github crimx / ext-saladict / src / content / components / MenuBar / MenuBar.tsx View on Github external
export const MenuBar: FC = props => {
  const { t } = useTranslate(['content', 'common'])

  const [updateProfileHeight, profileHeight$] = useObservableCallback(
    heightChangeTransform
  )

  const [updateSBHeight, searchBoxHeight$] = useObservableCallback(
    heightChangeTransform
  )

  // update panel min height
  useSubscription(
    useObservable(() =>
      combineLatest(profileHeight$, searchBoxHeight$).pipe(
        // a little delay for organic feeling
        debounceTime(100),
        map(heights => {
          const max = Math.max(...heights)
          return max > 0 ? max + 72 : 0
        })
      )
    ),
    props.onHeightChanged
  )

  return (
    <header>
      </header>
github crimx / ext-saladict / src / components / dictionaries / helpers.ts View on Github external
export const useVerticalScroll = () =&gt; {
  const [onWheel, onWHeel$] = useObservableCallback(_useVerticalScrollOnWheel)
  useSubscription(onWHeel$)

  const tabsRef = useRef(null)
  useEffect(() =&gt; {
    if (tabsRef.current) {
      // take the node out for cleaning up
      const node = tabsRef.current
      node.addEventListener('wheel', onWheel, { passive: false })
      return () =&gt; {
        node.removeEventListener('wheel', onWheel)
      }
    }
  }, [tabsRef.current])

  return tabsRef
}
function _useVerticalScrollOnWheel(event$: Observable) {
github crimx / ext-saladict / src / content / components / DictList / DictList.tsx View on Github external
dicts,
    onHeightChanged,
    touchMode,
    language,
    doubleClickDelay,
    newSelection,
    ...restProps
  } = props

  const heightRef = useRef({ dicts: {}, sum: 0 })

  const [updateHeight, iupdateHeight$] = useObservableCallback(event$ =&gt;
    debounceTime(10)(event$)
  )

  useSubscription(iupdateHeight$, onHeightChanged)

  const onItemHeightChanged = useRef((id: DictID, height: number) =&gt; {
    heightRef.current.sum =
      heightRef.current.sum - (heightRef.current.dicts[id] || 0) + height
    heightRef.current.dicts[id] = height
    updateHeight(heightRef.current.sum)
  }).current

  useEffect(() =&gt; {
    const oldHeight = heightRef.current
    heightRef.current = dicts.reduce(
      (height, { dictID }) =&gt; {
        height.dicts[dictID] = oldHeight.dicts[dictID] || 31
        height.sum += height.dicts[dictID] || 31
        return height
      },
github crimx / observable-hooks / examples / typeahead / src / custom-input.tsx View on Github external
export const CustomInput: React.FC = props =&gt; {
  const [onChange, textChange$] = useObservableCallback&lt;
    string,
    React.FormEvent
  &gt;(pluckCurrentTargetValue)

  useSubscription(textChange$, props.onChange)

  return (
    <input value="{props.text}" placeholder="Text input" type="text">
  )
}
github crimx / ext-saladict / src / content / components / SaladBowl / SaladBowl.tsx View on Github external
export const SaladBowl: FC = props =&gt; {
  const [onMouseOverOut, mouseOverOut$] = useObservableCallback&lt;
    boolean,
    React.MouseEvent
  &gt;(hoverWithDelay)

  useSubscription(mouseOverOut$, active =&gt; {
    props.onHover(active)
    if (active) {
      props.onActive()
    }
  })

  return (
github crimx / ext-saladict / src / components / MachineTrans / MachineTrans.tsx View on Github external
&gt; = props =&gt; {
  const { trans, searchText, langcodes, tl, sl } = props.result
  const { t } = useTranslate(['content', 'langcode'])

  const [isShowLang, setShowLang] = useState(false)
  const [onMouseOverOut, isShowLang$] = useObservableCallback&lt;
    boolean,
    React.MouseEvent
  &gt;(events$ =&gt;
    hover(events$).pipe(debounce(isOver =&gt; (isOver ? timer(500) : timer(800))))
  )
  useSubscription(isShowLang$, setShowLang)

  return (
    <div>
      <div>
        
        {searchText.paragraphs.join('').length &lt;= 100 ? (
          
        ) : (
          <div>
            <details>
              <summary>{t('machineTrans.stext')}</summary>
              
            </details>
          </div>
        )}
      </div></div>
github crimx / ext-saladict / src / content / components / WordEditor / Notes.tsx View on Github external
)

  const relatedWords = useObservableState(relatedWords$)!

  const [onTranslateCtx, translateCtx$] = useObservableCallback&lt;
    CtxTranslateResults,
    typeof ctxTransConfig
  &gt;(event$ =&gt;
    event$.pipe(
      withLatestFrom(word$),
      switchMap(([ctxTransConfig, word]) =&gt; {
        return translateCtxs(word.context || word.text, ctxTransConfig)
      })
    )
  )
  useSubscription(translateCtx$, setCtxTransResult)

  useEffect(() =&gt; {
    if (props.wordEditor.translateCtx) {
      onTranslateCtx(ctxTransConfig)
    }
  }, [])

  useEffect(getRelatedWords, [word.text, word.context])

  useUpdateEffect(() =&gt; {
    setWord({
      ...word,
      trans: genCtxText(word.trans, ctxTransResult)
    })
  }, [ctxTransResult])
github crimx / ext-saladict / src / selection / select-text.ts View on Github external
force: false
            }
      }),
      distinctUntilChanged((oldVal, newVal) =&gt; {
        return (
          !newVal.dbClick &amp;&amp;
          !!oldVal.word &amp;&amp;
          !!newVal.word &amp;&amp;
          oldVal.word.text === newVal.word.text &amp;&amp;
          oldVal.word.context === newVal.word.context
        )
      })
    )
  )

  useSubscription(output$, async result =&gt; {
    if (result.word) {
      result.word = await newSelectionWord(result.word)
    }
    newSelection(result as Message&lt;'SELECTION'&gt;['payload'])
  })

  return onMouseUp
}

observable-hooks

React hooks for RxJS Observables. Simple, flexible, testable and performant.

MIT
Latest version published 3 months ago

Package Health Score

72 / 100
Full package analysis