@octane-xplat/background-tasks 0.0.1 → 0.19.0
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/README.md +52 -0
- package/package.json +79 -2
- package/platforms/android/AndroidManifest.xml +4 -0
- package/platforms/android/include.gradle +3 -0
- package/platforms/android/java/com/octanexplat/tasks/TaskScheduler.java +25 -0
- package/platforms/android/java/com/octanexplat/tasks/TaskWorker.java +50 -0
- package/platforms/ios/Info.plist +14 -0
- package/src/background-tasks.linux.ts +16 -0
- package/src/background-tasks.macos.ts +82 -0
- package/src/background-tasks.ts +129 -0
- package/src/background-tasks.web.ts +15 -0
- package/src/background-tasks.windows.ts +16 -0
- package/src/index.macos.ts +2 -0
- package/src/index.ts +2 -0
- package/src/index.web.ts +2 -0
- package/src/types.ts +42 -0
- package/types/generated/.tsrx-typegen-manifest.json +14 -0
- package/types/generated/background-tasks.d.ts +3 -0
- package/types/generated/background-tasks.linux.d.ts +3 -0
- package/types/generated/background-tasks.macos.d.ts +3 -0
- package/types/generated/background-tasks.web.d.ts +3 -0
- package/types/generated/background-tasks.windows.d.ts +3 -0
- package/types/generated/index.d.ts +2 -0
- package/types/generated/index.macos.d.ts +2 -0
- package/types/generated/index.web.d.ts +2 -0
- package/types/generated/types.d.ts +39 -0
package/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# @octane-xplat/background-tasks
|
|
2
|
+
|
|
3
|
+
Define deferrable work for iOS/Android scheduler experiments. The OS chooses
|
|
4
|
+
when work runs; this is not an exact timer. The current Android cold-start/
|
|
5
|
+
completion bridge and iOS outcome/expiration/recurrence lifecycle have open
|
|
6
|
+
gaps ([#71](https://github.com/octane-xplat/octane-xplat/issues/71),
|
|
7
|
+
[#72](https://github.com/octane-xplat/octane-xplat/issues/72)). Do not treat
|
|
8
|
+
registration as a production delivery guarantee. Web/desktop are unsupported.
|
|
9
|
+
|
|
10
|
+
Follow [Run a background refresh](https://octane-xplat.goddardai.org/background-work)
|
|
11
|
+
for a maintained timestamp job, foreground recovery screen, native configuration,
|
|
12
|
+
launch-time registration, disable and execution checks.
|
|
13
|
+
|
|
14
|
+
## Define a small task
|
|
15
|
+
|
|
16
|
+
Install in an existing Xplat app:
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
bun add @octane-xplat/background-tasks @octane-xplat/platform
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Define tasks at module scope in a module imported at native startup. Keep their
|
|
23
|
+
results outside mounted screens, and confirm durable writes. This definition
|
|
24
|
+
records an execution attempt; it does not itself schedule or prove OS delivery:
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { backgroundTasks } from '@octane-xplat/background-tasks'
|
|
28
|
+
import { storage } from '@octane-xplat/platform'
|
|
29
|
+
|
|
30
|
+
backgroundTasks.defineTask('record-refresh', async () => {
|
|
31
|
+
try {
|
|
32
|
+
storage.setString('refresh.background-at', new Date().toISOString())
|
|
33
|
+
return storage.flush() ? 'success' : 'failed'
|
|
34
|
+
} catch {
|
|
35
|
+
return 'failed'
|
|
36
|
+
}
|
|
37
|
+
})
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Use the guide's [native configuration](https://octane-xplat.goddardai.org/background-work#configure-the-native-experiment)
|
|
41
|
+
and [startup request](https://octane-xplat.goddardai.org/background-work#request-once-at-startup)
|
|
42
|
+
before scheduling. On iOS, the task requires a permitted full identifier and a
|
|
43
|
+
handler registered once before app launch finishes. Repeated registration can
|
|
44
|
+
terminate the app; do not register from a button or hot-reloaded component.
|
|
45
|
+
Android carries a WorkManager bridge with a 15-minute interval floor, but its
|
|
46
|
+
current worker does not bootstrap a dead JS runtime.
|
|
47
|
+
|
|
48
|
+
Keep the [foreground fallback](https://octane-xplat.goddardai.org/background-work#disable-retry-and-fall-back).
|
|
49
|
+
The interval is in seconds and is a scheduling hint. `isRegistered` only checks
|
|
50
|
+
the current JS map, not the OS queue. Qualify a persisted execution timestamp,
|
|
51
|
+
actual task outcome and subsequent delivery using the guide's
|
|
52
|
+
[device procedure](https://octane-xplat.goddardai.org/background-work#prove-execution-separately).
|
package/package.json
CHANGED
|
@@ -1,11 +1,88 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@octane-xplat/background-tasks",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.19.0",
|
|
4
|
+
"description": "OS-scheduled periodic background work for Octane xplat — BGTaskScheduler and WorkManager",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"android",
|
|
7
|
+
"cross-platform",
|
|
8
|
+
"nativescript",
|
|
9
|
+
"octane",
|
|
10
|
+
"xplat",
|
|
11
|
+
"background-tasks",
|
|
12
|
+
"background-fetch",
|
|
13
|
+
"workmanager"
|
|
14
|
+
],
|
|
15
|
+
"homepage": "https://github.com/octane-xplat/octane-xplat/tree/main/packages/background-tasks",
|
|
5
16
|
"license": "MIT",
|
|
6
17
|
"repository": {
|
|
7
18
|
"type": "git",
|
|
8
19
|
"url": "https://github.com/octane-xplat/octane-xplat",
|
|
9
20
|
"directory": "packages/background-tasks"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"src",
|
|
24
|
+
"platforms",
|
|
25
|
+
"types",
|
|
26
|
+
"!src/**/*.test.*",
|
|
27
|
+
"!src/**/*.spec.*"
|
|
28
|
+
],
|
|
29
|
+
"type": "module",
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"types": "./types/generated/index.d.ts",
|
|
33
|
+
"macos": "./src/index.macos.ts",
|
|
34
|
+
"web": "./src/index.web.ts",
|
|
35
|
+
"native": "./src/index.ts",
|
|
36
|
+
"default": "./src/index.ts"
|
|
37
|
+
},
|
|
38
|
+
"./*": {
|
|
39
|
+
"types": "./types/generated/*.d.ts",
|
|
40
|
+
"default": "./src/*"
|
|
41
|
+
},
|
|
42
|
+
"./*.ts": {
|
|
43
|
+
"types": "./types/generated/*.d.ts",
|
|
44
|
+
"default": "./src/*.ts"
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"publishConfig": {
|
|
48
|
+
"access": "public"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"test": "node --test tests/*.test.ts",
|
|
52
|
+
"typegen": "tsrx-typegen --target octane",
|
|
53
|
+
"prepack": "bun run typegen && tsrx-typegen --pack-check",
|
|
54
|
+
"pack:check": "tsrx-typegen --pack-check",
|
|
55
|
+
"typecheck": "node ../../scripts/typecheck.ts"
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"@nativescript-community/octane": "0.2.5",
|
|
59
|
+
"@nativescript/core": "9.1.3",
|
|
60
|
+
"@nativescript/types": "9.1.4",
|
|
61
|
+
"@tsrx/typescript-plugin": "0.4.11",
|
|
62
|
+
"octane": "0.16.3",
|
|
63
|
+
"tsrx-typegen": "0.5.0",
|
|
64
|
+
"typescript": "5.9.3"
|
|
65
|
+
},
|
|
66
|
+
"peerDependencies": {
|
|
67
|
+
"@nativescript-community/octane": ">=0.2.1 <1",
|
|
68
|
+
"@nativescript/core": ">=9.1.0 <10",
|
|
69
|
+
"octane": ">=0.6.3 <1"
|
|
70
|
+
},
|
|
71
|
+
"peerDependenciesMeta": {
|
|
72
|
+
"@nativescript-community/octane": {
|
|
73
|
+
"optional": true
|
|
74
|
+
},
|
|
75
|
+
"@nativescript/core": {
|
|
76
|
+
"optional": true
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
"nativescript": {
|
|
80
|
+
"platforms": {
|
|
81
|
+
"android": "9.1.0",
|
|
82
|
+
"ios": "9.1.0"
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
"dependencies": {
|
|
86
|
+
"@octane-xplat/platform": "0.19.0"
|
|
10
87
|
}
|
|
11
88
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
package com.octanexplat.tasks;
|
|
2
|
+
|
|
3
|
+
import android.content.Context;
|
|
4
|
+
import androidx.work.*;
|
|
5
|
+
import com.tns.Utils;
|
|
6
|
+
import java.util.concurrent.TimeUnit;
|
|
7
|
+
|
|
8
|
+
public class TaskScheduler {
|
|
9
|
+
public static void schedule(String name, long intervalSeconds) {
|
|
10
|
+
long interval = Math.max(intervalSeconds, 15 * 60); // WorkManager floor
|
|
11
|
+
PeriodicWorkRequest request = new PeriodicWorkRequest.Builder(TaskWorker.class, interval, TimeUnit.SECONDS)
|
|
12
|
+
.addTag("xplat:" + name)
|
|
13
|
+
.build();
|
|
14
|
+
WorkManager.getInstance(context())
|
|
15
|
+
.enqueueUniquePeriodicWork("xplat:" + name, ExistingPeriodicWorkPolicy.UPDATE, request);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
public static void cancel(String name) {
|
|
19
|
+
WorkManager.getInstance(context()).cancelUniqueWork("xplat:" + name);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
private static Context context() {
|
|
23
|
+
return Utils.getApplicationContext();
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
package com.octanexplat.tasks;
|
|
2
|
+
|
|
3
|
+
import android.content.Context;
|
|
4
|
+
import androidx.work.*;
|
|
5
|
+
import com.tns.Runtime;
|
|
6
|
+
|
|
7
|
+
public class TaskWorker extends Worker {
|
|
8
|
+
public TaskWorker(Context context, WorkerParameters params) {
|
|
9
|
+
super(context, params);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
@Override
|
|
13
|
+
public Result doWork() {
|
|
14
|
+
String name = getTags().stream()
|
|
15
|
+
.filter(t -> t.startsWith("xplat:"))
|
|
16
|
+
.map(t -> t.substring(6))
|
|
17
|
+
.findFirst()
|
|
18
|
+
.orElse(null);
|
|
19
|
+
if (name == null) {
|
|
20
|
+
return Result.failure();
|
|
21
|
+
}
|
|
22
|
+
Runtime runtime = Runtime.getCurrentRuntime();
|
|
23
|
+
if (runtime == null) {
|
|
24
|
+
// Cold start: the runtime isn't up — WorkManager retry re-runs us.
|
|
25
|
+
return Result.retry();
|
|
26
|
+
}
|
|
27
|
+
final boolean[] done = { false };
|
|
28
|
+
final boolean[] ok = { false };
|
|
29
|
+
runtime.runOnJSThread(() -> {
|
|
30
|
+
try {
|
|
31
|
+
Object fn = ((com.tns.Runtime) runtime).callJSMethod(
|
|
32
|
+
com.tns.Runtime.getCurrentRuntime(),
|
|
33
|
+
"global.__xplatBackgroundTaskRun",
|
|
34
|
+
Object.class,
|
|
35
|
+
name,
|
|
36
|
+
(com.tns.CompletionListener) null);
|
|
37
|
+
ok[0] = !"failed".equals(String.valueOf(fn));
|
|
38
|
+
} catch (Throwable ignored) {
|
|
39
|
+
} finally {
|
|
40
|
+
done[0] = true;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
long waited = 0;
|
|
44
|
+
while (!done[0] && waited < 25_000) {
|
|
45
|
+
try { Thread.sleep(50); } catch (InterruptedException e) { break; }
|
|
46
|
+
waited += 50;
|
|
47
|
+
}
|
|
48
|
+
return ok[0] ? Result.success() : Result.retry();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
3
|
+
<plist version="1.0">
|
|
4
|
+
<dict>
|
|
5
|
+
<key>UIBackgroundModes</key>
|
|
6
|
+
<array>
|
|
7
|
+
<string>fetch</string>
|
|
8
|
+
<string>processing</string>
|
|
9
|
+
</array>
|
|
10
|
+
<!-- Apps must also add their task identifiers:
|
|
11
|
+
<key>BGTaskSchedulerPermittedIdentifiers</key>
|
|
12
|
+
<array><string>com.octanexplat.tasks.TASK_NAME</string></array> -->
|
|
13
|
+
</dict>
|
|
14
|
+
</plist>
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Unsupported leaf — platform-specific schedulers (NSBackgroundActivityScheduler
|
|
2
|
+
// on macOS) are follow-ups; the API surface stands.
|
|
3
|
+
import type { BackgroundTasks, TaskStatus } from './types'
|
|
4
|
+
|
|
5
|
+
const unsupported = () =>
|
|
6
|
+
Promise.reject(new Error('unsupported: background tasks are not wired on this platform'))
|
|
7
|
+
|
|
8
|
+
/** Background-task scheduler service — see BackgroundTasks for the contract. */
|
|
9
|
+
export const backgroundTasks: BackgroundTasks = {
|
|
10
|
+
supported: false,
|
|
11
|
+
defineTask: () => {},
|
|
12
|
+
register: unsupported,
|
|
13
|
+
unregister: unsupported,
|
|
14
|
+
getStatus: async (): Promise<TaskStatus> => 'unavailable',
|
|
15
|
+
isRegistered: async () => false,
|
|
16
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// Background tasks — AppKit leaf over NSBackgroundActivityScheduler. The
|
|
2
|
+
// scheduler is deferrable maintenance work: `interval` is a request the OS
|
|
3
|
+
// may stretch, the app must be running (no launch-on-event), and a deferred
|
|
4
|
+
// fire surfaces TaskResult through the executor's completion.
|
|
5
|
+
import { NSBackgroundActivityScheduler } from '@octane-xplat/platform/macos'
|
|
6
|
+
import type { BackgroundTasks, TaskExecutor, TaskResult, TaskStatus } from './types'
|
|
7
|
+
|
|
8
|
+
// NSBackgroundActivityResult: 1 deferred, 2 finished.
|
|
9
|
+
const RESULT_FINISHED = 2
|
|
10
|
+
const RESULT_DEFERRED = 1
|
|
11
|
+
|
|
12
|
+
const executors = new Map<string, TaskExecutor>()
|
|
13
|
+
const schedulers = new Map<string, any>()
|
|
14
|
+
|
|
15
|
+
const schedulerClass = (): any => {
|
|
16
|
+
try {
|
|
17
|
+
return typeof NSBackgroundActivityScheduler?.alloc === 'function'
|
|
18
|
+
? NSBackgroundActivityScheduler
|
|
19
|
+
: null
|
|
20
|
+
} catch {
|
|
21
|
+
return null
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Background-task scheduler service — see BackgroundTasks for the contract. */
|
|
26
|
+
export const backgroundTasks: BackgroundTasks = {
|
|
27
|
+
get supported() {
|
|
28
|
+
return schedulerClass() !== null
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
defineTask(name: string, executor: TaskExecutor): void {
|
|
32
|
+
executors.set(name, executor)
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
register: async (name: string, options?: { minimumInterval?: number }): Promise<void> => {
|
|
36
|
+
if (!executors.has(name)) {
|
|
37
|
+
throw new Error(`backgroundTasks.register: no task defined as '${name}'`)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const klass = schedulerClass()
|
|
41
|
+
if (!klass) {
|
|
42
|
+
throw new Error('backgroundTasks.register: NSBackgroundActivityScheduler is unavailable')
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
backgroundTasks.unregister(name).catch(() => {})
|
|
46
|
+
const scheduler = klass.alloc().initWithIdentifier(`xplat.background.${name}`)
|
|
47
|
+
scheduler.repeats = true
|
|
48
|
+
scheduler.interval = Math.max(options?.minimumInterval ?? 3600, 60)
|
|
49
|
+
scheduler.tolerance = scheduler.interval / 2
|
|
50
|
+
scheduler.qualityOfService = 0x09 // NSQualityOfServiceBackground
|
|
51
|
+
scheduler.scheduleWithBlock((completion: (result: number) => void) => {
|
|
52
|
+
const executor = executors.get(name)
|
|
53
|
+
if (scheduler.shouldDefer || !executor) {
|
|
54
|
+
completion(RESULT_DEFERRED)
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
Promise.resolve()
|
|
59
|
+
.then(() => executor({ name }))
|
|
60
|
+
.then((result: TaskResult) => completion(result === 'success' ? RESULT_FINISHED : RESULT_DEFERRED))
|
|
61
|
+
.catch(() => completion(RESULT_DEFERRED))
|
|
62
|
+
})
|
|
63
|
+
schedulers.set(name, scheduler)
|
|
64
|
+
},
|
|
65
|
+
|
|
66
|
+
unregister: async (name: string): Promise<void> => {
|
|
67
|
+
const scheduler = schedulers.get(name)
|
|
68
|
+
if (scheduler) {
|
|
69
|
+
schedulers.delete(name)
|
|
70
|
+
try {
|
|
71
|
+
scheduler.invalidate()
|
|
72
|
+
} catch {
|
|
73
|
+
// Invalidating a released scheduler must not poison unregister.
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
getStatus: async (): Promise<TaskStatus> =>
|
|
79
|
+
schedulerClass() ? 'available' : 'unavailable',
|
|
80
|
+
|
|
81
|
+
isRegistered: async (name: string): Promise<boolean> => schedulers.has(name),
|
|
82
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// Background tasks — native leaf, custom bridge (no NS plugin exists).
|
|
2
|
+
// iOS: BGTaskScheduler app-refresh requests; the Info.plist registers the
|
|
3
|
+
// task identifier and UIBackgroundModes fetch. Android: WorkManager periodic
|
|
4
|
+
// requests through platforms/android Gradle deps; the Worker spins up the
|
|
5
|
+
// runtime and calls back into __xplatBackgroundTaskRun.
|
|
6
|
+
//
|
|
7
|
+
// defineTask bodies must be registered at module scope — the OS can invoke a
|
|
8
|
+
// task before app code runs again, and a relaunch only re-evaluates module
|
|
9
|
+
// top-level. expo-task-manager's constraint, mirrored deliberately.
|
|
10
|
+
import { Application, Utils, isIOS } from '@nativescript/core'
|
|
11
|
+
import type { BackgroundTasks, TaskExecutor, TaskResult, TaskStatus } from './types'
|
|
12
|
+
|
|
13
|
+
declare const BGTaskScheduler: any
|
|
14
|
+
declare const BGAppRefreshTaskRequest: any
|
|
15
|
+
|
|
16
|
+
const executors = new Map<string, TaskExecutor>()
|
|
17
|
+
const registered = new Map<string, number>()
|
|
18
|
+
|
|
19
|
+
const TASK_PREFIX = 'com.octanexplat.tasks.'
|
|
20
|
+
|
|
21
|
+
async function runTask(name: string): Promise<TaskResult> {
|
|
22
|
+
const executor = executors.get(name)
|
|
23
|
+
if (!executor) {
|
|
24
|
+
return 'failed'
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
return (await executor({ name })) ?? 'success'
|
|
28
|
+
} catch {
|
|
29
|
+
return 'failed'
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// iOS BGTaskScheduler launch handler — the OS calls the registered handler on
|
|
34
|
+
// a background thread; we resolve via the JS executor then mark completion.
|
|
35
|
+
function iosHandle(task: any): void {
|
|
36
|
+
const identifier: string = task?.identifier ?? ''
|
|
37
|
+
const name = identifier.startsWith(TASK_PREFIX)
|
|
38
|
+
? identifier.slice(TASK_PREFIX.length)
|
|
39
|
+
: identifier
|
|
40
|
+
task.expirationHandler = () => {}
|
|
41
|
+
runTask(name).finally(() => {
|
|
42
|
+
try {
|
|
43
|
+
task.setTaskCompletedWithSuccess(true)
|
|
44
|
+
} catch {}
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Background-task scheduler service — see BackgroundTasks for the contract. */
|
|
49
|
+
export const backgroundTasks: BackgroundTasks = {
|
|
50
|
+
supported: isIOS ? typeof BGTaskScheduler !== 'undefined' : true,
|
|
51
|
+
|
|
52
|
+
defineTask(name: string, executor: TaskExecutor): void {
|
|
53
|
+
executors.set(name, executor)
|
|
54
|
+
},
|
|
55
|
+
|
|
56
|
+
async register(name: string, options?: { minimumInterval?: number }): Promise<void> {
|
|
57
|
+
if (!executors.has(name)) {
|
|
58
|
+
throw new Error(
|
|
59
|
+
`backgroundTasks.register: '${name}' has no defineTask — declare it at module scope`,
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
registered.set(name, options?.minimumInterval ?? 0)
|
|
63
|
+
if (isIOS) {
|
|
64
|
+
if (typeof BGTaskScheduler === 'undefined') {
|
|
65
|
+
throw new Error('unsupported: BGTaskScheduler is not available')
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const identifier = TASK_PREFIX + name
|
|
69
|
+
BGTaskScheduler.sharedScheduler.registerForTaskWithIdentifierLaunchHandler(
|
|
70
|
+
identifier,
|
|
71
|
+
null,
|
|
72
|
+
iosHandle,
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
const request = BGAppRefreshTaskRequest.alloc().initWithIdentifier(identifier)
|
|
76
|
+
const interval = options?.minimumInterval
|
|
77
|
+
if (interval) {
|
|
78
|
+
request.earliestBeginDate = (globalThis as any).NSDate.dateWithTimeIntervalSinceNow(
|
|
79
|
+
interval,
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
BGTaskScheduler.sharedScheduler.submitTaskRequestError(request, null)
|
|
84
|
+
return
|
|
85
|
+
}
|
|
86
|
+
// Android: WorkManager — the Java side reads the registered map off the
|
|
87
|
+
// global so a cold start rebuilds scheduled work from defineTask calls.
|
|
88
|
+
const worker = (globalThis as any).com?.octanexplat?.tasks?.TaskScheduler
|
|
89
|
+
if (worker?.schedule) {
|
|
90
|
+
worker.schedule(name, options?.minimumInterval ?? 0)
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
async unregister(name: string): Promise<void> {
|
|
95
|
+
registered.delete(name)
|
|
96
|
+
if (isIOS) {
|
|
97
|
+
BGTaskScheduler.sharedScheduler?.cancelTaskRequestWithIdentifier?.(TASK_PREFIX + name)
|
|
98
|
+
return
|
|
99
|
+
}
|
|
100
|
+
const worker = (globalThis as any).com?.octanexplat?.tasks?.TaskScheduler
|
|
101
|
+
worker?.cancel?.(name)
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
async getStatus(): Promise<TaskStatus> {
|
|
105
|
+
if (isIOS) {
|
|
106
|
+
if (typeof BGTaskScheduler === 'undefined') {
|
|
107
|
+
return 'unavailable'
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const status = (globalThis as any).UIApplication?.sharedApplication?.backgroundRefreshStatus
|
|
111
|
+
// UIBackgroundRefreshStatus: 0 restricted, 1 denied, 2 available
|
|
112
|
+
return status === 0 || status === 1 ? 'restricted' : 'available'
|
|
113
|
+
}
|
|
114
|
+
return 'available'
|
|
115
|
+
},
|
|
116
|
+
|
|
117
|
+
async isRegistered(name: string): Promise<boolean> {
|
|
118
|
+
return registered.has(name)
|
|
119
|
+
},
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Worker → JS entry. platforms/android's TaskWorker calls this after bringing
|
|
123
|
+
// the runtime up; buffered invocations replay once an executor exists.
|
|
124
|
+
;(globalThis as any).__xplatBackgroundTaskRun = (name: string, done: (r: string) => void) => {
|
|
125
|
+
runTask(name).then(
|
|
126
|
+
(r) => done(r),
|
|
127
|
+
() => done('failed'),
|
|
128
|
+
)
|
|
129
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Background tasks — no reliable web equivalent (Periodic Background Sync is
|
|
2
|
+
// Chromium-only + requires an installed PWA). Honest unsupported leaf.
|
|
3
|
+
import type { BackgroundTasks, TaskStatus } from './types'
|
|
4
|
+
|
|
5
|
+
const unsupported = () => Promise.reject(new Error('unsupported: no background task API on web'))
|
|
6
|
+
|
|
7
|
+
/** Background-task scheduler service — see BackgroundTasks for the contract. */
|
|
8
|
+
export const backgroundTasks: BackgroundTasks = {
|
|
9
|
+
supported: false,
|
|
10
|
+
defineTask: () => {},
|
|
11
|
+
register: unsupported,
|
|
12
|
+
unregister: unsupported,
|
|
13
|
+
getStatus: async (): Promise<TaskStatus> => 'unavailable',
|
|
14
|
+
isRegistered: async () => false,
|
|
15
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Unsupported leaf — platform-specific schedulers (NSBackgroundActivityScheduler
|
|
2
|
+
// on macOS) are follow-ups; the API surface stands.
|
|
3
|
+
import type { BackgroundTasks, TaskStatus } from './types'
|
|
4
|
+
|
|
5
|
+
const unsupported = () =>
|
|
6
|
+
Promise.reject(new Error('unsupported: background tasks are not wired on this platform'))
|
|
7
|
+
|
|
8
|
+
/** Background-task scheduler service — see BackgroundTasks for the contract. */
|
|
9
|
+
export const backgroundTasks: BackgroundTasks = {
|
|
10
|
+
supported: false,
|
|
11
|
+
defineTask: () => {},
|
|
12
|
+
register: unsupported,
|
|
13
|
+
unregister: unsupported,
|
|
14
|
+
getStatus: async (): Promise<TaskStatus> => 'unavailable',
|
|
15
|
+
isRegistered: async () => false,
|
|
16
|
+
}
|
package/src/index.ts
ADDED
package/src/index.web.ts
ADDED
package/src/types.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** Reported outcome of a task run — the OS uses it for scheduling heuristics
|
|
2
|
+
* (repeated 'failed' runs get deferred on both stores). */
|
|
3
|
+
export type TaskResult = 'success' | 'failed'
|
|
4
|
+
|
|
5
|
+
/** Invocation payload handed to a TaskExecutor. */
|
|
6
|
+
export interface TaskContext {
|
|
7
|
+
/** The registered task name — one executor may serve several names. */
|
|
8
|
+
name: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** The task body; resolves 'success' | 'failed' for OS scheduling feedback. */
|
|
12
|
+
export type TaskExecutor = (context: TaskContext) => Promise<TaskResult>
|
|
13
|
+
|
|
14
|
+
/** OS willingness to run background tasks — 'restricted' means the user or
|
|
15
|
+
* a battery/data saver mode has throttled them; 'unavailable' means the
|
|
16
|
+
* platform has no background-task path at all. */
|
|
17
|
+
export type TaskStatus = 'available' | 'restricted' | 'unavailable'
|
|
18
|
+
|
|
19
|
+
/** Background-task façade. Tasks are defined at module scope, then
|
|
20
|
+
* registered/unregistered against the OS scheduler (iOS BGTaskScheduler /
|
|
21
|
+
* Android WorkManager-style periodic work). Intervals are minimums — the OS
|
|
22
|
+
* decides actual timing. */
|
|
23
|
+
export interface BackgroundTasks {
|
|
24
|
+
/** Synchronous fast check — a background-task scheduler exists. */
|
|
25
|
+
readonly supported: boolean
|
|
26
|
+
/** Define a task at module scope — the OS may invoke the name before any
|
|
27
|
+
* other call, so registration must be load-time (expo-task-manager's
|
|
28
|
+
* structural constraint, mirrored deliberately). */
|
|
29
|
+
defineTask(name: string, executor: TaskExecutor): void
|
|
30
|
+
/** Schedule a defined task. `minimumInterval` is a hint in scheduler units
|
|
31
|
+
* (seconds on iOS via earliestBeginDate) — the OS decides actual timing
|
|
32
|
+
* and may defer well past it. */
|
|
33
|
+
register(name: string, options?: { minimumInterval?: number }): Promise<void>
|
|
34
|
+
/** Cancel a previously registered task; no-op when not registered. */
|
|
35
|
+
unregister(name: string): Promise<void>
|
|
36
|
+
/** Current OS scheduling posture for this app. */
|
|
37
|
+
getStatus(): Promise<TaskStatus>
|
|
38
|
+
/** Whether `name` is currently scheduled with the OS. */
|
|
39
|
+
isRegistered(name: string): Promise<boolean>
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export declare const backgroundTasks: BackgroundTasks
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 1,
|
|
3
|
+
"files": [
|
|
4
|
+
"background-tasks.d.ts",
|
|
5
|
+
"background-tasks.linux.d.ts",
|
|
6
|
+
"background-tasks.macos.d.ts",
|
|
7
|
+
"background-tasks.web.d.ts",
|
|
8
|
+
"background-tasks.windows.d.ts",
|
|
9
|
+
"index.d.ts",
|
|
10
|
+
"index.macos.d.ts",
|
|
11
|
+
"index.web.d.ts",
|
|
12
|
+
"types.d.ts"
|
|
13
|
+
]
|
|
14
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** Reported outcome of a task run — the OS uses it for scheduling heuristics
|
|
2
|
+
* (repeated 'failed' runs get deferred on both stores). */
|
|
3
|
+
export type TaskResult = 'success' | 'failed';
|
|
4
|
+
/** Invocation payload handed to a TaskExecutor. */
|
|
5
|
+
export interface TaskContext {
|
|
6
|
+
/** The registered task name — one executor may serve several names. */
|
|
7
|
+
name: string;
|
|
8
|
+
}
|
|
9
|
+
/** The task body; resolves 'success' | 'failed' for OS scheduling feedback. */
|
|
10
|
+
export type TaskExecutor = (context: TaskContext) => Promise<TaskResult>;
|
|
11
|
+
/** OS willingness to run background tasks — 'restricted' means the user or
|
|
12
|
+
* a battery/data saver mode has throttled them; 'unavailable' means the
|
|
13
|
+
* platform has no background-task path at all. */
|
|
14
|
+
export type TaskStatus = 'available' | 'restricted' | 'unavailable';
|
|
15
|
+
/** Background-task façade. Tasks are defined at module scope, then
|
|
16
|
+
* registered/unregistered against the OS scheduler (iOS BGTaskScheduler /
|
|
17
|
+
* Android WorkManager-style periodic work). Intervals are minimums — the OS
|
|
18
|
+
* decides actual timing. */
|
|
19
|
+
export interface BackgroundTasks {
|
|
20
|
+
/** Synchronous fast check — a background-task scheduler exists. */
|
|
21
|
+
readonly supported: boolean;
|
|
22
|
+
/** Define a task at module scope — the OS may invoke the name before any
|
|
23
|
+
* other call, so registration must be load-time (expo-task-manager's
|
|
24
|
+
* structural constraint, mirrored deliberately). */
|
|
25
|
+
defineTask(name: string, executor: TaskExecutor): void;
|
|
26
|
+
/** Schedule a defined task. `minimumInterval` is a hint in scheduler units
|
|
27
|
+
* (seconds on iOS via earliestBeginDate) — the OS decides actual timing
|
|
28
|
+
* and may defer well past it. */
|
|
29
|
+
register(name: string, options?: {
|
|
30
|
+
minimumInterval?: number;
|
|
31
|
+
}): Promise<void>;
|
|
32
|
+
/** Cancel a previously registered task; no-op when not registered. */
|
|
33
|
+
unregister(name: string): Promise<void>;
|
|
34
|
+
/** Current OS scheduling posture for this app. */
|
|
35
|
+
getStatus(): Promise<TaskStatus>;
|
|
36
|
+
/** Whether `name` is currently scheduled with the OS. */
|
|
37
|
+
isRegistered(name: string): Promise<boolean>;
|
|
38
|
+
}
|
|
39
|
+
export declare const backgroundTasks: BackgroundTasks;
|