@hoanggbao00/expo-network-traffic 0.1.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/README.md +35 -0
- package/android/build/.transforms/14f854a5a3c5e162552703180af1b021/results.bin +1 -0
- package/android/build/.transforms/14f854a5a3c5e162552703180af1b021/transformed/bundleLibRuntimeToDirDebug/bundleLibRuntimeToDirDebug_dex/com/hoanggbao00/networktraffic/BuildConfig.dex +0 -0
- package/android/build/.transforms/14f854a5a3c5e162552703180af1b021/transformed/bundleLibRuntimeToDirDebug/bundleLibRuntimeToDirDebug_dex/com/hoanggbao00/networktraffic/ExpoNetworkTrafficModule$definition$lambda$2$$inlined$FunctionWithoutArgs$1.dex +0 -0
- package/android/build/.transforms/14f854a5a3c5e162552703180af1b021/transformed/bundleLibRuntimeToDirDebug/bundleLibRuntimeToDirDebug_dex/com/hoanggbao00/networktraffic/ExpoNetworkTrafficModule$definition$lambda$2$$inlined$FunctionWithoutArgs$2.dex +0 -0
- package/android/build/.transforms/14f854a5a3c5e162552703180af1b021/transformed/bundleLibRuntimeToDirDebug/bundleLibRuntimeToDirDebug_dex/com/hoanggbao00/networktraffic/ExpoNetworkTrafficModule.dex +0 -0
- package/android/build/.transforms/14f854a5a3c5e162552703180af1b021/transformed/bundleLibRuntimeToDirDebug/desugar_graph.bin +0 -0
- package/android/build/.transforms/7e24c9af6437b49daca5c117e2717cd1/results.bin +1 -0
- package/android/build/.transforms/7e24c9af6437b49daca5c117e2717cd1/transformed/classes/classes_dex/classes.dex +0 -0
- package/android/build/generated/source/buildConfig/debug/com/hoanggbao00/networktraffic/BuildConfig.java +10 -0
- package/android/build/intermediates/aapt_friendly_merged_manifests/debug/processDebugManifest/aapt/AndroidManifest.xml +7 -0
- package/android/build/intermediates/aapt_friendly_merged_manifests/debug/processDebugManifest/aapt/output-metadata.json +18 -0
- package/android/build/intermediates/aar_metadata/debug/writeDebugAarMetadata/aar-metadata.properties +6 -0
- package/android/build/intermediates/annotation_processor_list/debug/javaPreCompileDebug/annotationProcessors.json +1 -0
- package/android/build/intermediates/compile_library_classes_jar/debug/bundleLibCompileToJarDebug/classes.jar +0 -0
- package/android/build/intermediates/compile_r_class_jar/debug/generateDebugRFile/R.jar +0 -0
- package/android/build/intermediates/compile_symbol_list/debug/generateDebugRFile/R.txt +0 -0
- package/android/build/intermediates/incremental/debug/packageDebugResources/compile-file-map.properties +1 -0
- package/android/build/intermediates/incremental/debug/packageDebugResources/merger.xml +2 -0
- package/android/build/intermediates/incremental/mergeDebugAssets/merger.xml +2 -0
- package/android/build/intermediates/incremental/mergeDebugJniLibFolders/merger.xml +2 -0
- package/android/build/intermediates/incremental/mergeDebugShaders/merger.xml +2 -0
- package/android/build/intermediates/java_res/debug/processDebugJavaRes/out/META-INF/expo-network-traffic_debug.kotlin_module +0 -0
- package/android/build/intermediates/javac/debug/compileDebugJavaWithJavac/classes/com/hoanggbao00/networktraffic/BuildConfig.class +0 -0
- package/android/build/intermediates/local_only_symbol_list/debug/parseDebugLocalResources/R-def.txt +2 -0
- package/android/build/intermediates/manifest_merge_blame_file/debug/processDebugManifest/manifest-merger-blame-debug-report.txt +7 -0
- package/android/build/intermediates/merged_manifest/debug/processDebugManifest/AndroidManifest.xml +7 -0
- package/android/build/intermediates/navigation_json/debug/extractDeepLinksDebug/navigation.json +1 -0
- package/android/build/intermediates/nested_resources_validation_report/debug/generateDebugResources/nestedResourcesValidationReport.txt +1 -0
- package/android/build/intermediates/runtime_library_classes_dir/debug/bundleLibRuntimeToDirDebug/META-INF/expo-network-traffic_debug.kotlin_module +0 -0
- package/android/build/intermediates/runtime_library_classes_dir/debug/bundleLibRuntimeToDirDebug/com/hoanggbao00/networktraffic/BuildConfig.class +0 -0
- package/android/build/intermediates/runtime_library_classes_dir/debug/bundleLibRuntimeToDirDebug/com/hoanggbao00/networktraffic/ExpoNetworkTrafficModule$definition$lambda$2$$inlined$FunctionWithoutArgs$1.class +0 -0
- package/android/build/intermediates/runtime_library_classes_dir/debug/bundleLibRuntimeToDirDebug/com/hoanggbao00/networktraffic/ExpoNetworkTrafficModule$definition$lambda$2$$inlined$FunctionWithoutArgs$2.class +0 -0
- package/android/build/intermediates/runtime_library_classes_dir/debug/bundleLibRuntimeToDirDebug/com/hoanggbao00/networktraffic/ExpoNetworkTrafficModule.class +0 -0
- package/android/build/intermediates/runtime_library_classes_jar/debug/bundleLibRuntimeToJarDebug/classes.jar +0 -0
- package/android/build/intermediates/symbol_list_with_package_name/debug/generateDebugRFile/package-aware-r.txt +1 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/inputs/source-to-output.tab +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/inputs/source-to-output.tab.keystream +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/inputs/source-to-output.tab.keystream.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/inputs/source-to-output.tab.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/inputs/source-to-output.tab.values.at +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/inputs/source-to-output.tab_i +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/inputs/source-to-output.tab_i.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-attributes.tab +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-attributes.tab.keystream +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-attributes.tab.keystream.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-attributes.tab.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-attributes.tab.values.at +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-attributes.tab_i +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-attributes.tab_i.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-fq-name-to-source.tab +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-fq-name-to-source.tab.keystream +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-fq-name-to-source.tab.keystream.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-fq-name-to-source.tab.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-fq-name-to-source.tab.values.at +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-fq-name-to-source.tab_i +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/class-fq-name-to-source.tab_i.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/internal-name-to-source.tab +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/internal-name-to-source.tab.keystream +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/internal-name-to-source.tab.keystream.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/internal-name-to-source.tab.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/internal-name-to-source.tab.values.at +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/internal-name-to-source.tab_i +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/internal-name-to-source.tab_i.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/proto.tab +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/proto.tab.keystream +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/proto.tab.keystream.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/proto.tab.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/proto.tab.values.at +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/proto.tab_i +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/proto.tab_i.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/source-to-classes.tab +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/source-to-classes.tab.keystream +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/source-to-classes.tab.keystream.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/source-to-classes.tab.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/source-to-classes.tab.values.at +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/source-to-classes.tab_i +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/source-to-classes.tab_i.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/subtypes.tab +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/subtypes.tab.keystream +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/subtypes.tab.keystream.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/subtypes.tab.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/subtypes.tab.values.at +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/subtypes.tab_i +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/subtypes.tab_i.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/supertypes.tab +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/supertypes.tab.keystream +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/supertypes.tab.keystream.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/supertypes.tab.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/supertypes.tab.values.at +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/supertypes.tab_i +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/jvm/kotlin/supertypes.tab_i.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/counters.tab +2 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/file-to-id.tab +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/file-to-id.tab.keystream +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/file-to-id.tab.keystream.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/file-to-id.tab.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/file-to-id.tab.values.at +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/file-to-id.tab_i +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/file-to-id.tab_i.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/id-to-file.tab +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/id-to-file.tab.keystream +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/id-to-file.tab.keystream.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/id-to-file.tab.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/id-to-file.tab.values.at +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/id-to-file.tab_i.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/lookups.tab +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/lookups.tab.keystream +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/lookups.tab.keystream.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/lookups.tab.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/lookups.tab.values.at +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/lookups.tab_i +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/caches-jvm/lookups/lookups.tab_i.len +0 -0
- package/android/build/kotlin/compileDebugKotlin/cacheable/last-build.bin +0 -0
- package/android/build/kotlin/compileDebugKotlin/classpath-snapshot/shrunk-classpath-snapshot.bin +0 -0
- package/android/build/kotlin/compileDebugKotlin/local-state/build-history.bin +0 -0
- package/android/build/outputs/logs/manifest-merger-debug-report.txt +14 -0
- package/android/build/tmp/compileDebugJavaWithJavac/previous-compilation-data.bin +0 -0
- package/android/build/tmp/kotlin-classes/debug/META-INF/expo-network-traffic_debug.kotlin_module +0 -0
- package/android/build/tmp/kotlin-classes/debug/com/hoanggbao00/networktraffic/ExpoNetworkTrafficModule$definition$lambda$2$$inlined$FunctionWithoutArgs$1.class +0 -0
- package/android/build/tmp/kotlin-classes/debug/com/hoanggbao00/networktraffic/ExpoNetworkTrafficModule$definition$lambda$2$$inlined$FunctionWithoutArgs$2.class +0 -0
- package/android/build/tmp/kotlin-classes/debug/com/hoanggbao00/networktraffic/ExpoNetworkTrafficModule.class +0 -0
- package/android/build.gradle +43 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/com/hoanggbao00/networktraffic/ExpoNetworkTrafficModule.kt +23 -0
- package/build/ExpoNetworkTraffic.types.d.ts +50 -0
- package/build/ExpoNetworkTraffic.types.d.ts.map +1 -0
- package/build/ExpoNetworkTraffic.types.js +2 -0
- package/build/ExpoNetworkTraffic.types.js.map +1 -0
- package/build/ExpoNetworkTrafficModule.d.ts +8 -0
- package/build/ExpoNetworkTrafficModule.d.ts.map +1 -0
- package/build/ExpoNetworkTrafficModule.js +4 -0
- package/build/ExpoNetworkTrafficModule.js.map +1 -0
- package/build/hooks/use-network-traffic.d.ts +8 -0
- package/build/hooks/use-network-traffic.d.ts.map +1 -0
- package/build/hooks/use-network-traffic.js +52 -0
- package/build/hooks/use-network-traffic.js.map +1 -0
- package/build/index.d.ts +3 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +5 -0
- package/build/index.js.map +1 -0
- package/build/utils/calculations.d.ts +3 -0
- package/build/utils/calculations.d.ts.map +1 -0
- package/build/utils/calculations.js +17 -0
- package/build/utils/calculations.js.map +1 -0
- package/build/utils/native-wrapper.d.ts +5 -0
- package/build/utils/native-wrapper.d.ts.map +1 -0
- package/build/utils/native-wrapper.js +13 -0
- package/build/utils/native-wrapper.js.map +1 -0
- package/expo-module.config.json +9 -0
- package/ios/ExpoNetworkTraffic.podspec +29 -0
- package/ios/ExpoNetworkTrafficModule.swift +15 -0
- package/package.json +62 -0
- package/src/ExpoNetworkTraffic.types.ts +50 -0
- package/src/ExpoNetworkTrafficModule.ts +9 -0
- package/src/hooks/use-network-traffic.ts +64 -0
- package/src/index.ts +5 -0
- package/src/utils/calculations.ts +21 -0
- package/src/utils/native-wrapper.ts +12 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ExpoNetworkTrafficModule.js","sourceRoot":"","sources":["../src/ExpoNetworkTrafficModule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,mBAAmB,EAAE,MAAM,MAAM,CAAC;AAOzD,wBAAwB;AACxB,eAAe,mBAAmB,CAA2B,oBAAoB,CAAC,CAAC","sourcesContent":["import { NativeModule, requireNativeModule } from 'expo';\n\ndeclare class ExpoNetworkTrafficModule extends NativeModule {\n getTotalRxBytes(): number;\n getTotalTxBytes(): number;\n}\n\n// Load module từ Native\nexport default requireNativeModule<ExpoNetworkTrafficModule>('ExpoNetworkTraffic');"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { NetworkTrafficOptions, NetworkTrafficResult } from '../ExpoNetworkTraffic.types';
|
|
2
|
+
/**
|
|
3
|
+
* A hook to get the network traffic result.
|
|
4
|
+
* @param options - The options for the network traffic hook.
|
|
5
|
+
* @returns The network traffic result.
|
|
6
|
+
*/
|
|
7
|
+
export declare function useNetworkTraffic(options?: NetworkTrafficOptions): NetworkTrafficResult;
|
|
8
|
+
//# sourceMappingURL=use-network-traffic.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-network-traffic.d.ts","sourceRoot":"","sources":["../../src/hooks/use-network-traffic.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AAU/F;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,GAAE,qBAAuC,GAAG,oBAAoB,CA+CxG"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { useEffect, useState, useRef } from 'react';
|
|
2
|
+
import { calculateSpeed, formatBytes } from '../utils/calculations';
|
|
3
|
+
import { getSafeTrafficData } from '../utils/native-wrapper';
|
|
4
|
+
const DEFAULT_OPTIONS = {
|
|
5
|
+
interval: 3000,
|
|
6
|
+
enabled: true,
|
|
7
|
+
decimals: 2,
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* A hook to get the network traffic result.
|
|
11
|
+
* @param options - The options for the network traffic hook.
|
|
12
|
+
* @returns The network traffic result.
|
|
13
|
+
*/
|
|
14
|
+
export function useNetworkTraffic(options = DEFAULT_OPTIONS) {
|
|
15
|
+
const { interval, enabled, decimals } = { ...DEFAULT_OPTIONS, ...options };
|
|
16
|
+
const [traffic, setTraffic] = useState({
|
|
17
|
+
downSpeed: 0,
|
|
18
|
+
upSpeed: 0,
|
|
19
|
+
totalDown: 0,
|
|
20
|
+
totalUp: 0,
|
|
21
|
+
downStr: '0 B/s',
|
|
22
|
+
upStr: '0 B/s',
|
|
23
|
+
});
|
|
24
|
+
const lastDown = useRef(0);
|
|
25
|
+
const lastUp = useRef(0);
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
if (!enabled)
|
|
28
|
+
return;
|
|
29
|
+
const initData = getSafeTrafficData();
|
|
30
|
+
lastDown.current = initData.down;
|
|
31
|
+
lastUp.current = initData.up;
|
|
32
|
+
const tick = () => {
|
|
33
|
+
const current = getSafeTrafficData();
|
|
34
|
+
const downSpeed = calculateSpeed(current.down, lastDown.current, interval);
|
|
35
|
+
const upSpeed = calculateSpeed(current.up, lastUp.current, interval);
|
|
36
|
+
lastDown.current = current.down;
|
|
37
|
+
lastUp.current = current.up;
|
|
38
|
+
setTraffic({
|
|
39
|
+
downSpeed: downSpeed,
|
|
40
|
+
upSpeed: upSpeed,
|
|
41
|
+
totalDown: current.down,
|
|
42
|
+
totalUp: current.up,
|
|
43
|
+
downStr: formatBytes(downSpeed, decimals),
|
|
44
|
+
upStr: formatBytes(upSpeed, decimals),
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
const timer = setInterval(tick, interval);
|
|
48
|
+
return () => clearInterval(timer);
|
|
49
|
+
}, [interval, enabled, decimals]);
|
|
50
|
+
return traffic;
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=use-network-traffic.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-network-traffic.js","sourceRoot":"","sources":["../../src/hooks/use-network-traffic.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAEpD,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpE,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAE7D,MAAM,eAAe,GAA0B;IAC7C,QAAQ,EAAE,IAAI;IACd,OAAO,EAAE,IAAI;IACb,QAAQ,EAAE,CAAC;CACZ,CAAC;AAEF;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,CAAC,UAAiC,eAAe;IAChF,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,EAAE,GAAG,eAAe,EAAE,GAAG,OAAO,EAAE,CAAC;IAE3E,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAuB;QAC3D,SAAS,EAAE,CAAC;QACZ,OAAO,EAAE,CAAC;QACV,SAAS,EAAE,CAAC;QACZ,OAAO,EAAE,CAAC;QACV,OAAO,EAAE,OAAO;QAChB,KAAK,EAAE,OAAO;KACf,CAAC,CAAC;IAEH,MAAM,QAAQ,GAAG,MAAM,CAAS,CAAC,CAAC,CAAC;IACnC,MAAM,MAAM,GAAG,MAAM,CAAS,CAAC,CAAC,CAAC;IAEjC,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,CAAC,OAAO;YAAE,OAAO;QAErB,MAAM,QAAQ,GAAG,kBAAkB,EAAE,CAAC;QACtC,QAAQ,CAAC,OAAO,GAAG,QAAQ,CAAC,IAAI,CAAC;QACjC,MAAM,CAAC,OAAO,GAAG,QAAQ,CAAC,EAAE,CAAC;QAE7B,MAAM,IAAI,GAAG,GAAG,EAAE;YAChB,MAAM,OAAO,GAAG,kBAAkB,EAAE,CAAC;YAErC,MAAM,SAAS,GAAG,cAAc,CAAC,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC,OAAO,EAAE,QAAS,CAAC,CAAC;YAC5E,MAAM,OAAO,GAAG,cAAc,CAAC,OAAO,CAAC,EAAE,EAAE,MAAM,CAAC,OAAO,EAAE,QAAS,CAAC,CAAC;YAEtE,QAAQ,CAAC,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;YAChC,MAAM,CAAC,OAAO,GAAG,OAAO,CAAC,EAAE,CAAC;YAE5B,UAAU,CAAC;gBACT,SAAS,EAAE,SAAS;gBACpB,OAAO,EAAE,OAAO;gBAChB,SAAS,EAAE,OAAO,CAAC,IAAI;gBACvB,OAAO,EAAE,OAAO,CAAC,EAAE;gBACnB,OAAO,EAAE,WAAW,CAAC,SAAS,EAAE,QAAQ,CAAC;gBACzC,KAAK,EAAE,WAAW,CAAC,OAAO,EAAE,QAAQ,CAAC;aACtC,CAAC,CAAC;QACL,CAAC,CAAC;QAEF,MAAM,KAAK,GAAG,WAAW,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;QAE1C,OAAO,GAAG,EAAE,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;IACpC,CAAC,EAAE,CAAC,QAAQ,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC;IAElC,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["import { useEffect, useState, useRef } from 'react';\nimport type { NetworkTrafficOptions, NetworkTrafficResult } from '../ExpoNetworkTraffic.types';\nimport { calculateSpeed, formatBytes } from '../utils/calculations';\nimport { getSafeTrafficData } from '../utils/native-wrapper';\n\nconst DEFAULT_OPTIONS: NetworkTrafficOptions = {\n interval: 3000,\n enabled: true,\n decimals: 2,\n};\n\n/**\n * A hook to get the network traffic result.\n * @param options - The options for the network traffic hook.\n * @returns The network traffic result.\n */\nexport function useNetworkTraffic(options: NetworkTrafficOptions = DEFAULT_OPTIONS): NetworkTrafficResult {\n const { interval, enabled, decimals } = { ...DEFAULT_OPTIONS, ...options };\n\n const [traffic, setTraffic] = useState<NetworkTrafficResult>({\n downSpeed: 0,\n upSpeed: 0,\n totalDown: 0,\n totalUp: 0,\n downStr: '0 B/s',\n upStr: '0 B/s',\n });\n\n const lastDown = useRef<number>(0);\n const lastUp = useRef<number>(0);\n\n useEffect(() => {\n if (!enabled) return;\n\n const initData = getSafeTrafficData();\n lastDown.current = initData.down;\n lastUp.current = initData.up;\n\n const tick = () => {\n const current = getSafeTrafficData();\n\n const downSpeed = calculateSpeed(current.down, lastDown.current, interval!);\n const upSpeed = calculateSpeed(current.up, lastUp.current, interval!);\n\n lastDown.current = current.down;\n lastUp.current = current.up;\n\n setTraffic({\n downSpeed: downSpeed,\n upSpeed: upSpeed,\n totalDown: current.down,\n totalUp: current.up,\n downStr: formatBytes(downSpeed, decimals),\n upStr: formatBytes(upSpeed, decimals),\n });\n };\n\n const timer = setInterval(tick, interval);\n\n return () => clearInterval(timer);\n }, [interval, enabled, decimals]);\n\n return traffic;\n}"]}
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,4BAA4B,CAAC;AAG3C,cAAc,6BAA6B,CAAC"}
|
package/build/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,eAAe;AACf,cAAc,4BAA4B,CAAC;AAE3C,cAAc;AACd,cAAc,6BAA6B,CAAC","sourcesContent":["// Export types\nexport * from './ExpoNetworkTraffic.types';\n\n// Export hook\nexport * from './hooks/use-network-traffic';"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"calculations.d.ts","sourceRoot":"","sources":["../../src/utils/calculations.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,cAAc,GACzB,cAAc,MAAM,EACpB,WAAW,MAAM,EACjB,YAAY,MAAM,KACjB,MAKF,CAAC;AAEF,eAAO,MAAM,WAAW,GAAI,OAAO,MAAM,EAAE,iBAAY,KAAG,MAQzD,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export const calculateSpeed = (currentBytes, lastBytes, intervalMs) => {
|
|
2
|
+
const diff = currentBytes - lastBytes;
|
|
3
|
+
if (diff <= 0)
|
|
4
|
+
return 0;
|
|
5
|
+
return diff / (intervalMs / 1000);
|
|
6
|
+
};
|
|
7
|
+
export const formatBytes = (bytes, decimals = 2) => {
|
|
8
|
+
if (bytes === 0)
|
|
9
|
+
return '0 B/s';
|
|
10
|
+
const k = 1024;
|
|
11
|
+
const dm = decimals < 0 ? 0 : decimals;
|
|
12
|
+
const sizes = ['B/s', 'KB/s', 'MB/s', 'GB/s', 'TB/s'];
|
|
13
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
14
|
+
const value = bytes / k ** i;
|
|
15
|
+
return `${Number.parseFloat(value.toFixed(dm))} ${sizes[i]}`;
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=calculations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"calculations.js","sourceRoot":"","sources":["../../src/utils/calculations.ts"],"names":[],"mappings":"AACA,MAAM,CAAC,MAAM,cAAc,GAAG,CAC5B,YAAoB,EACpB,SAAiB,EACjB,UAAkB,EACV,EAAE;IACV,MAAM,IAAI,GAAG,YAAY,GAAG,SAAS,CAAC;IACtC,IAAI,IAAI,IAAI,CAAC;QAAE,OAAO,CAAC,CAAC;IAExB,OAAO,IAAI,GAAG,CAAC,UAAU,GAAG,IAAI,CAAC,CAAC;AACpC,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,KAAa,EAAE,QAAQ,GAAG,CAAC,EAAU,EAAE;IACjE,IAAI,KAAK,KAAK,CAAC;QAAE,OAAO,OAAO,CAAC;IAChC,MAAM,CAAC,GAAG,IAAI,CAAC;IACf,MAAM,EAAE,GAAG,QAAQ,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;IACvC,MAAM,KAAK,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;IACtD,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IACpD,MAAM,KAAK,GAAG,KAAK,GAAG,CAAC,IAAI,CAAC,CAAC;IAC7B,OAAO,GAAG,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;AAC/D,CAAC,CAAC","sourcesContent":["\nexport const calculateSpeed = (\n currentBytes: number,\n lastBytes: number,\n intervalMs: number\n): number => {\n const diff = currentBytes - lastBytes;\n if (diff <= 0) return 0;\n \n return diff / (intervalMs / 1000);\n};\n\nexport const formatBytes = (bytes: number, decimals = 2): string => {\n if (bytes === 0) return '0 B/s';\n const k = 1024;\n const dm = decimals < 0 ? 0 : decimals;\n const sizes = ['B/s', 'KB/s', 'MB/s', 'GB/s', 'TB/s'];\n const i = Math.floor(Math.log(bytes) / Math.log(k));\n const value = bytes / k ** i;\n return `${Number.parseFloat(value.toFixed(dm))} ${sizes[i]}`;\n};"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"native-wrapper.d.ts","sourceRoot":"","sources":["../../src/utils/native-wrapper.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,kBAAkB;;;CAS9B,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import ExpoNetworkTrafficModule from '../ExpoNetworkTrafficModule';
|
|
2
|
+
export const getSafeTrafficData = () => {
|
|
3
|
+
try {
|
|
4
|
+
return {
|
|
5
|
+
down: ExpoNetworkTrafficModule.getTotalRxBytes(),
|
|
6
|
+
up: ExpoNetworkTrafficModule.getTotalTxBytes(),
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
catch (e) {
|
|
10
|
+
return { down: 0, up: 0 };
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
//# sourceMappingURL=native-wrapper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"native-wrapper.js","sourceRoot":"","sources":["../../src/utils/native-wrapper.ts"],"names":[],"mappings":"AAAA,OAAO,wBAAwB,MAAM,6BAA6B,CAAC;AAEnE,MAAM,CAAC,MAAM,kBAAkB,GAAG,GAAG,EAAE;IACrC,IAAI,CAAC;QACH,OAAO;YACL,IAAI,EAAE,wBAAwB,CAAC,eAAe,EAAE;YAChD,EAAE,EAAE,wBAAwB,CAAC,eAAe,EAAE;SAC/C,CAAC;IACJ,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,OAAO,EAAE,IAAI,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC;IAC5B,CAAC;AACH,CAAC,CAAC","sourcesContent":["import ExpoNetworkTrafficModule from '../ExpoNetworkTrafficModule';\n\nexport const getSafeTrafficData = () => {\n try {\n return {\n down: ExpoNetworkTrafficModule.getTotalRxBytes(),\n up: ExpoNetworkTrafficModule.getTotalTxBytes(),\n };\n } catch (e) {\n return { down: 0, up: 0 };\n }\n};"]}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, '..', 'package.json')))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = 'ExpoNetworkTraffic'
|
|
7
|
+
s.version = package['version']
|
|
8
|
+
s.summary = package['description']
|
|
9
|
+
s.description = package['description']
|
|
10
|
+
s.license = package['license']
|
|
11
|
+
s.author = package['author']
|
|
12
|
+
s.homepage = package['homepage']
|
|
13
|
+
s.platforms = {
|
|
14
|
+
:ios => '15.1',
|
|
15
|
+
:tvos => '15.1'
|
|
16
|
+
}
|
|
17
|
+
s.swift_version = '5.9'
|
|
18
|
+
s.source = { git: 'https://github.com/hoanggbao00/expo-network-traffic' }
|
|
19
|
+
s.static_framework = true
|
|
20
|
+
|
|
21
|
+
s.dependency 'ExpoModulesCore'
|
|
22
|
+
|
|
23
|
+
# Swift/Objective-C compatibility
|
|
24
|
+
s.pod_target_xcconfig = {
|
|
25
|
+
'DEFINES_MODULE' => 'YES',
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
s.source_files = "**/*.{h,m,mm,swift,hpp,cpp}"
|
|
29
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import ExpoModulesCore
|
|
2
|
+
|
|
3
|
+
public class ExpoNetworkTrafficModule: Module {
|
|
4
|
+
public func definition() -> ModuleDefinition {
|
|
5
|
+
Name("ExpoNetworkTraffic")
|
|
6
|
+
|
|
7
|
+
Function("getTotalRxBytes") {
|
|
8
|
+
return 0.0
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
Function("getTotalTxBytes") {
|
|
12
|
+
return 0.0
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hoanggbao00/expo-network-traffic",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A lightweight, real-time network traffic monitor for React Native & Expo, built with the modern Expo Modules API. (Android Only for now)",
|
|
5
|
+
"main": "build/index.js",
|
|
6
|
+
"types": "build/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "expo-module build",
|
|
9
|
+
"clean": "expo-module clean",
|
|
10
|
+
"lint": "expo-module lint",
|
|
11
|
+
"test": "expo-module test",
|
|
12
|
+
"prepare": "expo-module prepare",
|
|
13
|
+
"prepublishOnly": "expo-module prepublishOnly",
|
|
14
|
+
"expo-module": "expo-module",
|
|
15
|
+
"open:ios": "xed example/ios",
|
|
16
|
+
"open:android": "open -a \"Android Studio\" example/android"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"react-native",
|
|
20
|
+
"expo",
|
|
21
|
+
"expo-module",
|
|
22
|
+
"network",
|
|
23
|
+
"traffic",
|
|
24
|
+
"speed-monitor",
|
|
25
|
+
"bandwidth",
|
|
26
|
+
"android",
|
|
27
|
+
"real-time",
|
|
28
|
+
"internet-speed"
|
|
29
|
+
],
|
|
30
|
+
"repository": {
|
|
31
|
+
"url": "git+https://github.com/hoanggbao00/expo-network-traffic.git"
|
|
32
|
+
},
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/hoanggbao00/expo-network-traffic/issues"
|
|
35
|
+
},
|
|
36
|
+
"author": "hoanggbao00 <hoanggbao00@gmail.com> (https://github.com/hoanggbao00)",
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"homepage": "https://github.com/hoanggbao00/expo-network-traffic#readme",
|
|
39
|
+
"dependencies": {},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/react": "~19.1.0",
|
|
42
|
+
"expo-module-scripts": "^5.0.8",
|
|
43
|
+
"expo": "^54.0.27",
|
|
44
|
+
"react-native": "0.81.5"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"expo": "*",
|
|
48
|
+
"react": "*",
|
|
49
|
+
"react-native": "*"
|
|
50
|
+
},
|
|
51
|
+
"files": [
|
|
52
|
+
"build",
|
|
53
|
+
"src",
|
|
54
|
+
"android",
|
|
55
|
+
"ios",
|
|
56
|
+
"expo-module.config.json"
|
|
57
|
+
],
|
|
58
|
+
"publishConfig": {
|
|
59
|
+
"access": "public",
|
|
60
|
+
"registry": "https://registry.npmjs.org/"
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export interface NetworkTrafficOptions {
|
|
2
|
+
/**
|
|
3
|
+
* The interval in milliseconds to get the network traffic result.
|
|
4
|
+
* @default 3000
|
|
5
|
+
*/
|
|
6
|
+
interval?: number;
|
|
7
|
+
/**
|
|
8
|
+
* Whether the network traffic hook is enabled.
|
|
9
|
+
* @default true
|
|
10
|
+
*/
|
|
11
|
+
enabled?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* The number of decimals to display in the network traffic result.
|
|
14
|
+
* @default 2
|
|
15
|
+
*/
|
|
16
|
+
decimals?: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface NetworkTrafficResult {
|
|
20
|
+
/**
|
|
21
|
+
* The current downloaded speed in bytes per second.
|
|
22
|
+
* @example 1000
|
|
23
|
+
*/
|
|
24
|
+
downSpeed: number;
|
|
25
|
+
/**
|
|
26
|
+
* The current uploaded speed in bytes per second.
|
|
27
|
+
* @example 1000
|
|
28
|
+
*/
|
|
29
|
+
upSpeed: number;
|
|
30
|
+
/**
|
|
31
|
+
* The total downloaded bytes.
|
|
32
|
+
* @example 1000
|
|
33
|
+
*/
|
|
34
|
+
totalDown: number;
|
|
35
|
+
/**
|
|
36
|
+
* The total uploaded bytes.
|
|
37
|
+
* @example 1000
|
|
38
|
+
*/
|
|
39
|
+
totalUp: number;
|
|
40
|
+
/**
|
|
41
|
+
* The current downloaded speed in bytes per second formatted as a string.
|
|
42
|
+
* @example "1000 B/s"
|
|
43
|
+
*/
|
|
44
|
+
downStr: string;
|
|
45
|
+
/**
|
|
46
|
+
* The current uploaded speed in bytes per second formatted as a string.
|
|
47
|
+
* @example "1000 B/s"
|
|
48
|
+
*/
|
|
49
|
+
upStr: string;
|
|
50
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { NativeModule, requireNativeModule } from 'expo';
|
|
2
|
+
|
|
3
|
+
declare class ExpoNetworkTrafficModule extends NativeModule {
|
|
4
|
+
getTotalRxBytes(): number;
|
|
5
|
+
getTotalTxBytes(): number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// Load module từ Native
|
|
9
|
+
export default requireNativeModule<ExpoNetworkTrafficModule>('ExpoNetworkTraffic');
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { useEffect, useState, useRef } from 'react';
|
|
2
|
+
import type { NetworkTrafficOptions, NetworkTrafficResult } from '../ExpoNetworkTraffic.types';
|
|
3
|
+
import { calculateSpeed, formatBytes } from '../utils/calculations';
|
|
4
|
+
import { getSafeTrafficData } from '../utils/native-wrapper';
|
|
5
|
+
|
|
6
|
+
const DEFAULT_OPTIONS: NetworkTrafficOptions = {
|
|
7
|
+
interval: 3000,
|
|
8
|
+
enabled: true,
|
|
9
|
+
decimals: 2,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A hook to get the network traffic result.
|
|
14
|
+
* @param options - The options for the network traffic hook.
|
|
15
|
+
* @returns The network traffic result.
|
|
16
|
+
*/
|
|
17
|
+
export function useNetworkTraffic(options: NetworkTrafficOptions = DEFAULT_OPTIONS): NetworkTrafficResult {
|
|
18
|
+
const { interval, enabled, decimals } = { ...DEFAULT_OPTIONS, ...options };
|
|
19
|
+
|
|
20
|
+
const [traffic, setTraffic] = useState<NetworkTrafficResult>({
|
|
21
|
+
downSpeed: 0,
|
|
22
|
+
upSpeed: 0,
|
|
23
|
+
totalDown: 0,
|
|
24
|
+
totalUp: 0,
|
|
25
|
+
downStr: '0 B/s',
|
|
26
|
+
upStr: '0 B/s',
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const lastDown = useRef<number>(0);
|
|
30
|
+
const lastUp = useRef<number>(0);
|
|
31
|
+
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
if (!enabled) return;
|
|
34
|
+
|
|
35
|
+
const initData = getSafeTrafficData();
|
|
36
|
+
lastDown.current = initData.down;
|
|
37
|
+
lastUp.current = initData.up;
|
|
38
|
+
|
|
39
|
+
const tick = () => {
|
|
40
|
+
const current = getSafeTrafficData();
|
|
41
|
+
|
|
42
|
+
const downSpeed = calculateSpeed(current.down, lastDown.current, interval!);
|
|
43
|
+
const upSpeed = calculateSpeed(current.up, lastUp.current, interval!);
|
|
44
|
+
|
|
45
|
+
lastDown.current = current.down;
|
|
46
|
+
lastUp.current = current.up;
|
|
47
|
+
|
|
48
|
+
setTraffic({
|
|
49
|
+
downSpeed: downSpeed,
|
|
50
|
+
upSpeed: upSpeed,
|
|
51
|
+
totalDown: current.down,
|
|
52
|
+
totalUp: current.up,
|
|
53
|
+
downStr: formatBytes(downSpeed, decimals),
|
|
54
|
+
upStr: formatBytes(upSpeed, decimals),
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const timer = setInterval(tick, interval);
|
|
59
|
+
|
|
60
|
+
return () => clearInterval(timer);
|
|
61
|
+
}, [interval, enabled, decimals]);
|
|
62
|
+
|
|
63
|
+
return traffic;
|
|
64
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
|
|
2
|
+
export const calculateSpeed = (
|
|
3
|
+
currentBytes: number,
|
|
4
|
+
lastBytes: number,
|
|
5
|
+
intervalMs: number
|
|
6
|
+
): number => {
|
|
7
|
+
const diff = currentBytes - lastBytes;
|
|
8
|
+
if (diff <= 0) return 0;
|
|
9
|
+
|
|
10
|
+
return diff / (intervalMs / 1000);
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const formatBytes = (bytes: number, decimals = 2): string => {
|
|
14
|
+
if (bytes === 0) return '0 B/s';
|
|
15
|
+
const k = 1024;
|
|
16
|
+
const dm = decimals < 0 ? 0 : decimals;
|
|
17
|
+
const sizes = ['B/s', 'KB/s', 'MB/s', 'GB/s', 'TB/s'];
|
|
18
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
19
|
+
const value = bytes / k ** i;
|
|
20
|
+
return `${Number.parseFloat(value.toFixed(dm))} ${sizes[i]}`;
|
|
21
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import ExpoNetworkTrafficModule from '../ExpoNetworkTrafficModule';
|
|
2
|
+
|
|
3
|
+
export const getSafeTrafficData = () => {
|
|
4
|
+
try {
|
|
5
|
+
return {
|
|
6
|
+
down: ExpoNetworkTrafficModule.getTotalRxBytes(),
|
|
7
|
+
up: ExpoNetworkTrafficModule.getTotalTxBytes(),
|
|
8
|
+
};
|
|
9
|
+
} catch (e) {
|
|
10
|
+
return { down: 0, up: 0 };
|
|
11
|
+
}
|
|
12
|
+
};
|