Skip to content

Commit

Permalink
Add value to set/done event
Browse files Browse the repository at this point in the history
  • Loading branch information
yumauri committed Jan 12, 2022
1 parent 7af0045 commit 5a8eadf
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 17 deletions.
28 changes: 14 additions & 14 deletions .size-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ module.exports = [
// core
{
path: 'build/index.js',
limit: '894 B',
limit: '900 B',
import: '{ persist }',
ignore: ['effector'],
},
{
path: 'build/index.cjs',
limit: '1149 B',
limit: '1155 B',
import: '{ persist }',
ignore: ['effector'],
},
Expand Down Expand Up @@ -44,55 +44,55 @@ module.exports = [
// localStorage
{
path: 'build/local/index.js',
limit: '1167 B',
limit: '1173 B',
import: '{ persist }',
ignore: ['effector'],
},
{
path: 'build/local/index.cjs',
limit: '1485 B',
limit: '1494 B',
import: '{ persist }',
ignore: ['effector'],
},

// sessionStorage
{
path: 'build/session/index.js',
limit: '1164 B',
limit: '1171 B',
import: '{ persist }',
ignore: ['effector'],
},
{
path: 'build/session/index.cjs',
limit: '1486 B',
limit: '1491 B',
import: '{ persist }',
ignore: ['effector'],
},

// query string
{
path: 'build/query/index.js',
limit: '1235 B',
limit: '1239 B',
import: '{ persist }',
ignore: ['effector'],
},
{
path: 'build/query/index.cjs',
limit: '1579 B',
limit: '1587 B',
import: '{ persist }',
ignore: ['effector'],
},

// memory
{
path: 'build/memory/index.js',
limit: '945 B',
limit: '952 B',
import: '{ persist }',
ignore: ['effector'],
},
{
path: 'build/memory/index.cjs',
limit: '1223 B',
limit: '1229 B',
import: '{ persist }',
ignore: ['effector'],
},
Expand All @@ -114,25 +114,25 @@ module.exports = [
// react native async storages
{
path: 'build/rn/async/index.js',
limit: '1255 B',
limit: '1262 B',
import: '{ persist }',
ignore: ['effector', '@react-native-async-storage/async-storage'],
},
{
path: 'build/rn/async/index.cjs',
limit: '1388 B',
limit: '1391 B',
import: '{ persist }',
ignore: ['effector', '@react-native-async-storage/async-storage'],
},
{
path: 'build/rn/encrypted/index.js',
limit: '1254 B',
limit: '1260 B',
import: '{ persist }',
ignore: ['effector', 'react-native-encrypted-storage'],
},
{
path: 'build/rn/encrypted/index.cjs',
limit: '1387 B',
limit: '1392 B',
import: '{ persist }',
ignore: ['effector', 'react-native-encrypted-storage'],
},
Expand Down
17 changes: 15 additions & 2 deletions src/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,21 @@ export function persist<State, Err = Error>({
(operation: 'get' | 'set') =>
({ status, params, result, error }: any): any =>
status === 'done'
? { status, key, keyPrefix, operation, value: result }
: { status, key, keyPrefix, operation, value: params, error }
? {
status,
key,
keyPrefix,
operation,
value: operation === 'get' ? result : params,
}
: {
status,
key,
keyPrefix,
operation,
value: params,
error,
}

// create all auxiliary units and nodes within the region,
// to be able to remove them all at once on unsubscription
Expand Down
37 changes: 36 additions & 1 deletion tests/done.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const dumbAdapter: StorageAdapter = <T>() => {
let __: T = 0 as any
return {
get: (): T => __,
set: (value: T) => (__ = value),
set: (value: T) => {
__ = value
},
}
}

Expand Down Expand Up @@ -61,6 +63,39 @@ test('should fire done and finally events', () => {
])
})

test('should return value on successful `set` operation', () => {
const watch = snoop(() => undefined)

const done = createEvent<any>()
done.watch(watch.fn)

const $store = createStore(1)
persist({ store: $store, adapter: dumbAdapter, key: 'test', done })

assert.is(watch.callCount, 1)
assert.equal(watch.calls[0].arguments, [
{
key: 'test',
keyPrefix: '',
operation: 'get',
value: 0,
},
])

// set new value to store
;($store as any).setState(2)

assert.is(watch.callCount, 2)
assert.equal(watch.calls[1].arguments, [
{
key: 'test',
keyPrefix: '',
operation: 'set',
value: 2,
},
])
})

//
// Launch tests
//
Expand Down

0 comments on commit 5a8eadf

Please sign in to comment.