Introduced proper state types
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 c53a75f · Browse files · 6 files·+127 added−69 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+127 added−69 removed
- other:+127 added−69 removed
Provenance
- Commit
3842f85- Tree
dc4031b- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
c53a75f
6 files changed+127 added−69 removed
modified:src/es5.ts+29 added-16 removed
15 unmodified lines16 import {proxyMap, hasMapChanges} from "./map"17 import {proxySet, hasSetChanges} from "./set"18 19 import {ImmerScope} from "./scope"20-import {ImmerState} from "./types"20+import {ImmerState, Drafted, AnyObject, AnyMap} from "./types"21 22-export interface ES5Draft {23- [DRAFT_STATE]: ES5State24-}25-22 // TODO: merge with ImmerState?27-export interface ES5State<T = any> {23+interface ES5BaseState {24 scope: ImmerScope25 modified: boolean26 finalizing: boolean27 finalized: boolean32- assigned: Map<any, any> | {[key: string]: any}33- parent: ES5State34- base: T35- draft: T & ES5Draft36- drafts: Map<any, any> | null37- copy: T | null28+ assigned: {[key: string]: any}29+ parent: ImmerState30+ drafts: AnyMap | null // TODO: don't use map?31 revoke()32 revoked: boolean33 }34 35+export interface ES5ObjectState extends ES5BaseState {36+ type: "es5_object"37+ draft: Drafted<AnyObject, ES5ObjectState>38+ base: AnyObject39+ copy: AnyObject | null40+}41+42+export interface ES5ArrayState extends ES5BaseState {43+ type: "es5_array"44+ draft: Drafted<AnyObject, ES5ArrayState>45+ base: AnyObject46+ copy: AnyObject | null47+}48+49+type ES5State = ES5ArrayState | ES5ObjectState50+51 export function willFinalize(52 scope: ImmerScope,53 result: any,54 isReplaced: boolean13 unmodified lines68 markChangesSweep(scope.drafts)69 }70 }71 63-export function createProxy<T>(base: T, parent: ES5State): ES5Draft {72+export function createProxy<T>(73+ base: T,74+ parent: ImmerState75+): Drafted<any, ES5ObjectState | ES5ArrayState> {76 if (!base || typeof base !== "object" || !isDraftable(base)) {77 // TODO: || isDraft ?78 return base as any // TODO: fix79 }15 unmodified lines95 each(draft, prop => {96 proxyProperty(draft, prop, isArray || isEnumerable(base, prop))97 })98 87- const state: ES5State<T> = {99+ const state: ES5ObjectState | ES5ArrayState = {100+ type: isArray ? "es5_array" : ("es5_object" as any),101 scope,102 modified: false,103 finalizing: false, // es5 only104 finalized: false,11 unmodified lines116 scope.drafts!.push(draft)117 return draft118 }119 107-function revoke(this: ES5State) {120+function revoke(this: ES5BaseState) {121 this.revoked = true122 }123 124 // TODO: type these methods28 unmodified lines153 if (is(value, peek(latest(state), prop))) return154 markChanged(state)155 prepareCopy(state)156 }144- state.copy[prop] = value157+ state.copy![prop] = value158 }159 160 export function markChanged(state) {161 if (!state.modified) {206 unmodified linesmodified:src/map.ts+7 added-4 removed
123445678910111213141516171817192019212221232425262711 unmodified lines394041424344454647143 unmodified linesExpand allimport {isDraftable, DRAFT_STATE, latest} from "./common"
// TODO: kill:import {assertUnrevoked, ES5Draft} from "./es5"import {assertUnrevoked} from "./es5"import {ImmerScope} from "./scope"import {Immer} from "./immer"import {AnyMap, Drafted} from "./types"
// TODO: create own states// TODO: clean up the maps and such from ES5 / Proxy states
export interface MapState { type: "map" parent: any // TODO: type scope: ImmerScope modified: boolean finalizing: boolean finalized: boolean copy: Map<any, any> | undefined copy: AnyMap | undefined assigned: Map<any, boolean> | undefined base: Map<any, any> base: AnyMap revoke(): void draft: ES5Draft draft: Drafted<AnyMap, MapState>}
function prepareCopy(state: MapState) { if (!state.copy) {11 unmodified lines [DRAFT_STATE]: MapState constructor(target, parent) { super() this[DRAFT_STATE] = { type: "map", parent, scope: parent ? parent.scope : ImmerScope.current, modified: false, finalized: false,143 unmodified linesExpand allmodified:src/patches.ts+8 added-7 removed
1 import {get, each, isMap, isSet, has} from "./common"2 import {Patch, ImmerState} from "./types"3+import {SetState} from "./set"4 5 export function generatePatches(6 state: ImmerState,7 basePath: (string | number)[],8 patches: Patch[],9 inversePatches: Patch[]10 ) {11+ // TODO: use a proper switch here12 const generatePatchesFn = Array.isArray(state.base)13 ? generateArrayPatches14 : isSet(state.base)15 ? generateSetPatches16 : generatePatchesFromAssigned17 16- generatePatchesFn(state, basePath, patches, inversePatches)18+ generatePatchesFn(state as any, basePath, patches, inversePatches)19 }20 21 function generateArrayPatches(20- state: ImmerState,22+ state: any, // TODO: type properly with ImmerState23 basePath: (string | number)[],24 patches: Patch[],25 inversePatches: Patch[]26 ) {54 unmodified lines81 }82 83 // This is used for both Map objects and normal objects.84 function generatePatchesFromAssigned(83- state: ImmerState,85+ state: any, // TODO: type properly with ImmerState86 basePath: (number | string)[],87 patches: Patch[],88 inversePatches: Patch[]89 ) {16 unmodified lines106 })107 }108 109 function generateSetPatches(108- state: ImmerState,110+ state: SetState,111 basePath: (number | string)[],112 patches: Patch[],113 inversePatches: Patch[]114 ) {113- // TODO: if this doesn't use assigned, drop assigned from SetState stuff115 let {base, copy} = state116 117 let i = 0118 base.forEach(value => {118- if (!copy.has(value)) {119+ if (!copy!.has(value)) {120 const path = basePath.concat([i])121 patches.push({122 op: "remove",123 path,7 unmodified lines131 }132 i++133 })134 i = 0134- copy.forEach(value => {135+ copy!.forEach(value => {136 if (!base.has(value)) {137 const path = basePath.concat([i])138 patches.push({139 op: "add",85 unmodified linesmodified:src/proxy.ts+40 added-30 removed
17 unmodified lines18 } from "./common"19 import {ImmerScope} from "./scope"20 import {proxyMap} from "./map"21 import {proxySet} from "./set"22+import {AnyObject, Drafted, ImmerState, AnyArray} from "./types"23 24 // Do nothing before being finalized.25 export function willFinalize() {}26 26-interface ES6Draft {}27-28-interface ES6State<T = any> {27+interface ProxyBaseState {28 scope: ImmerScope29 modified: boolean30 finalized: boolean32- assigned:33- | {34- [property: string]: boolean35- }36- | Map<string, boolean> // TODO: always use a Map?37- parent: ES6State38- base: T39- draft: ES6Draft | null40- drafts:41- | {42- [property: string]: ES6Draft43- }44- | Map<string, ES6Draft> // TODO: always use a Map?45- copy: T | null31+ assigned: {32+ [property: string]: boolean33+ }34+ parent: ImmerState35+ drafts: {36+ [property: string]: Drafted<any, any>37+ }38 revoke: null | (() => void)39 }40 41+export interface ProxyObjectState extends ProxyBaseState {42+ type: "proxy_object"43+ base: AnyObject44+ copy: AnyObject | null45+ draft: Drafted<AnyObject, ProxyObjectState>46+}47+48+export interface ProxyArrayState extends ProxyBaseState {49+ type: "proxy_array"50+ base: AnyArray51+ copy: AnyArray | null52+ draft: Drafted<AnyArray, ProxyArrayState>53+}54+55 /**56 * Returns a new draft of the `base` object.57 *58 * The second argument is the parent draft-state (used internally).59 */60 export function createProxy<T extends object>(61 base: T,56- parent: ES6State57-): ES6Draft {62+ parent: ProxyObjectState | ProxyArrayState63+): Drafted<T, ProxyObjectState | ProxyArrayState> {64+ // TODO: extract create proxy and refine65 // TODO: dedupe66 if (!base || typeof base !== "object" || !isDraftable(base)) {67 // TODO: || isDraft ?68 return base as any // TODO: fix12 unmodified lines81 scope.drafts.push(draft)82 return draft83 }84 78- const state: ES6State<T> = {85+ const isArray = Array.isArray(base)86+ const state: ProxyObjectState | ProxyArrayState = {87+ type: isArray ? "proxy_array" : ("proxy_object" as any),88 // Track which produce call this is associated with.89 scope,90 // True for both shallow and deep changes.91 modified: false,5 unmodified lines97 parent,98 // The base state.99 base,100 // The base proxy.92- draft: null,101+ draft: null as any, // set below102 // Any property proxies.103 drafts: {},104 // The base copy with any updated values.105 copy: null,15 unmodified lines121 }122 123 const {revoke, proxy} = Proxy.revocable(target, traps)124 116- state.draft = proxy125+ state.draft = proxy as any126 state.revoke = revoke127 128 scope.drafts!.push(proxy)120- return proxy129+ return proxy as any130 }131 132 /**133 * Object drafts134 */126-const objectTraps: ProxyHandler<ES6State> = {135+const objectTraps: ProxyHandler<ProxyObjectState | ProxyArrayState> = {136 get(state, prop) {137 if (prop === DRAFT_STATE) return state138 let {drafts} = state139 140 // Check for existing draft in unmodified state.141 if (!state.modified && has(drafts, prop)) {133- return drafts[prop]142+ return drafts[prop as any]143 }144 145 const value = latest(state)[prop]146 if (state.finalized || !isDraftable(value)) {4 unmodified lines151 if (state.modified) {152 // Assigned values are never drafted. This catches any drafts we created, too.153 if (value !== peek(state.base, prop)) return value154 // Store drafts on the copy (when one exists).155+ // @ts-ignore TODO: this line seems of?156 drafts = state.copy157 }158 149- return (drafts[prop] = createProxy(value, state))159+ return (drafts[prop as any] = createProxy(value, state))160 },161 has(state, prop) {162 return prop in latest(state)163 },164 ownKeys(state) {165 return Reflect.ownKeys(latest(state))166 },157- set(state, prop, value) {167+ set(state, prop: string /* strictly not, but helps TS */, value) {168 if (!state.modified) {169 const baseValue = peek(state.base, prop)170 // Optimize based on value's truthiness. Truthy values are guaranteed to171 // never be undefined, so we can avoid the `in` operator. Lastly, truthy5 unmodified lines177 prepareCopy(state)178 markChanged(state)179 }180 state.assigned[prop] = true171- state.copy[prop] = value181+ state.copy![prop] = value182 return true183 },174- deleteProperty(state, prop) {184+ deleteProperty(state, prop: string) {185 // The `undefined` check is a fast path for pre-existing keys.186 if (peek(state.base, prop) !== undefined || prop in state.base) {187 state.assigned[prop] = false188 prepareCopy(state)30 unmodified lines219 /**220 * Array drafts221 */222 213-const arrayTraps: ProxyHandler<[ES6State]> = {}223+const arrayTraps: ProxyHandler<[ProxyArrayState]> = {}224 each(objectTraps, (key, fn) => {225 arrayTraps[key] = function() {226 arguments[0] = arguments[0][0]227 return fn.apply(this, arguments)80 unmodified linesmodified:src/set.ts+7 added-5 removed
1 import {isDraftable, DRAFT_STATE, latest} from "./common"2 3 // TODO: kill:4-import {assertUnrevoked, ES5Draft} from "./es5"4+import {assertUnrevoked} from "./es5"5 import {ImmerScope} from "./scope"6+import {AnySet, Drafted} from "./types"7 8 // TODO: create own states9 // TODO: clean up the maps and such from ES5 / Proxy states10 11 export interface SetState {12+ type: "set"13 parent: any // TODO: type14 scope: ImmerScope15 modified: boolean16 finalizing: boolean17 finalized: boolean16- copy: Set<any> | undefined17- // assigned: Map<any, boolean> | undefined;18- base: Set<any>18+ copy: AnySet | undefined19+ base: AnySet20 drafts: Map<any, any> // maps the original value to the draft value in the new set21 revoke(): void21- draft: ES5Draft22+ draft: Drafted<AnySet, SetState>23 }24 25 function prepareCopy(state: SetState) {26 if (!state.copy) {16 unmodified lines43 [DRAFT_STATE]: SetState44 constructor(target, parent) {45 super()46 this[DRAFT_STATE] = {47+ type: "set",48 parent,49 scope: parent ? parent.scope : ImmerScope.current,50 modified: false,51 finalized: false,116 unmodified linesmodified:src/types.ts+36 added-7 removed
1-import {Nothing} from "./common"1+import {Nothing, DRAFT_STATE} from "./common"2+import {SetState} from "./set"3+import {MapState} from "./map"4+import {ProxyObjectState, ProxyArrayState} from "./proxy"5+import {ES5ObjectState, ES5ArrayState} from "./es5"6 7 export type Objectish = any[] | Map<any, any> | Set<any> | {}8 9 export type ObjectishNoSet = any[] | Map<any, any> | {}10 7-export interface ImmerState<T = any> {8- parent?: ImmerState9- base: T10- copy: T11- assigned: {[prop: string]: boolean; [index: number]: boolean}12-}11+// export interface ImmerState<T = any> {12+// parent?: ImmerState13+// base: T14+// copy: T15+// assigned: {[prop: string]: boolean; [index: number]: boolean}16+// }17 18+export type AnyObject = {[key: string]: any}19+export type AnyArray = Array<any>20+export type AnySet = Set<any>21+export type AnyMap = Map<any, any>22+23+export type DraftType =24+ | "proxy_object"25+ | "proxy_array"26+ | "es5_object"27+ | "es5_array"28+ | "map"29+ | "set"30+31+export type ImmerState =32+ | ProxyObjectState33+ | ProxyArrayState34+ | ES5ObjectState35+ | ES5ArrayState36+ | MapState37+ | SetState38+39+export type Drafted<Base, T extends ImmerState> = {40+ [DRAFT_STATE]: T41+} & Base42+43 type Tail<T extends any[]> = ((...t: T) => any) extends ((44 _: any,45 ...tail: infer TT46 ) => any)170 unmodified lines