Processed bunch of todos
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 dd1808e · Browse files · 9 files·+118 added−108 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+118 added−108 removed
- other:+118 added−108 removed
Provenance
- Commit
00425e1- Tree
77f062f- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
dd1808e
9 files changed+118 added−108 removed
modified:src/common.ts+18 added-19 removed
14 unmodified lines15 // TODO: do this better, use unique symbol instead16 private _: any17 }18 19+const hasSymbol = typeof Symbol !== "undefined"20+export const hasMap = typeof Map !== "undefined"21+export const hasSet = typeof Set !== "undefined"22+23 /**24 * The sentinel value returned by producers to replace the draft with undefined.25 */22-export const NOTHING: Nothing =23- typeof Symbol !== "undefined"24- ? Symbol("immer-nothing")25- : ({["immer-nothing"]: true} as any)26+export const NOTHING: Nothing = hasSymbol27+ ? Symbol("immer-nothing")28+ : ({["immer-nothing"]: true} as any)29 30 /**31 * To let Immer treat your class instances as plain immutable objects32 * (albeit with a custom prototype), you must define either an instance property1 unmodified line34 *35 * Otherwise, your class instance will never be drafted, which means it won't be36 * safe to mutate in a produce callback.37 */35-export const DRAFTABLE: unique symbol =36- typeof Symbol !== "undefined" && Symbol.for37- ? Symbol.for("immer-draftable")38- : ("__$immer_draftable" as any)38+export const DRAFTABLE: unique symbol = hasSymbol39+ ? Symbol("immer-draftable")40+ : ("__$immer_draftable" as any)41 40-export const DRAFT_STATE: unique symbol =41- typeof Symbol !== "undefined" && Symbol.for42- ? Symbol.for("immer-state")43- : ("__$immer_state" as any)42+export const DRAFT_STATE: unique symbol = hasSymbol43+ ? Symbol("immer-state")44+ : ("__$immer_state" as any)45 46+export const iteratorSymbol: typeof Symbol.iterator = hasSymbol47+ ? Symbol.iterator48+ : ("@@iterator" as any)49+50 /** Returns true if the given value is an Immer draft */51 export function isDraft(value: any): boolean {52 return !!value && !!value[DRAFT_STATE]53 }55 unmodified lines109 }110 111 // TODO: use draft meta data if present?112 // make refelction methods to determine types?108-export function has(thing: ObjectishNoSet, prop: PropertyKey): boolean {113+export function has(thing: any, prop: PropertyKey): boolean {114 return !thing115 ? false116 : isMap(thing)117 ? thing.has(prop)42 unmodified lines160 return x !== x && y !== y161 }162 }163 159-export const hasSymbol = typeof Symbol !== "undefined"160-161-export const hasMap = typeof Map !== "undefined"162-164 export function isMap(target: any): target is AnyMap {165 return hasMap && target instanceof Map166 }167 167-export const hasSet = typeof Set !== "undefined"168-168 export function isSet(target: any): target is AnySet {169 return hasSet && target instanceof Set170 }171 69 unmodified linesmodified:src/es5.ts+17 added-9 removed
13 unmodified lines14 createHiddenProperty15 } from "./common"16 17 import {ImmerScope} from "./scope"18-import {ImmerState, Drafted, AnyObject, AnyMap, Objectish} from "./types"18+import {19+ ImmerState,20+ Drafted,21+ AnyObject,22+ AnyMap,23+ Objectish,24+ ImmerBaseState,25+ AnyArray26+} from "./types"27 20-interface ES5BaseState {21- scope: ImmerScope22- modified: boolean28+interface ES5BaseState extends ImmerBaseState {29 finalizing: boolean24- finalized: boolean30 assigned: {[key: string]: any}31 parent?: ImmerState32 revoke(): void33 revoked: boolean8 unmodified lines42 43 export interface ES5ArrayState extends ES5BaseState {44 type: "es5_array"45 draft: Drafted<AnyObject, ES5ArrayState>41- base: AnyObject42- copy: AnyObject | null46+ base: AnyArray47+ copy: AnyArray | null48 }49 50 type ES5State = ES5ArrayState | ES5ObjectState51 32 unmodified lines84 const state: ES5ObjectState | ES5ArrayState = {85 type: isArray ? "es5_array" : ("es5_object" as any),86 scope: parent ? parent.scope : ImmerScope.current!,87 modified: false,83- finalizing: false, // es5 only88+ finalizing: false,89 finalized: false,90 assigned: {},91 parent,92 base,93 draft,94 copy: null,95 revoke,91- revoked: false // es5 only96+ revoked: false,97+ isManual: false98 }99 100 createHiddenProperty(draft, DRAFT_STATE, state)101 return draft21 unmodified lines123 if (state.finalizing) return value124 // Create a draft if the value is unmodified.125 if (value === peek(state.base, prop) && isDraftable(value)) {126 prepareCopy(state)127+ // @ts-ignore128 return (state.copy![prop] = state.scope.immer.createProxy(value, state))129 }130 return value131 }5 unmodified lines137 if (is(value, peek(latest(state), prop))) return138 markChangedES5(state)139 prepareCopy(state)140 }141+ // @ts-ignore142 state.copy![prop] = value143 }144 145 export function markChangedES5(state: ImmerState) {164 unmodified linesmodified:src/immer.ts-1 removed
181 unmodified lines182 throw new Error("First argument to `createDraft` must be a plain object, an array, or an immerable object") // prettier-ignore183 }184 const scope = ImmerScope.enter(this)185 const proxy = this.createProxy(base, undefined)186- // @ts-ignore TODO: add to structures186 proxy[DRAFT_STATE].isManual = true187 scope.leave()188 return proxy189 }90 unmodified linesmodified:src/map.ts+13 added-19 removed
1123445667891081291011123 unmodified lines161718192425202122236 unmodified lines30313233403435364337383940414246 unmodified lines899091929893949596977 unmodified lines1051061071081141091101111121181131141151211161171181191204 unmodified lines1251261271281341291301311371321331341351364 unmodified lines14114214314415015114514614714814910 unmodified linesimport {isDraftable, DRAFT_STATE, latest} from "./common"import {isDraftable, DRAFT_STATE, latest, iteratorSymbol} from "./common"
import {ImmerScope} from "./scope"import {AnyMap, Drafted, ImmerState} from "./types"import {AnyMap, Drafted, ImmerState, ImmerBaseState} from "./types"
export interface MapState {export interface MapState extends ImmerBaseState { type: "map" parent?: ImmerState scope: ImmerScope modified: boolean finalizing: boolean finalized: boolean copy: AnyMap | undefined assigned: Map<any, boolean> | undefined base: AnyMap revoke(): void3 unmodified lines// Make sure DraftMap declarion doesn't die if Map is not avialable...const MapBase: MapConstructor = typeof Map !== "undefined" ? Map : (function FakeMap() {} as any)
// TODO: fix types for drafts// TODO: assert unrevoked, use freeze for thatexport class DraftMap<K, V> extends MapBase implements Map<K, V> { [DRAFT_STATE]: MapState constructor(target: AnyMap, parent?: ImmerState) { super()6 unmodified lines finalizing: false, copy: undefined, assigned: undefined, base: target, draft: this as any, // TODO: fix typing draft: this, revoke() { // TODO: make sure this marks the Map as revoked, and assert everywhere } }, isManual: false } }
get size(): number {46 unmodified lines cb.call(thisArg, this.get(key), key, this) }) }
get(key: K): V /* TODO: Draft<V> */ { get(key: K): V { const state = this[DRAFT_STATE] const value = latest(state).get(key) if (state.finalizing || state.finalized || !isDraftable(value)) { return value7 unmodified lines state.copy!.set(key, draft) return draft }
keys() { keys(): IterableIterator<K> { return latest(this[DRAFT_STATE]).keys() }
values() { values(): IterableIterator<V> { const iterator = this.keys() return { [Symbol.iterator]: () => this.values(), // TODO: don't use symbol directly [iteratorSymbol]: () => this.values(), next: () => { const r = iterator.next() if (r.done) return r const value = this.get(r.value)4 unmodified lines } } as any }
entries() { entries(): IterableIterator<[K, V]> { const iterator = this.keys() return { [Symbol.iterator]: () => this.entries(), // TODO: don't use symbol directly [iteratorSymbol]: () => this.entries(), next: () => { const r = iterator.next() if (r.done) return r const value = this.get(r.value)4 unmodified lines } } as any }
[Symbol.iterator]() { // TODO: don't use symbol directly [iteratorSymbol]() { return this.entries() }}
10 unmodified linesmodified:src/patches.ts+28 added-15 removed
1 import {get, each, isMap, isSet, has, die} from "./common"2 import {Patch, ImmerState} from "./types"3 import {SetState} from "./set"4+import {ES5ArrayState, ES5ObjectState} from "./es5"5+import {ProxyArrayState, ProxyObjectState} from "./proxy"6+import {MapState} from "./map"7 8 export type PatchPath = (string | number)[]9 10 export function generatePatches(11 state: ImmerState,12 basePath: PatchPath,13 patches: Patch[],14 inversePatches: Patch[]12-) {13- // TODO: use a proper switch here14- const generatePatchesFn = Array.isArray(state.base)15- ? generateArrayPatches16- : isSet(state.base)17- ? generateSetPatches18- : generatePatchesFromAssigned19-20- generatePatchesFn(state as any, basePath, patches, inversePatches)15+): void {16+ switch (state.type) {17+ case "proxy_object":18+ case "es5_object":19+ case "map":20+ return generatePatchesFromAssigned(21+ state,22+ basePath,23+ patches,24+ inversePatches25+ )26+ case "es5_array":27+ case "proxy_array":28+ return generateArrayPatches(state, basePath, patches, inversePatches)29+ case "set":30+ return generateSetPatches(state, basePath, patches, inversePatches)31+ }32 }33 34 function generateArrayPatches(24- state: any, // TODO: type properly with ImmerState35+ state: ES5ArrayState | ProxyArrayState,36 basePath: PatchPath,37 patches: Patch[],38 inversePatches: Patch[]39 ) {29- let {base, copy, assigned} = state40+ let {base, assigned, copy} = state41+ if (!copy) die()42 43 // Reduce complexity by ensuring `base` is never longer.32- if (copy.length < base.length) {44+ if (copy!.length < base.length) {45+ // @ts-ignore46 ;[base, copy] = [copy, base]47 ;[patches, inversePatches] = [inversePatches, patches]48 }49 46 unmodified lines96 }97 98 // This is used for both Map objects and normal objects.99 function generatePatchesFromAssigned(87- state: any, // TODO: type properly with ImmerState100+ state: MapState | ES5ObjectState | ProxyObjectState,101 basePath: PatchPath,102 patches: Patch[],103 inversePatches: Patch[]104 ) {105 const {base, copy} = state93- each(state.assigned, (key, assignedValue) => {106+ each(state.assigned!, (key, assignedValue) => {107 const origValue = get(base, key)95- const value = get(copy, key)108+ const value = get(copy!, key)109 const op = !assignedValue ? "remove" : has(base, key) ? "replace" : "add"110 if (origValue === value && op === "replace") return111 const path = basePath.concat(key as any)112 patches.push(op === "remove" ? {op, path} : {op, path, value})130 unmodified linesmodified:src/proxy.ts+23 added-23 removed
9 unmodified lines10 DRAFT_STATE,11 latest12 } from "./common"13 import {ImmerScope} from "./scope"14-import {AnyObject, Drafted, ImmerState, AnyArray, Objectish} from "./types"14+import {15+ AnyObject,16+ Drafted,17+ ImmerState,18+ AnyArray,19+ Objectish,20+ ImmerBaseState21+} from "./types"22 16-interface ProxyBaseState {17- scope: ImmerScope18- modified: boolean19- finalized: boolean23+interface ProxyBaseState extends ImmerBaseState {24 assigned: {25 [property: string]: boolean26 }27 parent?: ImmerState24- drafts: {28+ drafts?: {29 [property: string]: Drafted<any, any>30 }27- revoke: null | (() => void)31+ revoke(): void32 }33 34 export interface ProxyObjectState extends ProxyBaseState {35 type: "proxy_object"41 unmodified lines77 drafts: {},78 // The base copy with any updated values.79 copy: null,80 // Called by the `produce` function.77- revoke: null81+ revoke: null as any,82+ isManual: false83 }84 85 // the traps must target something, a bit like the 'real' base.86 // but also, we need to be able to determine from the target what the relevant state is23 unmodified lines110 let {drafts} = state111 112 // Check for existing draft in unmodified state.113 if (!state.modified && has(drafts, prop)) {109- return drafts[prop as any]114+ return drafts![prop as any]115 }116 117 const value = latest(state)[prop]118 if (state.finalized || !isDraftable(value)) {4 unmodified lines123 if (state.modified) {124 // Assigned values are never drafted. This catches any drafts we created, too.125 if (value !== peek(state.base, prop)) return value126 // Store drafts on the copy (when one exists).122- // @ts-ignore TODO: this line seems of?127+ // @ts-ignore128 drafts = state.copy129 }130 126- return (drafts[prop as any] = state.scope.immer.createProxy(value, state))131+ return (drafts![prop as any] = state.scope.immer.createProxy(value, state))132 },133 has(state, prop) {134 return prop in latest(state)135 },6 unmodified lines142 // Optimize based on value's truthiness. Truthy values are guaranteed to143 // never be undefined, so we can avoid the `in` operator. Lastly, truthy144 // values may be drafts, but falsy values are never drafts.145 const isUnchanged = value141- ? is(baseValue, value) || value === state.drafts[prop]146+ ? is(baseValue, value) || value === state.drafts![prop]147 : is(baseValue, value) && prop in state.base148 if (isUnchanged) return true149 prepareCopy(state)150 markChanged(state)80 unmodified lines231 232 export function markChanged(state: ImmerState) {233 if (!state.modified) {234 state.modified = true230- // @ts-ignore TODO231- const {base, drafts, parent} = state232- // TODO: get rid of isMap and isSet233- if (!isMap(base) && !isSet(base)) {234- // TODO: drop creating copies here?235- const copy = (state.copy = shallowCopy(base))236- each(drafts, (key, value) => {235+ if (state.type === "proxy_object" || state.type === "proxy_array") {236+ const copy = (state.copy = shallowCopy(state.base))237+ each(state.drafts!, (key, value) => {238 // @ts-ignore239 copy[key] = value240 })240- // @ts-ignore TODO241- state.drafts = null241+ state.drafts = undefined242 }243 244- if (parent) {245- markChanged(parent)244+ if (state.parent) {245+ markChanged(state.parent)246 }247 }248 }249 5 unmodified linesmodified:src/scope.ts+2 added-4 removed
20 unmodified lines21 22 // Whenever the modified draft contains a draft from another scope, we23 // need to prevent auto-freezing so the unowned draft can be finalized.24 this.canAutoFreeze = true25-26- // To avoid prototype lookups:27- this.patches = null as any // TODO:25 }26 27 usePatches(patchListener: PatchListener) {28 if (patchListener) {6 unmodified lines35 revoke() {36 this.leave()37 this.drafts.forEach(revoke)38 // @ts-ignore42- this.drafts = null // TODO: // Make draft-related methods throw.39+ this.drafts = null40 }41 42 leave() {43 if (this === ImmerScope.current) {8 unmodified lines52 }53 }54 55 function revoke(draft: Drafted) {56+ // TODO: switch per type and remove from data structures57 draft[DRAFT_STATE].revoke()58 }modified:src/set.ts+8 added-18 removed
1-import {DRAFT_STATE, latest, isDraftable} from "./common"1+import {DRAFT_STATE, latest, isDraftable, iteratorSymbol} from "./common"2 3-// TODO: kill:3 import {ImmerScope} from "./scope"5-import {AnySet, Drafted, ImmerState} from "./types"4+import {AnySet, Drafted, ImmerState, ImmerBaseState} from "./types"5 7-// TODO: create own states8-// TODO: clean up the maps and such from ES5 / Proxy states9-10-export interface SetState {6+export interface SetState extends ImmerBaseState {7 type: "set"12- parent?: ImmerState13- scope: ImmerScope14- modified: boolean8 finalizing: boolean16- finalized: boolean9 copy: AnySet | undefined10 base: AnySet11 drafts: Map<any, Drafted> // maps the original value to the draft value in the new set12 revoke(): void3 unmodified lines16 // Make sure DraftSet declarion doesn't die if Map is not avialable...17 const SetBase: SetConstructor =18 typeof Set !== "undefined" ? Set : (function FakeSet() {} as any)19 28-// TODO: fix types for drafts29-// TODO: assert unrevoked, use freeze for that20 export class DraftSet<K, V> extends SetBase implements Set<V> {21 [DRAFT_STATE]: SetState22 constructor(target: AnySet, parent?: ImmerState) {23 super()5 unmodified lines29 finalized: false,30 finalizing: false,31 copy: undefined,32 base: target,43- draft: this as any, // TODO: fix typing33+ draft: this,34 drafts: new Map(),35 revoke() {36 // TODO: make sure this marks the Map as revoked, and assert everywhere47- }37+ },38+ isManual: false39 }40 }41 42 get size(): number {17 unmodified lines60 if (state.copy) {61 state.copy.add(value)62 } else if (!state.base.has(value)) {63 prepareCopy(state)73- state.scope.immer.markChanged(state) // TODO: this needs to bubble up recursively correctly64+ state.scope.immer.markChanged(state)65 state.copy!.add(value)66 }67 return this68 }36 unmodified lines105 keys(): IterableIterator<V> {106 return this.values()107 }108 118- // TODO: factor out symbol119- [Symbol.iterator]() {109+ [iteratorSymbol]() {110 return this.values()111 }112 113 forEach(cb: (value: V, key: V, self: this) => void, thisArg?: any) {29 unmodified linesmodified:src/types.ts+9 added
1 unmodified line2 import {SetState} from "./set"3 import {MapState} from "./map"4 import {ProxyObjectState, ProxyArrayState} from "./proxy"5 import {ES5ObjectState, ES5ArrayState} from "./es5"6+import {ImmerScope} from "./scope"7 8 export type Objectish = AnyObject | AnyArray | AnyMap | AnySet9 export type ObjectishNoSet = AnyObject | AnyArray | AnyMap10 9 unmodified lines20 | "es5_array"21 | "map"22 | "set"23 24+export interface ImmerBaseState {25+ parent?: ImmerState26+ scope: ImmerScope27+ modified: boolean28+ finalized: boolean29+ isManual: boolean30+}31+32 export type ImmerState =33 | ProxyObjectState34 | ProxyArrayState35 | ES5ObjectState183 unmodified lines