Skip to content
Myr

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

9 files+118 added−108 removed

modified:src/common.ts+18 added-19 removed
14 unmodified lines
15 // TODO: do this better, use unique symbol instead16 private _: any17}1819const hasSymbol = typeof Symbol !== "undefined"20export const hasMap = typeof Map !== "undefined"21export const hasSet = typeof Set !== "undefined"2223/**24 * The sentinel value returned by producers to replace the draft with undefined.25 */22export const NOTHING: Nothing =23 typeof Symbol !== "undefined"24 ? Symbol("immer-nothing")25 : ({["immer-nothing"]: true} as any)26export const NOTHING: Nothing = hasSymbol27 ? Symbol("immer-nothing")28 : ({["immer-nothing"]: true} as any)2930/**31 * To let Immer treat your class instances as plain immutable objects32 * (albeit with a custom prototype), you must define either an instance property
1 unmodified line
34 *35 * Otherwise, your class instance will never be drafted, which means it won't be36 * safe to mutate in a produce callback.37 */35export const DRAFTABLE: unique symbol =36 typeof Symbol !== "undefined" && Symbol.for37 ? Symbol.for("immer-draftable")38 : ("__$immer_draftable" as any)38export const DRAFTABLE: unique symbol = hasSymbol39 ? Symbol("immer-draftable")40 : ("__$immer_draftable" as any)4140export const DRAFT_STATE: unique symbol =41 typeof Symbol !== "undefined" && Symbol.for42 ? Symbol.for("immer-state")43 : ("__$immer_state" as any)42export const DRAFT_STATE: unique symbol = hasSymbol43 ? Symbol("immer-state")44 : ("__$immer_state" as any)4546export const iteratorSymbol: typeof Symbol.iterator = hasSymbol47 ? Symbol.iterator48 : ("@@iterator" as any)4950/** Returns true if the given value is an Immer draft */51export function isDraft(value: any): boolean {52 return !!value && !!value[DRAFT_STATE]53}
55 unmodified lines
109}110111// TODO: use draft meta data if present?112// make refelction methods to determine types?108export function has(thing: ObjectishNoSet, prop: PropertyKey): boolean {113export function has(thing: any, prop: PropertyKey): boolean {114 return !thing115 ? false116 : isMap(thing)117 ? thing.has(prop)
42 unmodified lines
160 return x !== x && y !== y161 }162}163159export const hasSymbol = typeof Symbol !== "undefined"160161export const hasMap = typeof Map !== "undefined"162164export function isMap(target: any): target is AnyMap {165 return hasMap && target instanceof Map166}167167export const hasSet = typeof Set !== "undefined"168168export function isSet(target: any): target is AnySet {169 return hasSet && target instanceof Set170}171
69 unmodified lines
modified:src/es5.ts+17 added-9 removed
13 unmodified lines
14 createHiddenProperty15} from "./common"1617import {ImmerScope} from "./scope"18import {ImmerState, Drafted, AnyObject, AnyMap, Objectish} from "./types"18import {19 ImmerState,20 Drafted,21 AnyObject,22 AnyMap,23 Objectish,24 ImmerBaseState,25 AnyArray26} from "./types"2720interface ES5BaseState {21 scope: ImmerScope22 modified: boolean28interface ES5BaseState extends ImmerBaseState {29 finalizing: boolean24 finalized: boolean30 assigned: {[key: string]: any}31 parent?: ImmerState32 revoke(): void33 revoked: boolean
8 unmodified lines
4243export interface ES5ArrayState extends ES5BaseState {44 type: "es5_array"45 draft: Drafted<AnyObject, ES5ArrayState>41 base: AnyObject42 copy: AnyObject | null46 base: AnyArray47 copy: AnyArray | null48}4950type ES5State = ES5ArrayState | ES5ObjectState51
32 unmodified lines
84 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 }99100 createHiddenProperty(draft, DRAFT_STATE, state)101 return draft
21 unmodified lines
123 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 lines
137 if (is(value, peek(latest(state), prop))) return138 markChangedES5(state)139 prepareCopy(state)140 }141 // @ts-ignore142 state.copy![prop] = value143}144145export function markChangedES5(state: ImmerState) {
164 unmodified lines
modified:src/immer.ts-1 removed
181 unmodified lines
182 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 lines
modified:src/map.ts+13 added-19 removed
modified:src/patches.ts+28 added-15 removed
1import {get, each, isMap, isSet, has, die} from "./common"2import {Patch, ImmerState} from "./types"3import {SetState} from "./set"4import {ES5ArrayState, ES5ObjectState} from "./es5"5import {ProxyArrayState, ProxyObjectState} from "./proxy"6import {MapState} from "./map"78export type PatchPath = (string | number)[]910export 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 : generatePatchesFromAssigned1920 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}3334function 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()4243 // 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 lines
96}9798// This is used for both Map objects and normal objects.99function 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 lines
modified:src/proxy.ts+23 added-23 removed
9 unmodified lines
10 DRAFT_STATE,11 latest12} from "./common"13import {ImmerScope} from "./scope"14import {AnyObject, Drafted, ImmerState, AnyArray, Objectish} from "./types"14import {15 AnyObject,16 Drafted,17 ImmerState,18 AnyArray,19 Objectish,20 ImmerBaseState21} from "./types"2216interface ProxyBaseState {17 scope: ImmerScope18 modified: boolean19 finalized: boolean23interface 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}3334export interface ProxyObjectState extends ProxyBaseState {35 type: "proxy_object"
41 unmodified lines
77 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 }8485 // 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 is
23 unmodified lines
110 let {drafts} = state111112 // 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 }116117 const value = latest(state)[prop]118 if (state.finalized || !isDraftable(value)) {
4 unmodified lines
123 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 }130126 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 lines
142 // 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 lines
231232export 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 }243244 if (parent) {245 markChanged(parent)244 if (state.parent) {245 markChanged(state.parent)246 }247 }248}249
5 unmodified lines
modified:src/scope.ts+2 added-4 removed
20 unmodified lines
2122 // 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 = true2526 // To avoid prototype lookups:27 this.patches = null as any // TODO:25 }2627 usePatches(patchListener: PatchListener) {28 if (patchListener) {
6 unmodified lines
35 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 }4142 leave() {43 if (this === ImmerScope.current) {
8 unmodified lines
52 }53}5455function 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
1import {DRAFT_STATE, latest, isDraftable} from "./common"1import {DRAFT_STATE, latest, isDraftable, iteratorSymbol} from "./common"23// TODO: kill:3import {ImmerScope} from "./scope"5import {AnySet, Drafted, ImmerState} from "./types"4import {AnySet, Drafted, ImmerState, ImmerBaseState} from "./types"57// TODO: create own states8// TODO: clean up the maps and such from ES5 / Proxy states910export interface SetState {6export 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(): void
3 unmodified lines
16// Make sure DraftSet declarion doesn't die if Map is not avialable...17const SetBase: SetConstructor =18 typeof Set !== "undefined" ? Set : (function FakeSet() {} as any)1928// TODO: fix types for drafts29// TODO: assert unrevoked, use freeze for that20export class DraftSet<K, V> extends SetBase implements Set<V> {21 [DRAFT_STATE]: SetState22 constructor(target: AnySet, parent?: ImmerState) {23 super()
5 unmodified lines
29 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 }4142 get size(): number {
17 unmodified lines
60 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 lines
105 keys(): IterableIterator<V> {106 return this.values()107 }108118 // TODO: factor out symbol119 [Symbol.iterator]() {109 [iteratorSymbol]() {110 return this.values()111 }112113 forEach(cb: (value: V, key: V, self: this) => void, thisArg?: any) {
29 unmodified lines
modified:src/types.ts+9 added
1 unmodified line
2import {SetState} from "./set"3import {MapState} from "./map"4import {ProxyObjectState, ProxyArrayState} from "./proxy"5import {ES5ObjectState, ES5ArrayState} from "./es5"6import {ImmerScope} from "./scope"78export type Objectish = AnyObject | AnyArray | AnyMap | AnySet9export type ObjectishNoSet = AnyObject | AnyArray | AnyMap10
9 unmodified lines
20 | "es5_array"21 | "map"22 | "set"2324export interface ImmerBaseState {25 parent?: ImmerState26 scope: ImmerScope27 modified: boolean28 finalized: boolean29 isManual: boolean30}3132export type ImmerState =33 | ProxyObjectState34 | ProxyArrayState35 | ES5ObjectState
183 unmodified lines