Skip to content
Myr

Stop abusing modules as exports

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 3842f85 · Browse files · 3 files+55 added−42 removed
This commit

State

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

Diff stats

3 files+55 added−42 removed

  • other:+55 added−42 removed

3 files+55 added−42 removed

modified:src/es5.ts+16 added-16 removed
46 unmodified lines
47}4849type ES5State = ES5ArrayState | ES5ObjectState5051export function willFinalize(51export function willFinalizeES5(52 scope: ImmerScope,53 result: any,54 isReplaced: boolean55) {
12 unmodified lines
68 markChangesSweep(scope.drafts)69 }70}7172export function createProxy<T>(72export function createES5Proxy<T>(73 base: T,74 parent: ImmerState75): Drafted<any, ES5ObjectState | ES5ArrayState> {76 if (!base || typeof base !== "object" || !isDraftable(base)) {
63 unmodified lines
140 if (state.finalizing) return value141 // Create a draft if the value is unmodified.142 if (value === peek(state.base, prop) && isDraftable(value)) {143 prepareCopy(state)144 return (state.copy[prop] = createProxy(value, state))144 return (state.copy[prop] = createES5Proxy(value, state))145 }146 return value147}148
1 unmodified line
150 assertUnrevoked(state)151 state.assigned[prop] = true152 if (!state.modified) {153 if (is(value, peek(latest(state), prop))) return154 markChanged(state)154 markChangedES5(state)155 prepareCopy(state)156 }157 state.copy![prop] = value158}159160export function markChanged(state) {160export function markChangedES5(state) {161 if (!state.modified) {162 state.modified = true163 if (state.parent) markChanged(state.parent)163 if (state.parent) markChangedES5(state.parent)164 }165}166167// TODO: kill export
52 unmodified lines
220 for (let i = drafts.length - 1; i >= 0; i--) {221 const state = drafts[i][DRAFT_STATE]222 if (!state.modified) {223 if (Array.isArray(state.base)) {224 if (hasArrayChanges(state)) markChanged(state)224 if (hasArrayChanges(state)) markChangedES5(state)225 } else if (isMap(state.base)) {226 if (hasMapChanges(state)) markChanged(state)226 if (hasMapChanges(state)) markChangedES5(state)227 } else if (isSet(state.base)) {228 if (hasSetChanges(state)) markChanged(state)228 if (hasSetChanges(state)) markChangedES5(state)229 } else if (hasObjectChanges(state)) {230 markChanged(state)230 markChangedES5(state)231 }232 }233 }234}
6 unmodified lines
241 if (!state) return242 const {base, draft, assigned} = state243 if (isSet(object)) {244 if (hasSetChanges(state)) {245 markChanged(state)245 markChangedES5(state)246 object.forEach(v => {247 markChangesRecursively(v)248 })249 }
2 unmodified lines
252 object.forEach((value, key) => {253 if (assigned && base.get(key) === undefined && !has(base, key)) {254 // TODO: this code seems invalid for Maps!255 assigned.set(key, true)256 markChanged(state)256 markChangedES5(state)257 } else if (!assigned || !assigned.get(key)) {258 // TODO: === false?259 // Only untouched properties trigger recursion.260 markChangesRecursively(draft.get(key))
6 unmodified lines
267 base.forEach((value, key) => {268 // The `undefined` check is a fast path for pre-existing keys.269 if (draft.get(key) === undefined && !has(draft, key)) {270 assigned.set(key, false)271 markChanged(state)271 markChangedES5(state)272 }273 })274 // }275 } else if (!Array.isArray(object)) {
3 unmodified lines
279 // The `undefined` check is a fast path for pre-existing keys.280 if (base[key] === undefined && !has(base, key)) {281 // TODO: this code seems invalid for Maps!282 assigned[key] = true283 markChanged(state)283 markChangedES5(state)284 } else if (!assigned[key]) {285 // TODO: === false ?286 // Only untouched properties trigger recursion.287 markChangesRecursively(draft[key])
5 unmodified lines
293 Object.keys(base).forEach(key => {294 // The `undefined` check is a fast path for pre-existing keys.295 if (draft[key] === undefined && !has(draft, key)) {296 assigned[key] = false297 markChanged(state)297 markChangedES5(state)298 }299 })300 } else if (hasArrayChanges(state)) {301 markChanged(state)301 markChangedES5(state)302 assigned.length = true303 if (draft.length < base.length) {304 for (let i = draft.length; i < base.length; i++) assigned[i] = false305 } else {
62 unmodified lines
modified:src/immer.ts+34 added-15 removed
1// TODO: destructure these, as the * as import makes it unclear which methods get installed on the Immer class2import * as legacyProxy from "./es5"3import * as modernProxy from "./proxy"1import {createES5Proxy, willFinalizeES5, markChangedES5} from "./es5"2import {createProxy, markChanged} from "./proxy"34import {applyPatches, generatePatches} from "./patches"5import {6 assign,
12 unmodified lines
19 freeze,20 latest21} from "./common"22import {ImmerScope} from "./scope"24import {ImmerState, IProduce, IProduceWithPatches, Objectish, PatchListener, Draft, Patch} from "./types"23import {24 ImmerState,25 IProduce,26 IProduceWithPatches,27 Objectish,28 PatchListener,29 Draft,30 Patch31} from "./types"3233function verifyMinified() {}3435const configDefaults = {
17 unmodified lines
5354export class Immer implements ProducersFns {55 useProxies: boolean = false56 autoFreeze: boolean = false50 onAssign?: (51 state: ImmerState,52 prop: string | number,53 value: unknown54 ) => void57 onAssign?: (state: ImmerState, prop: string | number, value: unknown) => void58 onDelete?: (state: ImmerState, prop: string | number) => void59 onCopy?: (state: ImmerState) => void57 createProxy!:<T>(value: T, parent: any) => T;58 willFinalize!: (scope: ImmerScope, thing: any, isReplaced: boolean) => void;59 markChanged!:(state: any) => void; // TODO: immerState?6061 constructor(config?: {62 useProxies?: boolean63 autoFreeze?: boolean
117 unmodified lines
181 scope.leave()182 return proxy as any183 }184185 finishDraft<D extends Draft<any>>(draft: D, patchListener: PatchListener): D extends Draft<infer T> ? T : never {185 finishDraft<D extends Draft<any>>(186 draft: D,187 patchListener: PatchListener188 ): D extends Draft<infer T> ? T : never {189 const state = draft && draft[DRAFT_STATE]190 if (!state || !state.isManual) {191 throw new Error("First argument to `finishDraft` must be a draft returned by `createDraft`") // prettier-ignore192 }
21 unmodified lines
214 * By default, feature detection is used, so calling this is rarely necessary.215 */216 setUseProxies(value: boolean) {217 this.useProxies = value215 assign(this, value ? modernProxy : legacyProxy)218 }219220 applyPatches(base: Objectish, patches: Patch[]) {221 // If a patch replaces the entire state, take that replacement as base
54 unmodified lines
276 }277 return result !== NOTHING ? result : undefined278 }279280 createProxy<T>(value: any, parent: any) {281 if (this.useProxies) return createProxy(value, parent)282 else return createES5Proxy(value, parent)283 }284285 willFinalize(scope: ImmerScope, thing: any, isReplaced: boolean) {286 if (!this.useProxies) willFinalizeES5(scope, thing, isReplaced)287 }288289 markChanged(state: any) {290 if (this.useProxies) {291 markChanged(state)292 } else {293 markChangedES5(state)294 }295 }296297 /**298 * @internal299 * Finalize a draft, returning either the unmodified base state or a modified300 * copy of the base state.
60 unmodified lines
361 // if (state.modified && !state.copy) {362 // state.copy = shallowCopy(state.base, false)363 // }364 // else346 if (!this.useProxies && !isMap(root) && !isSet(root)) {365 if (!this.useProxies && !isMap(root) && !isSet(root)) {366 // Create the final copy, with added keys and without deleted keys.367 state.copy = shallowCopy(state.draft, true) // TODO: optimization, can we get rid of this and just use state.copy?368 }369
77 unmodified lines
modified:src/proxy.ts+5 added-11 removed
19 unmodified lines
20import {proxyMap} from "./map"21import {proxySet} from "./set"22import {AnyObject, Drafted, ImmerState, AnyArray} from "./types"2324// Do nothing before being finalized.25export function willFinalize() {}2624interface ProxyBaseState {25 scope: ImmerScope26 modified: boolean27 finalized: boolean
264 unmodified lines
292/** Create traps that all use the `Reflect` API on the `latest(state)` */293function makeReflectTraps<T extends object>(294 names: (keyof typeof Reflect)[]295): ProxyHandler<T> {299 return names.reduce(300 (traps, name) => {301 // @ts-ignore302 traps[name] = (state, ...args) => Reflect[name](latest(state), ...args)303 return traps304 },296 return names.reduce((traps, name) => {297 // @ts-ignore298 traps[name] = (state, ...args) => Reflect[name](latest(state), ...args)299 return traps300 }, {} as any)305 {} as any306 )301}