Last active
August 27, 2018 11:57
-
-
Save mmavko/5b2ceaa3b608754ee0dc30e9e574ef4a to your computer and use it in GitHub Desktop.
Rx.Observable.log()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rx.Observable.prototype.log = (function(){ | |
let count = 0; | |
return function log(optionalName) { | |
let str = `Observable ${optionalName || ++count}:\t`; | |
return this.do({ | |
next: v => str += `--${v}`, | |
error: () => console.log(str + "--#"), | |
complete: () => console.log(str + "--|"), | |
}); | |
}; | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
logger = (() => { | |
let count = 0; | |
return (name = ++count) => { | |
const log = m => console.log.bind(console, `Observer ${name}`, m); | |
return { | |
next: log("value"), | |
error: log("error"), | |
complete: log("complete"), | |
}; | |
} | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
timeLogger = (() => { | |
let count = 0; | |
const ms = (s) => String(s+' ms').padEnd(7); | |
return (name = ++count) => { | |
let firstDT = null; | |
let lastDT = null; | |
let str = `Observer ${name}:\n`; | |
const log = (m, print) => { | |
const newDT = Date.now(); | |
const msDiff = lastDT ? newDT - lastDT : 0; | |
firstDT || (firstDT = newDT); | |
lastDT = newDT; | |
str += `\t• ${ms(lastDT-firstDT)}\t[ Δ ${ms(msDiff)} ]\t${m}\n`; | |
print && console.log(str); | |
}; | |
log('subscribed'); | |
return { | |
next: v => log(`value: ${v}`), | |
error: e => log(`error: ${e}`, true), | |
complete: () => log('complete', true), | |
}; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment