Skip to content
This repository was archived by the owner on Dec 4, 2024. It is now read-only.

Commit 392884c

Browse files
authored
Merge pull request #4 from xl666666/master
react-native-background-timer修改入口文件,修改依赖
2 parents adb1bf2 + 1726e11 commit 392884c

File tree

4 files changed

+182
-4
lines changed

4 files changed

+182
-4
lines changed

BackgroundTimerExample.tsx

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
import {View, Button, StyleSheet, Text,TextInput} from 'react-native';
2+
import React, {useState} from 'react';
3+
import BackgroundTimer from "react-native-background-timer";
4+
export function BackgroundTimerExample() {
5+
let [count, setCount] = useState(0);
6+
let [text, setText] = useState("");
7+
// BackgroundTimer延时
8+
let [delay, setDelay] = useState("1000");
9+
// setTimeout延时
10+
let [timeoutDelay, setTimeoutDelay] = useState("1000");
11+
// setInterval延时
12+
let [intervalDelay, setIntervalDelay] = useState("1000");
13+
let timeoutList:number[] = []
14+
let [intervalList, setIntervalList] = useState<number[]>([]);
15+
16+
// runBackgroundTimer
17+
function onPressStart(){
18+
setText("开启定时器...")
19+
BackgroundTimer.runBackgroundTimer(()=>{
20+
setCount(count+=1)
21+
},parseInt(delay))
22+
}
23+
function onPressStop(){
24+
setText("结束定时器")
25+
BackgroundTimer.stopBackgroundTimer()
26+
}
27+
28+
// setTimeout
29+
function setTimeoutStart(){
30+
setText("开启定时器...")
31+
let timeoutId = BackgroundTimer.setTimeout(()=>{
32+
setCount(count+=1)
33+
},parseInt(timeoutDelay))
34+
timeoutList.push(timeoutId)
35+
}
36+
function setTimeoutStop(){
37+
setText("结束定时器")
38+
if(timeoutList.length>0){
39+
BackgroundTimer.clearTimeout(timeoutList[0])
40+
timeoutList.shift()
41+
}
42+
}
43+
44+
// setInterval
45+
function setIntervalStart(){
46+
setText("开启定时器...")
47+
let intervalId = BackgroundTimer.setInterval(()=>{
48+
setCount(count+=1)
49+
},parseInt(intervalDelay))
50+
setIntervalList([...intervalList,intervalId])
51+
}
52+
function setIntervalStop(){
53+
setText("结束定时器")
54+
if(intervalList.length>0){
55+
BackgroundTimer.clearInterval(intervalList[0])
56+
intervalList.shift()
57+
}
58+
}
59+
function resetNumber(){
60+
setCount(0)
61+
setText("")
62+
}
63+
return (
64+
<View style={{flexDirection: 'column', flex: 1,backgroundColor:'white'}}>
65+
<View style={styles.container}>
66+
<View style={styles.viewStyle}>
67+
<Button
68+
onPress={onPressStart}
69+
title="runBackgroundTimer"
70+
/>
71+
<TextInput
72+
style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
73+
placeholder="BackgroundTimer延时"
74+
onChangeText={(value)=>{setDelay(value)}}
75+
value={delay}
76+
/>
77+
</View>
78+
<View style={[styles.viewStyle]}>
79+
<Button
80+
onPress={onPressStop}
81+
title="stopBackgroundTimer"
82+
/>
83+
</View>
84+
</View>
85+
{/* --------------------------------------------setTimeout-------------------------------------------------- */}
86+
<View style={styles.container}>
87+
<View style={styles.viewStyle}>
88+
<Button
89+
onPress={setTimeoutStart}
90+
title="setTimeout"
91+
/>
92+
<TextInput
93+
style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
94+
placeholder="setTimeout延时"
95+
onChangeText={(value)=>{setTimeoutDelay(value)}}
96+
value={timeoutDelay}
97+
/>
98+
</View>
99+
<View style={[styles.viewStyle]}>
100+
<Button
101+
onPress={setTimeoutStop}
102+
title="clearTimeout"
103+
/>
104+
</View>
105+
</View>
106+
{/* --------------------------------------------setInterval-------------------------------------------------- */}
107+
<View style={styles.container}>
108+
<View style={styles.viewStyle}>
109+
<Button
110+
onPress={setIntervalStart}
111+
title="setInterval"
112+
/>
113+
<TextInput
114+
style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
115+
placeholder="setInterval延时"
116+
onChangeText={(value)=>{setIntervalDelay(value)}}
117+
value={intervalDelay}
118+
/>
119+
</View>
120+
<View style={[styles.viewStyle]}>
121+
<Button
122+
onPress={setIntervalStop}
123+
title="clearInterval"
124+
/>
125+
</View>
126+
</View>
127+
{/* -----------------------------------------------Reset----------------------------------------------------- */}
128+
<View style={[styles.viewStyle,styles.resetStyle]}>
129+
<Button
130+
onPress={resetNumber}
131+
title="Reset"
132+
/>
133+
</View>
134+
<Text style={styles.textStyle}>{count}</Text>
135+
<Text style={styles.textStyle}>{text}</Text>
136+
</View>
137+
);
138+
}
139+
140+
const styles = StyleSheet.create({
141+
container: {
142+
borderColor: 'black',
143+
borderTopWidth: 0,
144+
borderBottomWidth: 5,
145+
borderLeftWidth: 0,
146+
borderRightWidth: 0,
147+
padding: 10,
148+
},
149+
resetStyle: {
150+
paddingTop: 10,
151+
},
152+
viewStyle:{
153+
marginBottom: 10,
154+
},
155+
textStyle: {
156+
fontSize: 30,
157+
},
158+
});

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import {
44
NativeEventEmitter,
55
NativeModules,
66
Platform,
7+
TurboModuleRegistry,
78
} from 'react-native';
89

