Skip to content
Myr

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

6 files+127 added−69 removed

modified:src/es5.ts+29 added-16 removed
15 unmodified lines
16import {proxyMap, hasMapChanges} from "./map"17import {proxySet, hasSetChanges} from "./set"1819import {ImmerScope} from "./scope"20import {ImmerState} from "./types"20import {ImmerState, Drafted, AnyObject, AnyMap} from "./types"2122export interface ES5Draft {23 [DRAFT_STATE]: ES5State24}2522// TODO: merge with ImmerState?27export interface ES5State<T = any> {23interface 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}3435export interface ES5ObjectState extends ES5BaseState {36 type: "es5_object"37 draft: Drafted<AnyObject, ES5ObjectState>38 base: AnyObject39 copy: AnyObject | null40}4142export interface ES5ArrayState extends ES5BaseState {43 type: "es5_array"44 draft: Drafted<AnyObject, ES5ArrayState>45 base: AnyObject46 copy: AnyObject | null47}4849type ES5State = ES5ArrayState | ES5ObjectState5051export function willFinalize(52 scope: ImmerScope,53 result: any,54 isReplaced: boolean
13 unmodified lines
68 markChangesSweep(scope.drafts)69 }70}7163export function createProxy<T>(base: T, parent: ES5State): ES5Draft {72export 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 lines
95 each(draft, prop => {96 proxyProperty(draft, prop, isArray || isEnumerable(base, prop))97 })9887 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 lines
116 scope.drafts!.push(draft)117 return draft118}119107function revoke(this: ES5State) {120function revoke(this: ES5BaseState) {121 this.revoked = true122}123124// TODO: type these methods
28 unmodified lines
153 if (is(value, peek(latest(state), prop))) return154 markChanged(state)155 prepareCopy(state)156 }144 state.copy[prop] = value157 state.copy![prop] = value158}159160export function markChanged(state) {161 if (!state.modified) {
206 unmodified lines
modified:src/map.ts+7 added-4 removed
modified:src/patches.ts+8 added-7 removed
1import {get, each, isMap, isSet, has} from "./common"2import {Patch, ImmerState} from "./types"3import {SetState} from "./set"45export 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 : generatePatchesFromAssigned1716 generatePatchesFn(state, basePath, patches, inversePatches)18 generatePatchesFn(state as any, basePath, patches, inversePatches)19}2021function generateArrayPatches(20 state: ImmerState,22 state: any, // TODO: type properly with ImmerState23 basePath: (string | number)[],24 patches: Patch[],25 inversePatches: Patch[]26) {
54 unmodified lines
81}8283// This is used for both Map objects and normal objects.84function generatePatchesFromAssigned(83 state: ImmerState,85 state: any, // TODO: type properly with ImmerState86 basePath: (number | string)[],87 patches: Patch[],88 inversePatches: Patch[]89) {
16 unmodified lines
106 })107}108109function 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} = state116117 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 lines
131 }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 lines
modified:src/proxy.ts+40 added-30 removed
17 unmodified lines
18} from "./common"19import {ImmerScope} from "./scope"20import {proxyMap} from "./map"21import {proxySet} from "./set"22import {AnyObject, Drafted, ImmerState, AnyArray} from "./types"2324// Do nothing before being finalized.25export function willFinalize() {}2626interface ES6Draft {}2728interface ES6State<T = any> {27interface 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}4041export interface ProxyObjectState extends ProxyBaseState {42 type: "proxy_object"43 base: AnyObject44 copy: AnyObject | null45 draft: Drafted<AnyObject, ProxyObjectState>46}4748export interface ProxyArrayState extends ProxyBaseState {49 type: "proxy_array"50 base: AnyArray51 copy: AnyArray | null52 draft: Drafted<AnyArray, ProxyArrayState>53}5455/**56 * Returns a new draft of the `base` object.57 *58 * The second argument is the parent draft-state (used internally).59 */60export 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: fix
12 unmodified lines
81 scope.drafts.push(draft)82 return draft83 }8478 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 lines
97 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 lines
121 }122123 const {revoke, proxy} = Proxy.revocable(target, traps)124116 state.draft = proxy125 state.draft = proxy as any126 state.revoke = revoke127128 scope.drafts!.push(proxy)120 return proxy129 return proxy as any130}131132/**133 * Object drafts134 */126const objectTraps: ProxyHandler<ES6State> = {135const objectTraps: ProxyHandler<ProxyObjectState | ProxyArrayState> = {136 get(state, prop) {137 if (prop === DRAFT_STATE) return state138 let {drafts} = state139140 // 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 }144145 const value = latest(state)[prop]146 if (state.finalized || !isDraftable(value)) {
4 unmodified lines
151 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 }158149 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, truthy
5 unmodified lines
177 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 lines
219/**220 * Array drafts221 */222213const arrayTraps: ProxyHandler<[ES6State]> = {}223const arrayTraps: ProxyHandler<[ProxyArrayState]> = {}224each(objectTraps, (key, fn) => {225 arrayTraps[key] = function() {226 arguments[0] = arguments[0][0]227 return fn.apply(this, arguments)
80 unmodified lines
modified:src/set.ts+7 added-5 removed
1import {isDraftable, DRAFT_STATE, latest} from "./common"23// TODO: kill:4import {assertUnrevoked, ES5Draft} from "./es5"4import {assertUnrevoked} from "./es5"5import {ImmerScope} from "./scope"6import {AnySet, Drafted} from "./types"78// TODO: create own states9// TODO: clean up the maps and such from ES5 / Proxy states1011export 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}2425function prepareCopy(state: SetState) {26 if (!state.copy) {
16 unmodified lines
43 [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 lines
modified:src/types.ts+36 added-7 removed
1import {Nothing} from "./common"1import {Nothing, DRAFT_STATE} from "./common"2import {SetState} from "./set"3import {MapState} from "./map"4import {ProxyObjectState, ProxyArrayState} from "./proxy"5import {ES5ObjectState, ES5ArrayState} from "./es5"67export type Objectish = any[] | Map<any, any> | Set<any> | {}89export type ObjectishNoSet = any[] | Map<any, any> | {}107export 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// }1718export type AnyObject = {[key: string]: any}19export type AnyArray = Array<any>20export type AnySet = Set<any>21export type AnyMap = Map<any, any>2223export type DraftType =24 | "proxy_object"25 | "proxy_array"26 | "es5_object"27 | "es5_array"28 | "map"29 | "set"3031export type ImmerState =32 | ProxyObjectState33 | ProxyArrayState34 | ES5ObjectState35 | ES5ArrayState36 | MapState37 | SetState3839export type Drafted<Base, T extends ImmerState> = {40 [DRAFT_STATE]: T41} & Base4243type Tail<T extends any[]> = ((...t: T) => any) extends ((44 _: any,45 ...tail: infer TT46) => any)
170 unmodified lines