File size: 1,263 Bytes
20f83d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict';

function mergeLastGoodQuotes(marketSymbols, freshQuotes, previousQuotes) {
  const freshBySymbol = new Map(
    (Array.isArray(freshQuotes) ? freshQuotes : [])
      .filter((quote) => quote && typeof quote.symbol === 'string')
      .map((quote) => [quote.symbol, quote]),
  );
  const previousBySymbol = new Map(
    (Array.isArray(previousQuotes) ? previousQuotes : [])
      .filter((quote) => quote && typeof quote.symbol === 'string')
      .map((quote) => [quote.symbol, quote]),
  );

  return [...marketSymbols]
    .map((symbol) => freshBySymbol.get(symbol) || previousBySymbol.get(symbol))
    .filter(Boolean);
}

function planYahooRefresh({
  mandatoryYahooSymbols,
  missedPrimarySymbols,
  nowMs,
  lastRefreshAt,
  refreshIntervalMs,
}) {
  const now = Number(nowMs);
  const last = Number(lastRefreshAt);
  const interval = Number(refreshIntervalMs);
  const due = !Number.isFinite(last) || last <= 0 || !Number.isFinite(now)
    || !Number.isFinite(interval) || interval <= 0 || now < last || now - last >= interval;

  return {
    due,
    symbols: due
      ? [...new Set([...(mandatoryYahooSymbols || []), ...(missedPrimarySymbols || [])])]
      : [],
  };
}

module.exports = {
  mergeLastGoodQuotes,
  planYahooRefresh,
};