Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,26 @@ import android.content.IntentFilter
import android.os.Build
import android.os.PersistableBundle
import android.util.Log
import com.facebook.react.modules.core.DeviceEventManagerModule
import com.facebook.react.bridge.WritableMap
import com.facebook.react.bridge.Arguments

class ReactNativeBackgroundTaskModule internal constructor(context: ReactApplicationContext) :
ReactNativeBackgroundTaskSpec(context) {

private fun sendEvent(eventName: String, params: WritableMap?) {
reactApplicationContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
.emit(eventName, params)
}

private val taskReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val taskName = intent?.getStringExtra("taskName")
Log.d("ReactNativeBackgroundTaskModule", "Received task: $taskName")
emitOnBackgroundTaskExecution(taskName)
val params = Arguments.createMap()
params.putString("taskName", taskName)
sendEvent("onBackgroundTaskExecution", params)
}
}

Expand Down Expand Up @@ -80,6 +91,14 @@ class ReactNativeBackgroundTaskModule internal constructor(context: ReactApplica
}
}

override fun addListener(eventType: String?) {
// no-op
}

override fun removeListeners(count: Double) {
// no-op
}

companion object {
const val NAME = "ReactNativeBackgroundTask"
}
Expand Down
5 changes: 3 additions & 2 deletions modules/background-task/ios/ReactNativeBackgroundTask.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#import <React/RCTEventEmitter.h>
#ifdef RCT_NEW_ARCH_ENABLED
#import <ReactCodegen/RNReactNativeBackgroundTaskSpec/RNReactNativeBackgroundTaskSpec.h>
#import <BackgroundTasks/BackgroundTasks.h>

@interface ReactNativeBackgroundTask : NativeReactNativeBackgroundTaskSpecBase <NativeReactNativeBackgroundTaskSpec>
@interface ReactNativeBackgroundTask : RCTEventEmitter <NativeReactNativeBackgroundTaskSpec>
#else
#import <React/RCTBridgeModule.h>
#import <BackgroundTasks/BackgroundTasks.h>

@interface ReactNativeBackgroundTask : NSObject <RCTBridgeModule>
@interface ReactNativeBackgroundTask : RCTEventEmitter <RCTBridgeModule>
#endif

- (void)defineTask:(NSString *)taskName
Expand Down
6 changes: 5 additions & 1 deletion modules/background-task/ios/ReactNativeBackgroundTask.mm
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ - (BOOL)scheduleNewBackgroundTask:(NSString *)identifier error:(NSError **)outEr
// Execute all registered tasks
[self->_taskExecutors enumerateKeysAndObjectsUsingBlock:^(NSString *taskName, RCTResponseSenderBlock executor, BOOL *stop) {
NSLog(@"[ReactNativeBackgroundTask] Executing task: %@", taskName);
[self emitOnBackgroundTaskExecution:(taskName)];
[self sendEventWithName:@"onBackgroundTaskExecution" body:@{@"taskName": taskName}];
}];

NSError *scheduleError = nil;
Expand Down Expand Up @@ -105,6 +105,10 @@ - (BOOL)scheduleNewBackgroundTask:(NSString *)identifier error:(NSError **)outEr
_taskExecutors[taskName] = taskExecutor;
}

- (NSArray<NSString *> *)supportedEvents {
return @[@"onBackgroundTaskExecution"];
}

// Don't compile this code when we build for the old architecture.
#ifdef RCT_NEW_ARCH_ENABLED
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type {TurboModule} from 'react-native';
import {TurboModuleRegistry} from 'react-native';
import type {EventEmitter} from 'react-native/Libraries/Types/CodegenTypes';

// We need to export the interface inline for proper TypeScript type inference with TurboModules
// eslint-disable-next-line rulesdir/no-inline-named-export, @typescript-eslint/consistent-type-definitions
export interface Spec extends TurboModule {
defineTask(taskName: string, taskExecutor: (data: unknown) => void | Promise<void>): Promise<void>;
readonly onBackgroundTaskExecution: EventEmitter<string>;
addListener: (eventType: string) => void;
removeListeners: (count: number) => void;
}

export default TurboModuleRegistry.getEnforcing<Spec>('ReactNativeBackgroundTask');
17 changes: 15 additions & 2 deletions modules/background-task/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import type {EmitterSubscription} from 'react-native';
import {NativeEventEmitter} from 'react-native';
import NativeReactNativeBackgroundTask from './NativeReactNativeBackgroundTask';

type TaskManagerTaskExecutor<T = unknown> = (data: T) => void | Promise<void>;

const eventEmitter = new NativeEventEmitter(NativeReactNativeBackgroundTask);
const taskExecutors = new Map<string, TaskManagerTaskExecutor>();
let subscription: EmitterSubscription | undefined;

NativeReactNativeBackgroundTask.onBackgroundTaskExecution((taskName) => {
function onBackgroundTaskExecution({taskName}: {taskName: string}) {
const executor = taskExecutors.get(taskName);

if (executor) {
executor(taskName);
}
});
}

function addBackgroundTaskListener() {
if (subscription) {
subscription.remove();
}
subscription = eventEmitter.addListener('onBackgroundTaskExecution', onBackgroundTaskExecution);
}

const TaskManager = {
/**
Expand All @@ -32,4 +43,6 @@ const TaskManager = {
},
};

addBackgroundTaskListener();

export default TaskManager;
Loading