store.js 1.6 KB
Newer Older
D
David Golden 已提交
1 2
import { action, observable, computed, runInAction, makeObservable } from 'mobx'
import { enableStaticRendering } from 'mobx-react'
退
退之 已提交
3
import { useMemo } from 'react'
4
// eslint-disable-next-line react-hooks/rules-of-hooks
D
David Golden 已提交
5
enableStaticRendering(typeof window === 'undefined')
F
Florian Didron 已提交
6

退
退之 已提交
7 8 9
let store

class Store {
D
David Golden 已提交
10 11 12 13
  constructor() {
    makeObservable(this)
  }

F
Florian Didron 已提交
14 15 16 17 18
  @observable lastUpdate = 0
  @observable light = false

  @action start = () => {
    this.timer = setInterval(() => {
退
退之 已提交
19 20 21 22
      runInAction(() => {
        this.lastUpdate = Date.now()
        this.light = true
      })
23
    }, 1000)
F
Florian Didron 已提交
24 25
  }

退
退之 已提交
26
  @computed get timeString() {
J
Joe Haddad 已提交
27 28
    const pad = (n) => (n < 10 ? `0${n}` : n)
    const format = (t) =>
退
退之 已提交
29 30 31 32 33 34
      `${pad(t.getUTCHours())}:${pad(t.getUTCMinutes())}:${pad(
        t.getUTCSeconds()
      )}`
    return format(new Date(this.lastUpdate))
  }

F
Florian Didron 已提交
35
  stop = () => clearInterval(this.timer)
退
退之 已提交
36

37
  @action hydrate = (data) => {
退
退之 已提交
38 39 40 41 42 43 44
    if (!data) return

    this.lastUpdate = data.lastUpdate !== null ? data.lastUpdate : Date.now()
    this.light = !!data.light
  }
}

45
function initializeStore(initialData = null) {
退
退之 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  const _store = store ?? new Store()

  // If your page has Next.js data fetching methods that use a Mobx store, it will
  // get hydrated here, check `pages/ssg.js` and `pages/ssr.js` for more details
  if (initialData) {
    _store.hydrate(initialData)
  }
  // For SSG and SSR always create a new store
  if (typeof window === 'undefined') return _store
  // Create the store once in the client
  if (!store) store = _store

  return _store
}

export function useStore(initialState) {
  const store = useMemo(() => initializeStore(initialState), [initialState])
  return store
F
Florian Didron 已提交
64
}