Skip to content
Myr

Processed some todo's

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 895e0cc · Browse files · 8 files+62 added−40 removed
This commit

State

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

Diff stats

8 files+62 added−40 removed

  • other:+62 added−40 removed

8 files+62 added−40 removed

modified:src/core/finalize.ts+6 added-11 removed
modified:src/core/proxy.ts-2 removed
93 unmodified lines
94 target = [state] as any95 traps = arrayTraps96 }9798 // TODO: optimization: might be faster, cheaper if we created a non-revocable proxy99 // and administrate revoking ourselves98 const {revoke, proxy} = Proxy.revocable(target, traps)99 state.draft_ = proxy as any100 state.revoke_ = revoke101 return proxy as any
157 unmodified lines
modified:src/plugins/es5.ts-1 removed
35 unmodified lines
36 if (scope.patches_) {37 markChangesRecursively(scope.drafts_![0])38 }39 // This is faster when we don't care about which attributes changed.40 // TODO: should be prefixed with else!40 markChangesSweep(scope.drafts_)41 }42 // When a child draft is returned, look for changes.43 else if (
269 unmodified lines
modified:src/plugins/mapset.ts-1 removed
63 unmodified lines
64 return this65 }66 const p = DraftMap.prototype6768 // TODO: smaller build size if we create a util for Object.defineProperty68 Object.defineProperty(p, "size", {69 get: function() {70 return latest(this[DRAFT_STATE]).size71 }
268 unmodified lines
modified:src/plugins/patches.ts+47 added-22 removed
1// Import types only! TODO: force in TS 3.81import {2 ImmerState,3 Patch,4 SetState,
22 unmodified lines
27 die28} from "../internal"2930export function enablePatches() {31 const REPLACE = "replace"32 const ADD = "add"33 const REMOVE = "remove"3435 function generatePatches_(36 state: ImmerState,37 basePath: PatchPath,38 patches: Patch[],
56 unmodified lines
95 for (let i = start; i < end; ++i) {96 if (assigned_[i] && copy_[i] !== base_[i]) {97 const path = basePath.concat([i])98 patches.push({96 op: "replace",99 op: REPLACE,100 path,101 value: copy_[i]102 })103 inversePatches.push({101 op: "replace",104 op: REPLACE,105 path,106 value: base_[i]107 })108 }
4 unmodified lines
113 // Process added indices.114 for (let i = end + delta - 1; i >= end; --i) {115 const path = basePath.concat([i])116 patches[replaceCount + i - end] = {114 op: "add",117 op: ADD,118 path,119 value: copy_[i]120 }121 inversePatches.push({119 op: "remove",122 op: REMOVE,123 path124 })125 }126 }
8 unmodified lines
135 const {base_, copy_} = state136 each(state.assigned_!, (key, assignedValue) => {137 const origValue = get(base_, key)138 const value = get(copy_!, key)136 const op = !assignedValue ? "remove" : has(base_, key) ? "replace" : "add"137 if (origValue === value && op === "replace") return139 const op = !assignedValue ? REMOVE : has(base_, key) ? REPLACE : ADD140 if (origValue === value && op === REPLACE) return141 const path = basePath.concat(key as any)139 patches.push(op === "remove" ? {op, path} : {op, path, value})142 patches.push(op === REMOVE ? {op, path} : {op, path, value})143 inversePatches.push(141 op === "add"142 ? {op: "remove", path}143 : op === "remove"144 ? {op: "add", path, value: origValue}145 : {op: "replace", path, value: origValue}144 op === ADD145 ? {op: REMOVE, path}146 : op === REMOVE147 ? {op: ADD, path, value: origValue}148 : {op: REPLACE, path, value: origValue}149 )150 })151 }152
9 unmodified lines
162 base_.forEach((value: any) => {163 if (!copy_!.has(value)) {164 const path = basePath.concat([i])165 patches.push({163 op: "remove",166 op: REMOVE,167 path,168 value169 })170 inversePatches.unshift({168 op: "add",171 op: ADD,172 path,173 value174 })175 }
3 unmodified lines
179 copy_!.forEach((value: any) => {180 if (!base_.has(value)) {181 const path = basePath.concat([i])182 patches.push({180 op: "add",183 op: ADD,184 path,185 value186 })187 inversePatches.unshift({185 op: "remove",188 op: REMOVE,189 path,190 value191 })192 }193 i++194 })195 }196197 function generateReplacementPatches_(198 rootState: ImmerState,199 replacement: any,200 patches: Patch[],201 inversePatches: Patch[]202 ): void {203 patches.push({204 op: REPLACE,205 path: [],206 value: replacement207 })208 inversePatches.push({209 op: REPLACE,210 path: [],211 value: rootState.base_212 })213 }214215 function applyPatches_<T>(draft: T, patches: Patch[]): T {216 patches.forEach(patch => {217 const {path, op} = patch218
6 unmodified lines
225 const type = getArchtype(base)226 const value = deepClonePatchValue(patch.value) // used to clone patch to ensure original patch is not modified, see #411227 const key = path[path.length - 1]228 switch (op) {208 case "replace":229 case REPLACE:230 switch (type) {231 case ArchtypeMap:232 return base.set(key, value)233 /* istanbul ignore next */
5 unmodified lines
239 // so we use value from the cloned patch240 // @ts-ignore241 return (base[key] = value)242 }222 case "add":243 case ADD:244 switch (type) {245 case ArchtypeArray:246 return base.splice(key as any, 0, value)247 case ArchtypeMap:
2 unmodified lines
250 return base.add(value)251 default:252 return (base[key] = value)253 }233 case "remove":254 case REMOVE:255 switch (type) {256 case ArchtypeArray:257 return base.splice(key as any, 1)258 case ArchtypeMap:
10 unmodified lines
269270 return draft271 }272252 // TODO: optimize: this is quite a performance hit, can we detect intelligently when it is needed?273 // optimize: this is quite a performance hit, can we detect intelligently when it is needed?274 // E.g. auto-draft when new objects from outside are assigned and modified?275 // (See failing test when deepClone just returns obj)276 function deepClonePatchValue<T>(obj: T): T277 function deepClonePatchValue(obj: any) {
8 unmodified lines
286 for (const key in obj) cloned[key] = deepClonePatchValue(obj[key])287 return cloned288 }289269 loadPlugin("Patches", {applyPatches_, generatePatches_})290 loadPlugin("Patches", {291 applyPatches_,292 generatePatches_,293 generateReplacementPatches_294 })295}
modified:src/utils/common.ts+1 added-2 removed
152 unmodified lines
153): T154export function shallowCopy(base: any, invokeGetters = false) {155 if (Array.isArray(base)) return base.slice()156 const clone = Object.create(Object.getPrototypeOf(base))157 // TODO: each?158 ownKeys(base).forEach(key => {157 each(base, (key: any) => {158 if (key === DRAFT_STATE) {159 return // Never copy over draft state.160 }161 const desc = Object.getOwnPropertyDescriptor(base, key)!
30 unmodified lines
modified:src/utils/plugins.ts+6 added
22 unmodified lines
23 basePath: PatchPath,24 patches: Patch[],25 inversePatches: Patch[]26 ): void27 generateReplacementPatches_(28 rootState: ImmerState,29 replacement: any,30 patches: Patch[],31 inversePatches: Patch[]32 ): void33 applyPatches_<T>(draft: T, patches: Patch[]): T34 }35 ES5?: {36 willFinalizeES5_(scope: ImmerScope, result: any, isReplaced: boolean): void
78 unmodified lines
modified:package.json+2 added-1 removed
22 unmodified lines
23 "build": "rimraf dist/ && tsdx build --name immer --format esm,cjs,umd && yarn build:flow",24 "build:flow": "cpx 'src/types/immer.js.flow' dist -v",25 "publish-docs": "cd website && GIT_USER=mweststrate USE_SSH=true yarn run publish-gh-pages",26 "start": "cd website && yarn start",27 "test:size": "yarn build && yarn import-size --report . produce enableES5 enableMapSet enablePatches enableAllPlugins"27 "test:size": "yarn build && yarn import-size --report . produce enableES5 enableMapSet enablePatches enableAllPlugins",28 "test:sizequick": "tsdx build --name immer --format esm && yarn import-size . produce"29 },30 "husky": {31 "hooks": {32 "pre-commit": "pretty-quick --staged"
54 unmodified lines