| 1776 | * @category scheduler |
| 1777 | */ |
| 1778 | export class MicroSchedulerDefault implements MicroScheduler { |
| 1779 | private tasks: Array<() => void> = [] |
| 1780 | private running = false |
| 1781 | |
| 1782 | /** |
| 1783 | * @since 3.5.9 |
| 1784 | */ |
| 1785 | scheduleTask(task: () => void, _priority: number) { |
| 1786 | this.tasks.push(task) |
| 1787 | if (!this.running) { |
| 1788 | this.running = true |
| 1789 | setImmediate(this.afterScheduled) |
| 1790 | } |
| 1791 | } |
| 1792 | |
| 1793 | /** |
| 1794 | * @since 3.5.9 |
| 1795 | */ |
| 1796 | afterScheduled = () => { |
| 1797 | this.running = false |
| 1798 | this.runTasks() |
| 1799 | } |
| 1800 | |
| 1801 | /** |
| 1802 | * @since 3.5.9 |
| 1803 | */ |
| 1804 | runTasks() { |
| 1805 | const tasks = this.tasks |
| 1806 | this.tasks = [] |
| 1807 | for (let i = 0, len = tasks.length; i < len; i++) { |
| 1808 | tasks[i]() |
| 1809 | } |
| 1810 | } |
| 1811 | |
| 1812 | /** |
| 1813 | * @since 3.5.9 |
| 1814 | */ |
| 1815 | shouldYield(fiber: MicroFiber<unknown, unknown>) { |
| 1816 | return fiber.currentOpCount >= fiber.getRef(MaxOpsBeforeYield) |
| 1817 | } |
| 1818 | |
| 1819 | /** |
| 1820 | * @since 3.5.9 |
| 1821 | */ |
| 1822 | flush() { |
| 1823 | while (this.tasks.length > 0) { |
| 1824 | this.runTasks() |
| 1825 | } |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | /** |
| 1830 | * Access the given `Context.Tag` from the environment. |