@@ -46,24 +46,29 @@ export default class TicksService {
46
46
this . tickListeners = new Map ( ) ;
47
47
this . ohlcListeners = new Map ( ) ;
48
48
this . subscriptions = new Map ( ) ;
49
+ this . ticks_history_promise = null ;
50
+ this . active_symbols_promise = null ;
49
51
this . observe ( ) ;
50
52
}
51
53
requestPipSizes ( ) {
52
54
if ( this . pipSizes ) {
53
55
return Promise . resolve ( this . pipSizes ) ;
54
56
}
55
57
56
- return new Promise ( resolve => {
57
- this . api . getActiveSymbolsBrief ( ) . then ( r => {
58
- const { active_symbols : symbols } = r ;
59
- this . pipSizes = symbols . reduce ( ( accumulator , currSymbol ) => {
60
- // eslint-disable-next-line no-param-reassign
61
- accumulator [ currSymbol . symbol ] = `${ currSymbol . pip } ` . length - 2 ;
62
- return accumulator ;
63
- } , { } ) ;
64
- resolve ( this . pipSizes ) ;
58
+ if ( ! this . active_symbols_promise ) {
59
+ this . active_symbols_promise = new Promise ( resolve => {
60
+ this . api . getActiveSymbolsBrief ( ) . then ( r => {
61
+ const { active_symbols : symbols } = r ;
62
+ this . pipSizes = symbols . reduce ( ( accumulator , currSymbol ) => {
63
+ // eslint-disable-next-line no-param-reassign
64
+ accumulator [ currSymbol . symbol ] = `${ currSymbol . pip } ` . length - 2 ;
65
+ return accumulator ;
66
+ } , { } ) ;
67
+ resolve ( this . pipSizes ) ;
68
+ } ) ;
65
69
} ) ;
66
- } ) ;
70
+ }
71
+ return this . active_symbols_promise ;
67
72
}
68
73
request ( options ) {
69
74
const { symbol, granularity } = options ;
@@ -208,31 +213,34 @@ export default class TicksService {
208
213
requestTicks ( options ) {
209
214
const { symbol, granularity, style } = options ;
210
215
211
- return new Promise ( ( resolve , reject ) => {
212
- doUntilDone ( ( ) =>
213
- this . api . getTickHistory ( symbol , {
214
- subscribe : 1 ,
215
- end : 'latest' ,
216
- count : 1000 ,
217
- granularity : granularity ? Number ( granularity ) : undefined ,
218
- style,
219
- } )
220
- )
221
- . then ( r => {
222
- if ( style === 'ticks' ) {
223
- const ticks = historyToTicks ( r . history ) ;
224
-
225
- this . updateTicksAndCallListeners ( symbol , ticks ) ;
226
- resolve ( ticks ) ;
227
- } else {
228
- const candles = parseCandles ( r . candles ) ;
229
-
230
- this . updateCandlesAndCallListeners ( [ symbol , Number ( granularity ) ] , candles ) ;
231
-
232
- resolve ( candles ) ;
233
- }
234
- } )
235
- . catch ( reject ) ;
236
- } ) ;
216
+ if ( ! this . ticks_history_promise ) {
217
+ this . ticks_history_promise = new Promise ( ( resolve , reject ) => {
218
+ doUntilDone ( ( ) =>
219
+ this . api . getTickHistory ( symbol , {
220
+ subscribe : 1 ,
221
+ end : 'latest' ,
222
+ count : 1000 ,
223
+ granularity : granularity ? Number ( granularity ) : undefined ,
224
+ style,
225
+ } )
226
+ )
227
+ . then ( r => {
228
+ if ( style === 'ticks' ) {
229
+ const ticks = historyToTicks ( r . history ) ;
230
+
231
+ this . updateTicksAndCallListeners ( symbol , ticks ) ;
232
+ resolve ( ticks ) ;
233
+ } else {
234
+ const candles = parseCandles ( r . candles ) ;
235
+
236
+ this . updateCandlesAndCallListeners ( [ symbol , Number ( granularity ) ] , candles ) ;
237
+
238
+ resolve ( candles ) ;
239
+ }
240
+ } )
241
+ . catch ( reject ) ;
242
+ } ) ;
243
+ }
244
+ return this . ticks_history_promise ;
237
245
}
238
246
}
0 commit comments