metal-price-live 0.2.0
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/LICENSE +20 -0
- package/README.md +42 -0
- package/lib/commonjs/index.js +80 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/module/index.js +74 -0
- package/lib/module/index.js.map +1 -0
- package/lib/typescript/src/index.d.ts +29 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/package.json +165 -0
- package/src/index.tsx +113 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 S.M.Abdullah Al Mamun
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
in the Software without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# metal-price-live
|
|
2
|
+
|
|
3
|
+
Live metal prices for react and react native using websocket
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Using npm:
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
npm install metal-price-live
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Using yarn:
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
yarn add metal-price-live
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
import useMetalPriceLive from 'metal-price-live';
|
|
23
|
+
|
|
24
|
+
// ...
|
|
25
|
+
|
|
26
|
+
const { status, data, error } = useMetalPriceLive(
|
|
27
|
+
'wss://your-websocket-endpoint',
|
|
28
|
+
'your-api-key'
|
|
29
|
+
);
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Contributing
|
|
33
|
+
|
|
34
|
+
See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
|
|
35
|
+
|
|
36
|
+
## License
|
|
37
|
+
|
|
38
|
+
MIT
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _react = require("react");
|
|
8
|
+
const minReconnectDelay = 1000;
|
|
9
|
+
const maxReconnectDelay = 300000;
|
|
10
|
+
const authRetry = 5;
|
|
11
|
+
const errorReason = 'Unauthorized';
|
|
12
|
+
const useMetalPriceLive = (socketUrl, apiKey) => {
|
|
13
|
+
const [connectionState, setConnectionState] = (0, _react.useState)({
|
|
14
|
+
status: 'connecting'
|
|
15
|
+
});
|
|
16
|
+
const [retry, setRetry] = (0, _react.useState)(0);
|
|
17
|
+
const maxAuthRetryRef = (0, _react.useRef)(0);
|
|
18
|
+
const currentReconnectDelayRef = (0, _react.useRef)(minReconnectDelay);
|
|
19
|
+
const ws = (0, _react.useRef)(null);
|
|
20
|
+
const handleWsOpen = () => {
|
|
21
|
+
setConnectionState({
|
|
22
|
+
status: 'connecting'
|
|
23
|
+
});
|
|
24
|
+
currentReconnectDelayRef.current = minReconnectDelay;
|
|
25
|
+
};
|
|
26
|
+
const handleWsMessage = e => {
|
|
27
|
+
setConnectionState({
|
|
28
|
+
status: 'connected',
|
|
29
|
+
data: JSON.parse(e.data)
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
const handleWsError = e => {
|
|
33
|
+
setConnectionState({
|
|
34
|
+
status: 'error',
|
|
35
|
+
error: e.message || `WebSocket closed unexpectedly`
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
const handleWsClose = e => {
|
|
39
|
+
setConnectionState({
|
|
40
|
+
status: 'error',
|
|
41
|
+
error: (e === null || e === void 0 ? void 0 : e.reason) || `WebSocket closed unexpectedly`
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
//retry logic on auth fail
|
|
45
|
+
if ((e === null || e === void 0 ? void 0 : e.reason) === errorReason) {
|
|
46
|
+
maxAuthRetryRef.current++;
|
|
47
|
+
}
|
|
48
|
+
if (authRetry < maxAuthRetryRef.current) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Retry logic
|
|
53
|
+
if (ws.current && ws.current.readyState !== WebSocket.OPEN) {
|
|
54
|
+
setTimeout(() => {
|
|
55
|
+
//This state is triggering the useEffect for retry logic
|
|
56
|
+
setRetry(prev => prev + 1);
|
|
57
|
+
currentReconnectDelayRef.current = Math.min(currentReconnectDelayRef.current * 2, maxReconnectDelay);
|
|
58
|
+
}, currentReconnectDelayRef.current);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
(0, _react.useEffect)(() => {
|
|
62
|
+
ws.current = new WebSocket(`${socketUrl}/${apiKey}`);
|
|
63
|
+
ws.current.addEventListener('open', handleWsOpen);
|
|
64
|
+
ws.current.addEventListener('message', handleWsMessage);
|
|
65
|
+
ws.current.addEventListener('error', handleWsError);
|
|
66
|
+
ws.current.addEventListener('close', handleWsClose);
|
|
67
|
+
return () => {
|
|
68
|
+
if (ws.current) {
|
|
69
|
+
ws.current.close();
|
|
70
|
+
ws.current.removeEventListener('open', handleWsOpen);
|
|
71
|
+
ws.current.removeEventListener('message', handleWsMessage);
|
|
72
|
+
ws.current.removeEventListener('error', handleWsError);
|
|
73
|
+
ws.current.removeEventListener('close', handleWsClose);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}, [socketUrl, retry, apiKey]);
|
|
77
|
+
return connectionState;
|
|
78
|
+
};
|
|
79
|
+
var _default = exports.default = useMetalPriceLive;
|
|
80
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_react","require","minReconnectDelay","maxReconnectDelay","authRetry","errorReason","useMetalPriceLive","socketUrl","apiKey","connectionState","setConnectionState","useState","status","retry","setRetry","maxAuthRetryRef","useRef","currentReconnectDelayRef","ws","handleWsOpen","current","handleWsMessage","e","data","JSON","parse","handleWsError","error","message","handleWsClose","reason","readyState","WebSocket","OPEN","setTimeout","prev","Math","min","useEffect","addEventListener","close","removeEventListener","_default","exports","default"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAEA,MAAMC,iBAAiB,GAAG,IAAI;AAC9B,MAAMC,iBAAiB,GAAG,MAAM;AAChC,MAAMC,SAAS,GAAG,CAAC;AACnB,MAAMC,WAAW,GAAG,cAAc;AA+BlC,MAAMC,iBAAiB,GAAGA,CAACC,SAAiB,EAAEC,MAAc,KAAK;EAC/D,MAAM,CAACC,eAAe,EAAEC,kBAAkB,CA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AAC;EAE9B,OAAOC,eAAe;AACxB,CAAC;AAAC,IAAAiC,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAEatC,iBAAiB"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { useState, useEffect, useRef } from 'react';
|
|
2
|
+
const minReconnectDelay = 1000;
|
|
3
|
+
const maxReconnectDelay = 300000;
|
|
4
|
+
const authRetry = 5;
|
|
5
|
+
const errorReason = 'Unauthorized';
|
|
6
|
+
const useMetalPriceLive = (socketUrl, apiKey) => {
|
|
7
|
+
const [connectionState, setConnectionState] = useState({
|
|
8
|
+
status: 'connecting'
|
|
9
|
+
});
|
|
10
|
+
const [retry, setRetry] = useState(0);
|
|
11
|
+
const maxAuthRetryRef = useRef(0);
|
|
12
|
+
const currentReconnectDelayRef = useRef(minReconnectDelay);
|
|
13
|
+
const ws = useRef(null);
|
|
14
|
+
const handleWsOpen = () => {
|
|
15
|
+
setConnectionState({
|
|
16
|
+
status: 'connecting'
|
|
17
|
+
});
|
|
18
|
+
currentReconnectDelayRef.current = minReconnectDelay;
|
|
19
|
+
};
|
|
20
|
+
const handleWsMessage = e => {
|
|
21
|
+
setConnectionState({
|
|
22
|
+
status: 'connected',
|
|
23
|
+
data: JSON.parse(e.data)
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
const handleWsError = e => {
|
|
27
|
+
setConnectionState({
|
|
28
|
+
status: 'error',
|
|
29
|
+
error: e.message || `WebSocket closed unexpectedly`
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
const handleWsClose = e => {
|
|
33
|
+
setConnectionState({
|
|
34
|
+
status: 'error',
|
|
35
|
+
error: (e === null || e === void 0 ? void 0 : e.reason) || `WebSocket closed unexpectedly`
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
//retry logic on auth fail
|
|
39
|
+
if ((e === null || e === void 0 ? void 0 : e.reason) === errorReason) {
|
|
40
|
+
maxAuthRetryRef.current++;
|
|
41
|
+
}
|
|
42
|
+
if (authRetry < maxAuthRetryRef.current) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Retry logic
|
|
47
|
+
if (ws.current && ws.current.readyState !== WebSocket.OPEN) {
|
|
48
|
+
setTimeout(() => {
|
|
49
|
+
//This state is triggering the useEffect for retry logic
|
|
50
|
+
setRetry(prev => prev + 1);
|
|
51
|
+
currentReconnectDelayRef.current = Math.min(currentReconnectDelayRef.current * 2, maxReconnectDelay);
|
|
52
|
+
}, currentReconnectDelayRef.current);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
ws.current = new WebSocket(`${socketUrl}/${apiKey}`);
|
|
57
|
+
ws.current.addEventListener('open', handleWsOpen);
|
|
58
|
+
ws.current.addEventListener('message', handleWsMessage);
|
|
59
|
+
ws.current.addEventListener('error', handleWsError);
|
|
60
|
+
ws.current.addEventListener('close', handleWsClose);
|
|
61
|
+
return () => {
|
|
62
|
+
if (ws.current) {
|
|
63
|
+
ws.current.close();
|
|
64
|
+
ws.current.removeEventListener('open', handleWsOpen);
|
|
65
|
+
ws.current.removeEventListener('message', handleWsMessage);
|
|
66
|
+
ws.current.removeEventListener('error', handleWsError);
|
|
67
|
+
ws.current.removeEventListener('close', handleWsClose);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
}, [socketUrl, retry, apiKey]);
|
|
71
|
+
return connectionState;
|
|
72
|
+
};
|
|
73
|
+
export default useMetalPriceLive;
|
|
74
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useState","useEffect","useRef","minReconnectDelay","maxReconnectDelay","authRetry","errorReason","useMetalPriceLive","socketUrl","apiKey","connectionState","setConnectionState","status","retry","setRetry","maxAuthRetryRef","currentReconnectDelayRef","ws","handleWsOpen","current","handleWsMessage","e","data","JSON","parse","handleWsError","error","message","handleWsClose","reason","readyState","WebSocket","OPEN","setTimeout","prev","Math","min","addEventListener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CAAC;QACpDD,EAAE,CAACE,OAAO,CAACoB,mBAAmB,CAAC,SAAS,EAAEnB,eAAe,CAAC;QAC1DH,EAAE,CAACE,OAAO,CAACoB,mBAAmB,CAAC,OAAO,EAAEd,aAAa,CAAC;QACtDR,EAAE,CAACE,OAAO,CAACoB,mBAAmB,CAAC,OAAO,EAAEX,aAAa,CAAC;MACxD;IACF,CAAC;EACH,CAAC,EAAE,CAACpB,SAAS,EAAEK,KAAK,EAAEJ,MAAM,CAAC,CAAC;EAE9B,OAAOC,eAAe;AACxB,CAAC;AAED,eAAeH,iBAAiB"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
interface ApiData {
|
|
2
|
+
timestamp: number;
|
|
3
|
+
price: number;
|
|
4
|
+
ask: number;
|
|
5
|
+
bid: number;
|
|
6
|
+
price_gram_24k: number;
|
|
7
|
+
price_gram_22k: number;
|
|
8
|
+
price_gram_21k: number;
|
|
9
|
+
key: string;
|
|
10
|
+
}
|
|
11
|
+
interface Connected {
|
|
12
|
+
status: 'connected';
|
|
13
|
+
data: ApiData;
|
|
14
|
+
error?: undefined;
|
|
15
|
+
}
|
|
16
|
+
interface Connecting {
|
|
17
|
+
status: 'connecting';
|
|
18
|
+
data?: ApiData;
|
|
19
|
+
error?: string;
|
|
20
|
+
}
|
|
21
|
+
interface Error {
|
|
22
|
+
status: 'error';
|
|
23
|
+
data?: ApiData;
|
|
24
|
+
error: string;
|
|
25
|
+
}
|
|
26
|
+
type ConnectionState = Connected | Error | Connecting;
|
|
27
|
+
declare const useMetalPriceLive: (socketUrl: string, apiKey: string) => ConnectionState;
|
|
28
|
+
export default useMetalPriceLive;
|
|
29
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AAOA,UAAU,OAAO;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,cAAc,EAAE,MAAM,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,GAAG,EAAE,MAAM,CAAC;CACb;AAED,UAAU,SAAS;IACjB,MAAM,EAAE,WAAW,CAAC;IACpB,IAAI,EAAE,OAAO,CAAC;IACd,KAAK,CAAC,EAAE,SAAS,CAAC;CACnB;AAED,UAAU,UAAU;IAClB,MAAM,EAAE,YAAY,CAAC;IACrB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AACD,UAAU,KAAK;IACb,MAAM,EAAE,OAAO,CAAC;IAChB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;CACf;AAED,KAAK,eAAe,GAAG,SAAS,GAAG,KAAK,GAAG,UAAU,CAAC;AACtD,QAAA,MAAM,iBAAiB,cAAe,MAAM,UAAU,MAAM,oBA0E3D,CAAC;AAEF,eAAe,iBAAiB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "metal-price-live",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Live metal price hook for react and react native using websocket",
|
|
5
|
+
"main": "lib/commonjs/index",
|
|
6
|
+
"module": "lib/module/index",
|
|
7
|
+
"types": "lib/typescript/src/index.d.ts",
|
|
8
|
+
"react-native": "src/index",
|
|
9
|
+
"source": "src/index",
|
|
10
|
+
"files": [
|
|
11
|
+
"src",
|
|
12
|
+
"lib",
|
|
13
|
+
"android",
|
|
14
|
+
"ios",
|
|
15
|
+
"cpp",
|
|
16
|
+
"*.podspec",
|
|
17
|
+
"!ios/build",
|
|
18
|
+
"!android/build",
|
|
19
|
+
"!android/gradle",
|
|
20
|
+
"!android/gradlew",
|
|
21
|
+
"!android/gradlew.bat",
|
|
22
|
+
"!android/local.properties",
|
|
23
|
+
"!**/__tests__",
|
|
24
|
+
"!**/__fixtures__",
|
|
25
|
+
"!**/__mocks__",
|
|
26
|
+
"!**/.*"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"example": "yarn workspace metal-price-live-example",
|
|
30
|
+
"test": "jest",
|
|
31
|
+
"typecheck": "tsc --noEmit",
|
|
32
|
+
"lint": "eslint \"**/*.{js,ts,tsx}\"",
|
|
33
|
+
"clean": "del-cli lib",
|
|
34
|
+
"prepare": "bob build",
|
|
35
|
+
"release": "release-it",
|
|
36
|
+
"gittu": "git add . && git commit && git push"
|
|
37
|
+
},
|
|
38
|
+
"keywords": [
|
|
39
|
+
"react-native",
|
|
40
|
+
"ios",
|
|
41
|
+
"android"
|
|
42
|
+
],
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "git+https://github.com/smamun19/metal-price-live.git"
|
|
46
|
+
},
|
|
47
|
+
"author": "S.M.Abdullah Al Mamun <smamun19@gmail.com> (https://github.com/smamun19)",
|
|
48
|
+
"license": "MIT",
|
|
49
|
+
"bugs": {
|
|
50
|
+
"url": "https://github.com/smamun19/metal-price-live/issues"
|
|
51
|
+
},
|
|
52
|
+
"homepage": "https://github.com/smamun19/metal-price-live#readme",
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"registry": "https://registry.npmjs.org/"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@commitlint/config-conventional": "^17.0.2",
|
|
58
|
+
"@evilmartians/lefthook": "^1.5.0",
|
|
59
|
+
"@react-native/babel-preset": "^0.74.0",
|
|
60
|
+
"@react-native/eslint-config": "^0.72.2",
|
|
61
|
+
"@release-it/conventional-changelog": "^5.0.0",
|
|
62
|
+
"@types/jest": "^28.1.2",
|
|
63
|
+
"@types/react": "~17.0.21",
|
|
64
|
+
"@types/react-native": "0.70.0",
|
|
65
|
+
"commitlint": "^17.0.2",
|
|
66
|
+
"del-cli": "^5.0.0",
|
|
67
|
+
"eslint": "^8.4.1",
|
|
68
|
+
"eslint-config-prettier": "^8.5.0",
|
|
69
|
+
"eslint-plugin-prettier": "^4.0.0",
|
|
70
|
+
"jest": "^28.1.1",
|
|
71
|
+
"prettier": "^2.0.5",
|
|
72
|
+
"react": "18.2.0",
|
|
73
|
+
"react-native": "0.72.6",
|
|
74
|
+
"react-native-builder-bob": "^0.20.0",
|
|
75
|
+
"release-it": "^15.0.0",
|
|
76
|
+
"typescript": "^5.0.2"
|
|
77
|
+
},
|
|
78
|
+
"resolutions": {
|
|
79
|
+
"@types/react": "17.0.21"
|
|
80
|
+
},
|
|
81
|
+
"peerDependencies": {
|
|
82
|
+
"react": "*",
|
|
83
|
+
"react-native": "*"
|
|
84
|
+
},
|
|
85
|
+
"workspaces": [
|
|
86
|
+
"example"
|
|
87
|
+
],
|
|
88
|
+
"packageManager": "yarn@3.6.1",
|
|
89
|
+
"engines": {
|
|
90
|
+
"node": ">= 18.0.0"
|
|
91
|
+
},
|
|
92
|
+
"jest": {
|
|
93
|
+
"preset": "react-native",
|
|
94
|
+
"modulePathIgnorePatterns": [
|
|
95
|
+
"<rootDir>/example/node_modules",
|
|
96
|
+
"<rootDir>/lib/"
|
|
97
|
+
]
|
|
98
|
+
},
|
|
99
|
+
"commitlint": {
|
|
100
|
+
"extends": [
|
|
101
|
+
"@commitlint/config-conventional"
|
|
102
|
+
]
|
|
103
|
+
},
|
|
104
|
+
"release-it": {
|
|
105
|
+
"git": {
|
|
106
|
+
"commitMessage": "chore: release ${version}",
|
|
107
|
+
"tagName": "v${version}"
|
|
108
|
+
},
|
|
109
|
+
"npm": {
|
|
110
|
+
"publish": true
|
|
111
|
+
},
|
|
112
|
+
"github": {
|
|
113
|
+
"release": true
|
|
114
|
+
},
|
|
115
|
+
"plugins": {
|
|
116
|
+
"@release-it/conventional-changelog": {
|
|
117
|
+
"preset": "angular"
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
"eslintConfig": {
|
|
122
|
+
"root": true,
|
|
123
|
+
"extends": [
|
|
124
|
+
"@react-native",
|
|
125
|
+
"prettier"
|
|
126
|
+
],
|
|
127
|
+
"rules": {
|
|
128
|
+
"prettier/prettier": [
|
|
129
|
+
"error",
|
|
130
|
+
{
|
|
131
|
+
"quoteProps": "consistent",
|
|
132
|
+
"singleQuote": true,
|
|
133
|
+
"tabWidth": 2,
|
|
134
|
+
"trailingComma": "es5",
|
|
135
|
+
"useTabs": false
|
|
136
|
+
}
|
|
137
|
+
]
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
"eslintIgnore": [
|
|
141
|
+
"node_modules/",
|
|
142
|
+
"lib/"
|
|
143
|
+
],
|
|
144
|
+
"prettier": {
|
|
145
|
+
"quoteProps": "consistent",
|
|
146
|
+
"singleQuote": true,
|
|
147
|
+
"tabWidth": 2,
|
|
148
|
+
"trailingComma": "es5",
|
|
149
|
+
"useTabs": false
|
|
150
|
+
},
|
|
151
|
+
"react-native-builder-bob": {
|
|
152
|
+
"source": "src",
|
|
153
|
+
"output": "lib",
|
|
154
|
+
"targets": [
|
|
155
|
+
"commonjs",
|
|
156
|
+
"module",
|
|
157
|
+
[
|
|
158
|
+
"typescript",
|
|
159
|
+
{
|
|
160
|
+
"project": "tsconfig.build.json"
|
|
161
|
+
}
|
|
162
|
+
]
|
|
163
|
+
]
|
|
164
|
+
}
|
|
165
|
+
}
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { useState, useEffect, useRef } from 'react';
|
|
2
|
+
|
|
3
|
+
const minReconnectDelay = 1000;
|
|
4
|
+
const maxReconnectDelay = 300000;
|
|
5
|
+
const authRetry = 5;
|
|
6
|
+
const errorReason = 'Unauthorized';
|
|
7
|
+
|
|
8
|
+
interface ApiData {
|
|
9
|
+
timestamp: number;
|
|
10
|
+
price: number;
|
|
11
|
+
ask: number;
|
|
12
|
+
bid: number;
|
|
13
|
+
price_gram_24k: number;
|
|
14
|
+
price_gram_22k: number;
|
|
15
|
+
price_gram_21k: number;
|
|
16
|
+
key: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface Connected {
|
|
20
|
+
status: 'connected';
|
|
21
|
+
data: ApiData;
|
|
22
|
+
error?: undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface Connecting {
|
|
26
|
+
status: 'connecting';
|
|
27
|
+
data?: ApiData;
|
|
28
|
+
error?: string;
|
|
29
|
+
}
|
|
30
|
+
interface Error {
|
|
31
|
+
status: 'error';
|
|
32
|
+
data?: ApiData;
|
|
33
|
+
error: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type ConnectionState = Connected | Error | Connecting;
|
|
37
|
+
const useMetalPriceLive = (socketUrl: string, apiKey: string) => {
|
|
38
|
+
const [connectionState, setConnectionState] = useState<ConnectionState>({
|
|
39
|
+
status: 'connecting',
|
|
40
|
+
});
|
|
41
|
+
const [retry, setRetry] = useState(0);
|
|
42
|
+
const maxAuthRetryRef = useRef(0);
|
|
43
|
+
const currentReconnectDelayRef = useRef(minReconnectDelay);
|
|
44
|
+
|
|
45
|
+
const ws = useRef<WebSocket | null>(null);
|
|
46
|
+
|
|
47
|
+
const handleWsOpen = () => {
|
|
48
|
+
setConnectionState({
|
|
49
|
+
status: 'connecting',
|
|
50
|
+
});
|
|
51
|
+
currentReconnectDelayRef.current = minReconnectDelay;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const handleWsMessage = (e: WebSocketMessageEvent) => {
|
|
55
|
+
setConnectionState({ status: 'connected', data: JSON.parse(e.data) });
|
|
56
|
+
};
|
|
57
|
+
const handleWsError = (e: WebSocketErrorEvent) => {
|
|
58
|
+
setConnectionState({
|
|
59
|
+
status: 'error',
|
|
60
|
+
error: e.message || `WebSocket closed unexpectedly`,
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const handleWsClose = (e: WebSocketCloseEvent) => {
|
|
65
|
+
setConnectionState({
|
|
66
|
+
status: 'error',
|
|
67
|
+
error: e?.reason || `WebSocket closed unexpectedly`,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
//retry logic on auth fail
|
|
71
|
+
if (e?.reason === errorReason) {
|
|
72
|
+
maxAuthRetryRef.current++;
|
|
73
|
+
}
|
|
74
|
+
if (authRetry < maxAuthRetryRef.current) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Retry logic
|
|
79
|
+
if (ws.current && ws.current.readyState !== WebSocket.OPEN) {
|
|
80
|
+
setTimeout(() => {
|
|
81
|
+
//This state is triggering the useEffect for retry logic
|
|
82
|
+
setRetry((prev) => prev + 1);
|
|
83
|
+
|
|
84
|
+
currentReconnectDelayRef.current = Math.min(
|
|
85
|
+
currentReconnectDelayRef.current * 2,
|
|
86
|
+
maxReconnectDelay
|
|
87
|
+
);
|
|
88
|
+
}, currentReconnectDelayRef.current);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
useEffect(() => {
|
|
93
|
+
ws.current = new WebSocket(`${socketUrl}/${apiKey}`);
|
|
94
|
+
ws.current.addEventListener('open', handleWsOpen);
|
|
95
|
+
ws.current.addEventListener('message', handleWsMessage);
|
|
96
|
+
ws.current.addEventListener('error', handleWsError);
|
|
97
|
+
ws.current.addEventListener('close', handleWsClose);
|
|
98
|
+
|
|
99
|
+
return () => {
|
|
100
|
+
if (ws.current) {
|
|
101
|
+
ws.current.close();
|
|
102
|
+
ws.current.removeEventListener('open', handleWsOpen);
|
|
103
|
+
ws.current.removeEventListener('message', handleWsMessage);
|
|
104
|
+
ws.current.removeEventListener('error', handleWsError);
|
|
105
|
+
ws.current.removeEventListener('close', handleWsClose);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
}, [socketUrl, retry, apiKey]);
|
|
109
|
+
|
|
110
|
+
return connectionState;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export default useMetalPriceLive;
|