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