Skip to content

Commit

Permalink
feat: state getter (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvincent committed Dec 15, 2022
1 parent 77999dc commit a4d7aa5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ the value encoding of the underlying Hypercore is `binary` or `utf-8`, this will
be the byte length of all the blocks in the batch. If the value encoding is
`json` then this will be the number of entries in a batch.

### indexer.state

Type: `IndexState: { current: 'idle' | 'indexing', remaining: number, entriesPerSecond: number }`

A getter that returns the current `IndexState`, the same as the value emitted by the `index-state` event. This getter is useful for checking the state of the indexer before it has emitted any events.

### indexer.addCore(core)

#### core
Expand Down
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ class MultiCoreIndexer extends TypedEmitter {
this.#indexStream.on('indexing', this.#handleIndexingBound)
}

/**
* @type {IndexState}
*/
get state() {
return {
current: this.#state,
entriesPerSecond: this.#rate,
remaining: this.#lastRemaining,
}
}

/**
* Add a core to be indexed
* @param {import('hypercore')<T>} core
Expand Down
14 changes: 11 additions & 3 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,22 @@ async function throttledIdle(emitter) {
return new Promise((resolve) => {
/** @type {ReturnType<setTimeout>} */
let timeoutId
emitter.on('idle', function onIdle() {

/* @ts-ignore: we're using this helper with both MultiCoreIndexer and an indexer stream. checking that the state property exists is sufficient. */
if (emitter.state && emitter.state.current === 'idle') {
onIdle()
}

function onIdle() {
clearTimeout(timeoutId)
timeoutId = setTimeout(() => {
emitter.off('idle', onIdle)
emitter.off('indexing', onIndexing)
resolve()
}, 200)
})
}, 10)
}

emitter.on('idle', onIdle)
emitter.on('indexing', onIndexing)
function onIndexing() {
clearTimeout(timeoutId)
Expand Down
20 changes: 20 additions & 0 deletions test/multi-core-indexer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,23 @@ test('sync state / progress', async (t) => {
await indexer.close()
t.pass('Indexer closed')
})

test('state getter', async (t) => {
const cores = await createMultiple(2)
const entries = []
const indexer = new MultiCoreIndexer(cores, {
batch: async (data) => {
entries.push(...data)
},
storage: () => ram(),
})
t.same(indexer.state.current, 'idle')
await throttledIdle(indexer)
await generateFixtures(cores, 100)
t.same(indexer.state.current, 'indexing')
await throttledIdle(indexer)
t.same(indexer.state.current, 'idle')
t.same(entries.length, 200)
await indexer.close()
t.pass('Indexer closed')
})

0 comments on commit a4d7aa5

Please sign in to comment.