@icgio/icg-utils 1.9.9 → 1.9.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +1 -0
- package/lib/influxdb.js +1 -0
- package/lib/observable.js +54 -0
- package/lib/utils.js +0 -18
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -7,6 +7,7 @@ module.exports = {
|
|
|
7
7
|
ip: require('./lib/ip.js'),
|
|
8
8
|
logger: require('./lib/logger.js'),
|
|
9
9
|
mail: require('./lib/mail.js'),
|
|
10
|
+
observable: require('./lib/observable.js'),
|
|
10
11
|
ohlcv: require('./lib/ohlcv'),
|
|
11
12
|
rate_limiter_logger: require('./lib/rate-limit-logger.js'),
|
|
12
13
|
rate_limiter: require('./lib/rate-limiter.js'),
|
package/lib/influxdb.js
CHANGED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
class Observable {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.listeners = []
|
|
4
|
+
this.last_notify_time = 0
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Subscribe to changes
|
|
9
|
+
* @param {Function} callback - Function to call when an update occurs
|
|
10
|
+
*/
|
|
11
|
+
subscribe(callback) {
|
|
12
|
+
if (typeof callback !== 'function') {
|
|
13
|
+
throw new Error('Callback must be a function')
|
|
14
|
+
}
|
|
15
|
+
this.listeners.push(callback)
|
|
16
|
+
return () => this.unsubscribe(callback) // Return an unsubscribe function
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Unsubscribe from changes
|
|
21
|
+
* @param {Function} callback - The callback to remove
|
|
22
|
+
*/
|
|
23
|
+
unsubscribe(callback) {
|
|
24
|
+
this.listeners = this.listeners.filter((listener) => listener !== callback)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Notify all subscribers about an update
|
|
29
|
+
*/
|
|
30
|
+
notify() {
|
|
31
|
+
this.listeners.forEach((callback) => callback())
|
|
32
|
+
this.last_notify_time = Date.now()
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
module.exports = Observable
|
|
37
|
+
|
|
38
|
+
// // Example Usage
|
|
39
|
+
// const observable = new Observable()
|
|
40
|
+
|
|
41
|
+
// // Subscribe to updates
|
|
42
|
+
// const unsubscribe = observable.subscribe((update) => {
|
|
43
|
+
// console.log('Received update:', update)
|
|
44
|
+
// })
|
|
45
|
+
|
|
46
|
+
// // Emit updates
|
|
47
|
+
// observable.notify({ message: 'Hello, world!' }) // Logs: Received update: { message: 'Hello, world!' }
|
|
48
|
+
// observable.notify({ message: 'Another update!' }) // Logs: Received update: { message: 'Another update!' }
|
|
49
|
+
|
|
50
|
+
// // Unsubscribe from updates
|
|
51
|
+
// unsubscribe()
|
|
52
|
+
|
|
53
|
+
// // No logs after unsubscribe
|
|
54
|
+
// observable.notify({ message: 'No one will see this' })
|
package/lib/utils.js
CHANGED
|
@@ -839,21 +839,3 @@ module.exports.exchange_basecur_curs_converter = (exchange_basecur_curs) => {
|
|
|
839
839
|
module.exports.sort_keys = (dict) => {
|
|
840
840
|
return _.fromPairs(_.sortBy(_.toPairs(dict), (e) => e[0]))
|
|
841
841
|
}
|
|
842
|
-
|
|
843
|
-
module.exports.random_normal_range = (mean, std_dev, a, b) => {
|
|
844
|
-
let min = Math.min(a, b)
|
|
845
|
-
let max = Math.max(a, b)
|
|
846
|
-
let u = 1 - Math.random() // [0,1) -> (0,1]
|
|
847
|
-
let v = Math.random()
|
|
848
|
-
let z = Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v) // Box-Muller Transform
|
|
849
|
-
let num = mean + z * std_dev
|
|
850
|
-
return Math.max(min, Math.min(max, num))
|
|
851
|
-
}
|
|
852
|
-
|
|
853
|
-
// module.exports.random_beta_range = (alpha, beta, a, b) => {
|
|
854
|
-
// let u = Math.random()
|
|
855
|
-
// let v = Math.random()
|
|
856
|
-
// let value = Math.pow(u, 1 / alpha) / (Math.pow(u, 1 / alpha) + Math.pow(v, 1 / beta))
|
|
857
|
-
// return a + value * (b - a) // Scale result into [a, b] range
|
|
858
|
-
// }
|
|
859
|
-
// console.log(module.exports.random_beta_range(1, 100, 0.02, 0.03))
|