This is something that was discussed some time ago but never resolved.

To summarize:

When working with CollectionValueHolder one can only see whether the
collection as a whole changed and not how, e.g.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
col := OrderedCollection new asValueHolder.
col add: 1.
col add: 2.
col add: 3.
col add: 4.
col whenChangedDo: [ :c :changedValue |
1halt.
].
"col = #(1 2 3 4)"

col add: 4. "whenChangedDo: c = col, changedValue = 4"
col remove: 4. "whenChangedDo: c = col, changedValue = 4"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In the last two cases the changedValue is the same so I can't tell from the
block if the value was added or removed.

My proposition:

* introduce (at least) two more Announcements: ValueAdded and ValueRemoved
* add messages #whenAddedDo:/#whenChangedDo:
* maybe add #when:do: so it can be used as an announcable object
(valueHolder when: ValueSomething do: [ :ann | ... ])

The ValueAdded/ValueRemoved would be fired when an item is added, resp.
removed.

Thierry also proposed to add an index (I'd make it optional).
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
col whenAddedDo: [ :col :addedValue :optionalIndex | "here we know for sure
that value was added" ].

col whenRemovedDo: [ :col :addedValue :optionalIndex | "here we know for
sure that value was removed" ]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Finally Thierry mentioned a way to handle situations where values are
replaced (either single one or multiple ones). As this is edge use-case and
the current behavior is pretty much random I wouldn't address the
replacement unless a defined use case emerges.

Any thoughts,
Peter

Reply via email to