How to use the global-cache.get function in global-cache

To help you get started, we’ve selected a few global-cache 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 unihooks / unihooks / src / useAction.js View on Github external
import enhook from 'enhook'
import { useMemo, useRef, useCallback } from './standard'
import globalCache from 'global-cache'

let cacheKey = Symbol.for('!uhx:useAction')
if (!globalCache.get(cacheKey)) globalCache.set(cacheKey, {})
const cache = globalCache.get(cacheKey)

export default function useAction (name, action) {
  let ref = useRef()

  let storedAction = useMemo(() => {
    if (action || typeof name === 'function') return createAction(name, action)
    return cache[name]
  }, [name, action])

  if (!action && !storedAction) throw Error('useAction: unknown action `' + name + '`')

  let call = useMemo(() => {
    let call =  (...args) => {
      return ref.current = storedAction(...args)
    }
github unihooks / unihooks / src / useAction.js View on Github external
import enhook from 'enhook'
import { useMemo, useRef, useCallback } from './standard'
import globalCache from 'global-cache'

let cacheKey = Symbol.for('!uhx:useAction')
if (!globalCache.get(cacheKey)) globalCache.set(cacheKey, {})
const cache = globalCache.get(cacheKey)

export default function useAction (name, action) {
  let ref = useRef()

  let storedAction = useMemo(() => {
    if (action || typeof name === 'function') return createAction(name, action)
    return cache[name]
  }, [name, action])

  if (!action && !storedAction) throw Error('useAction: unknown action `' + name + '`')

  let call = useMemo(() => {
    let call =  (...args) => {
      return ref.current = storedAction(...args)
    }
    call[Symbol.iterator] = function* () { yield ref.current; yield call }

global-cache

Sometimes you have to do horrible things, like use the global object to share a singleton. Abstract that away, with this!

MIT
Latest version published 7 years ago

Package Health Score

53 / 100
Full package analysis