UNPKG

@tamagui/react-native-web-lite

Version:
106 lines (105 loc) 3.12 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(); const InteractionManager = { Events: { interactionStart: "interactionStart", interactionComplete: "interactionComplete" }, /** * Schedule a function to run after all interactions have completed. */ runAfterInteractions(task) { const tasks = []; const promise = new Promise(resolve => { _scheduleUpdate(); if (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; _addInteractionSet.add(handle); return 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; } }; const _interactionSet = /* @__PURE__ */new Set(); const _addInteractionSet = /* @__PURE__ */new Set(); const _deleteInteractionSet = /* @__PURE__ */new Set(); const _taskQueue = new TaskQueue({ onMoreTasks: _scheduleUpdate }); let _nextUpdateHandle = null; let _inc = 0; let _deadline = -1; function _scheduleUpdate() { if (!_nextUpdateHandle) { if (_deadline > 0) { _nextUpdateHandle = setTimeout(_processUpdate); } else { _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); } else if (interactionCount === 0 && nextInteractionCount !== 0) { _emitter.emit(InteractionManager.Events.interactionStart); } if (nextInteractionCount === 0) { const begin = Date.now(); while (_taskQueue.hasTasksToProcess()) { _taskQueue.processNext(); if (_deadline > 0 && Date.now() - begin >= _deadline) { _scheduleUpdate(); break; } } } _addInteractionSet.clear(); _deleteInteractionSet.clear(); } export { InteractionManager }; //# sourceMappingURL=index.mjs.map