UNPKG

@tamagui/react-native-web-lite

Version:
113 lines (111 loc) 4.06 kB
import { invariant } from "@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() : (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?.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; export { TaskQueue_default as default }; //# sourceMappingURL=TaskQueue.native.js.map