UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

189 lines (188 loc) • 6.27 kB
/** * DevExtreme (esm/__internal/data/abstract_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/ */ import { errors, handleError } from "../../common/data/errors"; import storeHelper from "../../common/data/store_helper"; import { processRequestResultLock } from "../../common/data/utils"; import { EventsStrategy } from "../../core/events_strategy"; import { compileGetter } from "../../core/utils/data"; import { Deferred, when } from "../../core/utils/deferred"; import { isEmptyObject } from "../../core/utils/type"; const { queryByOptions: queryByOptions } = storeHelper; const EVENT_OPTION_NAMES = ["onLoaded", "onLoading", "onInserted", "onInserting", "onUpdated", "onUpdating", "onPush", "onRemoved", "onRemoving", "onModified", "onModifying"]; const storeImpl = {}; class Store { constructor(options) { this._langParams = {}; const storeOptions = options || {}; this._eventsStrategy = new EventsStrategy(this); EVENT_OPTION_NAMES.forEach(optionName => { const handler = storeOptions[optionName]; if (handler) { this.on(optionName.slice(2).toLowerCase(), handler) } }); this._key = storeOptions.key; this._errorHandler = storeOptions.errorHandler; this._useDefaultSearch = true } static create(alias, options) { if (!(alias in storeImpl)) { throw errors.Error("E4020", alias) } return new storeImpl[alias](options) } static registerClass(type, alias) { if (alias) { storeImpl[alias] = type } return type } _clearCache() {} _customLoadOptions() { return null } key() { return this._key } keyOf(obj) { const keyGetter = this._keyGetter ?? compileGetter(this.key()); this._keyGetter = keyGetter; const result = keyGetter(obj); return result } _requireKey() { if (!this.key()) { throw errors.Error("E4005") } } load(options) { const loadOptions = options || {}; this._eventsStrategy.fireEvent("loading", [loadOptions]); return this._withLock(this._loadImpl(loadOptions)).done(result => { this._eventsStrategy.fireEvent("loaded", [result, loadOptions]) }) } _loadImpl(options) { const loadOptions = options || {}; if (!isEmptyObject(this._langParams)) { loadOptions._langParams = Object.assign({}, this._langParams, loadOptions._langParams) } const result = queryByOptions(this.createQuery(loadOptions), loadOptions, false).enumerate(); return result } _withLock(task) { const result = Deferred(); task.done(function() { for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key] } processRequestResultLock.promise().done(() => { result.resolveWith(this, args) }) }).fail(function() { for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { args[_key2] = arguments[_key2] } result.rejectWith(this, args) }); return result } createQuery(options) { throw errors.Error("E0001") } totalCount(options) { return this._totalCountImpl(options) } _totalCountImpl(options) { const result = queryByOptions(this.createQuery(options), options, true).count(); return result } byKey(key, extraOptions) { return this._addFailHandlers(this._withLock(this._byKeyImpl(key, extraOptions))) } _byKeyImpl(key, extraOptions) { throw errors.Error("E0001") } insert(values) { this._eventsStrategy.fireEvent("modifying", []); this._eventsStrategy.fireEvent("inserting", [values]); return this._addFailHandlers(this._insertImpl(values).done((callbackValues, callbackKey) => { this._eventsStrategy.fireEvent("inserted", [callbackValues, callbackKey]); this._eventsStrategy.fireEvent("modified", []) })) } _insertImpl(values) { throw errors.Error("E0001") } update(key, values) { this._eventsStrategy.fireEvent("modifying", []); this._eventsStrategy.fireEvent("updating", [key, values]); return this._addFailHandlers(this._updateImpl(key, values).done(() => { this._eventsStrategy.fireEvent("updated", [key, values]); this._eventsStrategy.fireEvent("modified", []) })) } _updateImpl(key, values) { throw errors.Error("E0001") } push(changes) { const beforePushArgs = { changes: changes, waitFor: [] }; this._eventsStrategy.fireEvent("beforePushAggregation", [beforePushArgs]); when(...beforePushArgs.waitFor).done(() => { this._pushImpl(changes); this._eventsStrategy.fireEvent("beforePush", [{ changes: changes }]); this._eventsStrategy.fireEvent("push", [changes]) }) } _pushImpl(changes) {} remove(key) { this._eventsStrategy.fireEvent("modifying", []); this._eventsStrategy.fireEvent("removing", [key]); return this._addFailHandlers(this._removeImpl(key).done(callbackKey => { this._eventsStrategy.fireEvent("removed", [callbackKey]); this._eventsStrategy.fireEvent("modified", []) })) } _removeImpl(key) { throw errors.Error("E0001") } _addFailHandlers(deferred) { return deferred.fail(this._errorHandler).fail(handleError) } on(eventName, eventHandler) { this._eventsStrategy.on(eventName, eventHandler); return this } off(eventName, eventHandler) { this._eventsStrategy.off(eventName, eventHandler); return this } } export default Store;