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

Commit

Permalink
Refactor undefined to null for height
Browse files Browse the repository at this point in the history
  • Loading branch information
sz-piotr committed Mar 21, 2024
1 parent e37b341 commit b4b4e7b
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 42 deletions.
28 changes: 14 additions & 14 deletions packages/uif/src/BaseIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export abstract class BaseIndexer implements Indexer {
/**
* Initializes the indexer. It should return a height that the indexer has
* synced up to. If the indexer has not synced any data, it should return
* undefined.
* `null`.
*
* This method is expected to read the height that was saved previously with
* `setSafeHeight`. It shouldn't call `setSafeHeight` itself.
Expand All @@ -45,17 +45,17 @@ export abstract class BaseIndexer implements Indexer {
* Since a root indexer probably doesn't save the height to a database, it
* can `return this.tick()` instead.
*/
abstract initialize(): Promise<number | undefined>
protected abstract initialize(): Promise<number | null>

/**
* Saves the height (most likely to a database). The height given is the
* smallest height from all parents and what the indexer itself synced to
* previously. It can be undefined.
* previously. It can be `null`.
*
* When `initialize` is called it is expected that it will read the same
* height that was saved here.
*/
protected abstract setSafeHeight(height: number | undefined): Promise<void>
protected abstract setSafeHeight(height: number | null): Promise<void>

/**
* This method should only be implemented for a child indexer.
Expand All @@ -66,23 +66,23 @@ export abstract class BaseIndexer implements Indexer {
* 110. The next time this method will be called with `.update(110, 200)`.
*
* @param from The height that the indexer has synced up to previously. Can
* be undefined if no data was synced. This value is inclusive so the indexer
* be `null` if no data was synced. This value is exclusive so the indexer
* should not fetch data for this height.
*
* @param to The height that the indexer should sync up to. This value is
* exclusive so the indexer should fetch data for this height.
* inclusive so the indexer should fetch data for this height.
*
* @returns The height that the indexer has synced up to. Returning `from`
* means that the indexer has not synced any data. Returning a value greater
* than `from` means that the indexer has synced up to that height. Returning
* a value less than `from` will trigger invalidation down to the returned
* value. Returning `undefined` will invalidate all data. Returning a value
* value. Returning `null` will invalidate all data. Returning a value
* greater than `to` is not permitted.
*/
protected abstract update(
from: number | undefined,
from: number | null,
to: number,
): Promise<number | undefined>
): Promise<number | null>

/**
* This method should only be implemented for a child indexer.
Expand All @@ -98,7 +98,7 @@ export abstract class BaseIndexer implements Indexer {
* steps, you can return a height that is larger than the target height.
*
* @param targetHeight The height that the indexer should invalidate down to.
* Can be undefined. If it is undefined, the indexer should invalidate all
* Can be `null`. If it is `null`, the indexer should invalidate all
* data.
*
* @returns The height that the indexer has invalidated down to. Returning
Expand All @@ -107,8 +107,8 @@ export abstract class BaseIndexer implements Indexer {
* has invalidated down to that height.
*/
protected abstract invalidate(
targetHeight: number | undefined,
): Promise<number | undefined>
targetHeight: number | null,
): Promise<number | null>

/**
* This method should only be implemented for a root indexer.
Expand All @@ -117,7 +117,7 @@ export abstract class BaseIndexer implements Indexer {
* Some good examples of this are: the current time or the last block number.
*
* As opposed to `update` and `invalidate`, this method cannot return
* `undefined`.
* `null`.
*/
protected abstract tick(): Promise<number>

Expand Down Expand Up @@ -177,7 +177,7 @@ export abstract class BaseIndexer implements Indexer {
this.dispatch({ type: 'ChildReady', index })
}

