CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
progressEventReducer.js45 linesDownload Raw Back to helpers
1import speedometer from "./speedometer.js";2import throttle from "./throttle.js";3import utils from "../utils.js";4 5export const progressEventReducer = (listener, isDownloadStream, freq = 3) => {6  let bytesNotified = 0;7  const _speedometer = speedometer(50, 250);8 9  return throttle(e => {10    const loaded = e.loaded;11    const total = e.lengthComputable ? e.total : undefined;12    const progressBytes = loaded - bytesNotified;13    const rate = _speedometer(progressBytes);14    const inRange = loaded <= total;15 16    bytesNotified = loaded;17 18    const data = {19      loaded,20      total,21      progress: total ? (loaded / total) : undefined,22      bytes: progressBytes,23      rate: rate ? rate : undefined,24      estimated: rate && total && inRange ? (total - loaded) / rate : undefined,25      event: e,26      lengthComputable: total != null,27      [isDownloadStream ? 'download' : 'upload']: true28    };29 30    listener(data);31  }, freq);32}33 34export const progressEventDecorator = (total, throttled) => {35  const lengthComputable = total != null;36 37  return [(loaded) => throttled[0]({38    lengthComputable,39    total,40    loaded41  }), throttled[1]];42}43 44export const asyncDecorator = (fn) => (...args) => utils.asap(() => fn(...args));45