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

Trigger update after calling delete on an object property #30

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
20 changes: 15 additions & 5 deletions addon/-private/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,17 @@ export default class TrackedObject {
target[prop] = value;

self.#dirtyStorageFor(prop);
setValue(self.#collection, null);
self.#dirtyCollection();

return true;
},

deleteProperty(target, prop) {
if (prop in target) {
delete target[prop];
self.#dirtyStorageFor(prop);
self.#dirtyCollection();
}

return true;
},
Expand All @@ -56,13 +66,10 @@ export default class TrackedObject {
});
}

// @private
#storages = new Map();

// @private
#collection = createStorage(null, () => false);

// @private
#readStorageFor(key) {
let storage = this.#storages.get(key);

Expand All @@ -74,12 +81,15 @@ export default class TrackedObject {
getValue(storage);
}

// @private
#dirtyStorageFor(key) {
const storage = this.#storages.get(key);

if (storage) {
setValue(storage, null);
}
}

#dirtyCollection() {
setValue(this.#collection, null);
}
}
15 changes: 15 additions & 0 deletions tests/unit/object-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,19 @@ module('TrackedObject', function (hooks) {
}
}
);

reactivityTest(
'delete works',
class extends Component {
obj: { foo?: number } = new TrackedObject({ foo: 1 });

get value() {
return this.obj.foo;
}

update() {
delete this.obj.foo;
}
}
);
});