UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

114 lines (111 loc) • 4.28 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var index_native_exports = {}; __export(index_native_exports, { BackgroundTasks: () => BackgroundTasks }); module.exports = __toCommonJS(index_native_exports); var import_validate = require("./validate.native.js"); var import_react_native = require("react-native"); var import_react_native_nitro_modules = require("react-native-nitro-modules"); var import_nativeError = require("../nativeError.native.js"); var import_unavailable = require("./unavailable.native.js"); var hybrid; var removeNativeListener; var handlers = /* @__PURE__ */ new Map(); var running = /* @__PURE__ */ new Map(); function native() { if (import_react_native.Platform.OS !== "ios" && import_react_native.Platform.OS !== "android") throw new Error("BackgroundTasks requires a native build"); hybrid !== null && hybrid !== void 0 ? hybrid : hybrid = import_react_native_nitro_modules.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 (import_react_native.Platform.OS === "android") { import_react_native.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) { (0, import_validate.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] : {}; (0, import_validate.validateSubmission)(identifier, options.earliestBeginDateMs); return native().submit(identifier, options.earliestBeginDateMs, options.requiresNetworkConnectivity, options.requiresExternalPower).catch(import_nativeError.rethrowNativeError); } function getPending() { return native().getPending().catch(import_nativeError.rethrowNativeError); } function cancel(identifier) { (0, import_validate.validateCancellation)(identifier); native().cancel(identifier); } var nativeBackgroundTasks = Object.freeze({ defineTask, submit, getPending, cancel }); var BackgroundTasks = import_react_native.Platform.OS === "ios" || import_react_native.Platform.OS === "android" ? nativeBackgroundTasks : import_unavailable.BackgroundTasks; //# sourceMappingURL=index.native.js.map