Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core): remove duplicated code #654

Merged
merged 1 commit into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"index.js": {
"bundled": 22369,
"minified": 8941,
"gzipped": 3292,
"bundled": 22130,
"minified": 8884,
"gzipped": 3283,
"treeshaked": {
"rollup": {
"code": 14,
Expand All @@ -28,9 +28,9 @@
}
},
"devtools.js": {
"bundled": 20665,
"minified": 8437,
"gzipped": 3180,
"bundled": 20426,
"minified": 8380,
"gzipped": 3171,
"treeshaked": {
"rollup": {
"code": 28,
Expand Down
31 changes: 10 additions & 21 deletions src/core/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,18 +258,7 @@ export const createStore = (
try {
const promiseOrValue = atom.read((a: AnyAtom) => {
dependencies.add(a)
if (a !== atom) {
const aState = readAtomState(a)
if (aState.e) {
throw aState.e // read error
}
if (aState.p) {
throw aState.p // read promise
}
return aState.v // value
}
// a === atom
const aState = getAtomState(a)
const aState = a === atom ? getAtomState(a) : readAtomState(a)
if (aState) {
if (aState.e) {
throw aState.e // read error
Expand All @@ -282,6 +271,7 @@ export const createStore = (
if (hasInitialValue(a)) {
return a.init
}
// NOTE invalid derived atoms can reach here
throw new Error('no atom init')
})
if (promiseOrValue instanceof Promise) {
Expand Down Expand Up @@ -638,15 +628,14 @@ export const createStore = (
[DEV_GET_ATOM_STATE]: (a: AnyAtom) => atomStateMap.get(a),
[DEV_GET_MOUNTED]: (a: AnyAtom) => mountedMap.get(a),
}
} else {
return {
[GET_VERSION]: () => version,
[READ_ATOM]: readAtom,
[WRITE_ATOM]: writeAtom,
[FLUSH_PENDING]: flushPending,
[SUBSCRIBE_ATOM]: subscribeAtom,
[RESTORE_ATOMS]: restoreAtoms,
}
}
return {
[GET_VERSION]: () => version,
[READ_ATOM]: readAtom,
[WRITE_ATOM]: writeAtom,
[FLUSH_PENDING]: flushPending,
[SUBSCRIBE_ATOM]: subscribeAtom,
[RESTORE_ATOMS]: restoreAtoms,
}
}

Expand Down