Skip to content

Commit

Permalink
refactor(core): remove duplicated code (#654)
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Aug 13, 2021
1 parent 1c1e183 commit bf204e0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
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

0 comments on commit bf204e0

Please sign in to comment.