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.20240517"
},
"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

}
70 changes: 70 additions & 0 deletions packages/mobx/src/types/observableset.ts
Expand Up @@ -243,6 +243,76 @@ export class ObservableSet<T = any> implements Set<T>, IInterceptable<ISetWillCh
} as any)
}

intersection<U>(otherSet: ReadonlySetLike<U> & Partial<Set<U>>): Set<T & U> {
if (typeof otherSet.intersection === "function") {
this.atom_.reportObserved()
return otherSet.intersection(this.data_)
} else {
const dehancedSet = new Set(this)
return dehancedSet.intersection(otherSet)
}
}

union<U>(otherSet: ReadonlySetLike<U> & Partial<Set<U>>): Set<T | U> {
if (typeof otherSet.union === "function") {
this.atom_.reportObserved()
return otherSet.union(this.data_)
} else {
const dehancedSet = new Set(this)
return dehancedSet.union(otherSet)
}
}

difference<U>(otherSet: ReadonlySetLike<U> & Partial<Set<T>>): Set<T> {
if (typeof otherSet.difference === "function") {
this.atom_.reportObserved()
return otherSet.difference(this.data_)
} else {
const dehancedSet = new Set(this)
return dehancedSet.difference(otherSet)
}
}

symmetricDifference<U>(otherSet: ReadonlySetLike<U> & Partial<Set<U>>): Set<T | U> {
if (typeof otherSet.symmetricDifference === "function") {
this.atom_.reportObserved()
return otherSet.symmetricDifference(this.data_)
} else {
const dehancedSet = new Set(this)
return dehancedSet.symmetricDifference(otherSet)
}
}

isSubsetOf(otherSet: ReadonlySetLike<unknown> & Partial<Set<unknown>>): boolean {
if (typeof otherSet.isSubsetOf === "function") {
this.atom_.reportObserved()
return otherSet.isSubsetOf(this.data_)
} else {
const dehancedSet = new Set(this)
return dehancedSet.isSubsetOf(otherSet)
}
}

isSupersetOf(otherSet: ReadonlySetLike<unknown> & Partial<Set<unknown>>): boolean {
if (typeof otherSet.isSupersetOf === "function") {
this.atom_.reportObserved()
return otherSet.isSupersetOf(this.data_)
} else {
const dehancedSet = new Set(this)
return dehancedSet.isSupersetOf(otherSet)
}
}

isDisjointFrom(otherSet: ReadonlySetLike<unknown> & Partial<Set<unknown>>): boolean {
if (typeof otherSet.isDisjointFrom === "function") {
this.atom_.reportObserved()
return otherSet.isDisjointFrom(this.data_)
} else {
const dehancedSet = new Set(this)
return dehancedSet.isDisjointFrom(otherSet)
}
}

replace(other: ObservableSet<T> | IObservableSetInitialValues<T>): ObservableSet<T> {
if (isObservableSet(other)) {
other = new Set(other)
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Expand Up @@ -3,7 +3,7 @@
"target": "es6",
"module": "esnext",
"moduleResolution": "node",
"lib": ["es6"],
"lib": ["es6", "ESNext"],
"downlevelIteration": true,
"alwaysStrict": true,
"sourceMap": true,
Expand Down