impr: improve load more data

This commit is contained in:
liihuu 2024-09-22 00:38:12 +08:00
parent dee7e2f41d
commit f067cf5b96
4 changed files with 46 additions and 33 deletions

View File

@ -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'
@ -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'
@ -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[]>
@ -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> {

View File

@ -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

View File

@ -72,9 +72,9 @@ 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
@ -82,14 +82,9 @@ export default class ChartStore {
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
@ -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
@ -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
@ -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
}
}
@ -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 = []

View File

@ -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
})