Skip to content

Commit

Permalink
feat!: use new EventEmitter API
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonrybczak committed Dec 10, 2024
1 parent 463edec commit 3ec2762
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 6 deletions.
25 changes: 25 additions & 0 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ PODS:
- EXImageLoader (5.0.0):
- ExpoModulesCore
- React-Core
- expensify-react-native-background-task (0.0.0):
- DoubleConversion
- glog
- hermes-engine
- RCT-Folly (= 2024.01.01.00)
- RCTRequired
- RCTTypeSafety
- React-Core
- React-debug
- React-Fabric
- React-featureflags
- React-graphics
- React-ImageManager
- React-NativeModulesApple
- React-RCTFabric
- React-rendererdebug
- React-utils
- ReactCodegen
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- Yoga
- Expo (52.0.14):
- ExpoModulesCore
- ExpoAsset (11.0.1):
Expand Down Expand Up @@ -2792,6 +2813,7 @@ DEPENDENCIES:
- DoubleConversion (from `../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec`)
- EXAV (from `../node_modules/expo-av/ios`)
- EXImageLoader (from `../node_modules/expo-image-loader/ios`)
- "expensify-react-native-background-task (from `../node_modules/@expensify/react-native-background-task`)"
- Expo (from `../node_modules/expo`)
- ExpoAsset (from `../node_modules/expo-asset/ios`)
- ExpoFont (from `../node_modules/expo-font/ios`)
Expand Down Expand Up @@ -2961,6 +2983,8 @@ EXTERNAL SOURCES:
:path: "../node_modules/expo-av/ios"
EXImageLoader:
:path: "../node_modules/expo-image-loader/ios"
expensify-react-native-background-task:
:path: "../node_modules/@expensify/react-native-background-task"
Expo:
:path: "../node_modules/expo"
ExpoAsset:
Expand Down Expand Up @@ -3207,6 +3231,7 @@ SPEC CHECKSUMS:
DoubleConversion: f16ae600a246532c4020132d54af21d0ddb2a385
EXAV: 9773c9799767c9925547b05e41a26a0240bb8ef2
EXImageLoader: 759063a65ab016b836f73972d3bb25404888713d
expensify-react-native-background-task: 6f797cf470b627912c246514b1631a205794775d
Expo: 0e7b52be71a24a38d5e919e3040d8f51a8739cd0
ExpoAsset: 8138f2a9ec55ae1ad7c3871448379f7d97692d15
ExpoFont: 7522d869d84ee2ee8093ee997fef5b86f85d856b
Expand Down
2 changes: 1 addition & 1 deletion modules/background-task/ios/ReactNativeBackgroundTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#import "RNReactNativeBackgroundTaskSpec.h"
#import <BackgroundTasks/BackgroundTasks.h>

@interface ReactNativeBackgroundTask : NSObject <NativeReactNativeBackgroundTaskSpec>
@interface ReactNativeBackgroundTask : NativeReactNativeBackgroundTaskSpecBase <NativeReactNativeBackgroundTaskSpec>
#else
#import <React/RCTBridgeModule.h>
#import <BackgroundTasks/BackgroundTasks.h>
Expand Down
6 changes: 1 addition & 5 deletions modules/background-task/ios/ReactNativeBackgroundTask.mm
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ - (BOOL)scheduleNewBackgroundTask:(NSString *)identifier {
// Execute all registered tasks
[self->_taskExecutors enumerateKeysAndObjectsUsingBlock:^(NSString *taskName, RCTResponseSenderBlock executor, BOOL *stop) {
NSLog(@"[ReactNativeBackgroundTask] Executing task: %@", taskName);
executor(@[@{
@"taskName": taskName,
@"type": @"background",
@"identifier": task.identifier
}]);
[self emitOnBackgroundTaskExecution:(taskName)];
}];

[self scheduleNewBackgroundTask:identifier];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type {TurboModule} from 'react-native';
import {TurboModuleRegistry} from 'react-native';
import type {EventEmitter} from 'react-native/Libraries/Types/CodegenTypes';

export interface Spec extends TurboModule {
defineTask(taskName: string, taskExecutor: (data: unknown) => void | Promise<void>): Promise<void>;
readonly onBackgroundTaskExecution: EventEmitter<string>;
}

export default TurboModuleRegistry.getEnforcing<Spec>('ReactNativeBackgroundTask');
12 changes: 12 additions & 0 deletions modules/background-task/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ import NativeReactNativeBackgroundTask from './NativeReactNativeBackgroundTask';

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

const taskExecutors = new Map<string, TaskManagerTaskExecutor>();

NativeReactNativeBackgroundTask.onBackgroundTaskExecution((taskName) => {
const executor = taskExecutors.get(taskName);

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

const TaskManager = {
/**
* Defines a task that can be executed in the background.
Expand All @@ -16,6 +26,8 @@ const TaskManager = {
throw new Error('Task executor must be a function');
}

taskExecutors.set(taskName, taskExecutor);

return NativeReactNativeBackgroundTask.defineTask(taskName, taskExecutor);
},
};
Expand Down
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3ec2762

Please sign in to comment.