@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
62 lines (61 loc) • 1.32 kB
JavaScript
// packages/editor/src/components/upload-progress-snackbar/tracker.js
import { useSyncExternalStore } from "@wordpress/element";
var state = null;
var listeners = /* @__PURE__ */ new Set();
function notify() {
listeners.forEach((listener) => listener());
}
function addFiles(filenames) {
if (!filenames.length) {
return;
}
if (!state) {
state = { total: 0, completed: 0, pending: [] };
}
state = {
total: state.total + filenames.length,
completed: state.completed,
pending: [...state.pending, ...filenames]
};
notify();
}
function advance(count) {
if (!state || count <= 0) {
return;
}
const completed = Math.min(state.total, state.completed + count);
const pending = state.pending.slice(count);
if (completed >= state.total) {
state = null;
} else {
state = { total: state.total, completed, pending };
}
notify();
}
function reset() {
if (state === null) {
return;
}
state = null;
notify();
}
function getState() {
return state;
}
function subscribe(listener) {
listeners.add(listener);
return () => {
listeners.delete(listener);
};
}
function useTracker() {
return useSyncExternalStore(subscribe, getState, getState);
}
export {
addFiles,
advance,
getState,
reset,
useTracker
};
//# sourceMappingURL=tracker.mjs.map