Skip to content

Commit

Permalink
Some additional comments, improvement in error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate authored and aleclarson committed Feb 2, 2019
1 parent e9f274e commit ed6dfcc
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
} from "./common"
import {ImmerScope} from "./scope"

// property descriptors are recycled to make sure we don't create a get and set closure per property,
// but share them all instead
const descriptors = {}

export function willFinalize(scope, result, isReplaced) {
Expand Down
10 changes: 6 additions & 4 deletions src/immer.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ export class Immer {
if (isDraftable(base)) {
const scope = ImmerScope.enter()
const baseDraft = this.createDraft(base)
let hasError = true
try {
result = recipe.call(baseDraft, baseDraft)
} catch (error) {
scope.revoke()
throw error
hasError = false
} finally {
// finally instead of catch + rethrow better preserves original stack
if (hasError) scope.revoke()
else scope.leave()
}
scope.leave()
if (result instanceof Promise) {
return result.then(
result => {
Expand Down
5 changes: 4 additions & 1 deletion src/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export function createDraft(base, parent) {
}

const {revoke, proxy} = Array.isArray(base)
? Proxy.revocable([state], arrayTraps)
? // [state] is used for arrays, to make sure the proxy is array-ish and not violate invariants,
// although state itself is an object
Proxy.revocable([state], arrayTraps)
: Proxy.revocable(state, objectTraps)

state.draft = proxy
Expand Down Expand Up @@ -92,6 +94,7 @@ arrayTraps.set = function(state, prop, value) {
return objectTraps.set.call(this, state[0], prop, value)
}

// returns the object we should be reading the current value from, which is base, until some change has been made
function source(state) {
return state.copy || state.base
}
Expand Down

0 comments on commit ed6dfcc

Please sign in to comment.