Skip to content

Commit

Permalink
introduce StrictMode enum
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Apr 27, 2024
1 parent 85a8f7b commit 511ccee
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
5 changes: 3 additions & 2 deletions __tests__/not-strict-copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import {
immerable,
produce,
setUseStrictShallowCopy,
setAutoFreeze
setAutoFreeze,
StrictMode
} from "../src/immer"

describe.each([true, false, "class_only" as const])(
"setUseStrictShallowCopy(true)",
(strictMode: boolean | "class_only") => {
(strictMode: StrictMode) => {
test("keep descriptors, mode: " + strictMode, () => {
setUseStrictShallowCopy(strictMode)

Expand Down
8 changes: 5 additions & 3 deletions src/core/immerClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ interface ProducersFns {
produceWithPatches: IProduceWithPatches
}

export type StrictMode = boolean | "class_only";

export class Immer implements ProducersFns {
autoFreeze_: boolean = true
useStrictShallowCopy_: boolean | "class_only" = false
useStrictShallowCopy_: StrictMode = false

constructor(config?: {
autoFreeze?: boolean
useStrictShallowCopy?: boolean | "class_only"
useStrictShallowCopy?: StrictMode
}) {
if (typeof config?.autoFreeze === "boolean")
this.setAutoFreeze(config!.autoFreeze)
Expand Down Expand Up @@ -166,7 +168,7 @@ export class Immer implements ProducersFns {
*
* By default, immer does not copy the object descriptors such as getter, setter and non-enumrable properties.
*/
setUseStrictShallowCopy(value: boolean | "class_only") {
setUseStrictShallowCopy(value: StrictMode) {
this.useStrictShallowCopy_ = value
}

Expand Down
3 changes: 2 additions & 1 deletion src/immer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export {
NOTHING as nothing,
DRAFTABLE as immerable,
freeze,
Objectish
Objectish,
StrictMode
} from "./internal"

const immer = new Immer()
Expand Down
5 changes: 3 additions & 2 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
AnySet,
ImmerState,
ArchType,
die
die,
StrictMode
} from "../internal"

export const getPrototypeOf = Object.getPrototypeOf
Expand Down Expand Up @@ -140,7 +141,7 @@ export function latest(state: ImmerState): any {
}

/*#__PURE__*/
export function shallowCopy(base: any, strict: boolean | "class_only") {
export function shallowCopy(base: any, strict: StrictMode) {
if (isMap(base)) {
return new Map(base)
}
Expand Down

0 comments on commit 511ccee

Please sign in to comment.