Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
t('useGlobalCache: must be writable', async t => {
globalCache.set('x', 1)
let log = []
enhook(() => {
let [x, setX] = useGlobalCache('x')
log.push(x)
setX(2)
})()
t.deepEqual(log, [1])
await tick(3)
t.deepEqual(log, [1, 2])
globalCache.delete('x')
t.end()
})
t('useGlobalCache: does not trigger unchanged updates', async t => {
let log = []
let f = enhook((i) => {
let [count, setCount] = useGlobalCache('count', i)
log.push(count)
useEffect(() => {
setCount(i + 1)
setCount(i)
}, [])
})
f(1)
t.deepEqual(log, [1])
await frame(3)
t.deepEqual(log, [1])
globalCache.delete('count')
t.end()
})
t('useGlobalCache: must be writable', async t => {
globalCache.set('x', 1)
let log = []
enhook(() => {
let [x, setX] = useGlobalCache('x')
log.push(x)
setX(2)
})()
t.deepEqual(log, [1])
await tick(3)
t.deepEqual(log, [1, 2])
globalCache.delete('x')
t.end()
})
t('useGlobalCache: basic', async t => {
globalCache.set('count', 0)
let log = []
let f = enhook(() => {
let [count, setCount] = useGlobalCache('count')
log.push(count)
useEffect(() => {
setCount(1)
}, [])
})
f()
t.deepEqual(log, [0])
await frame(4)
t.deepEqual(log, [0, 1])
globalCache.delete('count')
t.end()
})
function resetCache() {
debug('Resetting cache');
cache.delete('smallTalkServerSide');
cache.delete('dialogCache');
return generateCache();
}
function resetCacheAPI(req, res) {
debug('Resetting cache API');
cache.delete('smallTalkServerSide');
cache.delete('dialogCache');
if (res) {
generateCache().then(data => {
debug('cache reset');
res.send(data);
});
} else {
return generateCache();
}
return true;
}
function resetCacheAPI(req, res) {
debug('Resetting cache API');
cache.delete('smallTalkServerSide');
cache.delete('dialogCache');
if (res) {
generateCache().then(data => {
debug('cache reset');
res.send(data);
});
} else {
return generateCache();
}
return true;
}
function resetCache() {
debug('Resetting cache');
cache.delete('smallTalkServerSide');
cache.delete('dialogCache');
return generateCache();
}
function set(key, value) {
return cache.set(key, value);
}
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)
}