UNPKG

vite-plugin-react18-pages

Version:

<p> <a href="https://www.npmjs.com/package/vite-plugin-react-pages" target="_blank" rel="noopener"><img src="https://img.shields.io/npm/v/vite-plugin-react-pages.svg" alt="npm package" /></a> </p>

87 lines 3.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PageUpdateBuffer = void 0; const events_1 = require("events"); const mini_debounce_1 = require("mini-debounce"); /** * Buffer page data updates. * Can flush a batch of updates together * and cancle unnecessary updates */ class PageUpdateBuffer extends events_1.EventEmitter { constructor() { super(); /** * which pages should be updated */ this.pageUpdateBuffer = new Set(); /** * whether the page list should be updated */ this.pageListUpdateBuffer = false; this.scheduleFlush = (0, mini_debounce_1.debounce)(() => { let havePageUpdate = false; if (this.pageUpdateBuffer.size > 0) { havePageUpdate = true; const updates = [...this.pageUpdateBuffer.values()]; this.emit('page', updates); this.pageUpdateBuffer.clear(); } if (this.pageListUpdateBuffer) { // if we have just sent a page update, // we don't need to trigger page list update. // because during the page update hmr, the page list will automatically get updated // (because the whole import chain will get re-imported) if (!havePageUpdate) this.emit('page-list'); this.pageListUpdateBuffer = false; } }, 100); } scheduleUpdate(update) { switch (update.type) { case 'add': this.pageListUpdateBuffer = true; break; case 'update': if (update.dataType === 'static') this.pageListUpdateBuffer = true; else this.pageUpdateBuffer.add(update.pageId); break; case 'delete': this.pageListUpdateBuffer = true; this.pageUpdateBuffer.delete(update.pageId); break; default: throw new Error(`invalid update type ${JSON.stringify(update)}`); } this.scheduleFlush(); } async batchUpdate(exec) { let updates = []; const _this = this; try { await exec(scheduleUpdate); } finally { updates.forEach((update) => { _this.scheduleUpdate(update); }); updates = null; this.scheduleFlush(); } function scheduleUpdate(update) { if (!updates) { // the batch lifetime has already expired // add it to buffer directly _this.scheduleUpdate(update); return; } // store it, will flush these updates together later updates.push(update); } } } exports.PageUpdateBuffer = PageUpdateBuffer; //# sourceMappingURL=UpdateBuffer.js.map