Skip to content

Commit

Permalink
applied module ordering pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Jan 10, 2020
1 parent 8d4f723 commit 7639c29
Show file tree
Hide file tree
Showing 17 changed files with 232 additions and 93 deletions.
2 changes: 1 addition & 1 deletion __tests__/base.js
@@ -1,6 +1,6 @@
"use strict"
import {Immer, nothing, original, isDraft, immerable} from "../src/index"
import {each, shallowCopy, isEnumerable, DRAFT_STATE} from "../src/common"
import {each, shallowCopy, isEnumerable, DRAFT_STATE} from "../src/internal"
import deepFreeze from "deep-freeze"
import cloneDeep from "lodash.clonedeep"
import * as lodash from "lodash"
Expand Down
2 changes: 1 addition & 1 deletion __tests__/empty.ts
@@ -1,5 +1,5 @@
import {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)
Expand Down
2 changes: 1 addition & 1 deletion __tests__/polyfills.js
Expand Up @@ -7,7 +7,7 @@ Object.assign = undefined
Reflect.ownKeys = undefined

jest.resetModules()
const common = require("../src/common")
const common = require("../src/internal")

// Reset the globals to avoid unintended effects.
Symbol = SymbolConstructor
Expand Down
14 changes: 10 additions & 4 deletions src/common.ts
@@ -1,5 +1,7 @@
import {DRAFT_STATE, DRAFTABLE, hasSet} from "./env"
import {
DRAFT_STATE,
DRAFTABLE,
hasSet,
Objectish,
Drafted,
AnyObject,
Expand All @@ -8,9 +10,9 @@ import {
AnySet,
ImmerState,
ProxyType,
Archtype
} from "./types"
import {isMap} from "./map"
Archtype,
hasMap
} from "./internal"

/** Returns true if the given value is an Immer draft */
export function isDraft(value: any): boolean {
Expand Down Expand Up @@ -131,6 +133,10 @@ export function is(x: any, y: any): boolean {
}
}

export function isMap(target: any): target is AnyMap {
return hasMap && target instanceof Map
}

export function isSet(target: any): target is AnySet {
return hasSet && target instanceof Set
}
Expand Down
40 changes: 40 additions & 0 deletions src/env.ts
@@ -0,0 +1,40 @@
// Should be no imports here!

// SOme things that should be evaluated before all else...
const hasSymbol = typeof Symbol !== "undefined"
export const hasMap = typeof Map !== "undefined"
export const hasSet = typeof Set !== "undefined"

/**
* The sentinel value returned by producers to replace the draft with undefined.
*/
export const NOTHING: Nothing = hasSymbol
? Symbol("immer-nothing")
: ({["immer-nothing"]: true} as any)

/**
* To let Immer treat your class instances as plain immutable objects
* (albeit with a custom prototype), you must define either an instance property
* or a static property on each of your custom classes.
*
* Otherwise, your class instance will never be drafted, which means it won't be
* safe to mutate in a produce callback.
*/
export const DRAFTABLE: unique symbol = hasSymbol
? Symbol("immer-draftable")
: ("__$immer_draftable" as any)

export const DRAFT_STATE: unique symbol = hasSymbol
? Symbol("immer-state")
: ("__$immer_state" as any)

export const iteratorSymbol: typeof Symbol.iterator = hasSymbol
? Symbol.iterator
: ("@@iterator" as any)

/** Use a class type for `nothing` so its type is unique */
export class Nothing {
// This lets us do `Exclude<T, Nothing>`
// @ts-ignore
private _!: unique symbol
}
17 changes: 7 additions & 10 deletions src/es5.ts
Expand Up @@ -8,22 +8,19 @@ import {
isEnumerable,
shallowCopy,
latest,
createHiddenProperty
} from "./common"

import {ImmerScope} from "./scope"
import {
createHiddenProperty,
ImmerScope,
ImmerState,
Drafted,
AnyObject,
Objectish,
ImmerBaseState,
AnyArray,
ProxyType
} from "./types"
import {MapState} from "./map"
import {SetState} from "./set"
import {DRAFT_STATE} from "./env"
ProxyType,
MapState,
SetState,
DRAFT_STATE
} from "./internal"

interface ES5BaseState extends ImmerBaseState {
finalizing: boolean
Expand Down
23 changes: 23 additions & 0 deletions src/extends.ts
@@ -0,0 +1,23 @@
var extendStatics = function(d: any, b: any): any {
extendStatics =
Object.setPrototypeOf ||
({__proto__: []} instanceof Array &&
function(d, b) {
d.__proto__ = b
}) ||
function(d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]
}
return extendStatics(d, b)
}

// Ugly hack to resolve #502 and inherit built in Map / Set
export function __extends(d: any, b: any): any {
extendStatics(d, b)
function __(this: any): any {
this.constructor = d
}
d.prototype =
// @ts-ignore
((__.prototype = b.prototype), new __())
}
30 changes: 22 additions & 8 deletions src/finalize.ts
@@ -1,11 +1,25 @@
import {Immer} from "./immer"
import {ImmerState, Drafted, ProxyType} from "./types"
import {ImmerScope} from "./scope"
import {isSet, has, is, get, each, freeze, shallowCopy, set} from "./common"
import {isDraft, isDraftable} from "./index"
import {SetState} from "./set"
import {generatePatches, PatchPath} from "./patches"
import {DRAFT_STATE, NOTHING} from "./env"
import {
Immer,
ImmerScope,
DRAFT_STATE,
isDraftable,
NOTHING,
Drafted,
PatchPath,
ProxyType,
each,
has,
freeze,
generatePatches,
shallowCopy,
ImmerState,
isSet,
isDraft,
SetState,
set,
is,
get
} from "./internal"

