Skip to content
Open
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,10 @@ Emitted when the core has been appended to (i.e. has a new length / byteLength),

Emitted when the core has been truncated, either locally or remotely.

#### `core.on('clear', start, end)`

Emitted when a range of blocks has been cleared from local storage with `core.clear()`.

#### `core.on('peer-add')`

Emitted when a new connection has been established with a peer.
Expand Down
4 changes: 4 additions & 0 deletions lib/session-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,10 @@ class SessionState {
this.core.replicator.onhave(start, length, true)

if (this.core.hintsChanged) await this.core.flushHints()

for (let i = this.sessions.length - 1; i >= 0; i--) {
this.sessions[i].emit('clear', start, end)
}
}
} finally {
this._unlock()
Expand Down
13 changes: 13 additions & 0 deletions test/clear.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ test('clear', async function (t) {
await a.close()
})

test('clear emits a clear event with the cleared range', async function (t) {
const a = await create(t)
await a.append(['a', 'b', 'c'])

const cleared = new Promise((resolve) => a.once('clear', (start, end) => resolve({ start, end })))

await a.clear(1, 3)

t.alike(await cleared, { start: 1, end: 3 }, 'clear event fired with the cleared range')

await a.close()
})

test('clear + replication', async function (t) {
const a = await create(t)
const b = await create(t, a.key)
Expand Down