mozilla-b2g/gaia — pull request #16270 by jrburke (2014-02-14 01:14:54+00:00)
Title: Bug 971617 - [Sora]Switching accounts,and select the folder to move the mail,the current account folder list display error

This occurred because model.js was using evt.emitWhenListener('foldersSlice') to emit events about a foldersSlice change. With two account changes right in a row, there were two events queued up for 'foldersSlice', the one for the incorrect one was first.

So when mail_common's ValueSelector use asked for model.latestOnce(), it got the first, out of date, event in the _pendingEvents queue.

On further inspection, model should not have been using emitWhenListener because it was only using that for generating events based on property IDs, and the `evt.latest*` methods check for property names on the object with that name already.

So the only concern would be for consumers of `model` that were using `model.on()` to get notified of a change and they register too late to receive that change. However, the `evt.latest*` methods were constructed specifically for the `model` uses where code just wants the latest value on the object, or get notified when it arrives.

This bore out when searching the email code for `model.on()`: the only cases where it is used care only about change events, not the "get current value or wait until there is one" cases where `latest` is used:

* mail_app calls `model.on('acctsSlice', ...)` and does so before model is inited, so gets all the change events.
* message_list calls `model.on('newInboxMessages', ...)`, and 'newInboxMessages' is only emitted via `model.emit`, not `emitWhenListener`, so this changes does not affect it. Similarly, in my drawer branch, the 'foldersSliceOnChange' is a `model.on()` and 'model.emit()` pairing.

So while this code change has the potential to affect a few pathways, it most likely fixes them vs. breaking them.