@superutils/rx 0.1.13 → 0.1.14
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/browser/index.min.js +2 -2
- package/dist/browser/index.min.js.map +1 -1
- package/dist/index.cjs +94 -400
- package/dist/index.d.cts +127 -730
- package/dist/index.d.ts +127 -730
- package/dist/index.js +96 -410
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -30,55 +30,38 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
-
BehaviorSubject: () => import_rxjs.BehaviorSubject,
|
|
34
|
-
DataStorage: () => DataStorage,
|
|
35
33
|
IGNORE_UPDATE_SYMBOL: () => IGNORE_UPDATE_SYMBOL,
|
|
36
34
|
IntervalRunner: () => IntervalRunner,
|
|
37
35
|
IntervalSubject: () => IntervalSubject,
|
|
38
|
-
OnErrorType: () => OnErrorType,
|
|
39
|
-
Subject: () => import_rxjs.Subject,
|
|
40
|
-
Subscription: () => import_rxjs.Subscription,
|
|
41
36
|
asPromise: () => asPromise,
|
|
42
|
-
|
|
43
|
-
forceUpdateCache$: () => forceUpdateCache$,
|
|
44
|
-
isObservable: () => import_rxjs.isObservable,
|
|
45
|
-
isSubjectLike: () => isSubjectLike2,
|
|
37
|
+
copyRx: () => copyRx,
|
|
46
38
|
isSubscriptionLike: () => isSubscriptionLike,
|
|
47
|
-
objToMap: () => import_core7.objToMap,
|
|
48
|
-
skip: () => import_rxjs.skip,
|
|
49
39
|
unsubscribeAll: () => unsubscribeAll
|
|
50
40
|
});
|
|
51
41
|
module.exports = __toCommonJS(index_exports);
|
|
52
42
|
|
|
53
43
|
// src/asPromise.ts
|
|
54
44
|
var import_core = require("@superutils/core");
|
|
55
|
-
var import_promise =
|
|
56
|
-
|
|
57
|
-
// src/rxjs.ts
|
|
45
|
+
var import_promise = require("@superutils/promise");
|
|
58
46
|
var import_rxjs = require("rxjs");
|
|
59
|
-
|
|
60
|
-
// src/asPromise.ts
|
|
61
|
-
var asPromise = (subject, expectedValue, timeoutOrOptions) => {
|
|
62
|
-
if (!(0, import_core.isSubjectLike)(subject) && !(0, import_rxjs.isObservable)(subject))
|
|
63
|
-
return import_promise.default.reject(
|
|
64
|
-
new Error("subject must be an instance of BehaviorSubject")
|
|
65
|
-
);
|
|
47
|
+
var asPromise = (input$, expectedValue, timeoutOrOptions) => {
|
|
66
48
|
let subscription;
|
|
67
49
|
const options = (0, import_core.isObj)(timeoutOrOptions) ? timeoutOrOptions : { timeout: timeoutOrOptions };
|
|
68
50
|
options.timeout = (0, import_core.isPositiveNumber)(options.timeout) ? options.timeout : import_promise.TIMEOUT_MAX;
|
|
69
|
-
const promise = import_promise.
|
|
51
|
+
const promise = (0, import_promise.timeout)(
|
|
70
52
|
{
|
|
71
53
|
...options,
|
|
72
54
|
onTimeout: async () => {
|
|
73
|
-
|
|
74
|
-
const msg =
|
|
75
|
-
return
|
|
76
|
-
timeoutMsg != null ? timeoutMsg : "request timed out before an expected value is received"
|
|
77
|
-
);
|
|
55
|
+
var _a, _b;
|
|
56
|
+
const msg = (_a = options.timeoutMsg) != null ? _a : asPromise.defaults.timeoutMsg;
|
|
57
|
+
return (_b = await (0, import_core.fallbackIfFails)(options.onTimeout, [], void 0)) != null ? _b : (0, import_core.isError)(msg) ? msg : new Error(msg);
|
|
78
58
|
}
|
|
79
59
|
},
|
|
80
|
-
new
|
|
81
|
-
|
|
60
|
+
new Promise((resolve, reject) => {
|
|
61
|
+
var _a;
|
|
62
|
+
if (!(0, import_rxjs.isObservable)(input$))
|
|
63
|
+
return reject(new Error(asPromise.defaults.invalidInputMsg));
|
|
64
|
+
subscription = input$.pipe((0, import_rxjs.skip)((_a = options.skip) != null ? _a : 0)).subscribe((value) => {
|
|
82
65
|
const shouldResolve = value === expectedValue || expectedValue === void 0 || (0, import_core.isFn)(expectedValue) && (0, import_core.fallbackIfFails)(expectedValue, [value], false);
|
|
83
66
|
shouldResolve && resolve(value);
|
|
84
67
|
});
|
|
@@ -90,403 +73,123 @@ var asPromise = (subject, expectedValue, timeoutOrOptions) => {
|
|
|
90
73
|
});
|
|
91
74
|
return promise;
|
|
92
75
|
};
|
|
76
|
+
asPromise.defaults = {
|
|
77
|
+
invalidInputMsg: "Input must be an observable or subject instance",
|
|
78
|
+
timeoutMsg: "Request timed out before an expected value is received"
|
|
79
|
+
};
|
|
93
80
|
|
|
94
|
-
// src/
|
|
95
|
-
var
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
var import_core2 = require("@superutils/core");
|
|
99
|
-
var isSubjectLike2 = (x, withValue = false) => (0, import_core2.isSubjectLike)(x, withValue);
|
|
100
|
-
var isSubjectLike_default = isSubjectLike2;
|
|
81
|
+
// src/copyRx.ts
|
|
82
|
+
var import_core4 = require("@superutils/core");
|
|
83
|
+
var import_promise2 = __toESM(require("@superutils/promise"), 1);
|
|
84
|
+
var import_rxjs3 = require("rxjs");
|
|
101
85
|
|
|
102
86
|
// src/unsubscribeAll.ts
|
|
103
|
-
var
|
|
87
|
+
var import_core3 = require("@superutils/core");
|
|
104
88
|
|
|
105
89
|
// src/isSubscriptionLike.ts
|
|
106
|
-
var
|
|
90
|
+
var import_core2 = require("@superutils/core");
|
|
91
|
+
var import_rxjs2 = require("rxjs");
|
|
107
92
|
var isSubscriptionLike = (value, strict = false) => {
|
|
108
|
-
if (value instanceof
|
|
93
|
+
if (value instanceof import_rxjs2.Subscription) return true;
|
|
109
94
|
const sub = value;
|
|
110
|
-
return !strict && !!sub && (0,
|
|
95
|
+
return !strict && !!sub && (0, import_core2.isBool)(sub.closed) && (0, import_core2.isFn)(sub.unsubscribe);
|
|
111
96
|
};
|
|
112
97
|
var isSubscriptionLike_default = isSubscriptionLike;
|
|
113
98
|
|
|
114
99
|
// src/unsubscribeAll.ts
|
|
115
|
-
var unsubscribeAll = (
|
|
116
|
-
if (!
|
|
100
|
+
var unsubscribeAll = (candidate = {}, onError) => {
|
|
101
|
+
if (!candidate) return;
|
|
117
102
|
try {
|
|
118
|
-
if ((0,
|
|
119
|
-
|
|
120
|
-
|
|
103
|
+
if ((0, import_core3.isFn)(candidate)) {
|
|
104
|
+
candidate();
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (isSubscriptionLike_default(candidate)) {
|
|
108
|
+
candidate.unsubscribe();
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
Object.values(candidate).forEach(
|
|
121
112
|
(value) => unsubscribeAll(value, onError)
|
|
122
113
|
);
|
|
123
114
|
} catch (err) {
|
|
124
115
|
onError == null ? void 0 : onError(err);
|
|
125
116
|
}
|
|
126
117
|
};
|
|
127
|
-
var unsubscribeAll_default = unsubscribeAll;
|
|
128
118
|
|
|
129
|
-
// src/
|
|
130
|
-
var IGNORE_UPDATE_SYMBOL = /* @__PURE__ */ Symbol("ignore-
|
|
131
|
-
function
|
|
132
|
-
var _a;
|
|
133
|
-
|
|
134
|
-
options = {
|
|
135
|
-
...copyRxSubject.defaults,
|
|
136
|
-
...options != null ? options : {}
|
|
137
|
-
};
|
|
138
|
-
const sourceArr = isSubjectLike_default(source$) ? [source$] : (0, import_core5.isArr)(source$) ? source$ : [source$];
|
|
119
|
+
// src/copyRx.ts
|
|
120
|
+
var IGNORE_UPDATE_SYMBOL = /* @__PURE__ */ Symbol("ignore-update");
|
|
121
|
+
function copyRx(source$, transform, options) {
|
|
122
|
+
var _a, _b, _c, _d;
|
|
123
|
+
const sourceArr = (0, import_rxjs3.isObservable)(source$) ? [source$] : (0, import_core4.isArr)(source$) ? source$ : [source$];
|
|
139
124
|
const cache = new Map(
|
|
140
|
-
sourceArr.map((
|
|
125
|
+
sourceArr.map((x, i) => [
|
|
141
126
|
i,
|
|
142
|
-
|
|
127
|
+
(0, import_rxjs3.isObservable)(x) ? x.value : x
|
|
143
128
|
// fixed value provided
|
|
144
129
|
])
|
|
145
130
|
);
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
131
|
+
const _options = {
|
|
132
|
+
...copyRx.defaults,
|
|
133
|
+
...options != null ? options : {}
|
|
134
|
+
};
|
|
135
|
+
if (!(0, import_core4.isArr)(_options.skipEmits)) _options.skipEmits = [_options.skipEmits];
|
|
136
|
+
if (_options.thisArg !== void 0) {
|
|
137
|
+
_options.onError = (_b = (_a = _options.onError) == null ? void 0 : _a.bind) == null ? void 0 : _b.call(_a, _options.thisArg);
|
|
138
|
+
transform = (_c = transform == null ? void 0 : transform.bind) == null ? void 0 : _c.call(transform, _options.thisArg);
|
|
139
|
+
}
|
|
140
|
+
if ((0, import_core4.isFn)(transform) && _options.transformSequentially)
|
|
141
|
+
transform = import_promise2.default.deferredCallback(transform, {
|
|
142
|
+
delay: 0,
|
|
143
|
+
// enables sequential execution
|
|
144
|
+
onError: _options.onError,
|
|
145
|
+
thisArg: _options.thisArg
|
|
146
|
+
});
|
|
147
|
+
const copy$ = (0, import_core4.isSubjectLike)(_options.output) ? _options.output : new import_rxjs3.BehaviorSubject(_options.initialValue);
|
|
148
|
+
const updateOutput = (value) => value !== copyRx.IGNORE && copy$.next(value);
|
|
149
|
+
let handleChange = () => {
|
|
150
|
+
const currentValue = (0, import_core4.isArr)(source$) ? [...cache.values()] : cache.get(0);
|
|
151
|
+
if (!(0, import_core4.isFn)(transform)) return copy$.next(currentValue);
|
|
152
|
+
const result = (0, import_core4.fallbackIfFails)(
|
|
153
|
+
transform,
|
|
154
|
+
[currentValue, copy$],
|
|
152
155
|
(err) => {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
(_a2 = options.onError) == null ? void 0 : _a2.bind(options.thisArg),
|
|
156
|
-
[err],
|
|
157
|
-
void 0
|
|
158
|
-
);
|
|
159
|
-
return IGNORE_UPDATE_SYMBOL;
|
|
156
|
+
(0, import_core4.fallbackIfFails)(_options.onError, [err], void 0);
|
|
157
|
+
return copyRx.IGNORE;
|
|
160
158
|
}
|
|
161
159
|
);
|
|
162
|
-
|
|
160
|
+
!(0, import_core4.isPromise)(result) ? updateOutput(result) : result.then(updateOutput);
|
|
163
161
|
};
|
|
164
|
-
|
|
165
|
-
const subscriptions = sourceArr.map((
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
162
|
+
handleChange = (0, import_core4.isPositiveNumber)(_options.delay) ? (0, import_core4.deferred)(handleChange, _options.delay, _options) : handleChange;
|
|
163
|
+
const subscriptions = sourceArr.map((x, i) => {
|
|
164
|
+
var _a2;
|
|
165
|
+
if (!(0, import_rxjs3.isObservable)(x) || x.closed) return;
|
|
166
|
+
const skipEmit = (_a2 = _options.skipEmits[i]) != null ? _a2 : x instanceof import_rxjs3.BehaviorSubject ? 1 : 0;
|
|
167
|
+
if (skipEmit > 0) x = x.pipe((0, import_rxjs3.skip)(skipEmit));
|
|
168
|
+
return x.subscribe({
|
|
169
|
+
next: (newValue) => {
|
|
170
|
+
cache.set(i, newValue);
|
|
171
|
+
handleChange();
|
|
172
|
+
}
|
|
173
173
|
});
|
|
174
|
-
})
|
|
175
|
-
const unsubscribeOrg = (
|
|
174
|
+
});
|
|
175
|
+
const unsubscribeOrg = (_d = copy$.unsubscribe) == null ? void 0 : _d.bind(copy$);
|
|
176
176
|
copy$.unsubscribe = (...args) => {
|
|
177
177
|
unsubscribeOrg == null ? void 0 : unsubscribeOrg(...args);
|
|
178
178
|
unsubscribeAll(subscriptions);
|
|
179
179
|
cache.clear();
|
|
180
180
|
};
|
|
181
|
-
|
|
181
|
+
handleChange();
|
|
182
182
|
return copy$;
|
|
183
183
|
}
|
|
184
|
-
|
|
184
|
+
copyRx.defaults = {
|
|
185
185
|
delay: 0,
|
|
186
186
|
throttle: false
|
|
187
187
|
};
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
// src/data-storage/DataStorage.ts
|
|
191
|
-
var import_core6 = require("@superutils/core");
|
|
192
|
-
|
|
193
|
-
// src/data-storage/types.ts
|
|
194
|
-
var OnErrorType = /* @__PURE__ */ ((OnErrorType2) => {
|
|
195
|
-
OnErrorType2["onChange"] = "onChange";
|
|
196
|
-
OnErrorType2["parse"] = "parse";
|
|
197
|
-
OnErrorType2["parse_json"] = "parse-json";
|
|
198
|
-
OnErrorType2["stringify"] = "stringify";
|
|
199
|
-
OnErrorType2["stringify_json"] = "stringify-json";
|
|
200
|
-
OnErrorType2["write"] = "write";
|
|
201
|
-
return OnErrorType2;
|
|
202
|
-
})(OnErrorType || {});
|
|
203
|
-
|
|
204
|
-
// src/data-storage/DataStorage.ts
|
|
205
|
-
var forceUpdateCache$ = new import_rxjs.Subject();
|
|
206
|
-
var _DataStorage = class _DataStorage {
|
|
207
|
-
constructor(name, options) {
|
|
208
|
-
this.initialized = false;
|
|
209
|
-
this.subscriptions = {
|
|
210
|
-
subject: void 0,
|
|
211
|
-
forceUpdateCache: void 0
|
|
212
|
-
};
|
|
213
|
-
this.clear = () => this.setAll(/* @__PURE__ */ new Map(), true);
|
|
214
|
-
this.delete = (keys) => {
|
|
215
|
-
if (!(0, import_core6.isArr)(keys)) keys = [keys];
|
|
216
|
-
const data = this.getAll();
|
|
217
|
-
for (const k of keys) data.delete(k);
|
|
218
|
-
this.setAll(data, true);
|
|
219
|
-
return this;
|
|
220
|
-
};
|
|
221
|
-
this.filter = (...args) => (0, import_core6.filter)(this.getAll(), ...args);
|
|
222
|
-
this.find = (predicateOrOptions) => (0, import_core6.find)(this.getAll(), predicateOrOptions);
|
|
223
|
-
this.get = (key) => this.getAll().get(key);
|
|
224
|
-
this.getAll = (forceRead = false) => {
|
|
225
|
-
var _a, _b;
|
|
226
|
-
const wasInitialized = this.initialized;
|
|
227
|
-
if (!wasInitialized) this.init();
|
|
228
|
-
const readFromStorage = this.cacheDisabled || this.name && forceRead;
|
|
229
|
-
if (readFromStorage) {
|
|
230
|
-
const data = this.read();
|
|
231
|
-
const shouldTrigger = forceRead || !wasInitialized && !!data.size;
|
|
232
|
-
shouldTrigger && this.subject.next(data);
|
|
233
|
-
return data;
|
|
234
|
-
}
|
|
235
|
-
return (_b = (_a = this.subject) == null ? void 0 : _a.value) != null ? _b : /* @__PURE__ */ new Map();
|
|
236
|
-
};
|
|
237
|
-
this.handleForceUpdateCacheChange = (name) => {
|
|
238
|
-
const isTarget = !this.name ? false : (0, import_core6.isArr)(name) ? name.includes(this.name) : (0, import_core6.isStr)(name) ? name === this.name : name === true;
|
|
239
|
-
if (!isTarget) return;
|
|
240
|
-
const newData = this.read();
|
|
241
|
-
this.subject.next(newData);
|
|
242
|
-
};
|
|
243
|
-
this.handleSubjectChange = (data) => {
|
|
244
|
-
var _a;
|
|
245
|
-
if (!(0, import_core6.isMap)(data)) return this.subject.next(/* @__PURE__ */ new Map());
|
|
246
|
-
this.write(data);
|
|
247
|
-
(0, import_core6.fallbackIfFails)(
|
|
248
|
-
(_a = this.onChange) == null ? void 0 : _a.bind(this),
|
|
249
|
-
[data],
|
|
250
|
-
this.triggerOnError("onChange" /* onChange */)
|
|
251
|
-
);
|
|
252
|
-
};
|
|
253
|
-
this.has = (key) => this.getAll().has(key);
|
|
254
|
-
this.init = (initialValue) => {
|
|
255
|
-
var _a;
|
|
256
|
-
if (this.initialized) return false;
|
|
257
|
-
this.initialized = true;
|
|
258
|
-
let isEmpty = true;
|
|
259
|
-
if (!!(initialValue == null ? void 0 : initialValue.size) || !this.cacheDisabled) {
|
|
260
|
-
const dataStr = this.name ? (_a = this.storage) == null ? void 0 : _a.getItem(this.name) : null;
|
|
261
|
-
const existingValue = this.read(dataStr);
|
|
262
|
-
if ((0, import_core6.isDefined)(dataStr)) initialValue = existingValue;
|
|
263
|
-
isEmpty = this.cacheDisabled || existingValue.size === 0;
|
|
264
|
-
}
|
|
265
|
-
(initialValue == null ? void 0 : initialValue.size) && this.subject.next(initialValue);
|
|
266
|
-
unsubscribeAll_default(this.subscriptions);
|
|
267
|
-
if (!this.cacheDisabled) {
|
|
268
|
-
this.subscriptions.forceUpdateCache = forceUpdateCache$.subscribe(
|
|
269
|
-
this.handleForceUpdateCacheChange
|
|
270
|
-
);
|
|
271
|
-
}
|
|
272
|
-
this.subscriptions.subject = this.subject.pipe((0, import_rxjs.skip)(this.cacheDisabled || isEmpty ? 0 : 1)).subscribe(
|
|
273
|
-
!this.cacheDisabled && this.delay > 0 ? (0, import_core6.deferred)(this.handleSubjectChange, this.delay, {
|
|
274
|
-
thisArg: this,
|
|
275
|
-
...this.delayOptions
|
|
276
|
-
}) : this.handleSubjectChange
|
|
277
|
-
);
|
|
278
|
-
return true;
|
|
279
|
-
};
|
|
280
|
-
this.keys = () => (0, import_core6.getKeys)(this.getAll());
|
|
281
|
-
this.map = (callback) => this.toArray().map(
|
|
282
|
-
([key, value], index, entries) => callback(value, key, entries, index)
|
|
283
|
-
);
|
|
284
|
-
this.read = (dataStr = this.name ? ((_a) => (_a = this.storage) == null ? void 0 : _a.getItem(this.name))() : null) => {
|
|
285
|
-
var _a2;
|
|
286
|
-
if (!this.name) {
|
|
287
|
-
return (_a2 = this.subject.value) != null ? _a2 : /* @__PURE__ */ new Map();
|
|
288
|
-
}
|
|
289
|
-
const data = (0, import_core6.fallbackIfFails)(
|
|
290
|
-
(() => {
|
|
291
|
-
var _a3;
|
|
292
|
-
return (_a3 = this.parse) == null ? void 0 : _a3.call(this, dataStr);
|
|
293
|
-
}),
|
|
294
|
-
[],
|
|
295
|
-
this.triggerOnError("parse" /* parse */)
|
|
296
|
-
);
|
|
297
|
-
if ((0, import_core6.isMap)(data)) return data;
|
|
298
|
-
if (!(0, import_core6.isStr)(dataStr)) return /* @__PURE__ */ new Map();
|
|
299
|
-
return new Map(
|
|
300
|
-
(0, import_core6.fallbackIfFails)(
|
|
301
|
-
() => JSON.parse(dataStr),
|
|
302
|
-
[],
|
|
303
|
-
this.triggerOnError("parse-json" /* parse_json */)
|
|
304
|
-
)
|
|
305
|
-
);
|
|
306
|
-
};
|
|
307
|
-
this.search = (...args) => (0, import_core6.search)(this.getAll(), ...args);
|
|
308
|
-
this.set = (key, value) => this.setAll(/* @__PURE__ */ new Map([[key, value]]), false);
|
|
309
|
-
this.setAll = (data, replace = false) => {
|
|
310
|
-
if (!(0, import_core6.isMap)(data)) return this;
|
|
311
|
-
data = replace ? data : (0, import_core6.mapJoin)(this.getAll(), data);
|
|
312
|
-
this.subject.next(new Map(data));
|
|
313
|
-
return this;
|
|
314
|
-
};
|
|
315
|
-
this.sort = (...args) => {
|
|
316
|
-
var _a;
|
|
317
|
-
const result = (0, import_core6.sort)(
|
|
318
|
-
this.getAll(),
|
|
319
|
-
args[0],
|
|
320
|
-
args[1]
|
|
321
|
-
);
|
|
322
|
-
((_a = args[1]) == null ? void 0 : _a.save) && this.setAll(result, true);
|
|
323
|
-
return result;
|
|
324
|
-
};
|
|
325
|
-
this.toArray = () => (0, import_core6.getEntries)(this.getAll());
|
|
326
|
-
this.toJSON = (replacer, spacing = this.spaces, data = this.getAll()) => {
|
|
327
|
-
const str = (0, import_core6.fallbackIfFails)(
|
|
328
|
-
(() => {
|
|
329
|
-
var _a;
|
|
330
|
-
return (_a = this.stringify) == null ? void 0 : _a.call(this, data);
|
|
331
|
-
}),
|
|
332
|
-
[],
|
|
333
|
-
this.triggerOnError("stringify" /* stringify */)
|
|
334
|
-
);
|
|
335
|
-
if ((0, import_core6.isStr)(str)) return str;
|
|
336
|
-
return (0, import_core6.fallbackIfFails)(
|
|
337
|
-
() => JSON.stringify(
|
|
338
|
-
Array.from(data),
|
|
339
|
-
replacer,
|
|
340
|
-
spacing
|
|
341
|
-
),
|
|
342
|
-
[],
|
|
343
|
-
this.triggerOnError("stringify-json" /* stringify_json */, "")
|
|
344
|
-
);
|
|
345
|
-
};
|
|
346
|
-
this.toObject = (data = this.getAll()) => {
|
|
347
|
-
const obj = {};
|
|
348
|
-
if (!(0, import_core6.isMap)(data)) return obj;
|
|
349
|
-
for (const [key, value] of data)
|
|
350
|
-
obj[key] = value;
|
|
351
|
-
return obj;
|
|
352
|
-
};
|
|
353
|
-
this.toString = (data = this.getAll()) => this.toJSON(void 0, void 0, data);
|
|
354
|
-
this.triggerOnError = (type, returnValue = void 0) => (err) => {
|
|
355
|
-
var _a;
|
|
356
|
-
(0, import_core6.fallbackIfFails)(
|
|
357
|
-
(_a = this.onError) == null ? void 0 : _a.bind(this),
|
|
358
|
-
[err, type],
|
|
359
|
-
""
|
|
360
|
-
);
|
|
361
|
-
return returnValue;
|
|
362
|
-
};
|
|
363
|
-
this.unsubscribe = () => unsubscribeAll_default(this.subscriptions);
|
|
364
|
-
this.values = () => (0, import_core6.getValues)(this.getAll());
|
|
365
|
-
this.write = (data) => {
|
|
366
|
-
var _a;
|
|
367
|
-
try {
|
|
368
|
-
!this.initialized && this.init();
|
|
369
|
-
const finalData = data != null ? data : (_a = this.subject) == null ? void 0 : _a.value;
|
|
370
|
-
if (!this.name || !this.storage || !(0, import_core6.isMap)(finalData)) return false;
|
|
371
|
-
this.storage.setItem(this.name, this.toString(finalData));
|
|
372
|
-
return true;
|
|
373
|
-
} catch (err) {
|
|
374
|
-
this.triggerOnError("write" /* write */)(err);
|
|
375
|
-
return false;
|
|
376
|
-
}
|
|
377
|
-
};
|
|
378
|
-
const {
|
|
379
|
-
cacheDisabled = false,
|
|
380
|
-
delay,
|
|
381
|
-
delayOptions,
|
|
382
|
-
initialValue,
|
|
383
|
-
onError,
|
|
384
|
-
onChange,
|
|
385
|
-
parse,
|
|
386
|
-
spaces,
|
|
387
|
-
storage,
|
|
388
|
-
stringify
|
|
389
|
-
} = options != null ? options : {};
|
|
390
|
-
this.delay = delay === 0 || (0, import_core6.isPositiveNumber)(delay) ? delay : 300;
|
|
391
|
-
this.name = `${name != null ? name : ""}`.trim();
|
|
392
|
-
this.onError = onError;
|
|
393
|
-
this.onChange = onChange;
|
|
394
|
-
this.parse = parse;
|
|
395
|
-
this.storage = storage === null ? null : storage != null ? storage : (0, import_core6.fallbackIfFails)(
|
|
396
|
-
() => globalThis.localStorage,
|
|
397
|
-
[],
|
|
398
|
-
void 0
|
|
399
|
-
);
|
|
400
|
-
if (this.name && !this.storage)
|
|
401
|
-
throw new Error(
|
|
402
|
-
"options.storage: LocalStorage instance or equivalent required"
|
|
403
|
-
);
|
|
404
|
-
this.cacheDisabled = !!this.storage && cacheDisabled;
|
|
405
|
-
this.stringify = stringify;
|
|
406
|
-
this.spaces = spaces;
|
|
407
|
-
this.subject = this.cacheDisabled ? new import_rxjs.Subject() : new import_rxjs.BehaviorSubject(void 0);
|
|
408
|
-
this.delayOptions = delayOptions;
|
|
409
|
-
(0, import_core6.isMap)(initialValue) && initialValue.size && this.init(initialValue);
|
|
410
|
-
}
|
|
411
|
-
get size() {
|
|
412
|
-
return this.getAll().size;
|
|
413
|
-
}
|
|
414
|
-
};
|
|
415
|
-
/**
|
|
416
|
-
* Creates a {@link DataStorage} instance initialized from a plain object.
|
|
417
|
-
*
|
|
418
|
-
* This factory method automatically configures `parse` and `stringify` logic to
|
|
419
|
-
* treat the underlying storage as a serialized object, while providing a
|
|
420
|
-
* type-safe Map-like interface for individual properties.
|
|
421
|
-
*
|
|
422
|
-
* This default behavior can be overridden by providing custom `parse` and `stringify` implementations in `options`.
|
|
423
|
-
*
|
|
424
|
-
* @param name (optional) The name for the storage (e.g., localStorage key or filename).
|
|
425
|
-
* @param options (optional) Configuration options for the storage instance.
|
|
426
|
-
* @param options.initialValue (optional) An optional object to populate the storage if it's currently empty.
|
|
427
|
-
*
|
|
428
|
-
* @template T (optional) The structure of the object being stored. Can auto-infer from `options.initialValue`.
|
|
429
|
-
* @template CacheDisabled (optional) Literal type determining whether to disable in-memory caching.
|
|
430
|
-
*
|
|
431
|
-
* @returns A new DataStorage instance mapped to the object's keys and values.
|
|
432
|
-
*
|
|
433
|
-
* @example
|
|
434
|
-
* #### Store and access a User object
|
|
435
|
-
* ```typescript
|
|
436
|
-
* import { DataStorage } from '@superutils/rx'
|
|
437
|
-
*
|
|
438
|
-
* interface User {
|
|
439
|
-
* age: number;
|
|
440
|
-
* name: string;
|
|
441
|
-
* }
|
|
442
|
-
*
|
|
443
|
-
* const initialValue: User = {
|
|
444
|
-
* age: 99,
|
|
445
|
-
* name: 'Ninety Nine'
|
|
446
|
-
* }
|
|
447
|
-
*
|
|
448
|
-
* // Initialize storage from the object
|
|
449
|
-
* const storage = DataStorage.fromObject<User>('user-profile', { initialValue })
|
|
450
|
-
*
|
|
451
|
-
* // Keys are inferred from the User interface
|
|
452
|
-
* const name = storage.get('name') // Inferred as: string | undefined
|
|
453
|
-
* console.log(name) // Prints: 'Ninety Nine'
|
|
454
|
-
*
|
|
455
|
-
* // Update properties safely
|
|
456
|
-
* storage.set('age', 100)
|
|
457
|
-
*
|
|
458
|
-
* // Reconstruct the updated object
|
|
459
|
-
* const userObj = storage.toObject<User>()
|
|
460
|
-
* console.log(userObj) // { age: 100, name: 'Ninety Nine' }
|
|
461
|
-
* ```
|
|
462
|
-
*/
|
|
463
|
-
_DataStorage.fromObject = (name, options) => new _DataStorage(name, {
|
|
464
|
-
parse: (str) => (0, import_core6.objToMap)(JSON.parse(str != null ? str : "{}")),
|
|
465
|
-
stringify: function(data) {
|
|
466
|
-
return JSON.stringify(this.toObject(data));
|
|
467
|
-
},
|
|
468
|
-
...options,
|
|
469
|
-
initialValue: !(0, import_core6.isObj)(options == null ? void 0 : options.initialValue, true) ? options == null ? void 0 : options.initialValue : (0, import_core6.objToMap)(options.initialValue)
|
|
470
|
-
});
|
|
471
|
-
/**
|
|
472
|
-
* Trigger forced update of cached data from storage.
|
|
473
|
-
*
|
|
474
|
-
* @param name determines which cache-enabled storage instances to be updated.
|
|
475
|
-
* - name (`string` | `string[]`): update all instances with a specific name(s)
|
|
476
|
-
* - global (`true`): update all instances globally
|
|
477
|
-
*
|
|
478
|
-
* See {@link forceUpdateCache$} for more details.
|
|
479
|
-
*/
|
|
480
|
-
_DataStorage.forceUpdateCache = (name) => {
|
|
481
|
-
forceUpdateCache$.next(name);
|
|
482
|
-
};
|
|
483
|
-
var DataStorage = _DataStorage;
|
|
484
|
-
|
|
485
|
-
// src/data-storage/index.ts
|
|
486
|
-
var import_core7 = require("@superutils/core");
|
|
188
|
+
copyRx.IGNORE = IGNORE_UPDATE_SYMBOL;
|
|
487
189
|
|
|
488
190
|
// src/IntervalSubject.ts
|
|
489
|
-
var
|
|
191
|
+
var import_rxjs4 = require("rxjs");
|
|
192
|
+
var IntervalSubject = class extends import_rxjs4.BehaviorSubject {
|
|
490
193
|
constructor(autoStart, _delay = 1e3, initialValue = 0, incrementBy = 1) {
|
|
491
194
|
super(initialValue);
|
|
492
195
|
this.autoStart = autoStart;
|
|
@@ -529,7 +232,8 @@ var IntervalSubject = class extends import_rxjs.BehaviorSubject {
|
|
|
529
232
|
};
|
|
530
233
|
|
|
531
234
|
// src/IntervalRunner.ts
|
|
532
|
-
var
|
|
235
|
+
var import_core5 = require("@superutils/core");
|
|
236
|
+
var import_rxjs5 = require("rxjs");
|
|
533
237
|
var IntervalRunner = class {
|
|
534
238
|
constructor(taskFn, taskArgs, intervalMs, sequential = true, preExecute = true) {
|
|
535
239
|
this.taskFn = taskFn;
|
|
@@ -556,7 +260,7 @@ var IntervalRunner = class {
|
|
|
556
260
|
} catch (_err) {
|
|
557
261
|
err = _err;
|
|
558
262
|
}
|
|
559
|
-
(0,
|
|
263
|
+
(0, import_core5.fallbackIfFails)(
|
|
560
264
|
this.onResult,
|
|
561
265
|
[err != null ? err : null, result, this.runCount, once],
|
|
562
266
|
void 0
|
|
@@ -608,7 +312,7 @@ var IntervalRunner = class {
|
|
|
608
312
|
delayMs = Math.max((_a = newDelayMs != null ? newDelayMs : delayMs) != null ? _a : 0, this.minIntervalMs);
|
|
609
313
|
this.clearInterval();
|
|
610
314
|
const preExec = this.lastResult === void 0 && this.preExecute;
|
|
611
|
-
preExec && this.executeTask().catch(
|
|
315
|
+
preExec && this.executeTask().catch(import_core5.noop);
|
|
612
316
|
this.idInterval = !this.sequential ? setInterval(this.executeTask, delayMs) : !preExec ? setTimeout(this.executeTask, delayMs) : void 0;
|
|
613
317
|
});
|
|
614
318
|
return true;
|
|
@@ -626,26 +330,16 @@ var IntervalRunner = class {
|
|
|
626
330
|
this.clearInterval();
|
|
627
331
|
return this;
|
|
628
332
|
};
|
|
629
|
-
this.intervalMs$ = intervalMs instanceof
|
|
333
|
+
this.intervalMs$ = intervalMs instanceof import_rxjs5.BehaviorSubject ? intervalMs : new import_rxjs5.BehaviorSubject(intervalMs);
|
|
630
334
|
}
|
|
631
335
|
};
|
|
632
336
|
// Annotate the CommonJS export names for ESM import in node:
|
|
633
337
|
0 && (module.exports = {
|
|
634
|
-
BehaviorSubject,
|
|
635
|
-
DataStorage,
|
|
636
338
|
IGNORE_UPDATE_SYMBOL,
|
|
637
339
|
IntervalRunner,
|
|
638
340
|
IntervalSubject,
|
|
639
|
-
OnErrorType,
|
|
640
|
-
Subject,
|
|
641
|
-
Subscription,
|
|
642
341
|
asPromise,
|
|
643
|
-
|
|
644
|
-
forceUpdateCache$,
|
|
645
|
-
isObservable,
|
|
646
|
-
isSubjectLike,
|
|
342
|
+
copyRx,
|
|
647
343
|
isSubscriptionLike,
|
|
648
|
-
objToMap,
|
|
649
|
-
skip,
|
|
650
344
|
unsubscribeAll
|
|
651
345
|
});
|