Files
CompanyProject/滴滴淘/ddt_m/src/view/components/HOC/a.js
2024-09-27 01:32:49 +08:00

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();