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

Make configurations dynamic #167

Merged
merged 6 commits into from
Mar 29, 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
6 changes: 6 additions & 0 deletions packages/uif/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @l2beat/uif

## 0.3.1

### Patch Changes

- Add ability to provide configurations from different source to MultiIndexer

## 0.3.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/uif/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@l2beat/uif",
"description": "Universal Indexer Framework.",
"version": "0.3.0",
"version": "0.3.1",
"license": "MIT",
"repository": "https://github.com/l2beat/tools",
"bugs": {
Expand Down
13 changes: 12 additions & 1 deletion packages/uif/src/indexers/multi/MultiIndexer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,17 @@ describe(MultiIndexer.name, () => {
saved('c', 100, 500, 500),
])
})

it('gets configurations from different source', async () => {
const testIndexer = new TestMultiIndexer(undefined, [])
testIndexer.getInitialConfigurations = () => [
actual('a', 100, null),
actual('b', 100, null),
]

const newHeight = await testIndexer.initialize()
expect(newHeight).toEqual(99)
})
})

describe('multiUpdate', () => {
Expand Down Expand Up @@ -388,7 +399,7 @@ describe(MultiIndexer.name, () => {

class TestMultiIndexer extends MultiIndexer<null> {
constructor(
configurations: Configuration<null>[],
configurations: Configuration<null>[] | undefined,
private readonly _saved: SavedConfiguration<null>[],
) {
super(Logger.SILENT, [], configurations)
Expand Down
26 changes: 23 additions & 3 deletions packages/uif/src/indexers/multi/MultiIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,32 @@ import {
} from './types'

export abstract class MultiIndexer<T> extends ChildIndexer {
private readonly ranges: ConfigurationRange<T>[]
private ranges: ConfigurationRange<T>[] = []
private configurations: Configuration<T>[] = []
private saved: SavedConfiguration<T>[] = []

constructor(
logger: Logger,
parents: Indexer[],
readonly configurations: Configuration<T>[],
configurations?: Configuration<T>[],
options?: IndexerOptions,
) {
super(logger, parents, options)
this.ranges = toRanges(configurations)
if (configurations) {
this.configurations = configurations
}
}

/**
* This will run as the first step of initialize() function.
* Allow overriding to provide configurations from a different source.
* Example: your configurations have autoincrement id, so you need to
* first add them to the database to get the MultiIndexer logic to work (it assumes every
* configuration has a unique id)
* @returns The configurations that the indexer should use to sync data.
*/
getInitialConfigurations(): Promise<Configuration<T>[]> | Configuration<T>[] {
return this.configurations
}

/**
Expand Down Expand Up @@ -95,11 +110,16 @@ export abstract class MultiIndexer<T> extends ChildIndexer {
): Promise<void>

async initialize(): Promise<number> {
this.configurations = await this.getInitialConfigurations()
this.ranges = toRanges(this.configurations)

const saved = await this.multiInitialize()

const { toRemove, toSave, safeHeight } = diffConfigurations(
this.configurations,
saved,
)

this.saved = toSave
if (toRemove.length > 0) {
await this.removeData(toRemove)
Expand Down
Loading