export function processResult(immer: Immer, result: any, scope: ImmerScope) {
const baseDraft = scope.drafts![0]
Expand Down
41 changes: 25 additions & 16 deletions src/immer.ts
@@ -1,23 +1,32 @@
import {createES5Proxy, willFinalizeES5, markChangedES5} from "./es5"
import {createProxy, markChanged} from "./proxy"

import {applyPatches} from "./patches"
import {each, isDraft, isSet, isDraftable, die} from "./common"
import {ImmerScope} from "./scope"
import {
ImmerState,
IProduce,
createES5Proxy,
willFinalizeES5,
markChangedES5,
IProduceWithPatches,
IProduce,
ImmerState,
each,
Drafted,
isDraftable,
ImmerScope,
processResult,
NOTHING,
maybeFreeze,
die,
Patch,
Objectish,
PatchListener,
DRAFT_STATE,
Draft,
Patch,
Drafted
} from "./types"
import {proxyMap, isMap} from "./map"
import {proxySet} from "./set"
import {processResult, maybeFreeze} from "./finalize"
import {NOTHING, DRAFT_STATE} from "./env"
PatchListener,
isDraft,
applyPatches,
isMap,
proxyMap,
isSet,
proxySet,
createProxy,
markChanged
} from "./internal"

/* istanbul ignore next */
function verifyMinified() {}
Expand Down
23 changes: 12 additions & 11 deletions src/index.ts
@@ -1,7 +1,16 @@
import {Immer} from "./immer"
import {IProduce, IProduceWithPatches} from "./types"
import {IProduce, IProduceWithPatches, Immer} from "./internal"

export {Draft, Immutable, Patch, PatchListener} from "./types"
export {
Draft,
Immutable,
Patch,
PatchListener,
original,
isDraft,
isDraftable,
NOTHING as nothing,
DRAFTABLE as immerable
} from "./internal"

const immer = new Immer()

Expand Down Expand Up @@ -73,12 +82,4 @@ export const createDraft = immer.createDraft.bind(immer)
*/
export const finishDraft = immer.finishDraft.bind(immer)

export {
original,
isDraft,
isDraftable,
NOTHING as nothing,
DRAFTABLE as immerable
} from "./common"

export {Immer}
12 changes: 12 additions & 0 deletions src/internal.ts
@@ -0,0 +1,12 @@
export * from "./env"
export * from "./extends"
export * from "./types"
export * from "./common"
export * from "./scope"
export * from "./finalize"
export * from "./proxy"
export * from "./es5"
export * from "./map"
export * from "./set"
export * from "./patches"
export * from "./immer"
25 changes: 14 additions & 11 deletions src/map.ts
@@ -1,10 +1,17 @@
import {__extends} from "./extends"
import {isDraftable, latest} from "./common"

import {ImmerScope} from "./scope"
import {AnyMap, Drafted, ImmerState, ImmerBaseState, ProxyType} from "./types"
import {assertUnrevoked} from "./es5"
import {DRAFT_STATE, iteratorSymbol, hasMap} from "./env"
import {
__extends,
ImmerBaseState,
ProxyType,
AnyMap,
Drafted,
ImmerState,
DRAFT_STATE,
ImmerScope,
latest,
assertUnrevoked,
isDraftable,
iteratorSymbol
} from "./internal"

export interface MapState extends ImmerBaseState {
type: ProxyType.Map
Expand Down Expand Up @@ -178,7 +185,3 @@ function prepareCopy(state: MapState) {
state.copy = new Map(state.base)
}
}

export function isMap(target: any): target is AnyMap {
return hasMap && (target instanceof Map || target instanceof DraftMap) // TODO: fix
}
24 changes: 18 additions & 6 deletions src/patches.ts
@@ -1,9 +1,21 @@
import {get, each, has, die, getArchtype} from "./common"
import {Patch, ImmerState, ProxyType, Archtype} from "./types"
import {SetState} from "./set"
import {ES5ArrayState, ES5ObjectState} from "./es5"
import {ProxyArrayState, ProxyObjectState} from "./proxy"
import {MapState, isMap} from "./map"
import {
get,
each,
has,
die,
getArchtype,
ImmerState,
Patch,
ProxyType,
SetState,
ES5ArrayState,
ProxyArrayState,
MapState,
ES5ObjectState,
ProxyObjectState,
Archtype,
isMap
} from "./internal"

export type PatchPath = (string | number)[]

Expand Down
21 changes: 13 additions & 8 deletions src/proxy.ts
@@ -1,16 +1,21 @@
"use strict"
import {each, has, is, isDraftable, shallowCopy, latest} from "./common"
import {ImmerScope} from "./scope"
import {
AnyObject,
Drafted,
each,
has,
is,
isDraftable,
shallowCopy,
latest,
ImmerBaseState,
ImmerState,
Drafted,
ProxyType,
AnyObject,
AnyArray,
Objectish,
ImmerBaseState,
ProxyType
} from "./types"
import {DRAFT_STATE} from "./env"
ImmerScope,
DRAFT_STATE
} from "./internal"

interface ProxyBaseState extends ImmerBaseState {
assigned: {
Expand Down
11 changes: 8 additions & 3 deletions src/scope.ts
@@ -1,6 +1,11 @@
import {Patch, PatchListener, Drafted, ProxyType} from "./types"
import {Immer} from "./immer"
import {DRAFT_STATE} from "./env"
import {
Patch,
PatchListener,
Drafted,
ProxyType,
Immer,
DRAFT_STATE
} from "./internal"

/** Each scope represents a `produce` call. */
export class ImmerScope {
Expand Down

0 comments on commit 7639c29

Please sign in to comment.