applied module ordering pattern
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 8d4f723 · Browse files · 17 files·+232 added−93 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
17 files+232 added−93 removed
- tests:+3 added−3 removed
- other:+229 added−90 removed
Provenance
- Commit
7639c29- Tree
a3dfd5b- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
8d4f723
17 files changed+232 added−93 removed
modified:__tests__/base.js+1 added-1 removed
1 "use strict"2 import {Immer, nothing, original, isDraft, immerable} from "../src/index"3-import {each, shallowCopy, isEnumerable, DRAFT_STATE} from "../src/common"3+import {each, shallowCopy, isEnumerable, DRAFT_STATE} from "../src/internal"4 import deepFreeze from "deep-freeze"5 import cloneDeep from "lodash.clonedeep"6 import * as lodash from "lodash"7 1967 unmodified linesmodified:__tests__/empty.ts+1 added-1 removed
1223456136 unmodified linesExpand allimport {produce, produceWithPatches, setUseProxies} from "../src"import {DRAFT_STATE} from "../src/common"import {DRAFT_STATE} from "../src/internal"
test("empty stub test", () => { expect(true).toBe(true)})136 unmodified linesExpand allmodified:__tests__/polyfills.js+1 added-1 removed
5 unmodified lines678910101112131434 unmodified lines5 unmodified linesObject.assign = undefinedReflect.ownKeys = undefined
jest.resetModules()const common = require("../src/common")const common = require("../src/internal")
// Reset the globals to avoid unintended effects.Symbol = SymbolConstructorObject.assign = assign34 unmodified linesmodified:src/common.ts+10 added-4 removed
1-import {DRAFT_STATE, DRAFTABLE, hasSet} from "./env"1 import {2+ DRAFT_STATE,3+ DRAFTABLE,4+ hasSet,5 Objectish,6 Drafted,7 AnyObject,8 AnyArray,9 AnyMap,10 AnySet,11 ImmerState,12 ProxyType,11- Archtype12-} from "./types"13-import {isMap} from "./map"13+ Archtype,14+ hasMap15+} from "./internal"16 17 /** Returns true if the given value is an Immer draft */18 export function isDraft(value: any): boolean {19 return !!value && !!value[DRAFT_STATE]112 unmodified lines132 return x !== x && y !== y133 }134 }135 136+export function isMap(target: any): target is AnyMap {137+ return hasMap && target instanceof Map138+}139+140 export function isSet(target: any): target is AnySet {141 return hasSet && target instanceof Set142 }143 68 unmodified linesadded:src/env.ts+40 added
1+// Should be no imports here!2+3+// SOme things that should be evaluated before all else...4+const hasSymbol = typeof Symbol !== "undefined"5+export const hasMap = typeof Map !== "undefined"6+export const hasSet = typeof Set !== "undefined"7+8+/**9+ * The sentinel value returned by producers to replace the draft with undefined.10+ */11+export const NOTHING: Nothing = hasSymbol12+ ? Symbol("immer-nothing")13+ : ({["immer-nothing"]: true} as any)14+15+/**16+ * To let Immer treat your class instances as plain immutable objects17+ * (albeit with a custom prototype), you must define either an instance property18+ * or a static property on each of your custom classes.19+ *20+ * Otherwise, your class instance will never be drafted, which means it won't be21+ * safe to mutate in a produce callback.22+ */23+export const DRAFTABLE: unique symbol = hasSymbol24+ ? Symbol("immer-draftable")25+ : ("__$immer_draftable" as any)26+27+export const DRAFT_STATE: unique symbol = hasSymbol28+ ? Symbol("immer-state")29+ : ("__$immer_state" as any)30+31+export const iteratorSymbol: typeof Symbol.iterator = hasSymbol32+ ? Symbol.iterator33+ : ("@@iterator" as any)34+35+/** Use a class type for `nothing` so its type is unique */36+export class Nothing {37+ // This lets us do `Exclude<T, Nothing>`38+ // @ts-ignore39+ private _!: unique symbol40+}modified:src/es5.ts+7 added-10 removed
6 unmodified lines7 isDraftable,8 isEnumerable,9 shallowCopy,10 latest,11- createHiddenProperty12-} from "./common"13-14-import {ImmerScope} from "./scope"15-import {11+ createHiddenProperty,12+ ImmerScope,13 ImmerState,14 Drafted,15 AnyObject,16 Objectish,17 ImmerBaseState,18 AnyArray,22- ProxyType23-} from "./types"24-import {MapState} from "./map"25-import {SetState} from "./set"26-import {DRAFT_STATE} from "./env"19+ ProxyType,20+ MapState,21+ SetState,22+ DRAFT_STATE23+} from "./internal"24 25 interface ES5BaseState extends ImmerBaseState {26 finalizing: boolean27 assigned: {[key: string]: any}276 unmodified linesadded:src/extends.ts+23 added
1+var extendStatics = function(d: any, b: any): any {2+ extendStatics =3+ Object.setPrototypeOf ||4+ ({__proto__: []} instanceof Array &&5+ function(d, b) {6+ d.__proto__ = b7+ }) ||8+ function(d, b) {9+ for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]10+ }11+ return extendStatics(d, b)12+}13+14+// Ugly hack to resolve #502 and inherit built in Map / Set15+export function __extends(d: any, b: any): any {16+ extendStatics(d, b)17+ function __(this: any): any {18+ this.constructor = d19+ }20+ d.prototype =21+ // @ts-ignore22+ ((__.prototype = b.prototype), new __())23+}modified:src/finalize.ts+22 added-8 removed
1-import {Immer} from "./immer"2-import {ImmerState, Drafted, ProxyType} from "./types"3-import {ImmerScope} from "./scope"4-import {isSet, has, is, get, each, freeze, shallowCopy, set} from "./common"5-import {isDraft, isDraftable} from "./index"6-import {SetState} from "./set"7-import {generatePatches, PatchPath} from "./patches"8-import {DRAFT_STATE, NOTHING} from "./env"1+import {2+ Immer,3+ ImmerScope,4+ DRAFT_STATE,5+ isDraftable,6+ NOTHING,7+ Drafted,8+ PatchPath,9+ ProxyType,10+ each,11+ has,12+ freeze,13+ generatePatches,14+ shallowCopy,15+ ImmerState,16+ isSet,17+ isDraft,18+ SetState,19+ set,20+ is,21+ get22+} from "./internal"23 24 export function processResult(immer: Immer, result: any, scope: ImmerScope) {25 const baseDraft = scope.drafts![0]26 const isReplaced = result !== undefined && result !== baseDraft181 unmodified linesmodified:src/immer.ts+25 added-16 removed
1-import {createES5Proxy, willFinalizeES5, markChangedES5} from "./es5"2-import {createProxy, markChanged} from "./proxy"3-4-import {applyPatches} from "./patches"5-import {each, isDraft, isSet, isDraftable, die} from "./common"6-import {ImmerScope} from "./scope"1 import {8- ImmerState,9- IProduce,2+ createES5Proxy,3+ willFinalizeES5,4+ markChangedES5,5 IProduceWithPatches,6+ IProduce,7+ ImmerState,8+ each,9+ Drafted,10+ isDraftable,11+ ImmerScope,12+ processResult,13+ NOTHING,14+ maybeFreeze,15+ die,16+ Patch,17 Objectish,12- PatchListener,18+ DRAFT_STATE,19 Draft,14- Patch,15- Drafted16-} from "./types"17-import {proxyMap, isMap} from "./map"18-import {proxySet} from "./set"19-import {processResult, maybeFreeze} from "./finalize"20-import {NOTHING, DRAFT_STATE} from "./env"20+ PatchListener,21+ isDraft,22+ applyPatches,23+ isMap,24+ proxyMap,25+ isSet,26+ proxySet,27+ createProxy,28+ markChanged29+} from "./internal"30 31 /* istanbul ignore next */32 function verifyMinified() {}33 244 unmodified linesmodified:src/index.ts+12 added-11 removed
1-import {Immer} from "./immer"2-import {IProduce, IProduceWithPatches} from "./types"1+import {IProduce, IProduceWithPatches, Immer} from "./internal"2 4-export {Draft, Immutable, Patch, PatchListener} from "./types"3+export {4+ Draft,5+ Immutable,6+ Patch,7+ PatchListener,8+ original,9+ isDraft,10+ isDraftable,11+ NOTHING as nothing,12+ DRAFTABLE as immerable13+} from "./internal"14 15 const immer = new Immer()16 17 /**63 unmodified lines81 * changes that were made.82 */83 export const finishDraft = immer.finishDraft.bind(immer)84 76-export {77- original,78- isDraft,79- isDraftable,80- NOTHING as nothing,81- DRAFTABLE as immerable82-} from "./common"83-85 export {Immer}added:src/internal.ts+12 added
1+export * from "./env"2+export * from "./extends"3+export * from "./types"4+export * from "./common"5+export * from "./scope"6+export * from "./finalize"7+export * from "./proxy"8+export * from "./es5"9+export * from "./map"10+export * from "./set"11+export * from "./patches"12+export * from "./immer"modified:src/map.ts+14 added-11 removed
1-import {__extends} from "./extends"2-import {isDraftable, latest} from "./common"1+import {2+ __extends,3+ ImmerBaseState,4+ ProxyType,5+ AnyMap,6+ Drafted,7+ ImmerState,8+ DRAFT_STATE,9+ ImmerScope,10+ latest,11+ assertUnrevoked,12+ isDraftable,13+ iteratorSymbol14+} from "./internal"15 4-import {ImmerScope} from "./scope"5-import {AnyMap, Drafted, ImmerState, ImmerBaseState, ProxyType} from "./types"6-import {assertUnrevoked} from "./es5"7-import {DRAFT_STATE, iteratorSymbol, hasMap} from "./env"8-16 export interface MapState extends ImmerBaseState {17 type: ProxyType.Map18 copy: AnyMap | undefined19 assigned: Map<any, boolean> | undefined164 unmodified lines184 state.assigned = new Map()185 state.copy = new Map(state.base)186 }187 }181-182-export function isMap(target: any): target is AnyMap {183- return hasMap && (target instanceof Map || target instanceof DraftMap) // TODO: fix184-}modified:src/patches.ts+18 added-6 removed
1-import {get, each, has, die, getArchtype} from "./common"2-import {Patch, ImmerState, ProxyType, Archtype} from "./types"3-import {SetState} from "./set"4-import {ES5ArrayState, ES5ObjectState} from "./es5"5-import {ProxyArrayState, ProxyObjectState} from "./proxy"6-import {MapState, isMap} from "./map"1+import {2+ get,3+ each,4+ has,5+ die,6+ getArchtype,7+ ImmerState,8+ Patch,9+ ProxyType,10+ SetState,11+ ES5ArrayState,12+ ProxyArrayState,13+ MapState,14+ ES5ObjectState,15+ ProxyObjectState,16+ Archtype,17+ isMap18+} from "./internal"19 20 export type PatchPath = (string | number)[]21 22 export function generatePatches(240 unmodified linesmodified:src/proxy.ts+13 added-8 removed
1 "use strict"2-import {each, has, is, isDraftable, shallowCopy, latest} from "./common"3-import {ImmerScope} from "./scope"2 import {5- AnyObject,6- Drafted,3+ each,4+ has,5+ is,6+ isDraftable,7+ shallowCopy,8+ latest,9+ ImmerBaseState,10 ImmerState,11+ Drafted,12+ ProxyType,13+ AnyObject,14 AnyArray,15 Objectish,10- ImmerBaseState,11- ProxyType12-} from "./types"13-import {DRAFT_STATE} from "./env"16+ ImmerScope,17+ DRAFT_STATE18+} from "./internal"19 20 interface ProxyBaseState extends ImmerBaseState {21 assigned: {22 [property: string]: boolean235 unmodified linesmodified:src/scope.ts+8 added-3 removed
1-import {Patch, PatchListener, Drafted, ProxyType} from "./types"2-import {Immer} from "./immer"3-import {DRAFT_STATE} from "./env"1+import {2+ Patch,3+ PatchListener,4+ Drafted,5+ ProxyType,6+ Immer,7+ DRAFT_STATE8+} from "./internal"9 10 /** Each scope represents a `produce` call. */11 export class ImmerScope {12 static current?: ImmerScope56 unmodified linesmodified:src/set.ts+14 added-7 removed
1-import {__extends} from "./extends"2-import {latest, isDraftable} from "./common"1+import {2+ __extends,3+ ImmerBaseState,4+ ProxyType,5+ AnySet,6+ Drafted,7+ ImmerState,8+ DRAFT_STATE,9+ ImmerScope,10+ latest,11+ assertUnrevoked,12+ iteratorSymbol,13+ isDraftable14+} from "./internal"15 4-import {ImmerScope} from "./scope"5-import {AnySet, Drafted, ImmerState, ImmerBaseState, ProxyType} from "./types"6-import {assertUnrevoked} from "./es5"7-import {DRAFT_STATE, iteratorSymbol} from "./env"8-16 export interface SetState extends ImmerBaseState {17 type: ProxyType.Set18 copy: AnySet | undefined19 base: AnySet146 unmodified linesmodified:src/types.ts+11 added-6 removed
1-import {SetState} from "./set"2-import {MapState} from "./map"3-import {ProxyObjectState, ProxyArrayState} from "./proxy"4-import {ES5ObjectState, ES5ArrayState} from "./es5"5-import {ImmerScope} from "./scope"6-import {DRAFT_STATE, Nothing} from "./env"1+import {2+ SetState,3+ ImmerScope,4+ ProxyObjectState,5+ ProxyArrayState,6+ ES5ObjectState,7+ ES5ArrayState,8+ MapState,9+ DRAFT_STATE,10+ Nothing11+} from "./internal"12 13 export type Objectish = AnyObject | AnyArray | AnyMap | AnySet14 export type ObjectishNoSet = AnyObject | AnyArray | AnyMap15 206 unmodified lines