Hi There,
I have always struggled with the API for listening to changes on a collection.
@Ian_Ward - maybe you could shed some light on this?
The current API from the 10.0.0-beta.12 docs is:
interface CollectionChangeSet {
insertions: number[];
deletions: number[];
newModifications: number[];
oldModifications: number[];
}
type CollectionChangeCallback<T> = (collection: Collection<T>, changes: CollectionChangeSet) => void;
-
collection
: the collection instance that changed, -
changes
: a dictionary with keysinsertions
,newModifications
,oldModifications
anddeletions
, each containing a list of indices in the collection that were inserted, updated or deleted respectively.deletions
andoldModifications
are indices into the collection before the change happened, whileinsertions
andnewModifications
are indices into the new version of the collection.
Whilst it’s pretty trivial to manage insertions
and newModifications
, I can’t quite figure out the best way to manage deletions
and oldModifications
.
Lets focus on deletions
for a moment (mostly because I don’t know what oldModifications
would even be useful for - For that matter, even newModifications
doesn’t really help that much other than letting me know that something changed… see my second point below)
I’m not sure I fully understand how to leverage these deletion indices on a collection that changes over time.
Lets say we have a collection => [obj1, obj2, obj3, obj4]
Some time later, a change comes through and our listener is called with:
( [obj1, obj3, obj4], { deletions: [1] } ) => ...
So what am I to do with this index?
Let’s say in my case that to do something useful with this information I need the ID of the deleted object (obj2) - I do not use the results collection in an array, and even if I were, I would be forced to maintain the original order for the index to be meaningful.
Do I have to keep an array of IDs inside the listener and reference/mutate that in sync with the changes to the original collection?
I don’t imagine it would work to close over the original results collection and reference the object in that - if it is deleted, it won’t be there any more…
Besides, what happens the next time a change occurs in this collection, the original results collection would be stale.
Surely the API for these changes - especially deletions, should at least be an array of objects like:
[
{
index: <index of inserted/modified/deleted item>,
id: <ID of inserted/modified/deleted Item>
}
]
Secondly,
Lets say I wanted to listen for changes to individual objects in a collection. (The current API just says “something changed in the item at index x”)
Would the best approach be to map over the results and to add individual listeners to the constituent objects? This seems like what you’d need to do - but maybe the collection listener API needs an overhaul to encapsulate useable info like the the much more easily consumable ObjectChangeCallback
API.
i.e.
interface ObjectChangeSet {
deleted: boolean;
changedProperties: string[]
}
That way we can use similar approaches for listener callbacks on both collections and individual objects.
Either way - some guidance on this would be great.
Thanks in advance!
B