-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(returning): refactor handling return value via rawReturn() a…
…nd remove safeReturn() (#11)
- Loading branch information
Showing
20 changed files
with
744 additions
and
526 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { RAW_RETURN_SYMBOL } from './constant'; | ||
|
||
/** | ||
* Use rawReturn() to wrap the return value to skip the draft check and thus improve performance. | ||
* | ||
* ## Example | ||
* | ||
* ```ts | ||
* import { create, rawReturn } from '../index'; | ||
* | ||
* const baseState = { foo: { bar: 'str' }, arr: [] }; | ||
* const state = create( | ||
* baseState, | ||
* (draft) => { | ||
* return rawReturn(baseState); | ||
* }, | ||
* ); | ||
* expect(state).toBe(baseState); | ||
* ``` | ||
*/ | ||
export function rawReturn<T extends object | undefined>(value: T): T { | ||
if (arguments.length === 0) { | ||
throw new Error('rawReturn() must be called with a value.'); | ||
} | ||
if (arguments.length > 1) { | ||
throw new Error('rawReturn() must be called with one argument.'); | ||
} | ||
if ( | ||
__DEV__ && | ||
value !== undefined && | ||
(typeof value !== 'object' || value === null) | ||
) { | ||
console.warn( | ||
'rawReturn() must be called with an object(including plain object, arrays, Set, Map, etc.) or `undefined`, other types do not need to be returned via rawReturn().' | ||
); | ||
} | ||
return { | ||
[RAW_RETURN_SYMBOL]: [value], | ||
} as never; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.