one
Version:
One is a React framework for web, iOS, and Android apps.
91 lines (88 loc) • 3.13 kB
JavaScript
import { validateCancellation, validateDefinition, validateSubmission } from "./validate.native.js";
import { AppRegistry, Platform } from "react-native";
import { NitroModules } from "react-native-nitro-modules";
import { rethrowNativeError } from "../nativeError.native.js";
import { BackgroundTasks as BackgroundTasks$1 } from "./unavailable.native.js";
var hybrid;
var removeNativeListener;
var handlers = /* @__PURE__ */ new Map();
var running = /* @__PURE__ */ new Map();
function native() {
if (Platform.OS !== "ios" && Platform.OS !== "android") throw new Error("BackgroundTasks requires a native build");
hybrid !== null && hybrid !== void 0 ? hybrid : hybrid = NitroModules.createHybridObject("OneBackgroundTasks");
return hybrid;
}
async function runInvocation(invocation) {
var controller = new AbortController();
running.set(invocation.executionId, controller);
try {
var handler = handlers.get(invocation.identifier);
if (!handler) {
native().complete(invocation.executionId, false);
return;
}
try {
await handler({
...invocation,
signal: controller.signal
});
native().complete(invocation.executionId, true);
} catch (unused) {
native().complete(invocation.executionId, false);
}
} finally {
running.delete(invocation.executionId);
}
}
if (Platform.OS === "android") {
AppRegistry.registerHeadlessTask("OneBackgroundTasks", function() {
return runInvocation;
});
}
function addNativeListener() {
if (removeNativeListener) return;
removeNativeListener = native().addTaskListener(runInvocation, function(executionId) {
var _running_get;
(_running_get = running.get(executionId)) === null || _running_get === void 0 ? void 0 : _running_get.abort();
});
}
function defineTask(identifier, handler) {
validateDefinition(identifier, handler);
if (handlers.has(identifier)) throw new Error(`BackgroundTasks.defineTask: ${identifier} is already defined`);
handlers.set(identifier, handler);
try {
addNativeListener();
} catch (error) {
handlers.delete(identifier);
throw error;
}
return function() {
if (handlers.get(identifier) !== handler) return;
handlers.delete(identifier);
if (handlers.size === 0) {
removeNativeListener === null || removeNativeListener === void 0 ? void 0 : removeNativeListener();
removeNativeListener = void 0;
}
};
}
function submit(identifier) {
var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
validateSubmission(identifier, options.earliestBeginDateMs);
return native().submit(identifier, options.earliestBeginDateMs, options.requiresNetworkConnectivity, options.requiresExternalPower).catch(rethrowNativeError);
}
function getPending() {
return native().getPending().catch(rethrowNativeError);
}
function cancel(identifier) {
validateCancellation(identifier);
native().cancel(identifier);
}
var nativeBackgroundTasks = Object.freeze({
defineTask,
submit,
getPending,
cancel
});
var BackgroundTasks = Platform.OS === "ios" || Platform.OS === "android" ? nativeBackgroundTasks : BackgroundTasks$1;
export { BackgroundTasks };
//# sourceMappingURL=index.native.js.map