Skip to content

Commit

Permalink
plugin fast
Browse files Browse the repository at this point in the history
  • Loading branch information
hacknlove committed Oct 22, 2019
1 parent b8b8add commit dedf12e
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
71 changes: 71 additions & 0 deletions CI/__tests__/index.js
Expand Up @@ -316,3 +316,74 @@ describe('sessionstorage', () => {
})
})
})

describe('fast', () => {
beforeEach(() => {
while (unsubscribes.length) {
unsubscribes.pop()()
}
Object.keys(resources).forEach(key => delete resources[key])
})

describe('onGet', () => {
it('should not call cb if there is no initial value', () => {
const cb = jest.fn()
unsubscribes.push(onGet('fast://foo', cb))

expect(cb).not.toHaveBeenCalled()
})

it('should call cb with the initial value', () => {
const cb = jest.fn()
unsubscribes.push(onGet('fast://foo', cb, { first: 'world' }))

expect(cb).toHaveBeenCalledWith('world')
})

it('should call cb with the current value, if exists', () => {
set('fast://foo', 'mars')
const cb = jest.fn()

unsubscribes.push(onGet('fast://foo', cb))

expect(cb).toHaveBeenCalledWith('mars')
})

it('should call cb with the current value, even if pass a different initial one', async () => {
await set('fast://foo', 'mars')
const cb = jest.fn()

unsubscribes.push(onGet('fast://foo', cb, { first: 'world' }))

expect(cb).toHaveBeenCalledWith('mars')
})
})

describe('set', () => {
it('triggers the callbacks of the resource', async () => {
const cb = jest.fn()
unsubscribes.push(onGet('fast://foo', cb, { first: 'world' }))
expect(cb).toHaveBeenCalledWith('world')
set('fast://foo', 'mars')
expect(cb).toHaveBeenCalledWith('mars')
})

it('does not trigger the callbacks if the value is the same', async () => {
const cb = jest.fn()
unsubscribes.push(onGet('fast://foo', cb, { first: 'world' }))
expect(cb).toHaveBeenCalledWith('world')
set('fast://foo', 'world')

jest.runAllTimers()

expect(cb.mock.calls.length).toBe(1)
})
})

describe('get', () => {
it('returns the value for a url', async () => {
set('fast://foo', 'mars')
expect(get('fast://foo')).toBe('mars')
})
})
})
6 changes: 6 additions & 0 deletions dist/onGet.cjs.js
Expand Up @@ -1460,11 +1460,17 @@ var dotted = {
}
};

var fast = {
name: 'fast',
regex: /^fast:\/\/./
};

registerPlugin(fetch$1);
registerPlugin(plugin);
registerPlugin(plugin$1);
registerPlugin(plugin$2);
registerPlugin(dotted);
registerPlugin(fast);

exports.afterSet = afterSet;
exports.beforeSet = beforeSet;
Expand Down
6 changes: 6 additions & 0 deletions dist/onGet.es.js
Expand Up @@ -1454,10 +1454,16 @@ var dotted = {
}
};

var fast = {
name: 'fast',
regex: /^fast:\/\/./
};

registerPlugin(fetch$1);
registerPlugin(plugin);
registerPlugin(plugin$1);
registerPlugin(plugin$2);
registerPlugin(dotted);
registerPlugin(fast);

export { afterSet, beforeSet, command, conf, end, get, load, onGet, once, plugins, refresh, refreshRegExp, registerPlugin, resources, save, set, start, useOnGet, waitUntil };
2 changes: 2 additions & 0 deletions src/index.js
Expand Up @@ -21,12 +21,14 @@ import localStorage from './plugins/localstorage'
import sessionStorate from './plugins/sessionstorage'
import history from './plugins/history'
import dotted from './plugins/dotted'
import fast from './plugins/fast'

registerPlugin(fetch)
registerPlugin(localStorage)
registerPlugin(sessionStorate)
registerPlugin(history)
registerPlugin(dotted)
registerPlugin(fast)

export {
afterSet,
Expand Down
8 changes: 8 additions & 0 deletions src/plugins/fast.js
@@ -0,0 +1,8 @@
import { getValue, setValue, deleteValue } from '@hacknlove/deepobject'
import { isDifferent } from 'isdifferent'
import { resources } from '../lib/conf'

export default {
name: 'fast',
regex: /^fast:\/\/./
}

0 comments on commit dedf12e

Please sign in to comment.