diff --git a/spec/pane-spec.coffee b/spec/pane-spec.coffee index 980490d81..c66b88850 100644 --- a/spec/pane-spec.coffee +++ b/spec/pane-spec.coffee @@ -292,6 +292,19 @@ describe "Pane", -> expect(item3.isDestroyed()).toBe true expect(pane.getItems()).toEqual [] + describe "::observeItems()", -> + it "invokes the observer with all current and future items", -> + pane = new Pane(items: [new Item, new Item]) + [item1, item2] = pane.getItems() + + observed = [] + pane.observeItems (item) -> observed.push(item) + + item3 = new Item + pane.addItem(item3) + + expect(observed).toEqual [item1, item2, item3] + describe "when an item emits a destroyed event", -> it "removes it from the list of items", -> pane = new Pane(items: [new Item("A"), new Item("B"), new Item("C")]) diff --git a/src/pane.coffee b/src/pane.coffee index 7e28bd9e4..b95c72067 100644 --- a/src/pane.coffee +++ b/src/pane.coffee @@ -134,6 +134,15 @@ class Pane extends Model onDidMoveItem: (callback) -> @emitter.on 'did-move-item', callback + # Public: Invoke the given callback with all current and future items. + # + # * `callback` {Function} to be called with current and future items. + # + # Returns a {Disposable} on which `.dispose()` can be called to unsubscribe. + observeItems: (callback) -> + callback(item) for item in @getItems() + @onDidAddItem ({item}) -> callback(item) + # Public: Invoke the given callback when the value of {::getActiveItem} # changes. #