OverviewFilesCommitsIssuesDocsReleasesPeers

keet-deobfuscated / @holepunchto / keet-store / store / chat / list-view / list-view-middleware.js

26 lines · javascript ref:
import { getChatCurrentRoomId as t } from "../init"
import { chatListViewRenderedEvt as e, setChatMessagesInView as o } from "./list-view.action"
import { CHAT_LIST_IN_VIEW_INTERVAL as i } from "./list-view.constant"
export const createListViewMiddleware =
  (l = {}) =>
  ({ dispatch: n, getState: a }) => {
    const { intervalMs: r = i } = l
    let s = null,
      c = null,
      m = 0,
      u = null
    const p = () => {
      ;((c = null), (m = Date.now()), n(o(s)))
    }
    return (o) => (i) =>
      e.match(i)
        ? (((e) => {
            const o = t(a())
            ;(o !== u && (c && clearTimeout(c), (c = null), (m = 0), (u = o)),
              (s = e),
              c || (m + r < Date.now() ? p() : (c = setTimeout(p, r))))
          })(i.payload),
          i)
        : o(i)
  }