9-
const { RNBackgroundTimer } = NativeModules;
10+
const RNBackgroundTimer = TurboModuleRegistry ? TurboModuleRegistry.get('BackgroundTimerTurboModule') : NativeModules.BackgroundTimer;
1011
const Emitter = new NativeEventEmitter(RNBackgroundTimer);
1112

1213
class BackgroundTimer {
@@ -41,6 +42,7 @@ class BackgroundTimer {
4142
const EventEmitter = Platform.select({
4243
ios: () => NativeAppEventEmitter,
4344
android: () => DeviceEventEmitter,
45+
harmony: () => DeviceEventEmitter,
4446
})();
4547
this.start(0);
4648
this.backgroundListener = EventEmitter.addListener(

package.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "react-native-background-timer",
3-
"version": "2.4.1",
2+
"name": "@react-native-oh-tpl/react-native-background-timer",
3+
"version": "2.4.1-0.0.1",
44
"description": "Emit event periodically (even when app is in the background)",
55
"keywords": [
66
"react-native",
@@ -50,5 +50,13 @@
5050
"eslint --fix",
5151
"git add"
5252
]
53+
},
54+
"harmony": {
55+
"alias": "react-native-background-timer",
56+
"codegenConfig": {
57+
"specPaths": [
58+
"./src"
59+
]
60+
}
5361
}
54-
}
62+
}

src/RNBackgroundTimerSpec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { TurboModule } from 'react-native/Libraries/TurboModule/RCTExport';
2+
import { TurboModuleRegistry } from 'react-native';
3+
4+
export interface Spec extends TurboModule {
5+
start(delay: number): void;
6+
stop(): void;
7+
setTimeout(timeoutId:number,timeout:number): void;
8+
}
9+
10+
export default TurboModuleRegistry.get<Spec>('BackgroundTimerTurboModule') as Spec | null;

0 commit comments

Comments
 (0)