Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds optional callback method (logger) for Observable.log/spy() #315

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bacon-vs-kefir-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
| Use `unsub` function, or `Bacon.noMore` | `obs.offError(fn)` | |
| Use `unsub` function, or `Bacon.noMore` | `obs.offEnd(fn)` | |
| Use `unsub` function, or `Bacon.noMore` | `obs.offAny(fn)` | |
| `obs.log([name])` | `obs.log([name])` | The log format is different. Kefir returns `this` unlike Bacon, that returns `unusb` function |
| `obs.log([name])` | `obs.log([name, fn])` | The log format is different. Kefir returns `this` unlike Bacon, that returns `unusb` function |
| Use `unsub` function | `obs.offLog([name])` | |
| `obs.name(newName)` | `obs.setName(newName)` | |
| `observable.withDescription(param...)` | No alt. | |
Expand Down
8 changes: 4 additions & 4 deletions docs-src/descriptions/main-methods.pug
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ pre(title='console output')
+descr-method('off-any', 'offAny', 'obs.offAny(callback)').
Unsubscribes an #[b onAny] subscriber.

+descr-method('log', 'log', 'obs.log([name])').
Turns on logging of any event to the browser console.
+descr-method('log', 'log', 'obs.log([name, fn])').
Turns on logging of any event to the browser console (default) or using specified callback function.
Accepts an optional #[b name] argument that will be shown in the log if provided.

pre.javascript(title='example')
Expand All @@ -174,8 +174,8 @@ pre(title='console output')
Turns off logging. If #[b .log] was called with a #[b name] argument,
#[b offLog] must be called with the same #[b name] argument.

+descr-method('spy', 'spy', 'obs.spy([name])').
Turns on spying of any event to the browser console. Similar to
+descr-method('spy', 'spy', 'obs.spy([name, fn])').
Turns on spying of any event to the browser console (default) or using specified callback function. Similar to
#[b .log], however #[b .spy] will not cause the stream to activate by itself.
Accepts an optional #[b name] argument that will be shown in the log if provided.

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions src/observable.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,14 @@ extend(Observable.prototype, {
return this
},

log(name = this.toString()) {
log(name = this.toString(), logger = console.log) {
let isCurrent
let handler = function(event) {
let type = `<${event.type}${isCurrent ? ':current' : ''}>`
if (event.type === END) {
console.log(name, type)
logger(name, type)
} else {
console.log(name, type, event.value)
logger(name, type, event.value)
}
}

Expand Down Expand Up @@ -199,13 +199,13 @@ extend(Observable.prototype, {
return this
},

spy(name = this.toString()) {
spy(name = this.toString(), logger = console.log) {
let handler = function(event) {
let type = `<${event.type}>`
if (event.type === END) {
console.log(name, type)
logger(name, type)
} else {
console.log(name, type, event.value)
logger(name, type, event.value)
}
}
if (this._alive) {
Expand Down
16 changes: 15 additions & 1 deletion test/specs/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('log', () => {
})
})

describe('console', () => {
describe('default logger', () => {
let stub
beforeEach(() => (stub = sinon.stub(console, 'log')))

Expand Down Expand Up @@ -68,4 +68,18 @@ describe('log', () => {
})
})
})

describe('custom logger', () => {
it('should log using custom logger function', () => {
const a = stream()
const logger = sinon.fake()
a.log('logged', logger)
expect(a).to.emit([value(1), value(2), value(3)], () => {
send(a, [value(1), value(2), value(3)])
expect(logger).to.have.been.calledWith('logged', '<value>', 1)
expect(logger).to.have.been.calledWith('logged', '<value>', 2)
expect(logger).to.have.been.calledWith('logged', '<value>', 3)
})
})
})
})
16 changes: 15 additions & 1 deletion test/specs/spy.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('spy', () => {
})
})

describe('console', () => {
describe('default logger', () => {
let stub
beforeEach(() => (stub = sinon.stub(console, 'log')))

Expand Down Expand Up @@ -68,4 +68,18 @@ describe('spy', () => {
})
})
})

describe('custom logger', () => {
it('should log using custom logger function', () => {
const a = stream()
const logger = sinon.fake()
a.spy('spied', logger)
expect(a).to.emit([value(1), value(2), value(3)], () => {
send(a, [value(1), value(2), value(3)])
expect(logger).to.have.been.calledWith('spied', '<value>', 1)
expect(logger).to.have.been.calledWith('spied', '<value>', 2)
expect(logger).to.have.been.calledWith('spied', '<value>', 3)
})
})
})
})