43 lines
1.1 KiB
JavaScript
43 lines
1.1 KiB
JavaScript
import {
|
|
NativeModules,
|
|
} from 'react-native';
|
|
|
|
const { HocNotificationService } = NativeModules;
|
|
const Emitter = new NativeEventEmitter(HocNotificationService);
|
|
|
|
class BackgroundTimer {
|
|
constructor() {
|
|
this.uniqueId = 0;
|
|
this.callbacks = {};
|
|
|
|
Emitter.addListener('HocNotificationService.timeout', (id) => {
|
|
if (this.callbacks[id]) {
|
|
const callbackById = this.callbacks[id];
|
|
const { callback } = callbackById;
|
|
if (!this.callbacks[id].interval) {
|
|
delete this.callbacks[id];
|
|
} else {
|
|
RNBackgroundTimer.setTimeout(id, this.callbacks[id].timeout);
|
|
}
|
|
callback();
|
|
}
|
|
});
|
|
}
|
|
|
|
runBackgroundTimer(callback, delay) {
|
|
const EventEmitter = Platform.select({
|
|
ios: () => NativeAppEventEmitter,
|
|
android: () => DeviceEventEmitter,
|
|
})();
|
|
this.start(0);
|
|
this.backgroundListener = EventEmitter.addListener(
|
|
'backgroundTimer',
|
|
() => {
|
|
this.backgroundListener.remove();
|
|
this.backgroundClockMethod(callback, delay);
|
|
},
|
|
);
|
|
}
|
|
}
|
|
|
|
export default new BackgroundTimer(); |