devextreme
Version:
JavaScript/TypeScript Component Suite for Responsive Web Development
347 lines (330 loc) • 12.5 kB
JavaScript
/**
* DevExtreme (cjs/__internal/data/custom_store.js)
* Version: 26.1.6
* Build date: Fri Oct 16 2026
*
* Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED
* Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
*/
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
exports.isGroupItem = isGroupItem;
exports.isGroupItemsArray = isGroupItemsArray;
exports.isItemsArray = isItemsArray;
exports.isLoadResultObject = isLoadResultObject;
var _array_query = _interopRequireDefault(require("../../common/data/array_query"));
var _array_utils = require("../../common/data/array_utils");
var _errors = require("../../common/data/errors");
var _store_helper = _interopRequireDefault(require("../../common/data/store_helper"));
var _utils = require("../../common/data/utils");
var _config = _interopRequireDefault(require("../../core/config"));
var _deferred = require("../../core/utils/deferred");
var _type = require("../../core/utils/type");
var _abstract_store = _interopRequireDefault(require("../data/abstract_store"));
function _interopRequireDefault(e) {
return e && e.__esModule ? e : {
default: e
}
}
const TOTAL_COUNT = "totalCount";
const LOAD = "load";
const BY_KEY = "byKey";
const INSERT = "insert";
const UPDATE = "update";
const REMOVE = "remove";
function isPromise(obj) {
if ("object" !== typeof obj && "function" !== typeof obj) {
return false
}
return null !== obj && "then" in obj && (0, _type.isFunction)(obj.then)
}
function trivialPromise(value) {
return (0, _deferred.Deferred)().resolve(value).promise()
}
function ensureRequiredFuncOption(name, obj) {
if (!(0, _type.isFunction)(obj)) {
throw _errors.errors.Error("E4011", name)
}
}
function throwInvalidUserFuncResult(name) {
throw _errors.errors.Error("E4012", name)
}
function createUserFuncFailureHandler(pendingDeferred) {
return function() {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key]
}
const [arg] = args;
const argMessage = arg ? String(arg) : "";
const message = function(promiseArguments) {
const [xhr, textStatus] = promiseArguments;
if ("object" !== typeof xhr || null === xhr || !("getResponseHeader" in xhr) || !xhr.getResponseHeader) {
return null
}
const message = (0, _utils.errorMessageFromXhr)(xhr, textStatus);
return message
}(args) || argMessage || "Unknown error";
const error = arg instanceof Error ? arg : new Error(message);
if (error.message !== _utils.XHR_ERROR_UNLOAD) {
pendingDeferred.reject(error)
}
}
}
function invokeUserLoad(store, options) {
const userFunc = store._loadFunc;
ensureRequiredFuncOption(LOAD, userFunc);
let userResult = userFunc.apply(store, [options]);
if (Array.isArray(userResult)) {
userResult = trivialPromise(userResult)
} else if (null === userResult || void 0 === userResult) {
userResult = trivialPromise([])
} else if (!isPromise(userResult)) {
throwInvalidUserFuncResult(LOAD)
}
const result = (0, _deferred.fromPromise)(userResult);
return result
}
function invokeUserTotalCountFunc(store, options) {
const userFunc = store._totalCountFunc;
if (!(0, _type.isFunction)(userFunc)) {
throw _errors.errors.Error("E4021")
}
let userResult = userFunc.apply(store, [options]);
if (!isPromise(userResult)) {
const count = Number(userResult);
if (!isFinite(count)) {
throwInvalidUserFuncResult(TOTAL_COUNT)
}
userResult = trivialPromise(count)
}
const result = (0, _deferred.fromPromise)(userResult);
return result
}
function invokeUserByKeyFunc(store, key, extraOptions) {
const userFunc = store._byKeyFunc;
ensureRequiredFuncOption(BY_KEY, userFunc);
let userResult = userFunc.apply(store, [key, extraOptions]);
if (!isPromise(userResult)) {
userResult = trivialPromise(userResult)
}
const result = (0, _deferred.fromPromise)(userResult);
return result
}
function runRawLoad(pendingDeferred, store, userFuncOptions, continuation) {
if (store.__rawData) {
continuation(store.__rawData)
} else {
const loadPromise = store.__rawDataPromise ?? invokeUserLoad(store, userFuncOptions);
if (store._cacheRawData) {
store.__rawDataPromise = loadPromise
}
loadPromise.always(() => {
delete store.__rawDataPromise
}).done(rawData => {
if (store._cacheRawData) {
store.__rawData = rawData
}
continuation(rawData)
}).fail(error => {
var _store$_errorHandler;
const userFuncFailureHandler = createUserFuncFailureHandler(pendingDeferred);
null === (_store$_errorHandler = store._errorHandler) || void 0 === _store$_errorHandler || _store$_errorHandler.call(store, error);
userFuncFailureHandler(error)
})
}
}
function runRawLoadWithQuery(pendingDeferred, store, options, countOnly) {
const loadOptions = options || {};
const userFuncOptions = {};
if ("userData" in loadOptions) {
userFuncOptions.userData = loadOptions.userData
}
runRawLoad(pendingDeferred, store, userFuncOptions, rawData => {
const rawDataQuery = (0, _array_query.default)(rawData, {
errorHandler: store._errorHandler
});
const waitList = [];
const result = {};
if (!countOnly) {
const itemsQuery = _store_helper.default.queryByOptions(rawDataQuery, loadOptions, false);
if (itemsQuery === rawDataQuery) {
result.items = rawData.slice(0)
} else {
const itemsPromise = itemsQuery.enumerate().done(asyncResult => {
result.items = asyncResult
});
waitList.push(itemsPromise)
}
}
if (loadOptions.requireTotalCount || countOnly) {
const totalCountQuery = _store_helper.default.queryByOptions(rawDataQuery, loadOptions, true);
if (totalCountQuery === rawDataQuery) {
result.totalCount = rawData.length
} else {
const totalCountPromise = totalCountQuery.count().done(asyncResult => {
result.totalCount = asyncResult
});
waitList.push(totalCountPromise)
}
}(0, _deferred.when)(...waitList).done(() => {
if (countOnly) {
pendingDeferred.resolve(result.totalCount)
} else if (loadOptions.requireTotalCount) {
pendingDeferred.resolve(result.items, {
totalCount: result.totalCount
})
} else {
pendingDeferred.resolve(result.items)
}
}).fail(x => {
pendingDeferred.reject(x)
})
})
}
function runRawLoadWithKey(pendingDeferred, store, key) {
runRawLoad(pendingDeferred, store, {}, rawData => {
const keyExpr = store.key();
for (const item of rawData) {
if ((0, _utils.keysEqual)(keyExpr, store.keyOf(item), key)) {
pendingDeferred.resolve(item);
return
}
}
pendingDeferred.reject(_errors.errors.Error("E4009"))
})
}
function isGroupItem(item) {
if (void 0 === item || null === item || "object" !== typeof item) {
return false
}
return "key" in item && "items" in item
}
function isLoadResultObject(res) {
return !Array.isArray(res) && "object" === typeof res && null !== res && "data" in res
}
function isGroupItemsArray(res) {
return Array.isArray(res) && !!res.length && isGroupItem(res[0])
}
function isItemsArray(res) {
return Array.isArray(res) && !isGroupItem(res[0])
}
class CustomStore extends _abstract_store.default {
constructor(options) {
const storeOptions = options || {};
super(storeOptions);
this._useDefaultSearch = !!storeOptions.useDefaultSearch || "raw" === storeOptions.loadMode;
this._loadMode = storeOptions.loadMode;
this._cacheRawData = false !== storeOptions.cacheRawData;
this._loadFunc = storeOptions[LOAD];
this._totalCountFunc = storeOptions[TOTAL_COUNT];
this._byKeyFunc = storeOptions[BY_KEY];
this._insertFunc = storeOptions[INSERT];
this._updateFunc = storeOptions[UPDATE];
this._removeFunc = storeOptions[REMOVE]
}
_clearCache() {
delete this.__rawData
}
createQuery() {
throw _errors.errors.Error("E4010")
}
clearRawDataCache() {
this._clearCache()
}
_totalCountImpl(options) {
let d = (0, _deferred.Deferred)();
if ("raw" === this._loadMode && !this._totalCountFunc) {
runRawLoadWithQuery(d, this, options, true)
} else {
invokeUserTotalCountFunc(this, options).done(count => {
d.resolve(Number(count))
}).fail(createUserFuncFailureHandler(d));
d = this._addFailHandlers(d)
}
return d.promise()
}
_pushImpl(changes) {
if (this.__rawData) {
(0, _array_utils.applyBatch)({
keyInfo: this,
data: this.__rawData,
changes: changes
})
}
}
_loadImpl(options) {
let d = (0, _deferred.Deferred)();
if ("raw" === this._loadMode) {
runRawLoadWithQuery(d, this, options, false)
} else {
invokeUserLoad(this, options).done((data, extra) => {
d.resolve(data, extra)
}).fail(createUserFuncFailureHandler(d));
d = this._addFailHandlers(d)
}
return d.promise()
}
_byKeyImpl(key, extraOptions) {
const d = (0, _deferred.Deferred)();
if (this._byKeyViaLoad()) {
this._requireKey();
runRawLoadWithKey(d, this, key)
} else {
invokeUserByKeyFunc(this, key, extraOptions).done(obj => {
d.resolve(obj)
}).fail(createUserFuncFailureHandler(d))
}
return d.promise()
}
_byKeyViaLoad() {
return "raw" === this._loadMode && !this._byKeyFunc
}
_insertImpl(values) {
const userFunc = this._insertFunc;
const d = (0, _deferred.Deferred)();
ensureRequiredFuncOption(INSERT, userFunc);
let userResult = userFunc.apply(this, [values]);
if (!isPromise(userResult)) {
userResult = trivialPromise(userResult)
}(0, _deferred.fromPromise)(userResult).done(serverResponse => {
if ((0, _config.default)().useLegacyStoreResult) {
d.resolve(values, serverResponse)
} else {
d.resolve(serverResponse || values, this.keyOf(serverResponse))
}
}).fail(createUserFuncFailureHandler(d));
return d.promise()
}
_updateImpl(key, values) {
const userFunc = this._updateFunc;
const d = (0, _deferred.Deferred)();
ensureRequiredFuncOption(UPDATE, userFunc);
let userResult = userFunc.apply(this, [key, values]);
if (!isPromise(userResult)) {
userResult = trivialPromise(userResult)
}(0, _deferred.fromPromise)(userResult).done(serverResponse => {
if ((0, _config.default)().useLegacyStoreResult) {
d.resolve(key, values)
} else {
d.resolve(serverResponse || values, key)
}
}).fail(createUserFuncFailureHandler(d));
return d.promise()
}
_removeImpl(key) {
const userFunc = this._removeFunc;
const d = (0, _deferred.Deferred)();
ensureRequiredFuncOption(REMOVE, userFunc);
let userResult = userFunc.apply(this, [key]);
if (!isPromise(userResult)) {
userResult = trivialPromise()
}(0, _deferred.fromPromise)(userResult).done(() => {
d.resolve(key)
}).fail(createUserFuncFailureHandler(d));
return d.promise()
}
}
var _default = exports.default = CustomStore;