Skip to content
Myr

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

17 files+232 added−93 removed

modified:__tests__/base.js+1 added-1 removed
1"use strict"2import {Immer, nothing, original, isDraft, immerable} from "../src/index"3import {each, shallowCopy, isEnumerable, DRAFT_STATE} from "../src/common"3import {each, shallowCopy, isEnumerable, DRAFT_STATE} from "../src/internal"4import deepFreeze from "deep-freeze"5import cloneDeep from "lodash.clonedeep"6import * as lodash from "lodash"7
1967 unmodified lines
modified:__tests__/empty.ts+1 added-1 removed
modified:__tests__/polyfills.js+1 added-1 removed
modified:src/common.ts+10 added-4 removed
1import {DRAFT_STATE, DRAFTABLE, hasSet} from "./env"1import {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"13import {isMap} from "./map"13 Archtype,14 hasMap15} from "./internal"1617/** Returns true if the given value is an Immer draft */18export function isDraft(value: any): boolean {19 return !!value && !!value[DRAFT_STATE]
112 unmodified lines
132 return x !== x && y !== y133 }134}135136export function isMap(target: any): target is AnyMap {137 return hasMap && target instanceof Map138}139140export function isSet(target: any): target is AnySet {141 return hasSet && target instanceof Set142}143
68 unmodified lines
added:src/env.ts+40 added
1// Should be no imports here!23// SOme things that should be evaluated before all else...4const hasSymbol = typeof Symbol !== "undefined"5export const hasMap = typeof Map !== "undefined"6export const hasSet = typeof Set !== "undefined"78/**9 * The sentinel value returned by producers to replace the draft with undefined.10 */11export const NOTHING: Nothing = hasSymbol12 ? Symbol("immer-nothing")13 : ({["immer-nothing"]: true} as any)1415/**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 */23export const DRAFTABLE: unique symbol = hasSymbol24 ? Symbol("immer-draftable")25 : ("__$immer_draftable" as any)2627export const DRAFT_STATE: unique symbol = hasSymbol28 ? Symbol("immer-state")29 : ("__$immer_state" as any)3031export const iteratorSymbol: typeof Symbol.iterator = hasSymbol32 ? Symbol.iterator33 : ("@@iterator" as any)3435/** Use a class type for `nothing` so its type is unique */36export 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 lines
7 isDraftable,8 isEnumerable,9 shallowCopy,10 latest,11 createHiddenProperty12} from "./common"1314import {ImmerScope} from "./scope"15import {11 createHiddenProperty,12 ImmerScope,13 ImmerState,14 Drafted,15 AnyObject,16 Objectish,17 ImmerBaseState,18 AnyArray,22 ProxyType23} from "./types"24import {MapState} from "./map"25import {SetState} from "./set"26import {DRAFT_STATE} from "./env"19 ProxyType,20 MapState,21 SetState,22 DRAFT_STATE23} from "./internal"2425interface ES5BaseState extends ImmerBaseState {26 finalizing: boolean27 assigned: {[key: string]: any}
276 unmodified lines
added:src/extends.ts+23 added
1var 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}1314// Ugly hack to resolve #502 and inherit built in Map / Set15export 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
1import {Immer} from "./immer"2import {ImmerState, Drafted, ProxyType} from "./types"3import {ImmerScope} from "./scope"4import {isSet, has, is, get, each, freeze, shallowCopy, set} from "./common"5import {isDraft, isDraftable} from "./index"6import {SetState} from "./set"7import {generatePatches, PatchPath} from "./patches"8import {DRAFT_STATE, NOTHING} from "./env"1import {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"2324export function processResult(immer: Immer, result: any, scope: ImmerScope) {25 const baseDraft = scope.drafts![0]26 const isReplaced = result !== undefined && result !== baseDraft
181 unmodified lines
modified:src/immer.ts+25 added-16 removed
1import {createES5Proxy, willFinalizeES5, markChangedES5} from "./es5"2import {createProxy, markChanged} from "./proxy"34import {applyPatches} from "./patches"5import {each, isDraft, isSet, isDraftable, die} from "./common"6import {ImmerScope} from "./scope"1import {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"17import {proxyMap, isMap} from "./map"18import {proxySet} from "./set"19import {processResult, maybeFreeze} from "./finalize"20import {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"3031/* istanbul ignore next */32function verifyMinified() {}33
244 unmodified lines
modified:src/index.ts+12 added-11 removed
1import {Immer} from "./immer"2import {IProduce, IProduceWithPatches} from "./types"1import {IProduce, IProduceWithPatches, Immer} from "./internal"24export {Draft, Immutable, Patch, PatchListener} from "./types"3export {4 Draft,5 Immutable,6 Patch,7 PatchListener,8 original,9 isDraft,10 isDraftable,11 NOTHING as nothing,12 DRAFTABLE as immerable13} from "./internal"1415const immer = new Immer()1617/**
63 unmodified lines
81 * changes that were made.82 */83export const finishDraft = immer.finishDraft.bind(immer)8476export {77 original,78 isDraft,79 isDraftable,80 NOTHING as nothing,81 DRAFTABLE as immerable82} from "./common"8385export {Immer}
added:src/internal.ts+12 added
1export * from "./env"2export * from "./extends"3export * from "./types"4export * from "./common"5export * from "./scope"6export * from "./finalize"7export * from "./proxy"8export * from "./es5"9export * from "./map"10export * from "./set"11export * from "./patches"12export * from "./immer"
modified:src/map.ts+14 added-11 removed
1import {__extends} from "./extends"2import {isDraftable, latest} from "./common"1import {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"154import {ImmerScope} from "./scope"5import {AnyMap, Drafted, ImmerState, ImmerBaseState, ProxyType} from "./types"6import {assertUnrevoked} from "./es5"7import {DRAFT_STATE, iteratorSymbol, hasMap} from "./env"816export interface MapState extends ImmerBaseState {17 type: ProxyType.Map18 copy: AnyMap | undefined19 assigned: Map<any, boolean> | undefined
164 unmodified lines
184 state.assigned = new Map()185 state.copy = new Map(state.base)186 }187}181182export 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
1import {get, each, has, die, getArchtype} from "./common"2import {Patch, ImmerState, ProxyType, Archtype} from "./types"3import {SetState} from "./set"4import {ES5ArrayState, ES5ObjectState} from "./es5"5import {ProxyArrayState, ProxyObjectState} from "./proxy"6import {MapState, isMap} from "./map"1import {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"1920export type PatchPath = (string | number)[]2122export function generatePatches(
240 unmodified lines
modified:src/proxy.ts+13 added-8 removed
1"use strict"2import {each, has, is, isDraftable, shallowCopy, latest} from "./common"3import {ImmerScope} from "./scope"2import {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"13import {DRAFT_STATE} from "./env"16 ImmerScope,17 DRAFT_STATE18} from "./internal"1920interface ProxyBaseState extends ImmerBaseState {21 assigned: {22 [property: string]: boolean
235 unmodified lines
modified:src/scope.ts+8 added-3 removed
1import {Patch, PatchListener, Drafted, ProxyType} from "./types"2import {Immer} from "./immer"3import {DRAFT_STATE} from "./env"1import {2 Patch,3 PatchListener,4 Drafted,5 ProxyType,6 Immer,7 DRAFT_STATE8} from "./internal"910/** Each scope represents a `produce` call. */11export class ImmerScope {12 static current?: ImmerScope
56 unmodified lines
modified:src/set.ts+14 added-7 removed
1import {__extends} from "./extends"2import {latest, isDraftable} from "./common"1import {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"154import {ImmerScope} from "./scope"5import {AnySet, Drafted, ImmerState, ImmerBaseState, ProxyType} from "./types"6import {assertUnrevoked} from "./es5"7import {DRAFT_STATE, iteratorSymbol} from "./env"816export interface SetState extends ImmerBaseState {17 type: ProxyType.Set18 copy: AnySet | undefined19 base: AnySet
146 unmodified lines
modified:src/types.ts+11 added-6 removed
1import {SetState} from "./set"2import {MapState} from "./map"3import {ProxyObjectState, ProxyArrayState} from "./proxy"4import {ES5ObjectState, ES5ArrayState} from "./es5"5import {ImmerScope} from "./scope"6import {DRAFT_STATE, Nothing} from "./env"1import {2 SetState,3 ImmerScope,4 ProxyObjectState,5 ProxyArrayState,6 ES5ObjectState,7 ES5ArrayState,8 MapState,9 DRAFT_STATE,10 Nothing11} from "./internal"1213export type Objectish = AnyObject | AnyArray | AnyMap | AnySet14export type ObjectishNoSet = AnyObject | AnyArray | AnyMap15
206 unmodified lines