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

Stream management to read sequentially #1896

Merged
merged 2 commits into from
Mar 25, 2024
Merged
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
11 changes: 10 additions & 1 deletion backend/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,38 @@ sbp('sbp/selectors/register', {
let counter = 0
let currentHash = await sbp('chelonia/db/get', `_private_hidx=${contractID}#${height}`)
let prefix = '['
let ended = false
// NOTE: if this ever stops working you can also try Readable.from():
// https://nodejs.org/api/stream.html#stream_stream_readable_from_iterable_options
const stream = new Readable({
read (): void {
if (ended) {
this.destroy()
return
}
if (currentHash && counter < limit) {
sbp('chelonia/db/getEntry', currentHash).then(async entry => {
if (entry) {
this.push(`${prefix}"${strToB64(entry.serialize())}"`)
const currentPrefix = prefix
prefix = ','
counter++
currentHash = await sbp('chelonia/db/get', `_private_hidx=${contractID}#${entry.height() + 1}`)
this.push(`${currentPrefix}"${strToB64(entry.serialize())}"`)
} else {
this.push(counter > 0 ? ']' : '[]')
this.push(null)
ended = true
}
}).catch(e => {
console.error(`[backend] streamEntriesAfter: read(): ${e.message}:`, e)
this.push(counter > 0 ? ']' : '[]')
this.push(null)
ended = true
})
} else {
this.push(counter > 0 ? ']' : '[]')
this.push(null)
ended = true
}
}
})
Expand Down
1 change: 1 addition & 0 deletions frontend/views/containers/chatroom/ChatMain.vue
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ export default ({

for (;;) {
const { done, value: event } = await newEventsStreamReader.read()
console.error({ done, event })
if (done) break

const state = this.messageState.contract
Expand Down
Loading