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

Supporting a new set methods #3853

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -68,11 +68,12 @@
"tape": "^5.0.1",
"ts-jest": "^29.0.5",
"tsdx": "^0.14.1",
"typescript": "^5.0.2"
"typescript": "^5.5.0-dev.20240415"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO consolidate before merge

},
"husky": {
"hooks": {
"pre-commit": "pretty-quick --staged"
}
}
},
"packageManager": "yarn@1.22.19+sha256.732620bac8b1690d507274f025f3c6cfdc3627a84d9642e38a07452cc00e0f2e"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO remove before merge

}
17 changes: 17 additions & 0 deletions packages/mobx/src/global.d.ts
@@ -1 +1,18 @@
declare const __DEV__: boolean

// This code is a copy from TS PR#57230
// Should be removed after it merged
interface ReadonlySetLike<T> {
/**
* Despite its name, returns an iterable of the values in the set-like.
*/
keys(): Iterator<T>
/**
* @returns a boolean indicating whether an element with the specified value exists in the set-like or not.
*/
has(value: T): boolean
/**
* @returns the number of (unique) elements in the set-like.
*/
readonly size: number
}
56 changes: 56 additions & 0 deletions packages/mobx/src/types/observableset.ts
Expand Up @@ -243,6 +243,62 @@ export class ObservableSet<T = any> implements Set<T>, IInterceptable<ISetWillCh
} as any)
}

intersection<U>(otherSet: ReadonlySetLike<U>): Set<T & U> {
this.atom_.reportObserved()
inoyakaigor marked this conversation as resolved.
Show resolved Hide resolved

if (typeof otherSet.intersection !== "function") {
inoyakaigor marked this conversation as resolved.
Show resolved Hide resolved
return otherSet.intersection(this.data_)
} else {
const dehancedSet = new Set(this)
return dehancedSet.intersection(otherSet)
}
}

union<U>(otherSet: ReadonlySetLike<U>): Set<T | U> {
this.atom_.reportObserved()

if (typeof otherSet.union !== "function") {
return otherSet.union(this.data_)
} else {
const dehancedSet = new Set(this)
return dehancedSet.union(otherSet)
}
}

difference<U>(otherSet: ReadonlySetLike<U>): Set<T> {
this.atom_.reportObserved()

if (typeof otherSet.difference !== "function") {
return otherSet.difference(this.data_)
} else {
const dehancedSet = new Set(this)
return dehancedSet.difference(otherSet)
}
}

symmetricDifference<U>(otherSet: ReadonlySetLike<U>): Set<T | U> {
this.atom_.reportObserved()

if (typeof otherSet.symmetricDifference !== "function") {
return otherSet.symmetricDifference(this.data_)
} else {
const dehancedSet = new Set(this)
return dehancedSet.symmetricDifference(otherSet)
}
}

isSubsetOf(): IterableIterator<T> {
// TODO: implement
}

isSupersetOf(): IterableIterator<T> {
// TODO: implement
}

isDisjointFrom(): IterableIterator<T> {
// TODO: implement
}

replace(other: ObservableSet<T> | IObservableSetInitialValues<T>): ObservableSet<T> {
if (isObservableSet(other)) {
other = new Set(other)
Expand Down