UNPKG

@tamagui/react-native-web-lite

Version:
135 lines (134 loc) 4.98 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: !0 }); }, __copyProps = (to, from, except, desc) => { if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__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: !0 }), mod); var TaskQueue_exports = {}; __export(TaskQueue_exports, { default: () => TaskQueue_default }); module.exports = __toCommonJS(TaskQueue_exports); var import_react_native_web_internals = require("@tamagui/react-native-web-internals"); function _class_call_check(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor); } } function _create_class(Constructor, protoProps, staticProps) { return protoProps && _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), Constructor; } function _define_property(obj, key, value) { return key in obj ? Object.defineProperty(obj, key, { value, enumerable: !0, configurable: !0, writable: !0 }) : obj[key] = value, obj; } var TaskQueue = /* @__PURE__ */ function() { "use strict"; function TaskQueue2(param) { var { onMoreTasks } = param; _class_call_check(this, TaskQueue2), _define_property(this, "_queueStack", void 0), _define_property(this, "_onMoreTasks", void 0), this._onMoreTasks = onMoreTasks, this._queueStack = [ { tasks: [], popable: !0 } ]; } return _create_class(TaskQueue2, [ { key: "enqueue", value: function(task) { this._getCurrentQueue().push(task); } }, { key: "enqueueTasks", value: function(tasks) { var _this = this; tasks.forEach(function(task) { return _this.enqueue(task); }); } }, { key: "cancelTasks", value: function(tasksToCancel) { this._queueStack = this._queueStack.map(function(queue) { return { ...queue, tasks: queue.tasks.filter(function(task) { return !tasksToCancel.includes(task); }) }; }).filter(function(queue, idx) { return queue.tasks.length > 0 || idx === 0; }); } }, { key: "hasTasksToProcess", value: function() { return this._getCurrentQueue().length > 0; } }, { /** * Executes the next task in the queue. */ key: "processNext", value: function() { var queue = this._getCurrentQueue(); if (queue.length) { var task = queue.shift(); try { typeof task == "object" && "gen" in task ? this._genPromise(task) : typeof task == "object" && "run" in task ? task.run() : ((0, import_react_native_web_internals.invariant)(typeof task == "function", `Expected Function, SimpleTask, or PromiseTask, but got: ` + JSON.stringify(task, null, 2)), task()); } catch (e) { throw e instanceof Error && (e.message = "TaskQueue: Error with task " + ((task == null ? void 0 : task.name) || "") + ": " + e.message), e; } } } }, { key: "_getCurrentQueue", value: function() { var stackIdx = this._queueStack.length - 1, queue = this._queueStack[stackIdx]; return queue.popable && queue.tasks.length === 0 && stackIdx > 0 ? (this._queueStack.pop(), this._getCurrentQueue()) : queue.tasks; } }, { key: "_genPromise", value: function(task) { var _this = this, length = this._queueStack.push({ tasks: [], popable: !1 }), stackIdx = length - 1, stackItem = this._queueStack[stackIdx]; task.gen().then(function() { stackItem.popable = !0, _this.hasTasksToProcess() && _this._onMoreTasks(); }).catch(function(ex) { setTimeout(function() { throw ex instanceof Error && (ex.message = `TaskQueue: Error resolving Promise in task ${task.name}: ${ex.message}`), ex; }, 0); }); } } ]), TaskQueue2; }(), TaskQueue_default = TaskQueue; //# sourceMappingURL=TaskQueue.js.map