Skip to content
Myr

fix: #502

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 b2f23c4 · Browse files · 13 files+115 added−131 removed
This commit

State

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

Diff stats

13 files+115 added−131 removed

  • tests:+15 added−0 removed
  • other:+100 added−131 removed

13 files+115 added−131 removed

modified:__tests__/base.js+14 added
580 unmodified lines
581 expect(entries).toEqual([[key, 4]])582 expect(entries[0][0]).toBe(key)583 expect(entries[0][0].a).toBe(2)584 })585586 it("does support instanceof Map", () => {587 const map = new Map()588 produce(map, d => {589 expect(d instanceof Map).toBeTruthy()590 })591 })592 })593594 describe("set drafts", () => {595 it("supports iteration", () => {
259 unmodified lines
855 expect(() => m.add("x")).toThrow(856 "Cannot use a proxy that has been revoked"857 )858 })859860 it("does support instanceof Set", () => {861 const set = new Set()862 produce(set, d => {863 expect(d instanceof Set).toBeTruthy()864 })865 })866 })867868 it("supports `immerable` symbol on constructor", () => {869 class One {}
1105 unmodified lines
modified:__tests__/empty.ts+1 added
modified:src/common.ts+2 added-42 removed
1import {DRAFT_STATE, DRAFTABLE, hasSet} from "./env"2import {3 Objectish,4 Drafted,5 AnyObject,
3 unmodified lines
9 ImmerState,10 ProxyType,11 Archtype12} from "./types"13import {isMap} from "./map"1413/** Use a class type for `nothing` so its type is unique */14export class Nothing {15 // This lets us do `Exclude<T, Nothing>`16 // @ts-ignore17 private _!: unique symbol18}1920const hasSymbol = typeof Symbol !== "undefined"21export const hasMap = typeof Map !== "undefined"22export const hasSet = typeof Set !== "undefined"2324/**25 * The sentinel value returned by producers to replace the draft with undefined.26 */27export const NOTHING: Nothing = hasSymbol28 ? Symbol("immer-nothing")29 : ({["immer-nothing"]: true} as any)3031/**32 * To let Immer treat your class instances as plain immutable objects33 * (albeit with a custom prototype), you must define either an instance property34 * or a static property on each of your custom classes.35 *36 * Otherwise, your class instance will never be drafted, which means it won't be37 * safe to mutate in a produce callback.38 */39export const DRAFTABLE: unique symbol = hasSymbol40 ? Symbol("immer-draftable")41 : ("__$immer_draftable" as any)4243export const DRAFT_STATE: unique symbol = hasSymbol44 ? Symbol("immer-state")45 : ("__$immer_state" as any)4647export const iteratorSymbol: typeof Symbol.iterator = hasSymbol48 ? Symbol.iterator49 : ("@@iterator" as any)5015/** Returns true if the given value is an Immer draft */16export function isDraft(value: any): boolean {17 return !!value && !!value[DRAFT_STATE]18}
111 unmodified lines
130 return x !== x && y !== y131 }132}133170export function isMap(target: any): target is AnyMap {171 return hasMap && target instanceof Map172}173134export function isSet(target: any): target is AnySet {135 return hasSet && target instanceof Set136}137
68 unmodified lines
modified:src/es5.ts+1 added-1 removed
5 unmodified lines
6 isDraft,7 isDraftable,8 isEnumerable,9 shallowCopy,10 DRAFT_STATE,10 latest,11 createHiddenProperty12} from "./common"13
8 unmodified lines
22 ProxyType23} from "./types"24import {MapState} from "./map"25import {SetState} from "./set"26import {DRAFT_STATE} from "./env"2728interface ES5BaseState extends ImmerBaseState {29 finalizing: boolean30 assigned: {[key: string]: any}
276 unmodified lines
modified:src/finalize.ts+2 added-12 removed
1import {Immer} from "./immer"2import {ImmerState, Drafted, ProxyType} from "./types"3import {ImmerScope} from "./scope"4import {5 isSet,6 has,7 is,8 get,9 each,10 DRAFT_STATE,11 NOTHING,12 freeze,13 shallowCopy,14 set15} from "./common"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"910export function processResult(immer: Immer, result: any, scope: ImmerScope) {11 const baseDraft = scope.drafts![0]12 const isReplaced = result !== undefined && result !== baseDraft
181 unmodified lines
modified:src/immer.ts+3 added-11 removed
1import {createES5Proxy, willFinalizeES5, markChangedES5} from "./es5"2import {createProxy, markChanged} from "./proxy"34import {applyPatches} from "./patches"5import {6 each,7 isDraft,8 isSet,9 isMap,10 isDraftable,11 DRAFT_STATE,12 NOTHING,13 die14} from "./common"5import {each, isDraft, isSet, isDraftable, die} from "./common"6import {ImmerScope} from "./scope"7import {8 ImmerState,9 IProduce,
3 unmodified lines
13 Draft,14 Patch,15 Drafted16} from "./types"26import {proxyMap} from "./map"17import {proxyMap, isMap} from "./map"18import {proxySet} from "./set"19import {processResult, maybeFreeze} from "./finalize"20import {NOTHING, DRAFT_STATE} from "./env"2122/* istanbul ignore next */23function verifyMinified() {}24
244 unmodified lines
modified:src/map.ts+48 added-27 removed
1import {isDraftable, DRAFT_STATE, latest, iteratorSymbol} from "./common"1import {__extends} from "./extends"2import {isDraftable, latest} from "./common"34import {ImmerScope} from "./scope"5import {AnyMap, Drafted, ImmerState, ImmerBaseState, ProxyType} from "./types"6import {assertUnrevoked} from "./es5"7import {DRAFT_STATE, iteratorSymbol, hasMap} from "./env"89export interface MapState extends ImmerBaseState {10 type: ProxyType.Map11 copy: AnyMap | undefined
2 unmodified lines
14 revoked: boolean15 draft: Drafted<AnyMap, MapState>16}1716// Make sure DraftMap declarion doesn't die if Map is not avialable...17/* istanbul ignore next */18const MapBase: MapConstructor =19 typeof Map !== "undefined" ? Map : (function FakeMap() {} as any)2021export class DraftMap<K, V> extends MapBase implements Map<K, V> {22 [DRAFT_STATE]: MapState23 constructor(target: AnyMap, parent?: ImmerState) {24 super()18const DraftMap = (function(_super) {19 if (!_super) {20 /* istanbul ignore next */21 throw new Error("Map is not polyfilled")22 }23 __extends(DraftMap, _super)24 // Create class manually, cause #50225 function DraftMap(this: any, target: AnyMap, parent?: ImmerState): any {26 this[DRAFT_STATE] = {27 type: ProxyType.Map,28 parent,29 scope: parent ? parent.scope : ImmerScope.current!,
1 unmodified line
31 finalized: false,32 copy: undefined,33 assigned: undefined,34 base: target,34 draft: this,35 draft: this as any,36 isManual: false,37 revoked: false38 }39 return this40 }41 const p = DraftMap.prototype4243 // TODO: smaller build size if we create a util for Object.defineProperty44 Object.defineProperty(p, "size", {40 get size(): number {41 return latest(this[DRAFT_STATE]).size42 }45 get: function() {46 return latest(this[DRAFT_STATE]).size47 },48 enumerable: true,49 configurable: true50 })5144 has(key: K): boolean {52 p.has = function(key: any): boolean {53 return latest(this[DRAFT_STATE]).has(key)54 }5548 set(key: K, value: V): this {56 p.set = function(key: any, value: any) {57 const state = this[DRAFT_STATE]58 assertUnrevoked(state)59 if (latest(state).get(key) !== value) {60 prepareCopy(state)
4 unmodified lines
65 }66 return this67 }6861 delete(key: K): boolean {69 p.delete = function(key: any): boolean {70 if (!this.has(key)) {71 return false72 }73
5 unmodified lines
79 state.copy!.delete(key)80 return true81 }8275 clear() {83 p.clear = function() {84 const state = this[DRAFT_STATE]85 assertUnrevoked(state)86 prepareCopy(state)87 state.scope.immer.markChanged(state)
3 unmodified lines
91 }92 return state.copy!.clear()93 }9487 forEach(cb: (value: V, key: K, self: this) => void, thisArg?: any) {95 p.forEach = function(96 cb: (value: any, key: any, self: any) => void,97 thisArg?: any98 ) {99 const state = this[DRAFT_STATE]89 latest(state).forEach((_value: V, key: K, _map: this) => {100 latest(state).forEach((_value: any, key: any, _map: any) => {101 cb.call(thisArg, this.get(key), key, this)102 })103 }10494 get(key: K): V {105 p.get = function(key: any): any {106 const state = this[DRAFT_STATE]107 assertUnrevoked(state)108 const value = latest(state).get(key)109 if (state.finalized || !isDraftable(value)) {
8 unmodified lines
118 state.copy!.set(key, draft)119 return draft120 }121111 keys(): IterableIterator<K> {122 p.keys = function(): IterableIterator<any> {123 return latest(this[DRAFT_STATE]).keys()124 }125115 values(): IterableIterator<V> {126 p.values = function(): IterableIterator<any> {127 const iterator = this.keys()128 return {129 [iteratorSymbol]: () => this.values(),130 next: () => {
8 unmodified lines
139 }140 } as any141 }142132 entries(): IterableIterator<[K, V]> {143 p.entries = function(): IterableIterator<[any, any]> {144 const iterator = this.keys()145 return {146 [iteratorSymbol]: () => this.entries(),147 next: () => {
8 unmodified lines
156 }157 } as any158 }159149 [iteratorSymbol]() {160 p[iteratorSymbol] = function() {161 return this.entries()162 }152}163154export function proxyMap(target: AnyMap, parent?: ImmerState) {164 return DraftMap165})(Map)166167export function proxyMap<T extends AnyMap>(168 target: T,169 parent?: ImmerState170): T & {[DRAFT_STATE]: MapState} {171 // @ts-ignore172 return new DraftMap(target, parent)173}174175function prepareCopy(state: MapState) {
1 unmodified line
177 state.assigned = new Map()178 state.copy = new Map(state.base)179 }180}181182export function isMap(target: any): target is AnyMap {183 return hasMap && (target instanceof Map || target instanceof DraftMap) // TODO: fix184}
modified:src/patches.ts+2 added-2 removed
1import {get, each, isMap, has, die, getArchtype} from "./common"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} from "./map"6import {MapState, isMap} from "./map"78export type PatchPath = (string | number)[]910export function generatePatches(
240 unmodified lines
modified:src/proxy.ts+2 added-9 removed
1"use strict"2import {3 each,4 has,5 is,6 isDraftable,7 shallowCopy,8 DRAFT_STATE,9 latest10} from "./common"2import {each, has, is, isDraftable, shallowCopy, latest} from "./common"3import {ImmerScope} from "./scope"4import {5 AnyObject,6 Drafted,
2 unmodified lines
9 Objectish,10 ImmerBaseState,11 ProxyType12} from "./types"13import {DRAFT_STATE} from "./env"1415interface ProxyBaseState extends ImmerBaseState {16 assigned: {17 [property: string]: boolean
235 unmodified lines
modified:src/scope.ts+1 added-1 removed
1import {DRAFT_STATE} from "./common"1import {Patch, PatchListener, Drafted, ProxyType} from "./types"2import {Immer} from "./immer"3import {DRAFT_STATE} from "./env"45/** Each scope represents a `produce` call. */6export class ImmerScope {7 static current?: ImmerScope
56 unmodified lines
modified:src/set.ts+37 added-24 removed
1import {DRAFT_STATE, latest, isDraftable, iteratorSymbol} from "./common"1import {__extends} from "./extends"2import {latest, isDraftable} from "./common"34import {ImmerScope} from "./scope"5import {AnySet, Drafted, ImmerState, ImmerBaseState, ProxyType} from "./types"6import {assertUnrevoked} from "./es5"7import {DRAFT_STATE, iteratorSymbol} from "./env"89export interface SetState extends ImmerBaseState {10 type: ProxyType.Set11 copy: AnySet | undefined
2 unmodified lines
14 revoked: boolean15 draft: Drafted<AnySet, SetState>16}1716// Make sure DraftSet declarion doesn't die if Map is not avialable...17/* istanbul ignore next */18const SetBase: SetConstructor =19 typeof Set !== "undefined" ? Set : (function FakeSet() {} as any)2021export class DraftSet<K, V> extends SetBase implements Set<V> {22 [DRAFT_STATE]: SetState23 constructor(target: AnySet, parent?: ImmerState) {24 super()18const DraftSet = (function(_super) {19 if (!_super) {20 /* istanbul ignore next */21 throw new Error("Set is not polyfilled")22 }23 __extends(DraftSet, _super)24 // Create class manually, cause #50225 function DraftSet(this: any, target: AnySet, parent?: ImmerState) {26 this[DRAFT_STATE] = {27 type: ProxyType.Set,28 parent,29 scope: parent ? parent.scope : ImmerScope.current!,
5 unmodified lines
35 drafts: new Map(),36 revoked: false,37 isManual: false38 }39 return this40 }41 const p = DraftSet.prototype4243 Object.defineProperty(p, "size", {40 get size(): number {41 return latest(this[DRAFT_STATE]).size42 }44 get: function() {45 return latest(this[DRAFT_STATE]).size46 },47 enumerable: true,48 configurable: true49 })5044 has(value: V): boolean {51 p.has = function(value: any): boolean {52 const state = this[DRAFT_STATE]53 assertUnrevoked(state)54 // bit of trickery here, to be able to recognize both the value, and the draft of its value55 if (!state.copy) {
4 unmodified lines
60 return true61 return false62 }6357 add(value: V): this {64 p.add = function(value: any): any {65 const state = this[DRAFT_STATE]66 assertUnrevoked(state)67 if (state.copy) {68 state.copy.add(value)
4 unmodified lines
73 }74 return this75 }7670 delete(value: V): boolean {77 p.delete = function(value: any): any {78 if (!this.has(value)) {79 return false80 }81
8 unmodified lines
90 : /* istanbul ignore next */ false)91 )92 }9387 clear() {94 p.clear = function() {95 const state = this[DRAFT_STATE]96 assertUnrevoked(state)97 prepareCopy(state)98 state.scope.immer.markChanged(state)99 return state.copy!.clear()100 }10195 values(): IterableIterator<V> {102 p.values = function(): IterableIterator<any> {103 const state = this[DRAFT_STATE]104 assertUnrevoked(state)105 prepareCopy(state)106 return state.copy!.values()107 }108102 entries(): IterableIterator<[V, V]> {109 p.entries = function entries(): IterableIterator<[any, any]> {110 const state = this[DRAFT_STATE]111 assertUnrevoked(state)112 prepareCopy(state)113 return state.copy!.entries()114 }115109 keys(): IterableIterator<V> {116 p.keys = function(): IterableIterator<any> {117 return this.values()118 }119113 [iteratorSymbol]() {120 p[iteratorSymbol] = function() {121 return this.values()122 }123117 forEach(cb: (value: V, key: V, self: this) => void, thisArg?: any) {124 p.forEach = function forEach(cb: any, thisArg?: any) {125 const iterator = this.values()126 let result = iterator.next()127 while (!result.done) {128 cb.call(thisArg, result.value, result.value, this)129 result = iterator.next()130 }131 }125}132127export function proxySet(target: AnySet, parent?: ImmerState) {133 return DraftSet134})(Set)135136export function proxySet<T extends AnySet>(137 target: T,138 parent?: ImmerState139): T & {[DRAFT_STATE]: SetState} {140 // @ts-ignore141 return new DraftSet(target, parent)142}143144function prepareCopy(state: SetState) {
14 unmodified lines
modified:src/types.ts+1 added-1 removed
1import {Nothing, DRAFT_STATE} from "./common"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"78export type Objectish = AnyObject | AnyArray | AnyMap | AnySet9export type ObjectishNoSet = AnyObject | AnyArray | AnyMap10
206 unmodified lines
modified:tsconfig.json+1 added-1 removed
1{2 "compilerOptions": {3 "lib": ["es2015"],4 "target": "ES6",4 "target": "ES5",5 "strict": true,6 "declaration": true,7 "importHelpers": false,8 "noImplicitAny": true,
9 unmodified lines