UNPKG

@tamagui/react-native-web-lite

Version:
85 lines (84 loc) 3.16 kB
import { invariant } from "@tamagui/react-native-web-internals"; import EventEmitter from "../vendor/react-native/emitter/_EventEmitter.native.js"; import TaskQueue from "./TaskQueue.native.js"; import requestIdleCallback from "../modules/requestIdleCallback.native.js"; var _emitter = new EventEmitter(), InteractionManager = { Events: { interactionStart: "interactionStart", interactionComplete: "interactionComplete" }, /** * Schedule a function to run after all interactions have completed. */ runAfterInteractions(task) { var tasks = [], promise = new Promise(function (resolve) { _scheduleUpdate(), task && tasks.push(task), tasks.push({ run: resolve, name: "resolve " + (task && task.name || "?") }), _taskQueue.enqueueTasks(tasks); }); return { then: promise.then.bind(promise), done: promise.then.bind(promise), cancel: function () { _taskQueue.cancelTasks(tasks); } }; }, /** * Notify manager that an interaction has started. */ createInteractionHandle() { _scheduleUpdate(); var handle = ++_inc; return _addInteractionSet.add(handle), handle; }, /** * Notify manager that an interaction has completed. */ clearInteractionHandle(handle) { invariant(!!handle, "Must provide a handle to clear."), _scheduleUpdate(), _addInteractionSet.delete(handle), _deleteInteractionSet.add(handle); }, addListener: _emitter.addListener.bind(_emitter), /** * * @param deadline */ setDeadline(deadline) { _deadline = deadline; } }, _interactionSet = /* @__PURE__ */new Set(), _addInteractionSet = /* @__PURE__ */new Set(), _deleteInteractionSet = /* @__PURE__ */new Set(), _taskQueue = new TaskQueue({ onMoreTasks: _scheduleUpdate }), _nextUpdateHandle = null, _inc = 0, _deadline = -1; function _scheduleUpdate() { _nextUpdateHandle || (_deadline > 0 ? _nextUpdateHandle = setTimeout(_processUpdate) : _nextUpdateHandle = requestIdleCallback(_processUpdate)); } function _processUpdate() { _nextUpdateHandle = null; var interactionCount = _interactionSet.size; _addInteractionSet.forEach(function (handle) { return _interactionSet.add(handle); }), _deleteInteractionSet.forEach(function (handle) { return _interactionSet.delete(handle); }); var nextInteractionCount = _interactionSet.size; if (interactionCount !== 0 && nextInteractionCount === 0 ? _emitter.emit(InteractionManager.Events.interactionComplete) : interactionCount === 0 && nextInteractionCount !== 0 && _emitter.emit(InteractionManager.Events.interactionStart), nextInteractionCount === 0) { for (var begin = Date.now(); _taskQueue.hasTasksToProcess();) if (_taskQueue.processNext(), _deadline > 0 && Date.now() - begin >= _deadline) { _scheduleUpdate(); break; } } _addInteractionSet.clear(), _deleteInteractionSet.clear(); } var InteractionManager_default = InteractionManager; export { InteractionManager_default as default }; //# sourceMappingURL=index.native.js.map