From 495ed6a5dedd39b85607ba182651d9596e551e0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mati=20K=C3=A4rner?= Date: Wed, 12 May 2021 21:47:11 +0300 Subject: [PATCH] Adds optional callback method (logger) for Observable.log/spy() --- bacon-vs-kefir-api.md | 2 +- docs-src/descriptions/main-methods.pug | 8 ++++---- package-lock.json | 2 +- src/observable.js | 12 ++++++------ test/specs/log.js | 16 +++++++++++++++- test/specs/spy.js | 16 +++++++++++++++- 6 files changed, 42 insertions(+), 14 deletions(-) diff --git a/bacon-vs-kefir-api.md b/bacon-vs-kefir-api.md index 88734d3a..c405732e 100644 --- a/bacon-vs-kefir-api.md +++ b/bacon-vs-kefir-api.md @@ -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. | | diff --git a/docs-src/descriptions/main-methods.pug b/docs-src/descriptions/main-methods.pug index 3a809fe6..16038556 100644 --- a/docs-src/descriptions/main-methods.pug +++ b/docs-src/descriptions/main-methods.pug @@ -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') @@ -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. diff --git a/package-lock.json b/package-lock.json index 9433591e..9782838e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "kefir", - "version": "3.8.6", + "version": "3.8.8", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/observable.js b/src/observable.js index 677d152e..91e83739 100644 --- a/src/observable.js +++ b/src/observable.js @@ -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) } } @@ -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) { diff --git a/test/specs/log.js b/test/specs/log.js index db8bfa35..f900e81d 100644 --- a/test/specs/log.js +++ b/test/specs/log.js @@ -30,7 +30,7 @@ describe('log', () => { }) }) - describe('console', () => { + describe('default logger', () => { let stub beforeEach(() => (stub = sinon.stub(console, 'log'))) @@ -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', '', 1) + expect(logger).to.have.been.calledWith('logged', '', 2) + expect(logger).to.have.been.calledWith('logged', '', 3) + }) + }) + }) }) diff --git a/test/specs/spy.js b/test/specs/spy.js index 78f029d2..e4d2ba83 100644 --- a/test/specs/spy.js +++ b/test/specs/spy.js @@ -30,7 +30,7 @@ describe('spy', () => { }) }) - describe('console', () => { + describe('default logger', () => { let stub beforeEach(() => (stub = sinon.stub(console, 'log'))) @@ -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', '', 1) + expect(logger).to.have.been.calledWith('spied', '', 2) + expect(logger).to.have.been.calledWith('spied', '', 3) + }) + }) + }) })