Skip to content

Commit

Permalink
add ignorePastMessages option
Browse files Browse the repository at this point in the history
  • Loading branch information
yuta0801 committed Mar 1, 2020
1 parent 6b4d950 commit 2253f45
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
21 changes: 12 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,11 @@ class LiveChat extends EventEmitter {
*/
handler() {
this.handled = true
let lastRead = 0
let time = 0
this.on('json', data => {
for (const item of data.items) {
time = new Date(item.snippet.publishedAt).getTime()
if (lastRead < time) {
lastRead = time
const time = new Date(item.snippet.publishedAt).getTime()
if (this.lastRead < time) {
this.lastRead = time
/**
* Emitted whenever a new message is recepted.
* See {@link https://developers.google.com/youtube/v3/live/docs/liveChatMessages#resource}
Expand All @@ -123,7 +121,7 @@ class LiveChat extends EventEmitter {
* Listening options
* @typedef {Object} ListenOptions
* @property {number} [interval] Interval to get live chat messages. Default is 1000ms.
* @property {function} [filter] Filter to select live stream. Default is _all lives_.
* @property {function} [liveFilter] Filter to select live stream. Default is _all lives_.
*/

/**
Expand All @@ -135,11 +133,14 @@ class LiveChat extends EventEmitter {
options = Object.assign(
{
interval: 1000,
filter: c => c,
liveFilter: c => c,
ignorePastMessages: true,
},
options,
)

if (options.ignorePastMessages) this.lastRead = Date.now()

if (!this.handled) this.handler()
if (this.interval) this.stop()

Expand All @@ -151,7 +152,7 @@ class LiveChat extends EventEmitter {
this.chatIds = chatIds

this.interval = setInterval(
() => this.getMessages(options.filter(chatIds)),
() => this.getMessages(options.liveFilter(chatIds)),
options.interval,
)
}
Expand All @@ -168,8 +169,10 @@ class LiveChat extends EventEmitter {
* @param {ListenOptions} [options] Listening options. Default is options last passed to listen method_.
*/
restart(options = this.options) {
if (this.options.ignorePastMessages) this.lastRead = Date.now()

this.interval = setInterval(
() => this.getMessages(options.filter(this.chatIds)),
() => this.getMessages(options.liveFilter(this.chatIds)),
options.interval,
)
}
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ setTimeout(() => {
setTimeout(() => {
chat.stop()
console.log('--- stopped listening ---')
}, 3000)
}, 5000)
}, 3000)
}, 5000)

0 comments on commit 2253f45

Please sign in to comment.