Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Use safeHeight in MultiIndexer
Browse files Browse the repository at this point in the history
  • Loading branch information
sz-piotr committed Apr 2, 2024
1 parent 2b9249b commit 3f297dd
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
49 changes: 49 additions & 0 deletions packages/uif/src/indexers/multi/MultiIndexer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,49 @@ describe(MultiIndexer.name, () => {
saved('c', 300, null, 300),
])
})

it('calls getters in order', async () => {
const calls: string[] = []
const testIndexer = new TestMultiIndexer([], [])
testIndexer.multiInitialize = mockFn(async () => {
calls.push('multiInitialize')
return []
})
testIndexer.getInitialConfigurations = mockFn(() => {
calls.push('getInitialConfigurations')
return []
})
testIndexer.getSafeHeight = mockFn(() => {
calls.push('getSafeHeight')
return Promise.resolve(undefined)
})

await testIndexer.initialize()

expect(calls).toEqual([
'multiInitialize',
'getInitialConfigurations',
'getSafeHeight',
])
})

it('getSafeHeight lower than saved configs', async () => {
const testIndexer = new TestMultiIndexer(
[actual('a', 100, 200)],
[saved('a', 100, 200, 150)],
)
testIndexer.getSafeHeight.resolvesTo(130)
expect(await testIndexer.initialize()).toEqual(130)
})

it('getSafeHeight higher than saved configs', async () => {
const testIndexer = new TestMultiIndexer(
[actual('a', 100, 200)],
[saved('a', 100, 200, 150)],
)
testIndexer.getSafeHeight.resolvesTo(160)
expect(await testIndexer.initialize()).toEqual(150)
})
})

describe(MultiIndexer.prototype.update.name, () => {
Expand Down Expand Up @@ -405,6 +448,12 @@ class TestMultiIndexer extends MultiIndexer<null> {
super(Logger.SILENT, [], configurations)
}

getSafeHeight =
mockFn<MultiIndexer<null>['getSafeHeight']>().resolvesTo(undefined)

setSafeHeight =
mockFn<MultiIndexer<null>['setSafeHeight']>().resolvesTo(undefined)

override multiInitialize(): Promise<SavedConfiguration<null>[]> {
return Promise.resolve(this._saved)
}
Expand Down
23 changes: 16 additions & 7 deletions packages/uif/src/indexers/multi/MultiIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,23 +109,36 @@ export abstract class MultiIndexer<T> extends ChildIndexer {
configurations: SavedConfiguration<T>[],
): Promise<void>

/**
* It should return a height that the indexer has synced up to. If the indexer
* has not synced any data, it should return `undefined`.
*
* This method is expected to read the height that was saved previously with
* `setSafeHeight`. It shouldn't call `setSafeHeight` itself.
*
* @returns The height that the indexer has synced up to.
*/
abstract getSafeHeight(): Promise<number | undefined>

async initialize(): Promise<number> {
const saved = await this.multiInitialize()

this.configurations = await this.getInitialConfigurations()
this.ranges = toRanges(this.configurations)

const saved = await this.multiInitialize()

const { toRemove, toSave, safeHeight } = diffConfigurations(
this.configurations,
saved,
)
const oldSafeHeight = (await this.getSafeHeight()) ?? safeHeight

this.saved = toSave
if (toRemove.length > 0) {
await this.removeData(toRemove)
}
await this.saveConfigurations(toSave)
return safeHeight

return Math.min(safeHeight, oldSafeHeight)
}

async update(from: number, to: number): Promise<number> {
Expand Down Expand Up @@ -179,10 +192,6 @@ export abstract class MultiIndexer<T> extends ChildIndexer {
async invalidate(targetHeight: number): Promise<number> {
return Promise.resolve(targetHeight)
}

async setSafeHeight(): Promise<void> {
return Promise.resolve()
}
}

function findRange<T>(
Expand Down

0 comments on commit 3f297dd

Please sign in to comment.