Skip to content
Myr

WIP on arrays

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Michel Weststrate · committed by Michel Weststrate · parent a758751 · Browse files · 1 file+44 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+44 added−2 removed

  • other:+44 added−2 removed

1 file+44 added−2 removed

modified:immer.js+44 added-2 removed
75 unmodified lines
7677 markChanged() {78 if (!this.modified) {79 this.modified = true80 this.copy = Object.assign({}, this.base)80 this.copy = Array.isArray(this.base)81 ? this.base.slice()82 : Object.assign({}, this.base)83 if (this.parent) this.parent.markChanged()84 }85 }86 }
31 unmodified lines
118 throw new Error("Don't even try this...")119 }120 }121122 const arrayTraps = {123 get(target, prop) {124 if (prop === IS_PROXY) return true125 if (prop === PROXY_STATE) return target[0]126 return target[0].get(prop)127 },128 has(target, prop) {129 return prop in target[0].source130 },131 ownKeys(target) {132 return Reflect.ownKeys(target[0].source)133 },134 set(target, prop, value) {135 target[0].set(prop, value)136 return true137 },138 deleteProperty(target, property) {139 target[0].markChanged()140 delete target[0].copy[property]141 return true142 },143 getOwnPropertyDescriptor(target, prop) {144 return Reflect.getOwnPropertyDescriptor(target[0].source, prop)145 },146 defineProperty(target, property, descriptor) {147 target[0].markChanged()148 Object.defineProperty(target[0].copy, property, descriptor)149 return true150 },151 setPrototypeOf() {152 throw new Error("Don't even try this...")153 }154 }155156 // creates a proxy for plain objects / arrays157 function createProxy(parentState, base) {158 const state = new State(parentState, base)159 if (Array.isArray(base)) {160 // Proxy should be created with an array to make it an array for JS161 // so... here you have it!162 return new Proxy([state], arrayTraps)163 } else {123 return new Proxy(state, objectTraps)164 return new Proxy(state, objectTraps)165 }166 }167168 // given a base object, returns it if unmodified, or return the changed cloned if modified169 function finalize(base) {
93 unmodified lines