UNPKG

react-antd-admin-panel

Version:

Modern TypeScript-first React admin panel builder with Ant Design 6

148 lines (147 loc) 3.77 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); class Loader { constructor() { __publicField(this, "_requests", []); __publicField(this, "_hooks", {}); __publicField(this, "_parallel", true); __publicField(this, "_abortOnError", false); } /** * Add a request to the loader */ add(key, request, options) { this._requests.push({ key, request, required: (options == null ? void 0 : options.required) ?? false, condition: options == null ? void 0 : options.condition }); return this; } /** * Set whether requests execute in parallel (default) or sequentially */ parallel(value = true) { this._parallel = value; return this; } /** * Set whether to abort on first error (default: false) */ abortOnError(value = true) { this._abortOnError = value; return this; } /** * Hook called before any requests are made */ onBeforeLoad(callback) { this._hooks.onBeforeLoad = callback; return this; } /** * Hook called after all requests complete successfully */ onLoad(callback) { this._hooks.onLoad = callback; return this; } /** * Hook called after onLoad (useful for side effects) */ onAfterLoad(callback) { this._hooks.onAfterLoad = callback; return this; } /** * Hook called if any request fails */ onError(callback) { this._hooks.onError = callback; return this; } /** * Execute all requests with lifecycle management */ async execute() { const data = {}; try { if (this._hooks.onBeforeLoad) { await this._hooks.onBeforeLoad(); } if (this._parallel) { await this._executeParallel(data); } else { await this._executeSequential(data); } if (this._hooks.onLoad) { await this._hooks.onLoad(data); } if (this._hooks.onAfterLoad) { await this._hooks.onAfterLoad(data); } return data; } catch (error) { if (this._hooks.onError) { await this._hooks.onError(error); } throw error; } } /** * Execute requests in parallel */ async _executeParallel(data) { const promises = this._requests.map(async (req) => { if (req.condition && !req.condition(data)) { return; } try { const result = await req.request.execute(); data[req.key] = result; } catch (error) { if (req.required || this._abortOnError) { throw error; } data[req.key] = null; } }); await Promise.all(promises); } /** * Execute requests sequentially (allows conditions to depend on previous results) */ async _executeSequential(data) { for (const req of this._requests) { if (req.condition && !req.condition(data)) { continue; } try { const result = await req.request.execute(); data[req.key] = result; } catch (error) { if (req.required || this._abortOnError) { throw error; } data[req.key] = null; } } } /** * Create a loader from a configuration object */ static from(config) { const loader = new Loader(); loader._requests = config.requests; loader._hooks = config.hooks || {}; loader._parallel = config.parallel ?? true; loader._abortOnError = config.abortOnError ?? false; return loader; } } export { Loader as L }; //# sourceMappingURL=Loader-DiwrlZ4F.js.map