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

Make ObservableSet backport compatible with IE11 #1917

Merged
merged 1 commit into from
Mar 14, 2019
Merged
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
7 changes: 4 additions & 3 deletions src/api/object-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
endBatch,
defineObservableProperty,
invariant,
iteratorToArray,
getAdministration,
ObservableObjectAdministration
} from "../internal"
Expand All @@ -28,7 +29,7 @@ export function keys(obj: any): any {
return (obj as any)._keys.slice()
}
if (isObservableSet(obj)) {
return Array.from(obj.keys())
return iteratorToArray(obj.keys())
}
if (isObservableArray(obj)) {
return obj.map((_, index) => index)
Expand All @@ -51,7 +52,7 @@ export function values(obj: any): string[] {
return keys(obj).map(key => obj.get(key))
}
if (isObservableSet(obj)) {
return Array.from(obj.values())
return iteratorToArray(obj.values())
}
if (isObservableArray(obj)) {
return obj.slice()
Expand All @@ -74,7 +75,7 @@ export function entries(obj: any): any {
return keys(obj).map(key => [key, obj.get(key)])
}
if (isObservableSet(obj)) {
return Array.from(obj.entries())
return iteratorToArray(obj.entries())
}
if (isObservableArray(obj)) {
return obj.map((key, index) => [index, key])
Expand Down
19 changes: 14 additions & 5 deletions src/types/observableset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import {
isES6Set,
toStringTagSymbol,
declareIterator,
addHiddenFinalProp
addHiddenFinalProp,
iteratorToArray
} from "../internal"

const ObservableSetMarker = {}
Expand Down Expand Up @@ -192,8 +193,8 @@ export class ObservableSet<T = any> implements Set<T>, IInterceptable<ISetWillCh

entries() {
let nextIndex = 0
const keys = Array.from(this.keys())
const values = Array.from(this.values())
const keys = iteratorToArray(this.keys())
const values = iteratorToArray(this.values())
return makeIterable<[T, T]>({
next() {
const index = nextIndex
Expand All @@ -213,7 +214,15 @@ export class ObservableSet<T = any> implements Set<T>, IInterceptable<ISetWillCh
this._atom.reportObserved()
const self = this
let nextIndex = 0
const observableValues = Array.from(this._data.values())
let observableValues: any[]

if (this._data.values !== undefined) {
observableValues = iteratorToArray(this._data.values())
} else {
// There is no values function in IE11
observableValues = []
this._data.forEach(e => observableValues.push(e))
}
return makeIterable<T>({
next() {
return nextIndex < observableValues.length
Expand Down Expand Up @@ -262,7 +271,7 @@ export class ObservableSet<T = any> implements Set<T>, IInterceptable<ISetWillCh
}

toString(): string {
return this.name + "[ " + Array.from(this).join(", ") + " ]"
return this.name + "[ " + iteratorToArray(this.keys()).join(", ") + " ]"
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/eq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ function deepEq(a: any, b: any, aStack?: any[], bStack?: any[]) {
function unwrap(a: any) {
if (isObservableArray(a)) return a.peek()
if (isES6Map(a) || isObservableMap(a)) return iteratorToArray(a.entries())
if (isES6Set(a) || isObservableSet(a)) return Array.from(a.entries())
if (isES6Set(a) || isObservableSet(a)) return iteratorToArray(a.entries())
return a
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export function getMapLikeKeys(map: any): any {
}

// use Array.from in Mobx 5
export function iteratorToArray<T>(it: Iterator<T>): ReadonlyArray<T> {
export function iteratorToArray<T>(it: Iterator<T>): Array<T> {
const res: T[] = []
while (true) {
const r: any = it.next()
Expand Down