@hxa-rn/react-native-background-actions 4.1.0 → 4.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +178 -178
- package/harmony/background_actions/LICENSE +21 -21
- package/harmony/background_actions/oh-package.json5 +14 -14
- package/harmony/background_actions/src/main/cpp/CMakeLists.txt +2 -2
- package/harmony/background_actions/src/main/cpp/bridge/CMakeLists.txt +7 -7
- package/harmony/background_actions/src/main/cpp/generated/RNBackgroundActions.cpp +30 -30
- package/harmony/background_actions/src/main/cpp/generated/RNBackgroundActions.h +21 -21
- package/harmony/background_actions/src/main/cpp/generated/RNOHGeneratedPackage.h +76 -76
- package/harmony/background_actions/src/main/ets/BackgroundActionsModule.ets +425 -425
- package/harmony/background_actions/src/main/ets/BackgroundActionsTurboModulesFactory.ets +8 -8
- package/harmony/background_actions/src/main/ets/generated/components/ts.ts +5 -5
- package/harmony/background_actions/src/main/ets/generated/index.ets +8 -8
- package/harmony/background_actions/src/main/ets/generated/ts.ts +9 -9
- package/harmony/background_actions/src/main/ets/generated/turboModules/RNBackgroundActions.ts +65 -65
- package/harmony/background_actions/src/main/ets/generated/turboModules/ts.ts +8 -8
- package/harmony/background_actions/src/main/module.json5 +13 -13
- package/package.json +84 -84
- package/src/RNBackgroundActionsModule.ts +78 -78
- package/src/index.ts +131 -131
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { RNOHPackage, UITurboModule, UITurboModuleContext } from '@rnoh/react-native-openharmony';
|
|
2
|
-
import { BackgroundActionsModule } from './BackgroundActionsModule';
|
|
3
|
-
import { TM } from './generated/ts';
|
|
4
|
-
export class BackgroundActionsPackage extends RNOHPackage {
|
|
5
|
-
override getUITurboModuleFactoryByNameMap(): Map<string, (ctx: UITurboModuleContext) => UITurboModule | null> {
|
|
6
|
-
return new Map<string, (ctx: UITurboModuleContext) => UITurboModule | null>()
|
|
7
|
-
.set(TM.RNBackgroundActions.NAME, (ctx: UITurboModuleContext) => new BackgroundActionsModule(ctx));
|
|
8
|
-
}
|
|
1
|
+
import { RNOHPackage, UITurboModule, UITurboModuleContext } from '@rnoh/react-native-openharmony';
|
|
2
|
+
import { BackgroundActionsModule } from './BackgroundActionsModule';
|
|
3
|
+
import { TM } from './generated/ts';
|
|
4
|
+
export class BackgroundActionsPackage extends RNOHPackage {
|
|
5
|
+
override getUITurboModuleFactoryByNameMap(): Map<string, (ctx: UITurboModuleContext) => UITurboModule | null> {
|
|
6
|
+
return new Map<string, (ctx: UITurboModuleContext) => UITurboModule | null>()
|
|
7
|
+
.set(TM.RNBackgroundActions.NAME, (ctx: UITurboModuleContext) => new BackgroundActionsModule(ctx));
|
|
8
|
+
}
|
|
9
9
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
export {}
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export {}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This code was generated by "react-native codegen-harmony"
|
|
3
|
-
*
|
|
4
|
-
* Do not edit this file as changes may cause incorrect behavior and will be
|
|
5
|
-
* lost once the code is regenerated.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
export * from './ts'
|
|
1
|
+
/**
|
|
2
|
+
* This code was generated by "react-native codegen-harmony"
|
|
3
|
+
*
|
|
4
|
+
* Do not edit this file as changes may cause incorrect behavior and will be
|
|
5
|
+
* lost once the code is regenerated.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export * from './ts'
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This code was generated by "react-native codegen-harmony"
|
|
3
|
-
*
|
|
4
|
-
* Do not edit this file as changes may cause incorrect behavior and will be
|
|
5
|
-
* lost once the code is regenerated.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
export * as RNC from './components/ts'
|
|
9
|
-
export * as TM from './turboModules/ts'
|
|
1
|
+
/**
|
|
2
|
+
* This code was generated by "react-native codegen-harmony"
|
|
3
|
+
*
|
|
4
|
+
* Do not edit this file as changes may cause incorrect behavior and will be
|
|
5
|
+
* lost once the code is regenerated.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export * as RNC from './components/ts'
|
|
9
|
+
export * as TM from './turboModules/ts'
|
package/harmony/background_actions/src/main/ets/generated/turboModules/RNBackgroundActions.ts
CHANGED
|
@@ -1,65 +1,65 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This code was generated by "react-native codegen-harmony"
|
|
3
|
-
*
|
|
4
|
-
* Do not edit this file as changes may cause incorrect behavior and will be
|
|
5
|
-
* lost once the code is regenerated.
|
|
6
|
-
*
|
|
7
|
-
* @generatorVersion: 1
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { Tag } from '@rnoh/react-native-openharmony/ts'
|
|
11
|
-
|
|
12
|
-
export namespace RNBackgroundActions {
|
|
13
|
-
export const NAME = 'RNBackgroundActions' as const
|
|
14
|
-
|
|
15
|
-
export type NativeOptions = {
|
|
16
|
-
taskName: string,
|
|
17
|
-
taskTitle: string,
|
|
18
|
-
taskDesc: string,
|
|
19
|
-
taskIcon: TaskIcon,
|
|
20
|
-
color?: string,
|
|
21
|
-
linkingURI?: string,
|
|
22
|
-
progressBar?: ProgressBar,
|
|
23
|
-
foregroundServiceType?: string[],
|
|
24
|
-
harmonyBackgroundModes?: string[],
|
|
25
|
-
generation?: number
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export type ProgressBar = {max: number, value: number, indeterminate?: boolean}
|
|
29
|
-
|
|
30
|
-
export type TaskIcon = {name: string, type: string, package?: string}
|
|
31
|
-
|
|
32
|
-
export type TaskStatus = {
|
|
33
|
-
running: boolean,
|
|
34
|
-
mode: string,
|
|
35
|
-
requestId: number,
|
|
36
|
-
notificationId: number,
|
|
37
|
-
actualDelayTime: number,
|
|
38
|
-
cancelEventsSupported: boolean,
|
|
39
|
-
lastErrorCode: string,
|
|
40
|
-
lastErrorMessage: string
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export interface Spec {
|
|
44
|
-
start(options: NativeOptions): Promise<void>;
|
|
45
|
-
|
|
46
|
-
stop(): Promise<void>;
|
|
47
|
-
|
|
48
|
-
updateNotification(options: NativeOptions): Promise<void>;
|
|
49
|
-
|
|
50
|
-
isRunning(): boolean;
|
|
51
|
-
|
|
52
|
-
getStatus(): Promise<TaskStatus>;
|
|
53
|
-
|
|
54
|
-
requestNotifications(): Promise<boolean>;
|
|
55
|
-
|
|
56
|
-
scheduleTimer(id: number, delay: number, repeats: boolean): void;
|
|
57
|
-
|
|
58
|
-
cancelTimer(id: number): void;
|
|
59
|
-
|
|
60
|
-
addListener(eventName: string): void;
|
|
61
|
-
|
|
62
|
-
removeListeners(count: number): void;
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* This code was generated by "react-native codegen-harmony"
|
|
3
|
+
*
|
|
4
|
+
* Do not edit this file as changes may cause incorrect behavior and will be
|
|
5
|
+
* lost once the code is regenerated.
|
|
6
|
+
*
|
|
7
|
+
* @generatorVersion: 1
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { Tag } from '@rnoh/react-native-openharmony/ts'
|
|
11
|
+
|
|
12
|
+
export namespace RNBackgroundActions {
|
|
13
|
+
export const NAME = 'RNBackgroundActions' as const
|
|
14
|
+
|
|
15
|
+
export type NativeOptions = {
|
|
16
|
+
taskName: string,
|
|
17
|
+
taskTitle: string,
|
|
18
|
+
taskDesc: string,
|
|
19
|
+
taskIcon: TaskIcon,
|
|
20
|
+
color?: string,
|
|
21
|
+
linkingURI?: string,
|
|
22
|
+
progressBar?: ProgressBar,
|
|
23
|
+
foregroundServiceType?: string[],
|
|
24
|
+
harmonyBackgroundModes?: string[],
|
|
25
|
+
generation?: number
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type ProgressBar = {max: number, value: number, indeterminate?: boolean}
|
|
29
|
+
|
|
30
|
+
export type TaskIcon = {name: string, type: string, package?: string}
|
|
31
|
+
|
|
32
|
+
export type TaskStatus = {
|
|
33
|
+
running: boolean,
|
|
34
|
+
mode: string,
|
|
35
|
+
requestId: number,
|
|
36
|
+
notificationId: number,
|
|
37
|
+
actualDelayTime: number,
|
|
38
|
+
cancelEventsSupported: boolean,
|
|
39
|
+
lastErrorCode: string,
|
|
40
|
+
lastErrorMessage: string
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface Spec {
|
|
44
|
+
start(options: NativeOptions): Promise<void>;
|
|
45
|
+
|
|
46
|
+
stop(): Promise<void>;
|
|
47
|
+
|
|
48
|
+
updateNotification(options: NativeOptions): Promise<void>;
|
|
49
|
+
|
|
50
|
+
isRunning(): boolean;
|
|
51
|
+
|
|
52
|
+
getStatus(): Promise<TaskStatus>;
|
|
53
|
+
|
|
54
|
+
requestNotifications(): Promise<boolean>;
|
|
55
|
+
|
|
56
|
+
scheduleTimer(id: number, delay: number, repeats: boolean): void;
|
|
57
|
+
|
|
58
|
+
cancelTimer(id: number): void;
|
|
59
|
+
|
|
60
|
+
addListener(eventName: string): void;
|
|
61
|
+
|
|
62
|
+
removeListeners(count: number): void;
|
|
63
|
+
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This code was generated by "react-native codegen-harmony"
|
|
3
|
-
*
|
|
4
|
-
* Do not edit this file as changes may cause incorrect behavior and will be
|
|
5
|
-
* lost once the code is regenerated.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
export * from './RNBackgroundActions'
|
|
1
|
+
/**
|
|
2
|
+
* This code was generated by "react-native codegen-harmony"
|
|
3
|
+
*
|
|
4
|
+
* Do not edit this file as changes may cause incorrect behavior and will be
|
|
5
|
+
* lost once the code is regenerated.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export * from './RNBackgroundActions'
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
{
|
|
2
|
-
"module": {
|
|
3
|
-
"name": "background_actions",
|
|
4
|
-
"type": "har",
|
|
5
|
-
"deviceTypes": [
|
|
6
|
-
"default",
|
|
7
|
-
"tablet",
|
|
8
|
-
"2in1"
|
|
9
|
-
],
|
|
10
|
-
"requestPermissions": [{"name":"ohos.permission.KEEP_BACKGROUND_RUNNING"},
|
|
11
|
-
]
|
|
12
|
-
}
|
|
13
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"module": {
|
|
3
|
+
"name": "background_actions",
|
|
4
|
+
"type": "har",
|
|
5
|
+
"deviceTypes": [
|
|
6
|
+
"default",
|
|
7
|
+
"tablet",
|
|
8
|
+
"2in1"
|
|
9
|
+
],
|
|
10
|
+
"requestPermissions": [{"name":"ohos.permission.KEEP_BACKGROUND_RUNNING"},
|
|
11
|
+
]
|
|
12
|
+
}
|
|
13
|
+
}
|
package/package.json
CHANGED
|
@@ -1,84 +1,84 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@hxa-rn/react-native-background-actions",
|
|
3
|
-
"displayName": "@hxa-rn/react-native-background-actions",
|
|
4
|
-
"version": "4.1.
|
|
5
|
-
"description": "React Native background service library for running background tasks forever in Android & iOS",
|
|
6
|
-
"main": "./src/index.ts",
|
|
7
|
-
"module": "./src/index.ts",
|
|
8
|
-
"react-native": "./src/index.ts",
|
|
9
|
-
"types": "./src/index.ts",
|
|
10
|
-
"license": "MIT",
|
|
11
|
-
"private": false,
|
|
12
|
-
"repository": {
|
|
13
|
-
"type": "git",
|
|
14
|
-
"url": "https://gitcode.com/hxa-rn/react-native-background-actions.git"
|
|
15
|
-
},
|
|
16
|
-
"keywords": [
|
|
17
|
-
"react-native",
|
|
18
|
-
"harmony",
|
|
19
|
-
"harmonyos"
|
|
20
|
-
],
|
|
21
|
-
"homepage": "https://gitcode.com/hxa-rn/react-native-background-actions",
|
|
22
|
-
"publishConfig": {
|
|
23
|
-
"registry": "https://registry.npmjs.org/"
|
|
24
|
-
},
|
|
25
|
-
"files": [
|
|
26
|
-
"dist/",
|
|
27
|
-
"src/",
|
|
28
|
-
"harmony/",
|
|
29
|
-
"README.md",
|
|
30
|
-
"LICENSE"
|
|
31
|
-
],
|
|
32
|
-
"scripts": {
|
|
33
|
-
"prepare": "tsc --noEmit",
|
|
34
|
-
"codegen-lib": "react-native codegen-harmony --cpp-output-path ./harmony/background_actions/src/main/cpp/generated --ets-output-path ./harmony/background_actions/src/main/ets/generated"
|
|
35
|
-
},
|
|
36
|
-
"peerDependencies": {
|
|
37
|
-
"react-native": ">=0.72"
|
|
38
|
-
},
|
|
39
|
-
"devDependencies": {
|
|
40
|
-
"typescript": "5.0.4",
|
|
41
|
-
"@react-native/typescript-config": "0.79.5",
|
|
42
|
-
"react": "18.2.0",
|
|
43
|
-
"@types/react": "18.2.79",
|
|
44
|
-
"react-native": "0.72.5",
|
|
45
|
-
"jest": "^29.6.3",
|
|
46
|
-
"react-native-builder-bob": "^0.18.0",
|
|
47
|
-
"@react-native-oh/react-native-harmony-cli": "^0.77.50",
|
|
48
|
-
"metro": "^0.81.0",
|
|
49
|
-
"@react-native-oh/react-native-harmony": "0.72.139"
|
|
50
|
-
},
|
|
51
|
-
"engines": {
|
|
52
|
-
"node": ">=18"
|
|
53
|
-
},
|
|
54
|
-
"harmony": {
|
|
55
|
-
"alias": "react-native-background-actions",
|
|
56
|
-
"autolinking": {
|
|
57
|
-
"cmakeLibraryTargetName": "background_actions",
|
|
58
|
-
"ohPackageName": "@hxa-rn/react-native-background-actions",
|
|
59
|
-
"etsPackageClassName": "BackgroundActionsPackage",
|
|
60
|
-
"cppPackageClassName": "BackgroundActionsPackage"
|
|
61
|
-
},
|
|
62
|
-
"codegenConfig": [
|
|
63
|
-
{
|
|
64
|
-
"version": 1,
|
|
65
|
-
"specPaths": [
|
|
66
|
-
"./src/specs/v1"
|
|
67
|
-
]
|
|
68
|
-
}
|
|
69
|
-
]
|
|
70
|
-
},
|
|
71
|
-
"react-native-builder-bob": {
|
|
72
|
-
"source": "src",
|
|
73
|
-
"output": "dist",
|
|
74
|
-
"targets": [
|
|
75
|
-
"commonjs",
|
|
76
|
-
"module",
|
|
77
|
-
"typescript"
|
|
78
|
-
]
|
|
79
|
-
},
|
|
80
|
-
"dependencies": {
|
|
81
|
-
"eventemitter3": "4.0.7"
|
|
82
|
-
},
|
|
83
|
-
"author": "hxa-rn"
|
|
84
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@hxa-rn/react-native-background-actions",
|
|
3
|
+
"displayName": "@hxa-rn/react-native-background-actions",
|
|
4
|
+
"version": "4.1.1",
|
|
5
|
+
"description": "React Native background service library for running background tasks forever in Android & iOS",
|
|
6
|
+
"main": "./src/index.ts",
|
|
7
|
+
"module": "./src/index.ts",
|
|
8
|
+
"react-native": "./src/index.ts",
|
|
9
|
+
"types": "./src/index.ts",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"private": false,
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://gitcode.com/hxa-rn/react-native-background-actions.git"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"react-native",
|
|
18
|
+
"harmony",
|
|
19
|
+
"harmonyos"
|
|
20
|
+
],
|
|
21
|
+
"homepage": "https://gitcode.com/hxa-rn/react-native-background-actions",
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"registry": "https://registry.npmjs.org/"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist/",
|
|
27
|
+
"src/",
|
|
28
|
+
"harmony/",
|
|
29
|
+
"README.md",
|
|
30
|
+
"LICENSE"
|
|
31
|
+
],
|
|
32
|
+
"scripts": {
|
|
33
|
+
"prepare": "tsc --noEmit",
|
|
34
|
+
"codegen-lib": "react-native codegen-harmony --cpp-output-path ./harmony/background_actions/src/main/cpp/generated --ets-output-path ./harmony/background_actions/src/main/ets/generated"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"react-native": ">=0.72"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"typescript": "5.0.4",
|
|
41
|
+
"@react-native/typescript-config": "0.79.5",
|
|
42
|
+
"react": "18.2.0",
|
|
43
|
+
"@types/react": "18.2.79",
|
|
44
|
+
"react-native": "0.72.5",
|
|
45
|
+
"jest": "^29.6.3",
|
|
46
|
+
"react-native-builder-bob": "^0.18.0",
|
|
47
|
+
"@react-native-oh/react-native-harmony-cli": "^0.77.50",
|
|
48
|
+
"metro": "^0.81.0",
|
|
49
|
+
"@react-native-oh/react-native-harmony": "0.72.139"
|
|
50
|
+
},
|
|
51
|
+
"engines": {
|
|
52
|
+
"node": ">=18"
|
|
53
|
+
},
|
|
54
|
+
"harmony": {
|
|
55
|
+
"alias": "react-native-background-actions",
|
|
56
|
+
"autolinking": {
|
|
57
|
+
"cmakeLibraryTargetName": "background_actions",
|
|
58
|
+
"ohPackageName": "@hxa-rn/react-native-background-actions",
|
|
59
|
+
"etsPackageClassName": "BackgroundActionsPackage",
|
|
60
|
+
"cppPackageClassName": "BackgroundActionsPackage"
|
|
61
|
+
},
|
|
62
|
+
"codegenConfig": [
|
|
63
|
+
{
|
|
64
|
+
"version": 1,
|
|
65
|
+
"specPaths": [
|
|
66
|
+
"./src/specs/v1"
|
|
67
|
+
]
|
|
68
|
+
}
|
|
69
|
+
]
|
|
70
|
+
},
|
|
71
|
+
"react-native-builder-bob": {
|
|
72
|
+
"source": "src",
|
|
73
|
+
"output": "dist",
|
|
74
|
+
"targets": [
|
|
75
|
+
"commonjs",
|
|
76
|
+
"module",
|
|
77
|
+
"typescript"
|
|
78
|
+
]
|
|
79
|
+
},
|
|
80
|
+
"dependencies": {
|
|
81
|
+
"eventemitter3": "4.0.7"
|
|
82
|
+
},
|
|
83
|
+
"author": "hxa-rn"
|
|
84
|
+
}
|
|
@@ -1,78 +1,78 @@
|
|
|
1
|
-
import {NativeEventEmitter} from 'react-native';
|
|
2
|
-
import NativeRNBackgroundActions from './specs/v1/NativeRNBackgroundActionsSpec';
|
|
3
|
-
|
|
4
|
-
const RNBackgroundActions: typeof NativeRNBackgroundActions = {
|
|
5
|
-
start: options => NativeRNBackgroundActions.start(options),
|
|
6
|
-
stop: () => NativeRNBackgroundActions.stop(),
|
|
7
|
-
updateNotification: options => NativeRNBackgroundActions.updateNotification(options),
|
|
8
|
-
isRunning: () => NativeRNBackgroundActions.isRunning(),
|
|
9
|
-
getStatus: () => NativeRNBackgroundActions.getStatus(),
|
|
10
|
-
requestNotifications: () => NativeRNBackgroundActions.requestNotifications(),
|
|
11
|
-
scheduleTimer(id, delay, repeats): void { NativeRNBackgroundActions.scheduleTimer(id, delay, repeats); },
|
|
12
|
-
cancelTimer(id): void { NativeRNBackgroundActions.cancelTimer(id); },
|
|
13
|
-
addListener(eventName): void { NativeRNBackgroundActions.addListener(eventName); },
|
|
14
|
-
removeListeners(count): void { NativeRNBackgroundActions.removeListeners(count); },
|
|
15
|
-
};
|
|
16
|
-
type Listener = (...args: any[]) => void;
|
|
17
|
-
export class BackgroundEventSubscription {
|
|
18
|
-
removed = false;
|
|
19
|
-
once = false;
|
|
20
|
-
native?: {remove(): void};
|
|
21
|
-
constructor(readonly emitter: BackgroundNativeEventEmitter, readonly eventType: string,
|
|
22
|
-
readonly listener: Listener, readonly context?: unknown) {}
|
|
23
|
-
remove(): void {
|
|
24
|
-
if (this.removed) return;
|
|
25
|
-
this.removed = true;
|
|
26
|
-
this.native?.remove();
|
|
27
|
-
this.emitter.forget(this);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
// Preserve the old public NativeEventEmitter surface on the RN 0.72 baseline.
|
|
31
|
-
// Each subscription still registers with the real native bridge.
|
|
32
|
-
class BackgroundNativeEventEmitter {
|
|
33
|
-
private native = new NativeEventEmitter(RNBackgroundActions);
|
|
34
|
-
private subscriptions: BackgroundEventSubscription[] = [];
|
|
35
|
-
private current?: BackgroundEventSubscription;
|
|
36
|
-
addListener(eventType: string, listener: Listener, context?: unknown): BackgroundEventSubscription {
|
|
37
|
-
if (typeof listener !== 'function') throw new TypeError('listener must be a function');
|
|
38
|
-
const subscription = new BackgroundEventSubscription(this, eventType, listener, context);
|
|
39
|
-
subscription.native = this.native.addListener(eventType, (...args: any[]) => {
|
|
40
|
-
if (subscription.removed) return;
|
|
41
|
-
if (subscription.once) subscription.remove();
|
|
42
|
-
const previous = this.current;
|
|
43
|
-
this.current = subscription;
|
|
44
|
-
try { listener.apply(context, args); } finally { this.current = previous; }
|
|
45
|
-
});
|
|
46
|
-
this.subscriptions.push(subscription);
|
|
47
|
-
return subscription;
|
|
48
|
-
}
|
|
49
|
-
once(eventType: string, listener: Listener, context?: unknown): BackgroundEventSubscription {
|
|
50
|
-
if (typeof listener !== 'function') throw new TypeError('listener must be a function');
|
|
51
|
-
const subscription = this.addListener(eventType, listener, context);
|
|
52
|
-
subscription.once = true;
|
|
53
|
-
return subscription;
|
|
54
|
-
}
|
|
55
|
-
emit(eventType: string, ...args: any[]): void { this.native.emit(eventType, ...args); }
|
|
56
|
-
listeners(eventType: string): BackgroundEventSubscription[] {
|
|
57
|
-
if (!eventType) throw new TypeError('eventType is required');
|
|
58
|
-
return this.subscriptions.filter(subscription => subscription.eventType === eventType && !subscription.removed);
|
|
59
|
-
}
|
|
60
|
-
listenerCount(eventType: string): number { return this.listeners(eventType).length; }
|
|
61
|
-
removeListener(eventType: string, listener: Listener): void {
|
|
62
|
-
this.listeners(eventType).filter(subscription => subscription.listener === listener).forEach(subscription => subscription.remove());
|
|
63
|
-
}
|
|
64
|
-
removeAllListeners(eventType: string): void { this.listeners(eventType).forEach(subscription => subscription.remove()); }
|
|
65
|
-
removeSubscription(subscription: BackgroundEventSubscription): void {
|
|
66
|
-
if (!subscription || subscription.emitter !== this) throw new Error('Subscription belongs to a different emitter');
|
|
67
|
-
subscription.remove();
|
|
68
|
-
}
|
|
69
|
-
removeCurrentListener(): void {
|
|
70
|
-
if (!this.current) throw new Error('removeCurrentListener must be called during dispatch');
|
|
71
|
-
this.current.remove();
|
|
72
|
-
}
|
|
73
|
-
forget(subscription: BackgroundEventSubscription): void {
|
|
74
|
-
this.subscriptions = this.subscriptions.filter(item => item !== subscription);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
const nativeEventEmitter = new BackgroundNativeEventEmitter();
|
|
78
|
-
export {RNBackgroundActions, nativeEventEmitter};
|
|
1
|
+
import {NativeEventEmitter} from 'react-native';
|
|
2
|
+
import NativeRNBackgroundActions from './specs/v1/NativeRNBackgroundActionsSpec';
|
|
3
|
+
|
|
4
|
+
const RNBackgroundActions: typeof NativeRNBackgroundActions = {
|
|
5
|
+
start: options => NativeRNBackgroundActions.start(options),
|
|
6
|
+
stop: () => NativeRNBackgroundActions.stop(),
|
|
7
|
+
updateNotification: options => NativeRNBackgroundActions.updateNotification(options),
|
|
8
|
+
isRunning: () => NativeRNBackgroundActions.isRunning(),
|
|
9
|
+
getStatus: () => NativeRNBackgroundActions.getStatus(),
|
|
10
|
+
requestNotifications: () => NativeRNBackgroundActions.requestNotifications(),
|
|
11
|
+
scheduleTimer(id, delay, repeats): void { NativeRNBackgroundActions.scheduleTimer(id, delay, repeats); },
|
|
12
|
+
cancelTimer(id): void { NativeRNBackgroundActions.cancelTimer(id); },
|
|
13
|
+
addListener(eventName): void { NativeRNBackgroundActions.addListener(eventName); },
|
|
14
|
+
removeListeners(count): void { NativeRNBackgroundActions.removeListeners(count); },
|
|
15
|
+
};
|
|
16
|
+
type Listener = (...args: any[]) => void;
|
|
17
|
+
export class BackgroundEventSubscription {
|
|
18
|
+
removed = false;
|
|
19
|
+
once = false;
|
|
20
|
+
native?: {remove(): void};
|
|
21
|
+
constructor(readonly emitter: BackgroundNativeEventEmitter, readonly eventType: string,
|
|
22
|
+
readonly listener: Listener, readonly context?: unknown) {}
|
|
23
|
+
remove(): void {
|
|
24
|
+
if (this.removed) return;
|
|
25
|
+
this.removed = true;
|
|
26
|
+
this.native?.remove();
|
|
27
|
+
this.emitter.forget(this);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
// Preserve the old public NativeEventEmitter surface on the RN 0.72 baseline.
|
|
31
|
+
// Each subscription still registers with the real native bridge.
|
|
32
|
+
class BackgroundNativeEventEmitter {
|
|
33
|
+
private native = new NativeEventEmitter(RNBackgroundActions);
|
|
34
|
+
private subscriptions: BackgroundEventSubscription[] = [];
|
|
35
|
+
private current?: BackgroundEventSubscription;
|
|
36
|
+
addListener(eventType: string, listener: Listener, context?: unknown): BackgroundEventSubscription {
|
|
37
|
+
if (typeof listener !== 'function') throw new TypeError('listener must be a function');
|
|
38
|
+
const subscription = new BackgroundEventSubscription(this, eventType, listener, context);
|
|
39
|
+
subscription.native = this.native.addListener(eventType, (...args: any[]) => {
|
|
40
|
+
if (subscription.removed) return;
|
|
41
|
+
if (subscription.once) subscription.remove();
|
|
42
|
+
const previous = this.current;
|
|
43
|
+
this.current = subscription;
|
|
44
|
+
try { listener.apply(context, args); } finally { this.current = previous; }
|
|
45
|
+
});
|
|
46
|
+
this.subscriptions.push(subscription);
|
|
47
|
+
return subscription;
|
|
48
|
+
}
|
|
49
|
+
once(eventType: string, listener: Listener, context?: unknown): BackgroundEventSubscription {
|
|
50
|
+
if (typeof listener !== 'function') throw new TypeError('listener must be a function');
|
|
51
|
+
const subscription = this.addListener(eventType, listener, context);
|
|
52
|
+
subscription.once = true;
|
|
53
|
+
return subscription;
|
|
54
|
+
}
|
|
55
|
+
emit(eventType: string, ...args: any[]): void { this.native.emit(eventType, ...args); }
|
|
56
|
+
listeners(eventType: string): BackgroundEventSubscription[] {
|
|
57
|
+
if (!eventType) throw new TypeError('eventType is required');
|
|
58
|
+
return this.subscriptions.filter(subscription => subscription.eventType === eventType && !subscription.removed);
|
|
59
|
+
}
|
|
60
|
+
listenerCount(eventType: string): number { return this.listeners(eventType).length; }
|
|
61
|
+
removeListener(eventType: string, listener: Listener): void {
|
|
62
|
+
this.listeners(eventType).filter(subscription => subscription.listener === listener).forEach(subscription => subscription.remove());
|
|
63
|
+
}
|
|
64
|
+
removeAllListeners(eventType: string): void { this.listeners(eventType).forEach(subscription => subscription.remove()); }
|
|
65
|
+
removeSubscription(subscription: BackgroundEventSubscription): void {
|
|
66
|
+
if (!subscription || subscription.emitter !== this) throw new Error('Subscription belongs to a different emitter');
|
|
67
|
+
subscription.remove();
|
|
68
|
+
}
|
|
69
|
+
removeCurrentListener(): void {
|
|
70
|
+
if (!this.current) throw new Error('removeCurrentListener must be called during dispatch');
|
|
71
|
+
this.current.remove();
|
|
72
|
+
}
|
|
73
|
+
forget(subscription: BackgroundEventSubscription): void {
|
|
74
|
+
this.subscriptions = this.subscriptions.filter(item => item !== subscription);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const nativeEventEmitter = new BackgroundNativeEventEmitter();
|
|
78
|
+
export {RNBackgroundActions, nativeEventEmitter};
|