Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: compatibility with Secure EcmaScript #914

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 114 additions & 49 deletions src/plugins/mapset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export function enableMapSet() {
function __extends(d: any, b: any): any {
extendStatics(d, b)
function __(this: any): any {
this.constructor = d
Object.defineProperty(this, "constructor", {
value: d
})
}
d.prototype =
// @ts-ignore
Expand Down Expand Up @@ -65,19 +67,24 @@ export function enableMapSet() {
}
const p = DraftMap.prototype

Object.defineProperty(p, "size", {
Object.defineProperties(p, {
size: {
get: function() {
return latest(this[DRAFT_STATE]).size
}
// enumerable: false,
// configurable: true
})

p.has = function(key: any): boolean {
},
configurable: true
},
has: {
configurable: true,
writable: true,
value: function(key: any): boolean {
return latest(this[DRAFT_STATE]).has(key)
}

p.set = function(key: any, value: any) {
},
set: {
configurable: true,
writable: true,
value: function(key: any, value: any) {
const state: MapState = this[DRAFT_STATE]
assertUnrevoked(state)
if (!latest(state).has(key) || latest(state).get(key) !== value) {
Expand All @@ -89,8 +96,11 @@ export function enableMapSet() {
}
return this
}

p.delete = function(key: any): boolean {
},
delete: {
configurable: true,
writable: true,
value: function(key: any): boolean {
if (!this.has(key)) {
return false
}
Expand All @@ -107,8 +117,11 @@ export function enableMapSet() {
state.copy_!.delete(key)
return true
}

p.clear = function() {
},
clear: {
configurable: true,
writable: true,
value: function() {
const state: MapState = this[DRAFT_STATE]
assertUnrevoked(state)
if (latest(state).size) {
Expand All @@ -121,8 +134,11 @@ export function enableMapSet() {
state.copy_!.clear()
}
}

p.forEach = function(
},
forEach: {
configurable: true,
writable: true,
value: function(
cb: (value: any, key: any, self: any) => void,
thisArg?: any
) {
Expand All @@ -131,8 +147,11 @@ export function enableMapSet() {
cb.call(thisArg, this.get(key), key, this)
})
}

p.get = function(key: any): any {
},
get: {
configurable: true,
writable: true,
value: function(key: any): any {
const state: MapState = this[DRAFT_STATE]
assertUnrevoked(state)
const value = latest(state).get(key)
Expand All @@ -148,12 +167,18 @@ export function enableMapSet() {
state.copy_!.set(key, draft)
return draft
}

p.keys = function(): IterableIterator<any> {
},
keys: {
configurable: true,
writable: true,
value: function(): IterableIterator<any> {
return latest(this[DRAFT_STATE]).keys()
}

p.values = function(): IterableIterator<any> {
},
values: {
configurable: true,
writable: true,
value: function(): IterableIterator<any> {
const iterator = this.keys()
return {
[iteratorSymbol]: () => this.values(),
Expand All @@ -169,8 +194,11 @@ export function enableMapSet() {
}
} as any
}

p.entries = function(): IterableIterator<[any, any]> {
},
entries: {
configurable: true,
writable: true,
value: function(): IterableIterator<[any, any]> {
const iterator = this.keys()
return {
[iteratorSymbol]: () => this.entries(),
Expand All @@ -186,10 +214,15 @@ export function enableMapSet() {
}
} as any
}

p[iteratorSymbol] = function() {
},
[iteratorSymbol]: {
configurable: true,
writable: true,
value: function() {
return this.entries()
}
}
})

return DraftMap
})(Map)
Expand Down Expand Up @@ -227,27 +260,36 @@ export function enableMapSet() {
}
const p = DraftSet.prototype

Object.defineProperty(p, "size", {
Object.defineProperties(p, {
size: {
get: function() {
return latest(this[DRAFT_STATE]).size
}
// enumerable: true,
})

p.has = function(value: any): boolean {
},
configurable: true
},
has: {
configurable: true,
writable: true,
value: function(value: any): boolean {
const state: SetState = this[DRAFT_STATE]
assertUnrevoked(state)
// bit of trickery here, to be able to recognize both the value, and the draft of its value
if (!state.copy_) {
return state.base_.has(value)
}
if (state.copy_.has(value)) return true
if (state.drafts_.has(value) && state.copy_.has(state.drafts_.get(value)))
if (
state.drafts_.has(value) &&
state.copy_.has(state.drafts_.get(value))
)
return true
return false
}

p.add = function(value: any): any {
},
add: {
configurable: true,
writable: true,
value: function(value: any): any {
const state: SetState = this[DRAFT_STATE]
assertUnrevoked(state)
if (!this.has(value)) {
Expand All @@ -257,8 +299,11 @@ export function enableMapSet() {
}
return this
}

p.delete = function(value: any): any {
},
delete: {
configurable: true,
writable: true,
value: function(value: any): any {
if (!this.has(value)) {
return false
}
Expand All @@ -274,8 +319,11 @@ export function enableMapSet() {
: /* istanbul ignore next */ false)
)
}

p.clear = function() {
},
clear: {
configurable: true,
writable: true,
value: function() {
const state: SetState = this[DRAFT_STATE]
assertUnrevoked(state)
if (latest(state).size) {
Expand All @@ -284,37 +332,54 @@ export function enableMapSet() {
state.copy_!.clear()
}
}

p.values = function(): IterableIterator<any> {
},
values: {
configurable: true,
writable: true,
value: function(): IterableIterator<any> {
const state: SetState = this[DRAFT_STATE]
assertUnrevoked(state)
prepareSetCopy(state)
return state.copy_!.values()
}

p.entries = function entries(): IterableIterator<[any, any]> {
},
entries: {
configurable: true,
writable: true,
value: function entries(): IterableIterator<[any, any]> {
const state: SetState = this[DRAFT_STATE]
assertUnrevoked(state)
prepareSetCopy(state)
return state.copy_!.entries()
}

p.keys = function(): IterableIterator<any> {
},
keys: {
configurable: true,
writable: true,
value: function(): IterableIterator<any> {
return this.values()
}

p[iteratorSymbol] = function() {
},
[iteratorSymbol]: {
configurable: true,
writable: true,
value: function() {
return this.values()
}

p.forEach = function forEach(cb: any, thisArg?: any) {
},
forEach: {
configurable: true,
writable: true,
value: function forEach(cb: any, thisArg?: any) {
const iterator = this.values()
let result = iterator.next()
while (!result.done) {
cb.call(thisArg, result.value, result.value, this)
result = iterator.next()
}
}
}
})

return DraftSet
})(Set)
Expand Down
12 changes: 11 additions & 1 deletion src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,17 @@ export function freeze<T>(obj: T, deep?: boolean): T
export function freeze<T>(obj: any, deep: boolean = false): T {
if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) return obj
if (getArchtype(obj) > 1 /* Map or Set */) {
obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any
const desc: PropertyDescriptor = {
configurable: true,
writable: true,
value: dontMutateFrozenCollections
}
Object.defineProperties(obj, {
set: desc,
add: desc,
clear: desc,
delete: desc
})
}
Object.freeze(obj)
if (deep) each(obj, (key, value) => freeze(value, true), true)
Expand Down