cojson-storage-indexeddb 0.10.6 → 0.10.8

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.
@@ -1,224 +0,0 @@
1
- const isFunction = (func: any) => typeof func === "function";
2
-
3
- const isObject = (supposedObject: any) =>
4
- typeof supposedObject === "object" &&
5
- supposedObject !== null &&
6
- !Array.isArray(supposedObject);
7
-
8
- const isThenable = (obj: any) => isObject(obj) && isFunction(obj.then);
9
-
10
- const identity = (co: any) => co;
11
-
12
- export { identity, isFunction, isObject, isThenable };
13
-
14
- enum States {
15
- PENDING = "PENDING",
16
- RESOLVED = "RESOLVED",
17
- REJECTED = "REJECTED",
18
- }
19
-
20
- interface Handler<T, U> {
21
- onSuccess: HandlerOnSuccess<T, U>;
22
- onFail: HandlerOnFail<U>;
23
- }
24
-
25
- type HandlerOnSuccess<T, U = any> = (value: T) => U | Thenable<U>;
26
- type HandlerOnFail<U = any> = (reason: any) => U | Thenable<U>;
27
- type Finally<U> = () => U | Thenable<U>;
28
-
29
- interface Thenable<T> {
30
- then<U>(
31
- onSuccess?: HandlerOnSuccess<T, U>,
32
- onFail?: HandlerOnFail<U>,
33
- ): Thenable<U>;
34
- then<U>(
35
- onSuccess?: HandlerOnSuccess<T, U>,
36
- onFail?: (reason: any) => void,
37
- ): Thenable<U>;
38
- }
39
-
40
- type Resolve<R> = (value?: R | Thenable<R>) => void;
41
- type Reject = (value?: any) => void;
42
-
43
- export class SyncPromise<T> {
44
- private state: States = States.PENDING;
45
- private handlers: Handler<T, any>[] = [];
46
- private value: T | any;
47
-
48
- public constructor(callback: (resolve: Resolve<T>, reject: Reject) => void) {
49
- try {
50
- callback(this.resolve as Resolve<T>, this.reject);
51
- } catch (e) {
52
- this.reject(e);
53
- }
54
- }
55
-
56
- private resolve = (value: T) => {
57
- return this.setResult(value, States.RESOLVED);
58
- };
59
-
60
- private reject = (reason: any) => {
61
- return this.setResult(reason, States.REJECTED);
62
- };
63
-
64
- private setResult = (value: T | any, state: States) => {
65
- const set = () => {
66
- if (this.state !== States.PENDING) {
67
- return null;
68
- }
69
-
70
- if (isThenable(value)) {
71
- return (value as Thenable<T>).then(this.resolve, this.reject);
72
- }
73
-
74
- this.value = value;
75
- this.state = state;
76
-
77
- return this.executeHandlers();
78
- };
79
-
80
- void set();
81
- };
82
-
83
- private executeHandlers = () => {
84
- if (this.state === States.PENDING) {
85
- return null;
86
- }
87
-
88
- for (const handler of this.handlers) {
89
- if (this.state === States.REJECTED) {
90
- handler.onFail(this.value);
91
- } else {
92
- handler.onSuccess(this.value);
93
- }
94
- }
95
-
96
- this.handlers = [];
97
- };
98
-
99
- private attachHandler = (handler: Handler<T, any>) => {
100
- this.handlers = [...this.handlers, handler];
101
-
102
- this.executeHandlers();
103
- };
104
-
105
- // biome-ignore lint/suspicious/noThenProperty: TODO(JAZZ-561): Review
106
- public then<U>(onSuccess: HandlerOnSuccess<T, U>, onFail?: HandlerOnFail<U>) {
107
- return new SyncPromise<U>((resolve, reject) => {
108
- return this.attachHandler({
109
- onSuccess: (result) => {
110
- try {
111
- return resolve(onSuccess(result));
112
- } catch (e) {
113
- return reject(e);
114
- }
115
- },
116
- onFail: (reason) => {
117
- if (!onFail) {
118
- return reject(reason);
119
- }
120
-
121
- try {
122
- return resolve(onFail(reason));
123
- } catch (e) {
124
- return reject(e);
125
- }
126
- },
127
- });
128
- });
129
- }
130
-
131
- public catch<U>(onFail: HandlerOnFail<U>) {
132
- return this.then<U>(identity, onFail);
133
- }
134
-
135
- // methods
136
-
137
- public toString() {
138
- return "[object SyncPromise]";
139
- }
140
-
141
- public finally<U>(cb: Finally<U>) {
142
- return new SyncPromise<U>((resolve, reject) => {
143
- let co: U | any;
144
- let isRejected: boolean;
145
-
146
- return this.then(
147
- (value) => {
148
- isRejected = false;
149
- co = value;
150
- return cb();
151
- },
152
- (reason) => {
153
- isRejected = true;
154
- co = reason;
155
- return cb();
156
- },
157
- ).then(() => {
158
- if (isRejected) {
159
- return reject(co);
160
- }
161
-
162
- return resolve(co);
163
- });
164
- });
165
- }
166
-
167
- public spread<U>(handler: (...args: any[]) => U) {
168
- return this.then<U>((collection) => {
169
- if (Array.isArray(collection)) {
170
- return handler(...collection);
171
- }
172
-
173
- return handler(collection);
174
- });
175
- }
176
-
177
- // static
178
-
179
- public static resolve<U = any>(value?: U | Thenable<U>) {
180
- return new SyncPromise<U>((resolve) => {
181
- return resolve(value);
182
- });
183
- }
184
-
185
- public static reject<U>(reason?: any) {
186
- return new SyncPromise<U>((_resolve, reject) => {
187
- return reject(reason);
188
- });
189
- }
190
-
191
- public static all<U = any>(collection: (U | Thenable<U>)[]) {
192
- return new SyncPromise<U[]>((resolve, reject) => {
193
- if (!Array.isArray(collection)) {
194
- return reject(new TypeError("An array must be provided."));
195
- }
196
-
197
- if (collection.length === 0) {
198
- return resolve([]);
199
- }
200
-
201
- let counter = collection.length;
202
- const resolvedCollection: U[] = [];
203
-
204
- const tryResolve = (value: U, index: number) => {
205
- counter -= 1;
206
- resolvedCollection[index] = value;
207
-
208
- if (counter !== 0) {
209
- return null;
210
- }
211
-
212
- return resolve(resolvedCollection);
213
- };
214
-
215
- return collection.forEach((item, index) => {
216
- return SyncPromise.resolve(item)
217
- .then((value) => {
218
- return tryResolve(value, index);
219
- })
220
- .catch(reject);
221
- });
222
- });
223
- }
224
- }