Skip to content

Commit

Permalink
fix(observableset): use the new value returned from the interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
k-g-a committed Feb 19, 2025
1 parent 09edf2b commit 1b6b87d
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions packages/mobx/src/types/observableset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,20 @@ export type ISetDidChange<T = any> =
oldValue: T
}

export type ISetWillDeleteChange<T = any> = {
type: "delete"
object: ObservableSet<T>
oldValue: T
};
export type ISetWillAddChange<T = any> = {
type: "add"
object: ObservableSet<T>
newValue: T
};

export type ISetWillChange<T = any> =
| {
type: "delete"
object: ObservableSet<T>
oldValue: T
}
| {
type: "add"
object: ObservableSet<T>
newValue: T
}
| ISetWillDeleteChange<T>
| ISetWillAddChange<T>

export class ObservableSet<T = any> implements Set<T>, IInterceptable<ISetWillChange>, IListenable {
[$mobx] = ObservableSetMarker
Expand Down Expand Up @@ -120,16 +123,18 @@ export class ObservableSet<T = any> implements Set<T>, IInterceptable<ISetWillCh
add(value: T) {
checkIfStateModificationsAreAllowed(this.atom_)
if (hasInterceptors(this)) {
const change = interceptChange<ISetWillChange<T>>(this, {
const change = interceptChange<ISetWillAddChange<T>>(this, {
type: ADD,
object: this,
newValue: value
})
if (!change) {
return this
}
// ideally, value = change.value would be done here, so that values can be
// changed by interceptor. Same applies for other Set and Map api's.

// implemented reassignment same as it's done for ObservableMap
value = change.newValue!;

}
if (!this.has(value)) {
transaction(() => {
Expand Down Expand Up @@ -164,7 +169,7 @@ export class ObservableSet<T = any> implements Set<T>, IInterceptable<ISetWillCh

delete(value: T) {
if (hasInterceptors(this)) {
const change = interceptChange<ISetWillChange<T>>(this, {
const change = interceptChange<ISetWillDeleteChange<T>>(this, {
type: DELETE,
object: this,
oldValue: value
Expand Down

0 comments on commit 1b6b87d

Please sign in to comment.