@qlover/fe-corekit 2.1.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +6542 -1
- package/dist/index.d.ts +1073 -14
- package/dist/index.iife.js +6519 -1
- package/dist/index.iife.min.js +1 -0
- package/dist/index.js +624 -36
- package/package.json +4 -2
package/dist/index.js
CHANGED
|
@@ -3870,6 +3870,609 @@ var SyncExecutor = /*#__PURE__*/ function(Executor) {
|
|
|
3870
3870
|
]);
|
|
3871
3871
|
return SyncExecutor;
|
|
3872
3872
|
}(Executor);
|
|
3873
|
+
// src/executor/plugins/AbortPlugin.ts
|
|
3874
|
+
var ABORT_ERROR_ID = "ABORT_ERROR";
|
|
3875
|
+
var AbortError = /*#__PURE__*/ function(ExecutorError) {
|
|
3876
|
+
"use strict";
|
|
3877
|
+
_inherits(AbortError, ExecutorError);
|
|
3878
|
+
var _super = _create_super(AbortError);
|
|
3879
|
+
function AbortError(message, abortId, timeout) {
|
|
3880
|
+
_class_call_check(this, AbortError);
|
|
3881
|
+
var _this;
|
|
3882
|
+
_this = _super.call(this, ABORT_ERROR_ID, message);
|
|
3883
|
+
_this.abortId = abortId;
|
|
3884
|
+
_this.timeout = timeout;
|
|
3885
|
+
return _this;
|
|
3886
|
+
}
|
|
3887
|
+
_create_class(AbortError, [
|
|
3888
|
+
{
|
|
3889
|
+
/**
|
|
3890
|
+
* Checks if the abort was triggered by a timeout
|
|
3891
|
+
*
|
|
3892
|
+
* Useful for distinguishing between manual cancellations and timeout-based aborts,
|
|
3893
|
+
* enabling different error handling strategies
|
|
3894
|
+
*
|
|
3895
|
+
* @returns `true` if abort was caused by timeout, `false` otherwise
|
|
3896
|
+
*
|
|
3897
|
+
* @example
|
|
3898
|
+
* ```typescript
|
|
3899
|
+
* try {
|
|
3900
|
+
* await fetchData();
|
|
3901
|
+
* } catch (error) {
|
|
3902
|
+
* if (error instanceof AbortError && error.isTimeout()) {
|
|
3903
|
+
* console.log('Request timed out, please try again');
|
|
3904
|
+
* } else {
|
|
3905
|
+
* console.log('Request was cancelled');
|
|
3906
|
+
* }
|
|
3907
|
+
* }
|
|
3908
|
+
* ```
|
|
3909
|
+
*/ key: "isTimeout",
|
|
3910
|
+
value: function isTimeout() {
|
|
3911
|
+
return this.timeout !== void 0 && this.timeout > 0;
|
|
3912
|
+
}
|
|
3913
|
+
},
|
|
3914
|
+
{
|
|
3915
|
+
/**
|
|
3916
|
+
* Generates a user-friendly error description with context
|
|
3917
|
+
*
|
|
3918
|
+
* Combines the error message with additional context like request ID and timeout duration
|
|
3919
|
+
* Provides more informative error messages for debugging and user feedback
|
|
3920
|
+
*
|
|
3921
|
+
* @returns Formatted error description string
|
|
3922
|
+
*
|
|
3923
|
+
* @example Without context
|
|
3924
|
+
* ```typescript
|
|
3925
|
+
* const error = new AbortError('Operation aborted');
|
|
3926
|
+
* error.getDescription(); // "Operation aborted"
|
|
3927
|
+
* ```
|
|
3928
|
+
*
|
|
3929
|
+
* @example With request ID
|
|
3930
|
+
* ```typescript
|
|
3931
|
+
* const error = new AbortError('Operation aborted', 'req-123');
|
|
3932
|
+
* error.getDescription(); // "Operation aborted (Request: req-123)"
|
|
3933
|
+
* ```
|
|
3934
|
+
*
|
|
3935
|
+
* @example With timeout context
|
|
3936
|
+
* ```typescript
|
|
3937
|
+
* const error = new AbortError('Timeout', 'req-456', 5000);
|
|
3938
|
+
* error.getDescription(); // "Timeout (Request: req-456, Timeout: 5000ms)"
|
|
3939
|
+
* ```
|
|
3940
|
+
*/ key: "getDescription",
|
|
3941
|
+
value: function getDescription() {
|
|
3942
|
+
var parts = [];
|
|
3943
|
+
if (this.abortId) {
|
|
3944
|
+
parts.push("Request: ".concat(this.abortId));
|
|
3945
|
+
}
|
|
3946
|
+
if (this.isTimeout()) {
|
|
3947
|
+
parts.push("Timeout: ".concat(this.timeout, "ms"));
|
|
3948
|
+
}
|
|
3949
|
+
return parts.length > 0 ? "".concat(this.message, " (").concat(parts.join(", "), ")") : this.message;
|
|
3950
|
+
}
|
|
3951
|
+
}
|
|
3952
|
+
]);
|
|
3953
|
+
return AbortError;
|
|
3954
|
+
}(ExecutorError);
|
|
3955
|
+
var AbortPlugin = /*#__PURE__*/ function() {
|
|
3956
|
+
"use strict";
|
|
3957
|
+
function _AbortPlugin(options) {
|
|
3958
|
+
_class_call_check(this, _AbortPlugin);
|
|
3959
|
+
/**
|
|
3960
|
+
* Plugin identifier name
|
|
3961
|
+
*
|
|
3962
|
+
* Used by the executor system to identify this plugin
|
|
3963
|
+
*/ this.pluginName = "AbortPlugin";
|
|
3964
|
+
/**
|
|
3965
|
+
* Ensures only one instance of this plugin can be registered
|
|
3966
|
+
*
|
|
3967
|
+
* Prevents conflicts from multiple abort plugin instances
|
|
3968
|
+
*/ this.onlyOne = true;
|
|
3969
|
+
/**
|
|
3970
|
+
* Counter for auto-generating request identifiers
|
|
3971
|
+
*
|
|
3972
|
+
* Incremented each time a new request without ID is processed
|
|
3973
|
+
*
|
|
3974
|
+
* @private
|
|
3975
|
+
*/ this.requestCounter = 0;
|
|
3976
|
+
/**
|
|
3977
|
+
* Map of active abort controllers indexed by request key
|
|
3978
|
+
*
|
|
3979
|
+
* Stores `AbortController` instances for ongoing operations
|
|
3980
|
+
* Enables abort operations by request identifier
|
|
3981
|
+
*
|
|
3982
|
+
* @protected
|
|
3983
|
+
*/ this.controllers = /* @__PURE__ */ new Map();
|
|
3984
|
+
/**
|
|
3985
|
+
* Map of active timeout timers indexed by request key
|
|
3986
|
+
*
|
|
3987
|
+
* Stores timeout IDs for cleanup when operations complete
|
|
3988
|
+
* Prevents memory leaks from abandoned timers
|
|
3989
|
+
*
|
|
3990
|
+
* @protected
|
|
3991
|
+
*/ this.timeouts = /* @__PURE__ */ new Map();
|
|
3992
|
+
this.getConfig = (options === null || options === void 0 ? void 0 : options.getConfig) || function(parameters) {
|
|
3993
|
+
return parameters;
|
|
3994
|
+
};
|
|
3995
|
+
this.logger = options === null || options === void 0 ? void 0 : options.logger;
|
|
3996
|
+
}
|
|
3997
|
+
_create_class(_AbortPlugin, [
|
|
3998
|
+
{
|
|
3999
|
+
/**
|
|
4000
|
+
* Generates unique request identifier from configuration
|
|
4001
|
+
*
|
|
4002
|
+
* Priority order:
|
|
4003
|
+
* 1. `requestId` from config
|
|
4004
|
+
* 2. `id` from config
|
|
4005
|
+
* 3. Auto-generated identifier: `{pluginName}-{counter}`
|
|
4006
|
+
*
|
|
4007
|
+
* @param config - Abort plugin configuration
|
|
4008
|
+
* @returns Unique request identifier string
|
|
4009
|
+
*
|
|
4010
|
+
* @protected
|
|
4011
|
+
* @example
|
|
4012
|
+
* ```typescript
|
|
4013
|
+
* // With requestId
|
|
4014
|
+
* generateRequestKey({ requestId: 'fetch-user' }) // "fetch-user"
|
|
4015
|
+
*
|
|
4016
|
+
* // With id
|
|
4017
|
+
* generateRequestKey({ id: 'upload-file' }) // "upload-file"
|
|
4018
|
+
*
|
|
4019
|
+
* // Auto-generated
|
|
4020
|
+
* generateRequestKey({}) // "AbortPlugin-1"
|
|
4021
|
+
* ```
|
|
4022
|
+
*/ key: "generateRequestKey",
|
|
4023
|
+
value: function generateRequestKey(config) {
|
|
4024
|
+
var requestId = config.requestId, id = config.id;
|
|
4025
|
+
return requestId || id || "".concat(this.pluginName, "-").concat(++this.requestCounter);
|
|
4026
|
+
}
|
|
4027
|
+
},
|
|
4028
|
+
{
|
|
4029
|
+
/**
|
|
4030
|
+
* Cleans up resources associated with a request
|
|
4031
|
+
*
|
|
4032
|
+
* Removes abort controller and clears timeout timer for the specified request
|
|
4033
|
+
* Prevents memory leaks by releasing references and stopping timers
|
|
4034
|
+
* Only logs when resources actually exist to avoid noise
|
|
4035
|
+
*
|
|
4036
|
+
* @param config - Configuration object or request identifier string
|
|
4037
|
+
*
|
|
4038
|
+
* @example With config object
|
|
4039
|
+
* ```typescript
|
|
4040
|
+
* cleanup({ id: 'fetch-users' });
|
|
4041
|
+
* ```
|
|
4042
|
+
*
|
|
4043
|
+
* @example With identifier string
|
|
4044
|
+
* ```typescript
|
|
4045
|
+
* cleanup('fetch-users');
|
|
4046
|
+
* ```
|
|
4047
|
+
*/ key: "cleanup",
|
|
4048
|
+
value: function cleanup(config) {
|
|
4049
|
+
var key = typeof config === "string" ? config : this.generateRequestKey(config);
|
|
4050
|
+
var hasController = this.controllers.has(key);
|
|
4051
|
+
var hasTimeout = this.timeouts.has(key);
|
|
4052
|
+
if (hasController || hasTimeout) {
|
|
4053
|
+
var _this_logger;
|
|
4054
|
+
this.controllers.delete(key);
|
|
4055
|
+
var timeoutId = this.timeouts.get(key);
|
|
4056
|
+
if (timeoutId) {
|
|
4057
|
+
clearTimeout(timeoutId);
|
|
4058
|
+
this.timeouts.delete(key);
|
|
4059
|
+
}
|
|
4060
|
+
(_this_logger = this.logger) === null || _this_logger === void 0 ? void 0 : _this_logger.debug("[".concat(this.pluginName, "] cleanup: ").concat(key));
|
|
4061
|
+
}
|
|
4062
|
+
}
|
|
4063
|
+
},
|
|
4064
|
+
{
|
|
4065
|
+
/**
|
|
4066
|
+
* Executor lifecycle hook: called before operation execution
|
|
4067
|
+
*
|
|
4068
|
+
* Performs the following setup operations:
|
|
4069
|
+
* 1. Extracts abort configuration from context parameters
|
|
4070
|
+
* 2. Generates unique request key
|
|
4071
|
+
* 3. Aborts any existing request with same key (prevents duplicates)
|
|
4072
|
+
* 4. Creates new `AbortController` if signal not provided
|
|
4073
|
+
* 5. Sets up timeout timer if `abortTimeout` configured
|
|
4074
|
+
* 6. Injects abort signal into configuration
|
|
4075
|
+
*
|
|
4076
|
+
* @param context - Executor context containing parameters and metadata
|
|
4077
|
+
*
|
|
4078
|
+
* @example Configuration in context
|
|
4079
|
+
* ```typescript
|
|
4080
|
+
* executor.execute({
|
|
4081
|
+
* id: 'fetch-data',
|
|
4082
|
+
* abortTimeout: 5000,
|
|
4083
|
+
* onAborted: (config) => console.log('Aborted:', config.id)
|
|
4084
|
+
* });
|
|
4085
|
+
* ```
|
|
4086
|
+
*/ key: "onBefore",
|
|
4087
|
+
value: function onBefore(context) {
|
|
4088
|
+
var _this = this;
|
|
4089
|
+
var config = this.getConfig(context.parameters);
|
|
4090
|
+
var key = this.generateRequestKey(config);
|
|
4091
|
+
var abortTimeout = config.abortTimeout;
|
|
4092
|
+
if (this.controllers.has(key)) {
|
|
4093
|
+
var _this_logger;
|
|
4094
|
+
(_this_logger = this.logger) === null || _this_logger === void 0 ? void 0 : _this_logger.debug("[".concat(this.pluginName, "] aborting previous request: ").concat(key));
|
|
4095
|
+
this.abort(key);
|
|
4096
|
+
}
|
|
4097
|
+
if (!config.signal) {
|
|
4098
|
+
var controller = new AbortController();
|
|
4099
|
+
this.controllers.set(key, controller);
|
|
4100
|
+
config.signal = controller.signal;
|
|
4101
|
+
if (abortTimeout && abortTimeout > 0) {
|
|
4102
|
+
var timeoutId = setTimeout(function() {
|
|
4103
|
+
var ctrl = _this.controllers.get(key);
|
|
4104
|
+
if (ctrl) {
|
|
4105
|
+
var _this_logger;
|
|
4106
|
+
ctrl.abort(new AbortError("The operation was aborted due to timeout", key, abortTimeout));
|
|
4107
|
+
(_this_logger = _this.logger) === null || _this_logger === void 0 ? void 0 : _this_logger.info("[".concat(_this.pluginName, "] timeout abort: ").concat(key, " (").concat(abortTimeout, "ms)"));
|
|
4108
|
+
_this.cleanup(key);
|
|
4109
|
+
if (typeof config.onAborted === "function") {
|
|
4110
|
+
config.onAborted(_object_spread_props(_object_spread({}, config), {
|
|
4111
|
+
onAborted: void 0
|
|
4112
|
+
}));
|
|
4113
|
+
}
|
|
4114
|
+
}
|
|
4115
|
+
}, abortTimeout);
|
|
4116
|
+
this.timeouts.set(key, timeoutId);
|
|
4117
|
+
}
|
|
4118
|
+
}
|
|
4119
|
+
}
|
|
4120
|
+
},
|
|
4121
|
+
{
|
|
4122
|
+
/**
|
|
4123
|
+
* Executor lifecycle hook: called after successful execution
|
|
4124
|
+
*
|
|
4125
|
+
* Cleans up resources (controller and timeout) for completed operation
|
|
4126
|
+
* Ensures no memory leaks from successful operations
|
|
4127
|
+
*
|
|
4128
|
+
* @param context - Executor context containing parameters and metadata
|
|
4129
|
+
*
|
|
4130
|
+
* @example
|
|
4131
|
+
* ```typescript
|
|
4132
|
+
* // After successful execution, resources are automatically cleaned
|
|
4133
|
+
* await executor.execute({ id: 'task-1' });
|
|
4134
|
+
* // AbortController and timeout for 'task-1' are now removed
|
|
4135
|
+
* ```
|
|
4136
|
+
*/ key: "onSuccess",
|
|
4137
|
+
value: function onSuccess(param) {
|
|
4138
|
+
var parameters = param.parameters;
|
|
4139
|
+
if (parameters) {
|
|
4140
|
+
var config = this.getConfig(parameters);
|
|
4141
|
+
var key = this.generateRequestKey(config);
|
|
4142
|
+
this.cleanup(key);
|
|
4143
|
+
}
|
|
4144
|
+
}
|
|
4145
|
+
},
|
|
4146
|
+
{
|
|
4147
|
+
/**
|
|
4148
|
+
* Executor lifecycle hook: called when execution fails
|
|
4149
|
+
*
|
|
4150
|
+
* Handles abort-related errors and cleans up resources
|
|
4151
|
+
*
|
|
4152
|
+
* Error handling logic:
|
|
4153
|
+
* 1. Check if error is abort-related using `isAbortError()`
|
|
4154
|
+
* 2. If abort error: Extract reason from signal, cleanup resources, return `AbortError`
|
|
4155
|
+
* 3. If other error: Still cleanup resources to prevent leaks
|
|
4156
|
+
*
|
|
4157
|
+
* @param context - Executor context containing error, parameters, and metadata
|
|
4158
|
+
* @returns `AbortError` if error is abort-related, `void` otherwise
|
|
4159
|
+
*
|
|
4160
|
+
* @example Abort error handling
|
|
4161
|
+
* ```typescript
|
|
4162
|
+
* try {
|
|
4163
|
+
* await executor.execute({ id: 'task-1', abortTimeout: 100 });
|
|
4164
|
+
* } catch (error) {
|
|
4165
|
+
* if (error instanceof AbortError) {
|
|
4166
|
+
* console.log(error.getDescription());
|
|
4167
|
+
* // "The operation was aborted due to timeout (Request: task-1, Timeout: 100ms)"
|
|
4168
|
+
* }
|
|
4169
|
+
* }
|
|
4170
|
+
* ```
|
|
4171
|
+
*/ key: "onError",
|
|
4172
|
+
value: function onError(param) {
|
|
4173
|
+
var error = param.error, parameters = param.parameters;
|
|
4174
|
+
if (!parameters) return;
|
|
4175
|
+
var config = this.getConfig(parameters);
|
|
4176
|
+
var key = this.generateRequestKey(config);
|
|
4177
|
+
if (_AbortPlugin.isAbortError(error)) {
|
|
4178
|
+
var controller = this.controllers.get(key);
|
|
4179
|
+
var reason = controller === null || controller === void 0 ? void 0 : controller.signal.reason;
|
|
4180
|
+
this.cleanup(key);
|
|
4181
|
+
if (_instanceof(reason, AbortError)) {
|
|
4182
|
+
return reason;
|
|
4183
|
+
}
|
|
4184
|
+
return new AbortError((reason === null || reason === void 0 ? void 0 : reason.message) || (error === null || error === void 0 ? void 0 : error.message) || "The operation was aborted", key);
|
|
4185
|
+
} else {
|
|
4186
|
+
this.cleanup(key);
|
|
4187
|
+
}
|
|
4188
|
+
}
|
|
4189
|
+
},
|
|
4190
|
+
{
|
|
4191
|
+
/**
|
|
4192
|
+
* Manually aborts a specific operation
|
|
4193
|
+
*
|
|
4194
|
+
* Triggers abort for the specified request, calls `onAborted` callback if provided,
|
|
4195
|
+
* and cleans up all associated resources
|
|
4196
|
+
*
|
|
4197
|
+
* @param config - Configuration object with request identifier or identifier string
|
|
4198
|
+
* @returns `true` if operation was aborted, `false` if no matching operation found
|
|
4199
|
+
*
|
|
4200
|
+
* @example Abort by ID
|
|
4201
|
+
* ```typescript
|
|
4202
|
+
* abortPlugin.abort('fetch-users');
|
|
4203
|
+
* ```
|
|
4204
|
+
*
|
|
4205
|
+
* @example Abort with config
|
|
4206
|
+
* ```typescript
|
|
4207
|
+
* abortPlugin.abort({ id: 'upload-file' });
|
|
4208
|
+
* ```
|
|
4209
|
+
*
|
|
4210
|
+
* @example Conditional abort
|
|
4211
|
+
* ```typescript
|
|
4212
|
+
* if (userClickedCancel) {
|
|
4213
|
+
* const aborted = abortPlugin.abort('long-task');
|
|
4214
|
+
* if (aborted) {
|
|
4215
|
+
* console.log('Task cancelled successfully');
|
|
4216
|
+
* }
|
|
4217
|
+
* }
|
|
4218
|
+
* ```
|
|
4219
|
+
*/ key: "abort",
|
|
4220
|
+
value: function abort(config) {
|
|
4221
|
+
var key = typeof config === "string" ? config : this.generateRequestKey(config);
|
|
4222
|
+
var controller = this.controllers.get(key);
|
|
4223
|
+
if (controller) {
|
|
4224
|
+
var _this_logger;
|
|
4225
|
+
controller.abort(new AbortError("The operation was aborted", key));
|
|
4226
|
+
(_this_logger = this.logger) === null || _this_logger === void 0 ? void 0 : _this_logger.info("[".concat(this.pluginName, "] manual abort: ").concat(key));
|
|
4227
|
+
this.cleanup(key);
|
|
4228
|
+
if (typeof config !== "string" && typeof config.onAborted === "function") {
|
|
4229
|
+
config.onAborted(_object_spread_props(_object_spread({}, config), {
|
|
4230
|
+
onAborted: void 0
|
|
4231
|
+
}));
|
|
4232
|
+
}
|
|
4233
|
+
return true;
|
|
4234
|
+
}
|
|
4235
|
+
return false;
|
|
4236
|
+
}
|
|
4237
|
+
},
|
|
4238
|
+
{
|
|
4239
|
+
/**
|
|
4240
|
+
* Aborts all pending operations
|
|
4241
|
+
*
|
|
4242
|
+
* Iterates through all active controllers, aborts each operation,
|
|
4243
|
+
* and clears all controllers and timeout timers
|
|
4244
|
+
*
|
|
4245
|
+
* Use cases:
|
|
4246
|
+
* - User logs out
|
|
4247
|
+
* - Component unmounts
|
|
4248
|
+
* - Application shutdown
|
|
4249
|
+
* - Navigation away from page
|
|
4250
|
+
*
|
|
4251
|
+
* @example Component cleanup
|
|
4252
|
+
* ```typescript
|
|
4253
|
+
* class MyComponent {
|
|
4254
|
+
* private abortPlugin = new AbortPlugin();
|
|
4255
|
+
*
|
|
4256
|
+
* onDestroy() {
|
|
4257
|
+
* // Cancel all pending requests
|
|
4258
|
+
* this.abortPlugin.abortAll();
|
|
4259
|
+
* }
|
|
4260
|
+
* }
|
|
4261
|
+
* ```
|
|
4262
|
+
*
|
|
4263
|
+
* @example User logout
|
|
4264
|
+
* ```typescript
|
|
4265
|
+
* function logout() {
|
|
4266
|
+
* abortPlugin.abortAll(); // Cancel all API calls
|
|
4267
|
+
* clearUserData();
|
|
4268
|
+
* redirectToLogin();
|
|
4269
|
+
* }
|
|
4270
|
+
* ```
|
|
4271
|
+
*/ key: "abortAll",
|
|
4272
|
+
value: function abortAll() {
|
|
4273
|
+
var count = this.controllers.size;
|
|
4274
|
+
if (count > 0) {
|
|
4275
|
+
var _this_logger;
|
|
4276
|
+
(_this_logger = this.logger) === null || _this_logger === void 0 ? void 0 : _this_logger.debug("[".concat(this.pluginName, "] aborting all ").concat(count, " requests"));
|
|
4277
|
+
}
|
|
4278
|
+
this.controllers.forEach(function(controller, key) {
|
|
4279
|
+
controller.abort(new AbortError("All operations were aborted", key));
|
|
4280
|
+
});
|
|
4281
|
+
this.controllers.clear();
|
|
4282
|
+
this.timeouts.forEach(function(timeoutId) {
|
|
4283
|
+
clearTimeout(timeoutId);
|
|
4284
|
+
});
|
|
4285
|
+
this.timeouts.clear();
|
|
4286
|
+
}
|
|
4287
|
+
},
|
|
4288
|
+
{
|
|
4289
|
+
/**
|
|
4290
|
+
* Creates a Promise that rejects when signal is aborted
|
|
4291
|
+
*
|
|
4292
|
+
* Returns both the promise and a cleanup function to remove event listener
|
|
4293
|
+
* Prevents memory leaks by allowing proper cleanup of abort event handlers
|
|
4294
|
+
*
|
|
4295
|
+
* Implementation details:
|
|
4296
|
+
* 1. Immediately rejects if signal is already aborted
|
|
4297
|
+
* 2. Attaches event listener for future abort events
|
|
4298
|
+
* 3. Returns cleanup function to remove listener
|
|
4299
|
+
* 4. Uses signal reason if available, otherwise creates new `AbortError`
|
|
4300
|
+
*
|
|
4301
|
+
* @param signal - AbortSignal to monitor
|
|
4302
|
+
* @returns Object containing promise and cleanup function
|
|
4303
|
+
* @returns promise - Promise that rejects on abort
|
|
4304
|
+
* @returns cleanup - Function to remove event listener
|
|
4305
|
+
*
|
|
4306
|
+
* @private Internal use only
|
|
4307
|
+
*
|
|
4308
|
+
* @example Internal usage
|
|
4309
|
+
* ```typescript
|
|
4310
|
+
* const { promise, cleanup } = this.createAbortPromise(signal);
|
|
4311
|
+
* try {
|
|
4312
|
+
* await Promise.race([someOperation(), promise]);
|
|
4313
|
+
* } finally {
|
|
4314
|
+
* cleanup(); // Always cleanup to prevent memory leak
|
|
4315
|
+
* }
|
|
4316
|
+
* ```
|
|
4317
|
+
*/ key: "createAbortPromise",
|
|
4318
|
+
value: function createAbortPromise(signal) {
|
|
4319
|
+
var cleanup = function() {};
|
|
4320
|
+
var promise = new Promise(function(_, reject) {
|
|
4321
|
+
if (signal.aborted) {
|
|
4322
|
+
reject(signal.reason || new AbortError("The operation was aborted"));
|
|
4323
|
+
return;
|
|
4324
|
+
}
|
|
4325
|
+
var onAbort = function() {
|
|
4326
|
+
reject(signal.reason || new AbortError("The operation was aborted"));
|
|
4327
|
+
};
|
|
4328
|
+
signal.addEventListener("abort", onAbort);
|
|
4329
|
+
cleanup = function() {
|
|
4330
|
+
signal.removeEventListener("abort", onAbort);
|
|
4331
|
+
};
|
|
4332
|
+
});
|
|
4333
|
+
return {
|
|
4334
|
+
promise: promise,
|
|
4335
|
+
cleanup: cleanup
|
|
4336
|
+
};
|
|
4337
|
+
}
|
|
4338
|
+
},
|
|
4339
|
+
{
|
|
4340
|
+
/**
|
|
4341
|
+
* Wraps an async operation with `Promise.race` to ensure abort signal responsiveness
|
|
4342
|
+
*
|
|
4343
|
+
* Defensive mechanism for operations that don't natively check abort signals
|
|
4344
|
+
* Uses promise racing to interrupt operations when signal is aborted,
|
|
4345
|
+
* even if the underlying operation ignores the signal
|
|
4346
|
+
*
|
|
4347
|
+
* Use cases:
|
|
4348
|
+
* - Third-party APIs that don't support `AbortSignal`
|
|
4349
|
+
* - Legacy code without abort handling
|
|
4350
|
+
* - Gateway operations that don't check signal
|
|
4351
|
+
* - Any async operation needing guaranteed cancellation
|
|
4352
|
+
*
|
|
4353
|
+
* @template T - Type of the promise result
|
|
4354
|
+
* @param promise - Promise to wrap with abort capability
|
|
4355
|
+
* @param signal - AbortSignal to monitor, if not provided returns original promise
|
|
4356
|
+
* @returns Wrapped promise that rejects immediately when signal is aborted
|
|
4357
|
+
*
|
|
4358
|
+
* @example Basic usage
|
|
4359
|
+
* ```typescript
|
|
4360
|
+
* const controller = new AbortController();
|
|
4361
|
+
* const result = await abortPlugin.raceWithAbort(
|
|
4362
|
+
* fetch('/api/data'), // Even if fetch doesn't check signal
|
|
4363
|
+
* controller.signal
|
|
4364
|
+
* );
|
|
4365
|
+
* ```
|
|
4366
|
+
*
|
|
4367
|
+
* @example With third-party library
|
|
4368
|
+
* ```typescript
|
|
4369
|
+
* const result = await abortPlugin.raceWithAbort(
|
|
4370
|
+
* legacyApiCall(), // Library doesn't support abort
|
|
4371
|
+
* signal
|
|
4372
|
+
* );
|
|
4373
|
+
* ```
|
|
4374
|
+
*
|
|
4375
|
+
* @example Without signal (pass-through)
|
|
4376
|
+
* ```typescript
|
|
4377
|
+
* // When signal is not provided, returns original promise
|
|
4378
|
+
* const result = await abortPlugin.raceWithAbort(somePromise());
|
|
4379
|
+
* ```
|
|
4380
|
+
*
|
|
4381
|
+
* @example Timeout with abort
|
|
4382
|
+
* ```typescript
|
|
4383
|
+
* const controller = new AbortController();
|
|
4384
|
+
* setTimeout(() => controller.abort(), 5000);
|
|
4385
|
+
*
|
|
4386
|
+
* try {
|
|
4387
|
+
* const result = await abortPlugin.raceWithAbort(
|
|
4388
|
+
* longRunningOperation(),
|
|
4389
|
+
* controller.signal
|
|
4390
|
+
* );
|
|
4391
|
+
* } catch (error) {
|
|
4392
|
+
* if (AbortPlugin.isAbortError(error)) {
|
|
4393
|
+
* console.log('Operation timed out');
|
|
4394
|
+
* }
|
|
4395
|
+
* }
|
|
4396
|
+
* ```
|
|
4397
|
+
*/ key: "raceWithAbort",
|
|
4398
|
+
value: function raceWithAbort(promise, signal) {
|
|
4399
|
+
if (!signal) {
|
|
4400
|
+
return promise;
|
|
4401
|
+
}
|
|
4402
|
+
var _this_createAbortPromise = this.createAbortPromise(signal), abortPromise = _this_createAbortPromise.promise, cleanup = _this_createAbortPromise.cleanup;
|
|
4403
|
+
return Promise.race([
|
|
4404
|
+
promise,
|
|
4405
|
+
abortPromise
|
|
4406
|
+
]).finally(function() {
|
|
4407
|
+
cleanup();
|
|
4408
|
+
});
|
|
4409
|
+
}
|
|
4410
|
+
}
|
|
4411
|
+
], [
|
|
4412
|
+
{
|
|
4413
|
+
key: "isAbortError",
|
|
4414
|
+
value: /**
|
|
4415
|
+
* Checks if an error is abort-related (static utility method)
|
|
4416
|
+
*
|
|
4417
|
+
* Comprehensive check for various abort error types including custom `AbortError`,
|
|
4418
|
+
* native browser `AbortError`, `ExecutorError` with abort ID, `DOMException`,
|
|
4419
|
+
* and abort events. Can be used independently without plugin instance.
|
|
4420
|
+
*
|
|
4421
|
+
* Detection logic:
|
|
4422
|
+
* 1. Custom `AbortError` instance
|
|
4423
|
+
* 2. Native `Error` with name 'AbortError'
|
|
4424
|
+
* 3. `ExecutorError` with `ABORT_ERROR_ID`
|
|
4425
|
+
* 4. `DOMException` with name 'AbortError'
|
|
4426
|
+
* 5. `Event` with type 'abort'
|
|
4427
|
+
*
|
|
4428
|
+
* @param error - Error object to check
|
|
4429
|
+
* @returns `true` if error is abort-related, `false` otherwise
|
|
4430
|
+
*
|
|
4431
|
+
* @example Basic usage
|
|
4432
|
+
* ```typescript
|
|
4433
|
+
* try {
|
|
4434
|
+
* await fetch(url, { signal });
|
|
4435
|
+
* } catch (error) {
|
|
4436
|
+
* if (AbortPlugin.isAbortError(error)) {
|
|
4437
|
+
* console.log('Request was cancelled');
|
|
4438
|
+
* } else {
|
|
4439
|
+
* console.error('Request failed:', error);
|
|
4440
|
+
* }
|
|
4441
|
+
* }
|
|
4442
|
+
* ```
|
|
4443
|
+
*
|
|
4444
|
+
* @example In error handler
|
|
4445
|
+
* ```typescript
|
|
4446
|
+
* function handleError(error: unknown) {
|
|
4447
|
+
* if (AbortPlugin.isAbortError(error)) {
|
|
4448
|
+
* // User cancelled - don't show error message
|
|
4449
|
+
* return;
|
|
4450
|
+
* }
|
|
4451
|
+
* showErrorNotification(error);
|
|
4452
|
+
* }
|
|
4453
|
+
* ```
|
|
4454
|
+
*/ function isAbortError(error) {
|
|
4455
|
+
if (_instanceof(error, AbortError)) {
|
|
4456
|
+
return true;
|
|
4457
|
+
}
|
|
4458
|
+
if (_instanceof(error, Error) && error.name === "AbortError") {
|
|
4459
|
+
return true;
|
|
4460
|
+
}
|
|
4461
|
+
if (_instanceof(error, ExecutorError) && (error === null || error === void 0 ? void 0 : error.id) === ABORT_ERROR_ID) {
|
|
4462
|
+
return true;
|
|
4463
|
+
}
|
|
4464
|
+
if (_instanceof(error, DOMException) && error.name === "AbortError") {
|
|
4465
|
+
return true;
|
|
4466
|
+
}
|
|
4467
|
+
if (_instanceof(error, Event) && error.type === "abort") {
|
|
4468
|
+
return true;
|
|
4469
|
+
}
|
|
4470
|
+
return false;
|
|
4471
|
+
}
|
|
4472
|
+
}
|
|
4473
|
+
]);
|
|
4474
|
+
return _AbortPlugin;
|
|
4475
|
+
}();
|
|
3873
4476
|
// src/executor/plugins/RetryPlugin.ts
|
|
3874
4477
|
var SAFE_MAX_RETRIES = 16;
|
|
3875
4478
|
var DEFAULT_MAX_RETRIES = 3;
|
|
@@ -5311,20 +5914,29 @@ var Base64Serializer = /*#__PURE__*/ function() {
|
|
|
5311
5914
|
]);
|
|
5312
5915
|
return Base64Serializer;
|
|
5313
5916
|
}();
|
|
5314
|
-
// src/storage/
|
|
5315
|
-
var
|
|
5917
|
+
// src/storage/impl/KeyStorage.ts
|
|
5918
|
+
var KeyStorage = /*#__PURE__*/ function() {
|
|
5316
5919
|
"use strict";
|
|
5317
|
-
function
|
|
5920
|
+
function KeyStorage(key) {
|
|
5318
5921
|
var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
5319
|
-
_class_call_check(this,
|
|
5320
|
-
var _options_storage;
|
|
5922
|
+
_class_call_check(this, KeyStorage);
|
|
5321
5923
|
this.key = key;
|
|
5322
5924
|
this.options = options;
|
|
5323
|
-
|
|
5324
|
-
|
|
5325
|
-
|
|
5925
|
+
try {
|
|
5926
|
+
var _options_storage;
|
|
5927
|
+
var localValue = (_options_storage = options.storage) === null || _options_storage === void 0 ? void 0 : _options_storage.getItem(key);
|
|
5928
|
+
this.value = localValue !== null && localValue !== void 0 ? localValue : null;
|
|
5929
|
+
} catch (e) {
|
|
5930
|
+
this.value = null;
|
|
5931
|
+
}
|
|
5326
5932
|
}
|
|
5327
|
-
_create_class(
|
|
5933
|
+
_create_class(KeyStorage, [
|
|
5934
|
+
{
|
|
5935
|
+
key: "mergeOptions",
|
|
5936
|
+
value: function mergeOptions(options) {
|
|
5937
|
+
return _object_spread({}, this.options, options);
|
|
5938
|
+
}
|
|
5939
|
+
},
|
|
5328
5940
|
{
|
|
5329
5941
|
key: "getKey",
|
|
5330
5942
|
value: function getKey() {
|
|
@@ -5334,31 +5946,7 @@ var KeyStorageInterface = /*#__PURE__*/ function() {
|
|
|
5334
5946
|
{
|
|
5335
5947
|
key: "getValue",
|
|
5336
5948
|
value: function getValue() {
|
|
5337
|
-
return this.
|
|
5338
|
-
}
|
|
5339
|
-
}
|
|
5340
|
-
]);
|
|
5341
|
-
return KeyStorageInterface;
|
|
5342
|
-
}();
|
|
5343
|
-
// src/storage/impl/KeyStorage.ts
|
|
5344
|
-
var KeyStorage = /*#__PURE__*/ function(KeyStorageInterface) {
|
|
5345
|
-
"use strict";
|
|
5346
|
-
_inherits(KeyStorage, KeyStorageInterface);
|
|
5347
|
-
var _super = _create_super(KeyStorage);
|
|
5348
|
-
function KeyStorage() {
|
|
5349
|
-
_class_call_check(this, KeyStorage);
|
|
5350
|
-
var _this;
|
|
5351
|
-
_this = _super.call.apply(_super, [
|
|
5352
|
-
this
|
|
5353
|
-
].concat(Array.prototype.slice.call(arguments)));
|
|
5354
|
-
_this.value = null;
|
|
5355
|
-
return _this;
|
|
5356
|
-
}
|
|
5357
|
-
_create_class(KeyStorage, [
|
|
5358
|
-
{
|
|
5359
|
-
key: "mergeOptions",
|
|
5360
|
-
value: function mergeOptions(options) {
|
|
5361
|
-
return _object_spread({}, this.options, options);
|
|
5949
|
+
return this.value;
|
|
5362
5950
|
}
|
|
5363
5951
|
},
|
|
5364
5952
|
{
|
|
@@ -5406,7 +5994,7 @@ var KeyStorage = /*#__PURE__*/ function(KeyStorageInterface) {
|
|
|
5406
5994
|
}
|
|
5407
5995
|
]);
|
|
5408
5996
|
return KeyStorage;
|
|
5409
|
-
}(
|
|
5997
|
+
}();
|
|
5410
5998
|
// src/storage/impl/ObjectStorage.ts
|
|
5411
5999
|
var ObjectStorage = /*#__PURE__*/ function() {
|
|
5412
6000
|
"use strict";
|
|
@@ -5865,4 +6453,4 @@ var SyncStorage = /*#__PURE__*/ function() {
|
|
|
5865
6453
|
]);
|
|
5866
6454
|
return SyncStorage;
|
|
5867
6455
|
}();
|
|
5868
|
-
export { AsyncExecutor, Base64Serializer, Executor, ExecutorError, FetchAbortPlugin, FetchURLPlugin, JSONSerializer, KeyStorage,
|
|
6456
|
+
export { ABORT_ERROR_ID, AbortError, AbortPlugin, AsyncExecutor, Base64Serializer, Executor, ExecutorError, FetchAbortPlugin, FetchURLPlugin, JSONSerializer, KeyStorage, ObjectStorage, RequestAdapterAxios, RequestAdapterFetch, RequestError, RequestErrorID, RequestManager, RequestScheduler, RequestTransaction, RetryPlugin, SyncExecutor, SyncStorage };
|