UNPKG

@tamagui/react-native-web-lite

Version:
82 lines (81 loc) 3.1 kB
import { invariant } from "@tamagui/react-native-web-internals"; import EventEmitter from "../vendor/react-native/emitter/_EventEmitter.mjs"; import TaskQueue from "./TaskQueue.mjs"; import requestIdleCallback from "../modules/requestIdleCallback.mjs"; const _emitter = new EventEmitter(), InteractionManager = { Events: { interactionStart: "interactionStart", interactionComplete: "interactionComplete" }, /** * Schedule a function to run after all interactions have completed. */ runAfterInteractions(task) { const tasks = [], promise = new Promise(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: () => { _taskQueue.cancelTasks(tasks); } }; }, /** * Notify manager that an interaction has started. */ createInteractionHandle() { _scheduleUpdate(); const 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 }); let _nextUpdateHandle = null, _inc = 0, _deadline = -1; function _scheduleUpdate() { _nextUpdateHandle || (_deadline > 0 ? _nextUpdateHandle = setTimeout(_processUpdate) : _nextUpdateHandle = requestIdleCallback(_processUpdate)); } function _processUpdate() { _nextUpdateHandle = null; const interactionCount = _interactionSet.size; _addInteractionSet.forEach(handle => _interactionSet.add(handle)), _deleteInteractionSet.forEach(handle => _interactionSet.delete(handle)); const nextInteractionCount = _interactionSet.size; if (interactionCount !== 0 && nextInteractionCount === 0 ? _emitter.emit(InteractionManager.Events.interactionComplete) : interactionCount === 0 && nextInteractionCount !== 0 && _emitter.emit(InteractionManager.Events.interactionStart), nextInteractionCount === 0) { const begin = Date.now(); for (; _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.mjs.map