RangeChanges

An abstract collection that provides the interface for listening to and dispatching notifications when values are removed then added at an index.

Every change to an array, or any flat collection of values, can model any content change as a values added or removed at a particular index.

Every method that changes an array can be implemented in terms of splice(index, length, ...values). For example, every time you set(index, value) on an array, it can be modeled as splice(index, 1, value). Every time you push a value onto an array, it can be modeled as splice(length, 0, value). Every time you shift a value off an array, it cam be modeled as splice(0, 1). Each of these changes can be communicated with a single message, (plus, minus, index): the values removed after that index, then the values that were added after that index, and the index, in that order.

Range change listeners receive such messages synchronously, as the array changes.

Methods

addRangeChangeListener(listener, token?, beforeChange?)

Adds a listener for when values are added or removed at any position.

removeRangeChangeListener(listener, token?, beforeChange?)

Unregisters a range change listener provided by addRangeChangeListener.

dispatchRangeChange(plus, minus, index, beforeChange?)

Informs range change listeners that values were removed then added at an index.

addBeforeRangeChangeListener(listener, token?)

Adds a listener for before values are added or removed at any position.

removeBeforeRangeChangeListener(listener, token?)

Unregisters a range change listener provided by addBeforeRangeChangeListener or addRangeChangeListener with the beforeChange flag.

dispatchBeforeRangeChange(plus, minus, index)

Informs range change listeners that values will be removed then added at an index.

Usage

require("collections/shim-object");
var RangeChange = require("collections/listen/range-changes");
Object.addEach(MyRange.prototype, RangeChange.prototype);
  • RangeChanges
Source code