This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
CurrencyService.ts
179 lines (153 loc) · 5.08 KB
/
CurrencyService.ts
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) 2022. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.
import { Currency } from "./types/Currency";
import { CurrencyFetchRatesCallback } from "./types/CurrencyFetchRatesCallback";
import { LogService } from "./LogService";
import { CurrencyRates } from "./types/CurrencyRates";
import { CurrencyUtils } from "./CurrencyUtils";
import { Observer, ObserverCallback, ObserverDestructor } from "./Observer";
import { Disposable } from "./types/Disposable";
const LOG = LogService.createLogger('CurrencyService');
const DEFAULT_FETCH_INTERVAL_SECONDS = 24*60*60;
export enum CurrencyServiceEvent {
INITIALIZED = "CurrencyService:initialized",
RATES_UPDATED = "CurrencyService:ratesUpdated",
STARTED = "CurrencyService:started",
STOPPED = "CurrencyService:stopped",
}
export type CurrencyServiceDestructor = ObserverDestructor;
export class CurrencyService implements Disposable {
public static Event = CurrencyServiceEvent;
private readonly _observer : Observer<CurrencyServiceEvent>;
private readonly _fetchRatesCallback : CurrencyFetchRatesCallback;
private readonly _fetchIntervalMinutes : number;
private _rates : CurrencyRates | undefined;
private _fetchIntervalId : any | undefined;
private _initializing : boolean = false;
/**
*
* @param callback
* @param fetchInterval
*/
public constructor (
callback: CurrencyFetchRatesCallback,
fetchInterval : number = DEFAULT_FETCH_INTERVAL_SECONDS
) {
this._observer = new Observer<CurrencyServiceEvent>("CurrencyService");
this._fetchRatesCallback = callback;
this._rates = undefined;
this._fetchIntervalMinutes = fetchInterval;
}
public on (
name: CurrencyServiceEvent,
callback: ObserverCallback<CurrencyServiceEvent>
): CurrencyServiceDestructor {
return this._observer.listenEvent(name, callback);
}
public async initialize () : Promise<void> {
this._initializing = true;
try {
await this._updateRates();
this.start();
} finally {
this._initializing = false;
}
if (this._observer.hasCallbacks(CurrencyServiceEvent.INITIALIZED)) {
this._observer.triggerEvent(CurrencyServiceEvent.INITIALIZED)
}
}
public destroy (): void {
this.stop();
this._observer.destroy();
}
public start () {
if (this._fetchIntervalId === undefined) {
this._startInterval();
}
}
public stop () {
if (this._fetchIntervalId !== undefined) {
this._stopInterval();
}
}
public isInitializing () : boolean {
return this._initializing;
}
public isStarted () : boolean {
return this._fetchIntervalId !== undefined;
}
public hasRates () : boolean {
return this._rates !== undefined;
}
public getRates () : CurrencyRates | undefined {
return this._rates;
}
/**
* Set rates directly
* @param rates
*/
public setRates (rates : CurrencyRates) {
if (rates !== this._rates) {
this._rates = rates;
if (this._observer.hasCallbacks(CurrencyServiceEvent.RATES_UPDATED)) {
this._observer.triggerEvent(CurrencyServiceEvent.RATES_UPDATED)
}
}
}
/**
* Update rates using fetch callback
*/
public updateRates () {
this._updateRates().catch((err) => {
LOG.error(`Could not update rates: `, err);
});
}
/**
*
* @param amount
* @param from
* @param to
* @param accuracy
*/
public convertCurrencyAmount (
amount : number,
from : Currency,
to : Currency,
accuracy : number
) : number {
if (this._rates === undefined) {
throw new TypeError(`CurrencyService does not have rates defined yet`);
}
return CurrencyUtils.convertCurrencyAmount(this._rates, amount, from, to, accuracy);
}
/**
*
* @private
*/
private async _updateRates () {
this._rates = await this._fetchRatesCallback();
if (this._observer.hasCallbacks(CurrencyServiceEvent.RATES_UPDATED)) {
this._observer.triggerEvent(CurrencyServiceEvent.RATES_UPDATED)
}
}
private _stopInterval () {
clearInterval(this._fetchIntervalId);
this._fetchIntervalId = undefined;
if (this._observer.hasCallbacks(CurrencyServiceEvent.STOPPED)) {
this._observer.triggerEvent(CurrencyServiceEvent.STOPPED)
}
}
private _startInterval () {
if (this._fetchIntervalId !== undefined) {
this._stopInterval();
}
this._fetchIntervalId = setInterval(
() => {
this.updateRates();
},
this._fetchIntervalMinutes * 1000
);
if (this._observer.hasCallbacks(CurrencyServiceEvent.STARTED)) {
this._observer.triggerEvent(CurrencyServiceEvent.STARTED)
}
}
}