How to use the observable-hooks.useObservableState 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 / observable-hooks / examples / typeahead / src / Suggests.tsx View on Github external
export const Suggests: React.FC = props => {
  const fetchFunc$ = useObservable(pluckFirst, [props.fetchFunc])
  return useObservableState(
    // A stream of React elements! I know it's mind-blowing.
    useObservable(
      inputs$ =>
        inputs$.pipe(
          filter(([text]) => text.length > 1),
          distinctUntilChanged(),
          switchMap(([text]) =>
            // delay in sub-stream so that users can see the
            // searching state quickly. But no actual request
            // is performed until the delay is hit.
            forkJoin(
              // minimum 1s delay to prevent flickering if user got really greate network condition
              timer(1000),
              timer(750).pipe(
                tap(() => console.log('>>> really start searching...')),
                withLatestFrom(fetchFunc$),
github crimx / ext-saladict / src / content / components / MenuBar / SearchBox.tsx View on Github external
export const SearchBox: FC = props => {
  // Textarea also shares the text so only replace here
  const text = props.text.replace(/\s+/g, ' ')

  const [onSearchBoxFocusBlur, searchBoxFocusBlur$] = useObservableCallback(
    focusBlur
  )

  const [onSuggestFocusBlur, suggestFocusBlur$] = useObservableCallback(
    focusBlur
  )

  const [onShowSugget, onShowSugget$] = useObservableCallback(identity)

  const isShowSuggest = useObservableState(
    useObservable(
      inputs$ =>
        combineLatest(
          inputs$,
          merge(
            // only show suggest when start typing
            searchBoxFocusBlur$.pipe(filter(isFocus => !isFocus)),
            suggestFocusBlur$,
            onShowSugget$
          )
        ).pipe(
          map(([[enableSuggest, text], shouldShowSuggest]) =>
            Boolean(enableSuggest && text && shouldShowSuggest)
          ),
          distinctUntilChanged()
        ),
github crimx / ext-saladict / src / content / components / MtaBox / MtaBox.tsx View on Github external
export const MtaBox: FC = props => {
  const isTypedRef = useRef(false)
  const textareaRef = useRef(null)
  const [height, setHeight] = useState(0)

  const [isTyping, onKeyDown] = useObservableState(transformTyping, false)

  const firstExpandRef = useRef(true)
  useEffect(() => {
    if (props.expand) {
      if (!firstExpandRef.current || props.shouldFocus) {
        if (textareaRef.current) {
          textareaRef.current.focus()
          textareaRef.current.select()
        }
      }
      firstExpandRef.current = false
    }
  }, [props.expand])

  useEffect(() => {
    // could be from clipboard with delay
github crimx / ext-saladict / src / content / components / WordEditor / Notes.tsx View on Github external
return of([])
        }

        return getWordsByText('notebook', word.text)
          .then(date => {
            console.log(date, 'z>>')
            return date
          })
          .then(words => words.filter(({ date }) => date !== word.date))
          .catch(() => [])
      }),
      startWith([])
    )
  )

  const relatedWords = useObservableState(relatedWords$)!

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

  useEffect(() => {
    if (props.wordEditor.translateCtx) {
github crimx / ext-saladict / src / content / components / MenuBar / SearchBox.tsx View on Github external
searchBoxFocusBlur$.pipe(filter(isFocus => !isFocus)),
            suggestFocusBlur$,
            onShowSugget$
          )
        ).pipe(
          map(([[enableSuggest, text], shouldShowSuggest]) =>
            Boolean(enableSuggest && text && shouldShowSuggest)
          ),
          distinctUntilChanged()
        ),
      [props.enableSuggest, props.text] as [boolean, string]
    ),
    false
  )

  const isExpand = useObservableState(searchBoxFocusBlur$)

  const hasTypedRef = useRef(false)

  const inputRef = useRef(null)
  const suggestRef = useRef(null)

  const focusInput = useRef(() => {
    if (inputRef.current) {
      inputRef.current.focus()
      inputRef.current.select()
    }
  }).current

  const searchText = (text: unknown) => {
    hasTypedRef.current = false
    onShowSugget(false)
github crimx / ext-saladict / src / content / components / MenuBar / Suggest.tsx View on Github external
(props: SuggestProps, ref: React.Ref) => {
    return useObservableState(
      useObservable(
        inputs$ =>
          inputs$.pipe(
            map(([text]) => text),
            filter(Boolean),
            distinctUntilChanged(),
            debounceTime(750),
            switchMap(text =>
              from(
                message
                  .send<'GET_SUGGESTS'>({
                    type: 'GET_SUGGESTS',
                    payload: text
                  })
                  .catch(() => [] as readonly SuggestItem[])
              ).pipe(
github crimx / ext-saladict / src / content / components / MenuBar / Profiles.tsx View on Github external
boolean,
    React.MouseEvent<element>
  &gt;(hoverWithDelay)

  const [onMouseOverOut, mouseOverOut$] = useObservableCallback&lt;
    boolean,
    React.MouseEvent<element>
  &gt;(hover)

  const [onFocusBlur, focusBlur$] = useObservableCallback(focusBlur)

  const [showHideProfiles, showHideProfiles$] = useObservableCallback(
    identity
  )

  const isShowProfiles = useObservableState(
    useObservable(() =&gt;
      merge(mouseOverOut$, mouseOverOutDelay$, focusBlur$, showHideProfiles$)
    ).pipe(debounceTime(100)),
    false
  )

  const listItem = props.profiles.map(p =&gt; {
    return {
      key: p.id,
      content: (
        <span>
          {getProfileName(p.name, props.t)}</span></element></element>
github crimx / observable-hooks / examples / typeahead / src / input-time-ago.tsx View on Github external
export const InputTimeAgo: React.FC = props =&gt; {
  const [text, diff] = useObservableState(
    useObservable(
      inputs$ =&gt;
        inputs$.pipe(
          switchMap(([text]) =&gt;
            interval(1000).pipe(
              startWith(-1),
              map(count =&gt; [text, fromNow(count + 1)] as [string, string])
            )
          )
        ),
      [props.text]
    )
  )!

  return (
    <div></div>

observable-hooks

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

MIT
Latest version published 9 months ago

Package Health Score

71 / 100
Full package analysis