Skip to content

Commit e315f20

Browse files
authored
Merge pull request #82 from webKrafters/reset-update
updating the resetState logic
2 parents 9c75c59 + 6f7d702 commit e315f20

3 files changed

Lines changed: 28 additions & 33 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
"test:watch": "eslint --fix && jest --updateSnapshot --watchAll"
114114
},
115115
"types": "dist/index.d.ts",
116-
"version": "6.0.5",
116+
"version": "6.0.6",
117117
"dependencies": {
118118
"@webkrafters/auto-immutable": "^2.0.5"
119119
}

src/main/hooks/use-store/index.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,10 @@ describe( 'useStore', () => {
228228
'dsdfd.sfgrwfg'
229229
];
230230
resetData = {
231-
a: { [ REPLACE_TAG ]: initialState.a },
232-
dsdfd: { [ DELETE_TAG ]: [ 'adfsdff', 'sfgrwfg' ] }
231+
a: {
232+
[ REPLACE_TAG ]: initialState.a
233+
},
234+
[ DELETE_TAG ]: [ 'dsdfd' ]
233235
};
234236
store.resetState( ConnectionMock, nonInitStatePaths );
235237
});
@@ -440,7 +442,7 @@ describe( 'useStore', () => {
440442
}
441443
}
442444
);
443-
expect(() => result.current.resetState( ConnectionMock, expect.anything() ))
445+
expect(() => result.current.resetState( ConnectionMock ))
444446
.toThrow( '`resetState` prehook must return a boolean value.' );
445447
} );
446448
} );

src/main/hooks/use-store/index.ts

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import isEmpty from 'lodash.isempty';
3232

3333
import mapPathsToObject from '@webkrafters/data-distillery';
3434
import stringToDotPath from '@webkrafters/path-dotize';
35+
import getProperty from '@webkrafters/get-property';
3536
import AutoImmutable from '@webkrafters/auto-immutable';
3637

3738
import {
@@ -138,38 +139,30 @@ const useStore = <T extends State>(
138139
const resetState = useCallback<StoreInternal<T>["resetState"]>(
139140
( connection, propertyPaths = [] ) => {
140141
const original = _storage.clone( _storage.getItem( storageKey.current ) );
141-
let resetData;
142-
if( !propertyPaths.length ) {
143-
resetData = {};
144-
} else if( propertyPaths.includes( FULL_STATE_SELECTOR ) ) {
142+
let resetData = {};
143+
if( propertyPaths.includes( FULL_STATE_SELECTOR ) ) {
145144
resetData = isEmpty( original ) ? CLEAR_TAG : { [ REPLACE_TAG ]: original };
146145
} else {
147-
const visitedPathMap = {};
148-
const transformer = ({ trail, value } : PropertyInfo ) => {
149-
visitedPathMap[ trail.join( '.' ) ] = null;
150-
return { [ REPLACE_TAG ]: value };
151-
}
152-
resetData = mapPathsToObject( original, propertyPaths, transformer as Transform );
153-
if( Object.keys( visitedPathMap ).length < propertyPaths.length ) {
154-
for( let path of propertyPaths ) {
155-
path = stringToDotPath( path );
156-
if( path in visitedPathMap ) { continue }
157-
let trail = path.split( '.' );
158-
const keyTuple = trail.slice( -1 );
159-
trail = trail.slice( 0, -1 );
160-
let node = resetData;
161-
for( const t of trail ) {
162-
if( isEmpty( node[ t ] ) ) {
163-
node[ t ] = {};
164-
}
165-
node = node[ t ];
166-
}
167-
if( DELETE_TAG in node ) {
168-
node[ DELETE_TAG ].push( ...keyTuple );
169-
} else {
170-
node[ DELETE_TAG ] = keyTuple;
171-
}
146+
for( let path of propertyPaths ) {
147+
let node = resetData;
148+
const tokens = stringToDotPath( path ).split( '.' );
149+
const { trail, ...pInfo } = getProperty( original, tokens );
150+
for( let { length, ...keys } = trail, k = 0; k < length; k++ ) {
151+
if( REPLACE_TAG in node ) { continue }
152+
const key = keys[ k ];
153+
if( !( key in node ) ) { node[ key ] = {} }
154+
node = node[ key ];
172155
}
156+
if( REPLACE_TAG in node ) { continue }
157+
if( pInfo.exists ) {
158+
for( const k in node ) { delete node[ k ] }
159+
node[ REPLACE_TAG ] = pInfo._value;
160+
continue;
161+
}
162+
if( !( DELETE_TAG in node ) ) { node[ DELETE_TAG ] = [] }
163+
const deletingKey = tokens[ trail.length ];
164+
!node[ DELETE_TAG ].includes( deletingKey ) &&
165+
node[ DELETE_TAG ].push( deletingKey );
173166
}
174167
}
175168
runPrehook( prehooksRef.current, 'resetState', [

0 commit comments

Comments
 (0)