class DataFeedBase { configuration: DatafeedConfiguration
constructor(props: Partial<ChartingLibraryWidgetOptions>) { this.configuration = { supports_time: true, supports_timescale_marks: true, supports_marks: true, supported_resolutions: ['1', '5', '15', '30', '60', '240', '1D', '1W', '1M'], intraday_multipliers: ['1', '5', '15', '30', '60', '240', '1D', '1W', '1M'] } as DatafeedConfiguration
this.setActiveSymbolInfo = props.setActiveSymbolInfo this.removeActiveSymbol = props.removeActiveSymbol this.getDataFeedBarCallback = props.getDataFeedBarCallback this.isZh = props.locale === 'zh_TW' }
onReady(callback) { setTimeout(() => { callback(this.configuration) }, 0) }
async resolveSymbol(symbolName, onSymbolResolvedCallback, onResolveErrorCallback, extension) { const resolution = String(STORAGE_GET_TRADINGVIEW_RESOLUTION() || '') const ENV = getEnv() const urlPrefix = ENV.isApp ? getInjectParams().baseUrl : ''
let symbolInfo if (!ENV.isApp) { const res = await request(`${urlPrefix}/api/trade-core/coreApi/symbols/symbol/detail?symbol=${symbolName}`) symbolInfo = res?.data || {} } else { symbolInfo = { ...(ENV?.injectParams?.symbolInfo || {}), ...(stores.global.symbolInfo || {}) } }
const currentSymbol = { ...symbolInfo, precision: symbolInfo?.symbolDecimal || 2, description: symbolInfo?.remark || '', exchange: '', session: '24x7', name: symbolInfo.symbol, dataSourceCode: symbolInfo.dataSourceCode }
const commonSymbolInfo = { has_intraday: true, has_daily: true, has_weekly_and_monthly: true, intraday_multipliers: this.configuration.intraday_multipliers, supported_resolutions: this.configuration.supported_resolutions, data_status: 'streaming', format: 'price', minmov: 1, pricescale: Math.pow(10, currentSymbol.precision), ticker: currentSymbol?.name } as LibrarySymbolInfo
const currentSymbolInfo = { ...commonSymbolInfo, ...currentSymbol, description: this.isZh ? currentSymbol.description : currentSymbol?.name, exchange: this.isZh ? currentSymbol?.exchange : '', session: '0000-0000|0000-0000:1234567;1', timezone: ['D', 'W', 'M', 'Y'].some((item) => resolution.endsWith(item)) ? 'Etc/UTC' : 'Asia/Shanghai' } as LibrarySymbolInfo
setTimeout(() => { onSymbolResolvedCallback(currentSymbolInfo) }, 0) }
searchSymbols(userInput, exchange, symbolType, onResultReadyCallback) { const keyword = userInput || '' const resultArr = symbolInfoArr .filter((item) => item.name.includes(keyword)) .map((item) => ({ symbol: item.name, name: item.name, full_name: `${item.name}`, description: this.isZh ? item.description : item.name, exchange: this.isZh ? item.exchange : '', type: item.type, ticker: item.name }))
setTimeout(() => { onResultReadyCallback(resultArr) }, 0) }
getBars(symbolInfo, resolution, periodParams, onHistoryCallback, onErrorCallback) { const { from, to, firstDataRequest, countBack } = periodParams this.setActiveSymbolInfo({ symbolInfo, resolution }) this.getDataFeedBarCallback({ symbolInfo, resolution, from, to, countBack, onHistoryCallback, onErrorCallback, firstDataRequest }) }
subscribeBars(symbolInfo, resolution, onRealtimeCallback, subscriberUID, onResetCacheNeededCallback) { this.setActiveSymbolInfo({ symbolInfo, resolution, onRealtimeCallback, subscriberUID, onResetCacheNeededCallback }) mitt.on('symbol_change', () => { onResetCacheNeededCallback() }) }
unsubscribeBars(subscriberUID) { this.removeActiveSymbol(subscriberUID) } }
export default DataFeedBase
|