How to use the mobx.computed function in mobx

To help you get started, we’ve selected a few mobx 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 mobxjs / mobx-utils / src / computedFn.ts View on Github external
const self = this
        const entry = d.entry(args)
        // cache hit, return
        if (entry.exists()) return entry.get().get()
        // if function is invoked, and its a cache miss without reactive, there is no point in caching...
        if (!opts.keepAlive && !_isComputingDerivation()) {
            if (!memoWarned) {
                console.warn(
                    "invoking a computedFn from outside an reactive context won't be memoized, unless keepAlive is set"
                )
                memoWarned = true
            }
            return fn.apply(self, args)
        }
        // create new entry
        const c = computed(
            () => {
                return fn.apply(self, args)
            },
            {
                ...opts,
                name: `computedFn(${fn.name}#${++i})`
            }
        )
        entry.set(c)
        // clean up if no longer observed
        if (!opts.keepAlive)
            onBecomeUnobserved(c, () => {
                d.entry(args).delete()
            })
        // return current val
        return c.get()
github czy0729 / Bangumi / screens / home / index / store.js View on Github external
eps(subjectId) {
    return computed(() => {
      const eps = subjectStore.subjectEp(subjectId).eps || []
      const { length } = eps

      // 集数超过了1页的显示个数
      if (length > Eps.pageLimit) {
        const userProgress = this.userProgress(subjectId)
        const index = eps.findIndex(
          item => item.type === 0 && userProgress[item.id] !== '看过'
        )

        // 找不到未看集数, 返回最后的数据
        if (index === -1) {
          return eps.slice(length - Eps.pageLimit - 1, length - 1)
        }

        // 找到第1个未看过的集数, 返回1个看过的集数和剩余的集数
github czy0729 / Bangumi / screens / rakuen / index / store.js View on Github external
type(page) {
    return computed(() => {
      const { title } = tabs[page]
      if (title === '小组') {
        const { group } = this.state
        const label = MODEL_RAKUEN_TYPE_GROUP.getLabel(group)
        return MODEL_RAKUEN_TYPE_GROUP.getValue(label)
      }

      if (title === '人物') {
        const { mono } = this.state
        const label = MODEL_RAKUEN_TYPE_MONO.getLabel(mono)
        return MODEL_RAKUEN_TYPE_MONO.getValue(label)
      }

      return MODEL_RAKUEN_TYPE.getValue(title)
    }).get()
  }
github czy0729 / Bangumi / stores / tag / index.js View on Github external
tag(text = '', type = DEFAULT_TYPE, airtime = '') {
    const _text = text.replace(/ /g, '+')
    return computed(
      () => this.state.tag[`${_text}|${type}|${airtime}`] || LIST_EMPTY
    ).get()
  }
github czy0729 / Bangumi / stores / tinygrail / index.js View on Github external
temple(hash = this.hash) {
    return computed(() => this.state.temple[hash]).get() || LIST_EMPTY
  }
github czy0729 / Bangumi / screens / tinygrail / bid / store.js View on Github external
list(key = 'bid') {
    return computed(() => tinygrailStore.list(key)).get()
  }
github czy0729 / Bangumi / screens / timeline / say / store.js View on Github external
usersInfo(id) {
    return computed(() => userStore.usersInfo(id)).get()
  }
github NUbots / NUsight2 / src / client / base / disposable_computed.ts View on Github external
const disposableComputedExpr = (fn: () => T): IComputedValue => {
  let latest: T | undefined
  const expr = computed(() => {
    latest && latest.dispose()
    latest = fn()
    return latest
  })
  onBecomeUnobserved(expr, () => latest && latest.dispose())
  return expr
}
github czy0729 / Bangumi / screens / rakuen / history / store.js View on Github external
isFavor(topicId) {
    return computed(() => rakuenStore.favor(topicId)).get()
  }