Skip to content

Commit

Permalink
tko.observable) Trigger immediately with current with TC39 {next}-s…
Browse files Browse the repository at this point in the history
…tyle subscribe

So when a TC39 style `subscribe` call is made, we trigger with the current value immediately, contrasting with the prior behaviour - namely triggering only on changes to the state.

tc39/proposal-observable#190
  • Loading branch information
brianmhunt committed Jun 24, 2018
1 parent 9d3e892 commit 01eac29
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ For TODO between alpha and release, see https://github.com/knockout/tko/issues/1
* Expose `createViewModel` on Components registered with `Component.register`
* Changed `Component.elementName` to `Component.customElementName` and use a kebab-case version of the class name for the custom element name by default
* Pass `{element, templateNodes}` to the `Component` constructor as the second parameter of descendants of the `Component` class
* Add support for `<ko binding='...'>`
* Add basic support for `ko.subscribable` as TC39-Observables

## 🚚 Alpha-4a (8 Nov 2017)

Expand Down
10 changes: 10 additions & 0 deletions packages/tko.observable/spec/observableBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,18 @@ describe('Observable', function () {
expect(myObservable.customFunction1).toBe(customFunction1)
expect(myObservable.customFunction2).toBe(customFunction2)
})

it('immediately emits any value when called with {next: ...}', function () {
const instance = observable(1)
let x
instance.subscribe({next: v => (x = v)})
expect(x).toEqual(1)
observable(2)
expect(x).toEqual(1)
})
})


describe('unwrap', function () {
it('Should return the supplied value for non-observables', function () {
var someObject = { abc: 123 }
Expand Down
11 changes: 9 additions & 2 deletions packages/tko.observable/src/observable.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,16 @@ observable.fn = {
valueWillMutate () {
this.notifySubscribers(this[observableLatestValue], 'beforeChange')
},
// subscribe (callback, callbackTarget, event) {

// Note that TC39 `subscribe` ought to immediately emit.
// https://github.com/tc39/proposal-observable/issues/190
// }
subscribe (callback, callbackTarget, event) {
const isTC39Callback = typeof callback === 'object' && callback.next
if (isTC39Callback) {
callback.next(this())
}
return subscribable.fn.subscribe.call(this, callback, callbackTarget, event)
},

// Some observables may not always be writeable, notably computeds.
isWriteable: true
Expand Down

0 comments on commit 01eac29

Please sign in to comment.