UNPKG

@rpldy/uploader

Version:

the processing and queuing engine for react-uploady

65 lines (64 loc) 2.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.FILE_STATE_TO_EVENT_MAP = void 0; var _shared = require("@rpldy/shared"); var _consts = require("../consts"); var _batchHelpers = require("./batchHelpers"); var _itemHelpers = require("./itemHelpers"); const FILE_STATE_TO_EVENT_MAP = exports.FILE_STATE_TO_EVENT_MAP = { [_shared.FILE_STATES.PENDING.valueOf()]: null, [_shared.FILE_STATES.ADDED.valueOf()]: _consts.UPLOADER_EVENTS.ITEM_START, [_shared.FILE_STATES.FINISHED.valueOf()]: _consts.UPLOADER_EVENTS.ITEM_FINISH, [_shared.FILE_STATES.ERROR.valueOf()]: _consts.UPLOADER_EVENTS.ITEM_ERROR, [_shared.FILE_STATES.CANCELLED.valueOf()]: _consts.UPLOADER_EVENTS.ITEM_CANCEL, [_shared.FILE_STATES.ABORTED.valueOf()]: _consts.UPLOADER_EVENTS.ITEM_ABORT, [_shared.FILE_STATES.UPLOADING.valueOf()]: _consts.UPLOADER_EVENTS.ITEM_PROGRESS }; const getIsFinalized = item => !!~_consts.ITEM_FINALIZE_STATES.indexOf(item.state); const processFinishedRequest = (queue, finishedData, next) => { finishedData.forEach(itemData => { const state = queue.getState(); const { id, info } = itemData; _shared.logger.debugLog("uploader.processor.queue: request finished for item - ", { id, info }); if (state.items[id]) { queue.updateState(state => { const item = state.items[id]; item.state = info.state; item.uploadResponse = info.response; item.uploadStatus = info.status; if (getIsFinalized(item)) { delete state.aborts[id]; } }); const item = queue.getState().items[id]; if (info.state === _shared.FILE_STATES.FINISHED && item.completed < 100) { const size = item.file?.size || 0; queue.handleItemProgress(item, 100, size, size); } const { itemBatchOptions } = (0, _batchHelpers.getBatchDataFromItemId)(queue, id); const batchOptions = itemBatchOptions[id]; const itemState = item.state.valueOf(); if (FILE_STATE_TO_EVENT_MAP[itemState]) { queue.trigger(FILE_STATE_TO_EVENT_MAP[itemState], item, batchOptions); } if (getIsFinalized(item)) { (0, _batchHelpers.incrementBatchFinishedCounter)(queue, item.batchId); queue.trigger(_consts.UPLOADER_EVENTS.ITEM_FINALIZE, item, batchOptions); } } (0, _itemHelpers.finalizeItem)(queue, id); }); (0, _batchHelpers.cleanUpFinishedBatches)(queue); next(queue); }; var _default = exports.default = processFinishedRequest;