Skip to content

Commit

Permalink
some build fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Jan 6, 2020
1 parent 7f5e301 commit 65a377f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 102 deletions.
2 changes: 1 addition & 1 deletion __tests__/patch.js
Expand Up @@ -618,7 +618,7 @@ describe("sets - add, delete, add - 2", () => {
)
})

describe.only("sets - mutate - 1", () => {
describe("sets - mutate - 1", () => {
const findById = (set, id) => {
for (const item of set) {
if (item.id === id) return item
Expand Down
1 change: 0 additions & 1 deletion src/es5.ts
Expand Up @@ -11,7 +11,6 @@ import {
hasSymbol,
shallowCopy,
DRAFT_STATE,
iterateMapValues,
makeIterable,
latest
} from "./common"
Expand Down
106 changes: 7 additions & 99 deletions src/proxy.ts
Expand Up @@ -15,12 +15,11 @@ import {
assignMap,
assignSet,
original,
iterateMapValues,
latest
} from "./common"
import {ImmerScope} from "./scope"
import { proxyMap } from "./map"
import { proxySet } from "./set"
import {proxyMap} from "./map"
import {proxySet} from "./set"

// Do nothing before being finalized.
export function willFinalize() {}
Expand Down Expand Up @@ -68,13 +67,13 @@ export function createProxy<T extends object>(
// TODO: dedupe this, it is the same for ES5
if (isMap(base)) {
const draft = proxyMap(base, parent) as any // TODO: typefix
scope.drafts.push(draft);
return draft;
scope.drafts.push(draft)
return draft
}
if (isSet(base)) {
const draft = proxySet(base, parent) as any // TODO: typefix
scope.drafts.push(draft);
return draft;
scope.drafts.push(draft)
return draft
}

const state: ES6State<T> = {
Expand Down Expand Up @@ -249,78 +248,6 @@ const reflectTraps = makeReflectTraps([
* Map drafts
*/

const mapTraps = makeTrapsForGetters<Map<any, any>>({
[DRAFT_STATE]: state => state,
size: state => latest(state).size,
has: state => key => latest(state).has(key),
set: state => (key, value) => {
const values = latest(state)
if (!values.has(key) || values.get(key) !== value) {
markChanged(state)
// @ts-ignore
state.assigned.set(key, true)
state.copy!.set(key, value)
}
return state.draft
},
delete: state => key => {
if (latest(state).has(key)) {
markChanged(state)
// @ts-ignore
state.assigned.set(key, false)
return state.copy!.delete(key)
}
return false
},
clear: state => () => {
markChanged(state)
state.assigned = new Map()
each(latest(state).keys(), (_, key) => {
// @ts-ignore
state.assigned.set(key, false)
})
return state.copy!.clear()
},
// @ts-ignore
forEach: (state, _, receiver) => (cb, thisArg) =>
latest(state).forEach((_, key, map) => {
const value = receiver.get(key)
cb.call(thisArg, value, key, map)
}),
get: state => key => {
const drafts = state.modified ? state.copy : state.drafts

// @ts-ignore TODO: ...or fix by using different ES6Draft types (but better just unify to maps)
if (drafts!.has(key)) {
// @ts-ignore
const value = drafts.get(key)

if (isDraft(value) || !isDraftable(value)) return value

const draft = createProxy(value, state)
// @ts-ignore
drafts.set(key, draft)
return draft
}

const value = latest(state).get(key)
if (state.finalized || !isDraftable(value)) {
return value
}

const draft = createProxy(value, state)
//@ts-ignore
drafts.set(key, draft)
return draft
},
keys: state => () => latest(state).keys(),
//@ts-ignore
values: iterateMapValues,
//@ts-ignore
entries: iterateMapValues,
[hasSymbol ? Symbol.iterator : "@@iterator"]: iterateMapValues
})

// Access a property without creating an Immer draft.
function peek(draft, prop) {
const state = draft[DRAFT_STATE]
Expand All @@ -338,7 +265,7 @@ export function markChanged(state) {
const {base, drafts, parent} = state
if (!isMap(base) && !isSet(base)) {
// TODO: drop creating copies here?
const copy = state.copy = shallowCopy(base)
const copy = (state.copy = shallowCopy(base))
assign(copy, drafts)
state.drafts = null
}
Expand Down Expand Up @@ -369,22 +296,3 @@ function makeReflectTraps<T extends object>(
{} as any
)
}

function makeTrapsForGetters<T extends object>(
getters: {
[K in keyof T]: (
state: ES6State<T>
) => /* Skip first arg of: ProxyHandler<T>[K] */ any
}
): ProxyHandler<T> {
return assign({}, reflectTraps, {
get(state, prop, receiver) {
return getters.hasOwnProperty(prop)
? getters[prop](state, prop, receiver)
: Reflect.get(state, prop, receiver)
},
setPrototypeOf(state) {
throw new Error("Object.setPrototypeOf() cannot be used on an Immer draft") // prettier-ignore
}
})
}
3 changes: 2 additions & 1 deletion tsconfig.json
Expand Up @@ -7,6 +7,7 @@
"strict": true,
"declaration": true,
"importHelpers": false,
"noImplicitAny": false // TODO: true,
"noImplicitAny": false, // TODO: true,
"esModuleInterop": true
}
}

0 comments on commit 65a377f

Please sign in to comment.