notifyUpdate(parent: Indexer, safeHeight: number | undefined): void {
notifyUpdate(parent: Indexer, safeHeight: number | null): void {
this.logger.debug('Someone has updated', {
parent: parent.constructor.name,
})
Expand Down
2 changes: 1 addition & 1 deletion packages/uif/src/Indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export interface UpdateEvent {
export interface Indexer {
subscribe(child: Indexer): void
notifyReady(child: Indexer): void
notifyUpdate(parent: Indexer, safeHeight: number | undefined): void
notifyUpdate(parent: Indexer, safeHeight: number | null): void
start(): Promise<void>
}
24 changes: 9 additions & 15 deletions packages/uif/src/height.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,36 @@ export const Height = {
min,
}

function lt(heightA: number | undefined, heightB: number | undefined): boolean {
function lt(heightA: number | null, heightB: number | null): boolean {
if (heightA === heightB) {
return false
}
return !gt(heightA, heightB)
}

function lte(
heightA: number | undefined,
heightB: number | undefined,
): boolean {
function lte(heightA: number | null, heightB: number | null): boolean {
return !gt(heightA, heightB)
}

function gt(heightA: number | undefined, heightB: number | undefined): boolean {
if (heightA === undefined) {
function gt(heightA: number | null, heightB: number | null): boolean {
if (heightA === null) {
return false
}
if (heightB === undefined) {
if (heightB === null) {
return true
}
return heightA > heightB
}

function gte(
heightA: number | undefined,
heightB: number | undefined,
): boolean {
function gte(heightA: number | null, heightB: number | null): boolean {
return !lt(heightA, heightB)
}

function min(...heights: (number | undefined)[]): number | undefined {
function min(...heights: (number | null)[]): number | null {
if (heights.length === 0) {
return undefined
return null
}
let minHeight = heights[0]
let minHeight = heights[0] ?? null
for (const height of heights) {
if (gt(minHeight, height)) {
minHeight = height
Expand Down
2 changes: 1 addition & 1 deletion packages/uif/src/reducer/helpers/continueOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export function continueOperations(
!shouldInvalidate &&
initializedParents.length > 0 &&
Height.gt(parentHeight, state.height) &&
parentHeight !== undefined
parentHeight !== null

if (shouldInvalidate) {
if (state.invalidateBlocked || state.waiting || state.status !== 'idle') {
Expand Down
10 changes: 5 additions & 5 deletions packages/uif/src/reducer/types/IndexerAction.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export interface InitializedAction {
type: 'Initialized'
safeHeight: number | undefined
safeHeight: number | null
childCount: number
}

export interface ParentUpdatedAction {
type: 'ParentUpdated'
index: number
safeHeight: number | undefined
safeHeight: number | null
}

export interface ChildReadyAction {
Expand All @@ -17,8 +17,8 @@ export interface ChildReadyAction {

export interface UpdateSucceededAction {
type: 'UpdateSucceeded'
from: number | undefined
newHeight: number | undefined
from: number | null
newHeight: number | null
}

export interface UpdateFailedAction {
Expand All @@ -32,7 +32,7 @@ export interface RetryUpdateAction {

export interface InvalidateSucceededAction {
type: 'InvalidateSucceeded'
targetHeight: number | undefined
targetHeight: number | null
}

export interface InvalidateFailedAction {
Expand Down
4 changes: 2 additions & 2 deletions packages/uif/src/reducer/types/IndexerEffect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export interface UpdateEffect {

export interface InvalidateEffect {
type: 'Invalidate'
targetHeight: number | undefined
targetHeight: number | null
}

export interface SetSafeHeightEffect {
type: 'SetSafeHeight'
safeHeight: number | undefined
safeHeight: number | null
}

export interface NotifyReadyEffect {
Expand Down
8 changes: 4 additions & 4 deletions packages/uif/src/reducer/types/IndexerState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ export interface IndexerState {
| 'invalidating'
| 'ticking'
| 'errored'
readonly height: number | undefined
readonly invalidateToHeight: number | undefined
readonly height: number | null
readonly invalidateToHeight: number | null
readonly forceInvalidate: boolean
// When we change safe height to a lower value we become waiting
// and we mark all children as not ready
readonly safeHeight: number | undefined
readonly safeHeight: number | null
readonly waiting: boolean
readonly tickScheduled: boolean
readonly initializedSelf: boolean
readonly parents: {
readonly initialized: boolean
// When the parent changes safeHeight to a lower value
// we mark them as waiting and will notify them when we're ready
readonly safeHeight: number | undefined
readonly safeHeight: number | null
readonly waiting: boolean
}[]
readonly children: {
Expand Down

0 comments on commit b4b4e7b

Please sign in to comment.