Skip to content

Commit

Permalink
impr: improve load more data
Browse files Browse the repository at this point in the history
  • Loading branch information
liihuu committed Sep 21, 2024
1 parent dee7e2f commit f067cf5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 33 deletions.
24 changes: 16 additions & 8 deletions src/Chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { UpdateLevel } from './common/Updater'
import type { Styles } from './common/Styles'
import type Crosshair from './common/Crosshair'
import { ActionType, type ActionCallback } from './common/Action'
import type { LoadDataCallback } from './common/LoadDataCallback'
import type { LoadDataCallback, LoadDataMore } from './common/LoadDataCallback'
import type Precision from './common/Precision'
import type VisibleRange from './common/VisibleRange'
import { type CustomApi, LayoutChildType, type Options } from './Options'
Expand All @@ -32,7 +32,7 @@ import Animation from './common/Animation'
import { createId } from './common/utils/id'
import { createDom } from './common/utils/dom'
import { getPixelRatio } from './common/utils/canvas'
import { isString, isArray, isValid, merge, isNumber } from './common/utils/typeChecks'
import { isString, isArray, isValid, merge, isNumber, isBoolean } from './common/utils/typeChecks'
import { logWarn } from './common/utils/logger'
import { binarySearchNearest } from './common/utils/number'
import { LoadDataType } from './common/LoadDataCallback'
Expand Down Expand Up @@ -94,9 +94,9 @@ export interface Chart {
getVisibleRange: () => VisibleRange
clearData: () => void
getDataList: () => KLineData[]
applyNewData: (dataList: KLineData[], more?: boolean) => void
applyNewData: (dataList: KLineData[], more?: boolean | Partial<LoadDataMore>) => void
updateData: (data: KLineData) => void
setLoadDataCallback: (cb: LoadDataCallback) => void
setLoadMoreDataCallback: (cb: LoadDataCallback) => void
createIndicator: (value: string | IndicatorCreate, isStack?: boolean, paneOptions?: PaneOptions) => Nullable<string>
overrideIndicator: (override: IndicatorCreate) => void
getIndicators: (filter?: IndicatorFilter) => Map<string, Indicator[]>
Expand Down Expand Up @@ -664,16 +664,24 @@ export default class ChartImp implements Chart {
return this._chartStore.getDataList()
}

applyNewData (data: KLineData[], more?: boolean): void {
this._chartStore.addData(data, LoadDataType.Init, more)
applyNewData (data: KLineData[], more?: boolean | Partial<LoadDataMore>): void {
let loadDataMore = { forward: false, backward: false }
if (isBoolean(more)) {
loadDataMore.forward = more
loadDataMore.backward = more
} else {
loadDataMore = { ...loadDataMore, ...more }
}

this._chartStore.addData(data, LoadDataType.Init, loadDataMore)
}

updateData (data: KLineData): void {
this._chartStore.addData(data, LoadDataType.Update)
}

setLoadDataCallback (cb: LoadDataCallback): void {
this._chartStore.setLoadDataCallback(cb)
setLoadMoreDataCallback (cb: LoadDataCallback): void {
this._chartStore.setLoadMoreDataCallback(cb)
}

createIndicator (value: string | IndicatorCreate, isStack?: boolean, paneOptions?: Nullable<PaneOptions>): Nullable<string> {
Expand Down
5 changes: 5 additions & 0 deletions src/common/LoadDataCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ export interface LoadDataParams {
callback: (dataList: KLineData[], more?: boolean) => void
}

export interface LoadDataMore {
[LoadDataType.Backward]: boolean
[LoadDataType.Forward]: boolean
}

export type LoadDataCallback = (params: LoadDataParams) => void
46 changes: 23 additions & 23 deletions src/store/ChartStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,19 @@ export default class ChartStore {
private _dataList: KLineData[] = []

/**
* Load data callback
* Load more data callback
*/
private _loadDataCallback: Nullable<LoadDataCallback> = null
private _loadMoreDataCallback: Nullable<LoadDataCallback> = null

/**
* Is loading data flag
*/
private _loading = true

/**
* Whether there are forward more flag
* Whether there are forward and backward more flag
*/
private _forwardMore = true

/**
* Whether there are forward more flag
*/
private _backwardMore = true
private readonly _loadDataMore = { forward: false, backward: false }

/**
* Time scale store
Expand Down Expand Up @@ -222,7 +217,11 @@ export default class ChartStore {
return this._visibleDataList
}

addData (data: KLineData | KLineData[], type: LoadDataType, more?: boolean): void {
addData (
data: KLineData | KLineData[],
type: LoadDataType,
more?: { forward: boolean, backward: boolean }
): void {
let success = false
let adjustFlag = false
let dataLengthChange = 0
Expand All @@ -232,7 +231,8 @@ export default class ChartStore {
case LoadDataType.Init: {
this.clear()
this._dataList = data
this._forwardMore = more ?? true
this._loadDataMore.backward = more?.forward ?? false
this._loadDataMore.forward = more?.forward ?? false
this._timeScaleStore.classifyTimeTicks(this._dataList)
this._timeScaleStore.resetOffsetRightDistance()
adjustFlag = true
Expand All @@ -241,14 +241,14 @@ export default class ChartStore {
case LoadDataType.Backward: {
this._timeScaleStore.classifyTimeTicks(data, true)
this._dataList = this._dataList.concat(data)
this._backwardMore = more ?? false
this._loadDataMore.backward = more?.backward ?? false
adjustFlag = dataLengthChange > 0
break
}
case LoadDataType.Forward: {
this._dataList = data.concat(this._dataList)
this._timeScaleStore.classifyTimeTicks(this._dataList)
this._forwardMore = more ?? false
this._loadDataMore.forward = more?.forward ?? false
adjustFlag = dataLengthChange > 0
}
}
Expand Down Expand Up @@ -286,30 +286,30 @@ export default class ChartStore {
}
}

setLoadDataCallback (callback: LoadDataCallback): void {
this._loadDataCallback = callback
setLoadMoreDataCallback (callback: LoadDataCallback): void {
this._loadMoreDataCallback = callback
}

executeLoadDataCallback (params: Omit<LoadDataParams, 'callback'>): void {
executeLoadMoreDataCallback (params: Omit<LoadDataParams, 'callback'>): void {
if (
!this._loading &&
isValid(this._loadDataCallback) &&
isValid(this._loadMoreDataCallback) &&
(
(this._forwardMore && params.type === LoadDataType.Forward) ||
(this._backwardMore && params.type === LoadDataType.Backward)
(this._loadDataMore.forward && params.type === LoadDataType.Forward) ||
(this._loadDataMore.backward && params.type === LoadDataType.Backward)
)
) {
const cb: ((data: KLineData[], more?: boolean) => void) = (data: KLineData[], more?: boolean) => {
this.addData(data, params.type, more)
this.addData(data, params.type, { forward: more ?? false, backward: more ?? false })
}
this._loading = true
this._loadDataCallback({ ...params, callback: cb })
this._loadMoreDataCallback({ ...params, callback: cb })
}
}

clear (): void {
this._forwardMore = true
this._backwardMore = true
this._loadDataMore.backward = false
this._loadDataMore.forward = false
this._loading = true
this._dataList = []
this._visibleDataList = []
Expand Down
4 changes: 2 additions & 2 deletions src/store/TimeScaleStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,13 @@ export default class TimeScaleStore {
// More processing and loading, more loading if there are callback methods and no data is being loaded
if (from === 0) {
const firstData = dataList[0]
this._chartStore.executeLoadDataCallback({
this._chartStore.executeLoadMoreDataCallback({
type: LoadDataType.Forward,
data: firstData ?? null
})
}
if (to === totalBarCount) {
this._chartStore.executeLoadDataCallback({
this._chartStore.executeLoadMoreDataCallback({
type: LoadDataType.Backward,
data: dataList[totalBarCount - 1] ?? null
})
Expand Down

0 comments on commit f067cf5

Please sign in to comment.