Hello world

First open source release of Campfire 🎉
This commit is contained in:
Kevin McConnell
2025-08-15 11:02:42 +01:00
commit df76a227dc
664 changed files with 36235 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
const REFRESH_INTERVAL = 1000
const TYPING_TIMEOUT = 5000
export default class TypingTracker {
constructor(callback) {
this.callback = callback
this.currentlyTyping = {}
this.timer = setInterval(this.#refresh.bind(this), REFRESH_INTERVAL)
}
close() {
clearInterval(this.timer)
}
add(name) {
this.currentlyTyping[name] = Date.now()
this.#refresh()
}
remove(name) {
delete this.currentlyTyping[name]
this.#refresh()
}
#refresh() {
this.#purgeInactive()
const names = Object.keys(this.currentlyTyping).sort()
if (names.length > 0) {
this.callback(`${names.join(", ")}`)
} else {
this.callback(null)
}
}
#purgeInactive() {
const cutoff = Date.now() - TYPING_TIMEOUT
this.currentlyTyping = Object.fromEntries(
Object.entries(this.currentlyTyping).filter(([_name, timestamp]) => timestamp > cutoff)
)
}
}