@gaddario98/react-core 2.0.0 → 2.0.2
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/form/index.js +4566 -1
- package/dist/form/index.js.map +1 -1
- package/dist/form/index.mjs +4566 -1
- package/dist/form/index.mjs.map +1 -1
- package/dist/index.d.ts +2152 -7
- package/dist/index.js +20713 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +20713 -1
- package/dist/index.mjs.map +1 -1
- package/dist/localization/index.js +318 -1
- package/dist/localization/index.js.map +1 -1
- package/dist/localization/index.mjs +318 -1
- package/dist/localization/index.mjs.map +1 -1
- package/dist/notifications/index.js +84 -0
- package/dist/notifications/index.js.map +1 -0
- package/dist/notifications/index.mjs +84 -0
- package/dist/notifications/index.mjs.map +1 -0
- package/dist/pages/index.js +4652 -1
- package/dist/pages/index.js.map +1 -1
- package/dist/pages/index.mjs +4652 -1
- package/dist/pages/index.mjs.map +1 -1
- package/dist/providers/index.d.ts +12 -1
- package/dist/providers/index.js +17 -1
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/index.mjs +17 -1
- package/dist/providers/index.mjs.map +1 -1
- package/dist/queries/index.js +8903 -1
- package/dist/queries/index.js.map +1 -1
- package/dist/queries/index.mjs +8903 -1
- package/dist/queries/index.mjs.map +1 -1
- package/dist/state/index.js +1927 -1
- package/dist/state/index.js.map +1 -1
- package/dist/state/index.mjs +1927 -1
- package/dist/state/index.mjs.map +1 -1
- package/dist/utiles/index.d.ts +15 -1
- package/dist/utiles/index.js +2973 -1
- package/dist/utiles/index.js.map +1 -1
- package/dist/utiles/index.mjs +2973 -1
- package/dist/utiles/index.mjs.map +1 -1
- package/package.json +12 -6
- package/dist/form/index.d.ts +0 -1
- package/dist/localization/index.d.ts +0 -1
- package/dist/pages/index.d.ts +0 -1
- package/dist/queries/index.d.ts +0 -1
- package/dist/state/index.d.ts +0 -1
package/dist/state/index.js
CHANGED
|
@@ -1 +1,1927 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var compilerRuntime=require('react/compiler-runtime'),React=require('react');// DEFLATE is a complex format; to read this code, you should probably check the RFC first:
|
|
2
|
+
// https://tools.ietf.org/html/rfc1951
|
|
3
|
+
// You may also wish to take a look at the guide I made about this program:
|
|
4
|
+
// https://gist.github.com/101arrowz/253f31eb5abc3d9275ab943003ffecad
|
|
5
|
+
// Some of the following code is similar to that of UZIP.js:
|
|
6
|
+
// https://github.com/photopea/UZIP.js
|
|
7
|
+
// However, the vast majority of the codebase has diverged from UZIP.js to increase performance and reduce bundle size.
|
|
8
|
+
// Sometimes 0 will appear where -1 would be more appropriate. This is because using a uint
|
|
9
|
+
// is better for memory in most engines (I *think*).
|
|
10
|
+
|
|
11
|
+
// aliases for shorter compressed code (most minifers don't do this)
|
|
12
|
+
var u8 = Uint8Array,
|
|
13
|
+
u16 = Uint16Array,
|
|
14
|
+
i32 = Int32Array;
|
|
15
|
+
// fixed length extra bits
|
|
16
|
+
var fleb = new u8([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, /* unused */0, 0, /* impossible */0]);
|
|
17
|
+
// fixed distance extra bits
|
|
18
|
+
var fdeb = new u8([0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, /* unused */0, 0]);
|
|
19
|
+
// code length index map
|
|
20
|
+
var clim = new u8([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]);
|
|
21
|
+
// get base, reverse index map from extra bits
|
|
22
|
+
var freb = function (eb, start) {
|
|
23
|
+
var b = new u16(31);
|
|
24
|
+
for (var i = 0; i < 31; ++i) {
|
|
25
|
+
b[i] = start += 1 << eb[i - 1];
|
|
26
|
+
}
|
|
27
|
+
// numbers here are at max 18 bits
|
|
28
|
+
var r = new i32(b[30]);
|
|
29
|
+
for (var i = 1; i < 30; ++i) {
|
|
30
|
+
for (var j = b[i]; j < b[i + 1]; ++j) {
|
|
31
|
+
r[j] = j - b[i] << 5 | i;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
b: b,
|
|
36
|
+
r: r
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
var _a = freb(fleb, 2),
|
|
40
|
+
fl = _a.b,
|
|
41
|
+
revfl = _a.r;
|
|
42
|
+
// we can ignore the fact that the other numbers are wrong; they never happen anyway
|
|
43
|
+
fl[28] = 258, revfl[258] = 28;
|
|
44
|
+
var _b = freb(fdeb, 0),
|
|
45
|
+
fd = _b.b,
|
|
46
|
+
revfd = _b.r;
|
|
47
|
+
// map of value to reverse (assuming 16 bits)
|
|
48
|
+
var rev = new u16(32768);
|
|
49
|
+
for (var i = 0; i < 32768; ++i) {
|
|
50
|
+
// reverse table algorithm from SO
|
|
51
|
+
var x = (i & 0xAAAA) >> 1 | (i & 0x5555) << 1;
|
|
52
|
+
x = (x & 0xCCCC) >> 2 | (x & 0x3333) << 2;
|
|
53
|
+
x = (x & 0xF0F0) >> 4 | (x & 0x0F0F) << 4;
|
|
54
|
+
rev[i] = ((x & 0xFF00) >> 8 | (x & 0x00FF) << 8) >> 1;
|
|
55
|
+
}
|
|
56
|
+
// create huffman tree from u8 "map": index -> code length for code index
|
|
57
|
+
// mb (max bits) must be at most 15
|
|
58
|
+
// TODO: optimize/split up?
|
|
59
|
+
var hMap = function (cd, mb, r) {
|
|
60
|
+
var s = cd.length;
|
|
61
|
+
// index
|
|
62
|
+
var i = 0;
|
|
63
|
+
// u16 "map": index -> # of codes with bit length = index
|
|
64
|
+
var l = new u16(mb);
|
|
65
|
+
// length of cd must be 288 (total # of codes)
|
|
66
|
+
for (; i < s; ++i) {
|
|
67
|
+
if (cd[i]) ++l[cd[i] - 1];
|
|
68
|
+
}
|
|
69
|
+
// u16 "map": index -> minimum code for bit length = index
|
|
70
|
+
var le = new u16(mb);
|
|
71
|
+
for (i = 1; i < mb; ++i) {
|
|
72
|
+
le[i] = le[i - 1] + l[i - 1] << 1;
|
|
73
|
+
}
|
|
74
|
+
var co;
|
|
75
|
+
if (r) {
|
|
76
|
+
// u16 "map": index -> number of actual bits, symbol for code
|
|
77
|
+
co = new u16(1 << mb);
|
|
78
|
+
// bits to remove for reverser
|
|
79
|
+
var rvb = 15 - mb;
|
|
80
|
+
for (i = 0; i < s; ++i) {
|
|
81
|
+
// ignore 0 lengths
|
|
82
|
+
if (cd[i]) {
|
|
83
|
+
// num encoding both symbol and bits read
|
|
84
|
+
var sv = i << 4 | cd[i];
|
|
85
|
+
// free bits
|
|
86
|
+
var r_1 = mb - cd[i];
|
|
87
|
+
// start value
|
|
88
|
+
var v = le[cd[i] - 1]++ << r_1;
|
|
89
|
+
// m is end value
|
|
90
|
+
for (var m = v | (1 << r_1) - 1; v <= m; ++v) {
|
|
91
|
+
// every 16 bit value starting with the code yields the same result
|
|
92
|
+
co[rev[v] >> rvb] = sv;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
} else {
|
|
97
|
+
co = new u16(s);
|
|
98
|
+
for (i = 0; i < s; ++i) {
|
|
99
|
+
if (cd[i]) {
|
|
100
|
+
co[i] = rev[le[cd[i] - 1]++] >> 15 - cd[i];
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return co;
|
|
105
|
+
};
|
|
106
|
+
// fixed length tree
|
|
107
|
+
var flt = new u8(288);
|
|
108
|
+
for (var i = 0; i < 144; ++i) flt[i] = 8;
|
|
109
|
+
for (var i = 144; i < 256; ++i) flt[i] = 9;
|
|
110
|
+
for (var i = 256; i < 280; ++i) flt[i] = 7;
|
|
111
|
+
for (var i = 280; i < 288; ++i) flt[i] = 8;
|
|
112
|
+
// fixed distance tree
|
|
113
|
+
var fdt = new u8(32);
|
|
114
|
+
for (var i = 0; i < 32; ++i) fdt[i] = 5;
|
|
115
|
+
// fixed length map
|
|
116
|
+
var flm = /*#__PURE__*/hMap(flt, 9, 0),
|
|
117
|
+
flrm = /*#__PURE__*/hMap(flt, 9, 1);
|
|
118
|
+
// fixed distance map
|
|
119
|
+
var fdm = /*#__PURE__*/hMap(fdt, 5, 0),
|
|
120
|
+
fdrm = /*#__PURE__*/hMap(fdt, 5, 1);
|
|
121
|
+
// find max of array
|
|
122
|
+
var max = function (a) {
|
|
123
|
+
var m = a[0];
|
|
124
|
+
for (var i = 1; i < a.length; ++i) {
|
|
125
|
+
if (a[i] > m) m = a[i];
|
|
126
|
+
}
|
|
127
|
+
return m;
|
|
128
|
+
};
|
|
129
|
+
// read d, starting at bit p and mask with m
|
|
130
|
+
var bits = function (d, p, m) {
|
|
131
|
+
var o = p / 8 | 0;
|
|
132
|
+
return (d[o] | d[o + 1] << 8) >> (p & 7) & m;
|
|
133
|
+
};
|
|
134
|
+
// read d, starting at bit p continuing for at least 16 bits
|
|
135
|
+
var bits16 = function (d, p) {
|
|
136
|
+
var o = p / 8 | 0;
|
|
137
|
+
return (d[o] | d[o + 1] << 8 | d[o + 2] << 16) >> (p & 7);
|
|
138
|
+
};
|
|
139
|
+
// get end of byte
|
|
140
|
+
var shft = function (p) {
|
|
141
|
+
return (p + 7) / 8 | 0;
|
|
142
|
+
};
|
|
143
|
+
// typed array slice - allows garbage collector to free original reference,
|
|
144
|
+
// while being more compatible than .slice
|
|
145
|
+
var slc = function (v, s, e) {
|
|
146
|
+
if (s == null || s < 0) s = 0;
|
|
147
|
+
if (e == null || e > v.length) e = v.length;
|
|
148
|
+
// can't use .constructor in case user-supplied
|
|
149
|
+
return new u8(v.subarray(s, e));
|
|
150
|
+
};
|
|
151
|
+
// error codes
|
|
152
|
+
var ec = ['unexpected EOF', 'invalid block type', 'invalid length/literal', 'invalid distance', 'stream finished', 'no stream handler',, 'no callback', 'invalid UTF-8 data', 'extra field too long', 'date not in range 1980-2099', 'filename too long', 'stream finishing', 'invalid zip data'
|
|
153
|
+
// determined by unknown compression method
|
|
154
|
+
];
|
|
155
|
+
var err = function (ind, msg, nt) {
|
|
156
|
+
var e = new Error(msg || ec[ind]);
|
|
157
|
+
e.code = ind;
|
|
158
|
+
if (Error.captureStackTrace) Error.captureStackTrace(e, err);
|
|
159
|
+
if (!nt) throw e;
|
|
160
|
+
return e;
|
|
161
|
+
};
|
|
162
|
+
// expands raw DEFLATE data
|
|
163
|
+
var inflt = function (dat, st, buf, dict) {
|
|
164
|
+
// source length dict length
|
|
165
|
+
var sl = dat.length,
|
|
166
|
+
dl = 0;
|
|
167
|
+
if (!sl || st.f && !st.l) return buf || new u8(0);
|
|
168
|
+
var noBuf = !buf;
|
|
169
|
+
// have to estimate size
|
|
170
|
+
var resize = noBuf || st.i != 2;
|
|
171
|
+
// Assumes roughly 33% compression ratio average
|
|
172
|
+
if (noBuf) buf = new u8(sl * 3);
|
|
173
|
+
// ensure buffer can fit at least l elements
|
|
174
|
+
var cbuf = function (l) {
|
|
175
|
+
var bl = buf.length;
|
|
176
|
+
// need to increase size to fit
|
|
177
|
+
if (l > bl) {
|
|
178
|
+
// Double or set to necessary, whichever is greater
|
|
179
|
+
var nbuf = new u8(Math.max(bl * 2, l));
|
|
180
|
+
nbuf.set(buf);
|
|
181
|
+
buf = nbuf;
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
// last chunk bitpos bytes
|
|
185
|
+
var final = st.f || 0,
|
|
186
|
+
pos = st.p || 0,
|
|
187
|
+
bt = st.b || 0,
|
|
188
|
+
lm = st.l,
|
|
189
|
+
dm = st.d,
|
|
190
|
+
lbt = st.m,
|
|
191
|
+
dbt = st.n;
|
|
192
|
+
// total bits
|
|
193
|
+
var tbts = sl * 8;
|
|
194
|
+
do {
|
|
195
|
+
if (!lm) {
|
|
196
|
+
// BFINAL - this is only 1 when last chunk is next
|
|
197
|
+
final = bits(dat, pos, 1);
|
|
198
|
+
// type: 0 = no compression, 1 = fixed huffman, 2 = dynamic huffman
|
|
199
|
+
var type = bits(dat, pos + 1, 3);
|
|
200
|
+
pos += 3;
|
|
201
|
+
if (!type) {
|
|
202
|
+
// go to end of byte boundary
|
|
203
|
+
var s = shft(pos) + 4,
|
|
204
|
+
l = dat[s - 4] | dat[s - 3] << 8,
|
|
205
|
+
t = s + l;
|
|
206
|
+
if (t > sl) {
|
|
207
|
+
err(0);
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
// ensure size
|
|
211
|
+
if (resize) cbuf(bt + l);
|
|
212
|
+
// Copy over uncompressed data
|
|
213
|
+
buf.set(dat.subarray(s, t), bt);
|
|
214
|
+
// Get new bitpos, update byte count
|
|
215
|
+
st.b = bt += l, st.p = pos = t * 8, st.f = final;
|
|
216
|
+
continue;
|
|
217
|
+
} else if (type == 1) lm = flrm, dm = fdrm, lbt = 9, dbt = 5;else if (type == 2) {
|
|
218
|
+
// literal lengths
|
|
219
|
+
var hLit = bits(dat, pos, 31) + 257,
|
|
220
|
+
hcLen = bits(dat, pos + 10, 15) + 4;
|
|
221
|
+
var tl = hLit + bits(dat, pos + 5, 31) + 1;
|
|
222
|
+
pos += 14;
|
|
223
|
+
// length+distance tree
|
|
224
|
+
var ldt = new u8(tl);
|
|
225
|
+
// code length tree
|
|
226
|
+
var clt = new u8(19);
|
|
227
|
+
for (var i = 0; i < hcLen; ++i) {
|
|
228
|
+
// use index map to get real code
|
|
229
|
+
clt[clim[i]] = bits(dat, pos + i * 3, 7);
|
|
230
|
+
}
|
|
231
|
+
pos += hcLen * 3;
|
|
232
|
+
// code lengths bits
|
|
233
|
+
var clb = max(clt),
|
|
234
|
+
clbmsk = (1 << clb) - 1;
|
|
235
|
+
// code lengths map
|
|
236
|
+
var clm = hMap(clt, clb, 1);
|
|
237
|
+
for (var i = 0; i < tl;) {
|
|
238
|
+
var r = clm[bits(dat, pos, clbmsk)];
|
|
239
|
+
// bits read
|
|
240
|
+
pos += r & 15;
|
|
241
|
+
// symbol
|
|
242
|
+
var s = r >> 4;
|
|
243
|
+
// code length to copy
|
|
244
|
+
if (s < 16) {
|
|
245
|
+
ldt[i++] = s;
|
|
246
|
+
} else {
|
|
247
|
+
// copy count
|
|
248
|
+
var c = 0,
|
|
249
|
+
n = 0;
|
|
250
|
+
if (s == 16) n = 3 + bits(dat, pos, 3), pos += 2, c = ldt[i - 1];else if (s == 17) n = 3 + bits(dat, pos, 7), pos += 3;else if (s == 18) n = 11 + bits(dat, pos, 127), pos += 7;
|
|
251
|
+
while (n--) ldt[i++] = c;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
// length tree distance tree
|
|
255
|
+
var lt = ldt.subarray(0, hLit),
|
|
256
|
+
dt = ldt.subarray(hLit);
|
|
257
|
+
// max length bits
|
|
258
|
+
lbt = max(lt);
|
|
259
|
+
// max dist bits
|
|
260
|
+
dbt = max(dt);
|
|
261
|
+
lm = hMap(lt, lbt, 1);
|
|
262
|
+
dm = hMap(dt, dbt, 1);
|
|
263
|
+
} else err(1);
|
|
264
|
+
if (pos > tbts) {
|
|
265
|
+
err(0);
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
// Make sure the buffer can hold this + the largest possible addition
|
|
270
|
+
// Maximum chunk size (practically, theoretically infinite) is 2^17
|
|
271
|
+
if (resize) cbuf(bt + 131072);
|
|
272
|
+
var lms = (1 << lbt) - 1,
|
|
273
|
+
dms = (1 << dbt) - 1;
|
|
274
|
+
var lpos = pos;
|
|
275
|
+
for (;; lpos = pos) {
|
|
276
|
+
// bits read, code
|
|
277
|
+
var c = lm[bits16(dat, pos) & lms],
|
|
278
|
+
sym = c >> 4;
|
|
279
|
+
pos += c & 15;
|
|
280
|
+
if (pos > tbts) {
|
|
281
|
+
err(0);
|
|
282
|
+
break;
|
|
283
|
+
}
|
|
284
|
+
if (!c) err(2);
|
|
285
|
+
if (sym < 256) buf[bt++] = sym;else if (sym == 256) {
|
|
286
|
+
lpos = pos, lm = null;
|
|
287
|
+
break;
|
|
288
|
+
} else {
|
|
289
|
+
var add = sym - 254;
|
|
290
|
+
// no extra bits needed if less
|
|
291
|
+
if (sym > 264) {
|
|
292
|
+
// index
|
|
293
|
+
var i = sym - 257,
|
|
294
|
+
b = fleb[i];
|
|
295
|
+
add = bits(dat, pos, (1 << b) - 1) + fl[i];
|
|
296
|
+
pos += b;
|
|
297
|
+
}
|
|
298
|
+
// dist
|
|
299
|
+
var d = dm[bits16(dat, pos) & dms],
|
|
300
|
+
dsym = d >> 4;
|
|
301
|
+
if (!d) err(3);
|
|
302
|
+
pos += d & 15;
|
|
303
|
+
var dt = fd[dsym];
|
|
304
|
+
if (dsym > 3) {
|
|
305
|
+
var b = fdeb[dsym];
|
|
306
|
+
dt += bits16(dat, pos) & (1 << b) - 1, pos += b;
|
|
307
|
+
}
|
|
308
|
+
if (pos > tbts) {
|
|
309
|
+
err(0);
|
|
310
|
+
break;
|
|
311
|
+
}
|
|
312
|
+
if (resize) cbuf(bt + 131072);
|
|
313
|
+
var end = bt + add;
|
|
314
|
+
if (bt < dt) {
|
|
315
|
+
var shift = dl - dt,
|
|
316
|
+
dend = Math.min(dt, end);
|
|
317
|
+
if (shift + bt < 0) err(3);
|
|
318
|
+
for (; bt < dend; ++bt) buf[bt] = dict[shift + bt];
|
|
319
|
+
}
|
|
320
|
+
for (; bt < end; ++bt) buf[bt] = buf[bt - dt];
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
st.l = lm, st.p = lpos, st.b = bt, st.f = final;
|
|
324
|
+
if (lm) final = 1, st.m = lbt, st.d = dm, st.n = dbt;
|
|
325
|
+
} while (!final);
|
|
326
|
+
// don't reallocate for streams or user buffers
|
|
327
|
+
return bt != buf.length && noBuf ? slc(buf, 0, bt) : buf.subarray(0, bt);
|
|
328
|
+
};
|
|
329
|
+
// starting at p, write the minimum number of bits that can hold v to d
|
|
330
|
+
var wbits = function (d, p, v) {
|
|
331
|
+
v <<= p & 7;
|
|
332
|
+
var o = p / 8 | 0;
|
|
333
|
+
d[o] |= v;
|
|
334
|
+
d[o + 1] |= v >> 8;
|
|
335
|
+
};
|
|
336
|
+
// starting at p, write the minimum number of bits (>8) that can hold v to d
|
|
337
|
+
var wbits16 = function (d, p, v) {
|
|
338
|
+
v <<= p & 7;
|
|
339
|
+
var o = p / 8 | 0;
|
|
340
|
+
d[o] |= v;
|
|
341
|
+
d[o + 1] |= v >> 8;
|
|
342
|
+
d[o + 2] |= v >> 16;
|
|
343
|
+
};
|
|
344
|
+
// creates code lengths from a frequency table
|
|
345
|
+
var hTree = function (d, mb) {
|
|
346
|
+
// Need extra info to make a tree
|
|
347
|
+
var t = [];
|
|
348
|
+
for (var i = 0; i < d.length; ++i) {
|
|
349
|
+
if (d[i]) t.push({
|
|
350
|
+
s: i,
|
|
351
|
+
f: d[i]
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
var s = t.length;
|
|
355
|
+
var t2 = t.slice();
|
|
356
|
+
if (!s) return {
|
|
357
|
+
t: et,
|
|
358
|
+
l: 0
|
|
359
|
+
};
|
|
360
|
+
if (s == 1) {
|
|
361
|
+
var v = new u8(t[0].s + 1);
|
|
362
|
+
v[t[0].s] = 1;
|
|
363
|
+
return {
|
|
364
|
+
t: v,
|
|
365
|
+
l: 1
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
t.sort(function (a, b) {
|
|
369
|
+
return a.f - b.f;
|
|
370
|
+
});
|
|
371
|
+
// after i2 reaches last ind, will be stopped
|
|
372
|
+
// freq must be greater than largest possible number of symbols
|
|
373
|
+
t.push({
|
|
374
|
+
s: -1,
|
|
375
|
+
f: 25001
|
|
376
|
+
});
|
|
377
|
+
var l = t[0],
|
|
378
|
+
r = t[1],
|
|
379
|
+
i0 = 0,
|
|
380
|
+
i1 = 1,
|
|
381
|
+
i2 = 2;
|
|
382
|
+
t[0] = {
|
|
383
|
+
s: -1,
|
|
384
|
+
f: l.f + r.f,
|
|
385
|
+
l: l,
|
|
386
|
+
r: r
|
|
387
|
+
};
|
|
388
|
+
// efficient algorithm from UZIP.js
|
|
389
|
+
// i0 is lookbehind, i2 is lookahead - after processing two low-freq
|
|
390
|
+
// symbols that combined have high freq, will start processing i2 (high-freq,
|
|
391
|
+
// non-composite) symbols instead
|
|
392
|
+
// see https://reddit.com/r/photopea/comments/ikekht/uzipjs_questions/
|
|
393
|
+
while (i1 != s - 1) {
|
|
394
|
+
l = t[t[i0].f < t[i2].f ? i0++ : i2++];
|
|
395
|
+
r = t[i0 != i1 && t[i0].f < t[i2].f ? i0++ : i2++];
|
|
396
|
+
t[i1++] = {
|
|
397
|
+
s: -1,
|
|
398
|
+
f: l.f + r.f,
|
|
399
|
+
l: l,
|
|
400
|
+
r: r
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
var maxSym = t2[0].s;
|
|
404
|
+
for (var i = 1; i < s; ++i) {
|
|
405
|
+
if (t2[i].s > maxSym) maxSym = t2[i].s;
|
|
406
|
+
}
|
|
407
|
+
// code lengths
|
|
408
|
+
var tr = new u16(maxSym + 1);
|
|
409
|
+
// max bits in tree
|
|
410
|
+
var mbt = ln(t[i1 - 1], tr, 0);
|
|
411
|
+
if (mbt > mb) {
|
|
412
|
+
// more algorithms from UZIP.js
|
|
413
|
+
// TODO: find out how this code works (debt)
|
|
414
|
+
// ind debt
|
|
415
|
+
var i = 0,
|
|
416
|
+
dt = 0;
|
|
417
|
+
// left cost
|
|
418
|
+
var lft = mbt - mb,
|
|
419
|
+
cst = 1 << lft;
|
|
420
|
+
t2.sort(function (a, b) {
|
|
421
|
+
return tr[b.s] - tr[a.s] || a.f - b.f;
|
|
422
|
+
});
|
|
423
|
+
for (; i < s; ++i) {
|
|
424
|
+
var i2_1 = t2[i].s;
|
|
425
|
+
if (tr[i2_1] > mb) {
|
|
426
|
+
dt += cst - (1 << mbt - tr[i2_1]);
|
|
427
|
+
tr[i2_1] = mb;
|
|
428
|
+
} else break;
|
|
429
|
+
}
|
|
430
|
+
dt >>= lft;
|
|
431
|
+
while (dt > 0) {
|
|
432
|
+
var i2_2 = t2[i].s;
|
|
433
|
+
if (tr[i2_2] < mb) dt -= 1 << mb - tr[i2_2]++ - 1;else ++i;
|
|
434
|
+
}
|
|
435
|
+
for (; i >= 0 && dt; --i) {
|
|
436
|
+
var i2_3 = t2[i].s;
|
|
437
|
+
if (tr[i2_3] == mb) {
|
|
438
|
+
--tr[i2_3];
|
|
439
|
+
++dt;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
mbt = mb;
|
|
443
|
+
}
|
|
444
|
+
return {
|
|
445
|
+
t: new u8(tr),
|
|
446
|
+
l: mbt
|
|
447
|
+
};
|
|
448
|
+
};
|
|
449
|
+
// get the max length and assign length codes
|
|
450
|
+
var ln = function (n, l, d) {
|
|
451
|
+
return n.s == -1 ? Math.max(ln(n.l, l, d + 1), ln(n.r, l, d + 1)) : l[n.s] = d;
|
|
452
|
+
};
|
|
453
|
+
// length codes generation
|
|
454
|
+
var lc = function (c) {
|
|
455
|
+
var s = c.length;
|
|
456
|
+
// Note that the semicolon was intentional
|
|
457
|
+
while (s && !c[--s]);
|
|
458
|
+
var cl = new u16(++s);
|
|
459
|
+
// ind num streak
|
|
460
|
+
var cli = 0,
|
|
461
|
+
cln = c[0],
|
|
462
|
+
cls = 1;
|
|
463
|
+
var w = function (v) {
|
|
464
|
+
cl[cli++] = v;
|
|
465
|
+
};
|
|
466
|
+
for (var i = 1; i <= s; ++i) {
|
|
467
|
+
if (c[i] == cln && i != s) ++cls;else {
|
|
468
|
+
if (!cln && cls > 2) {
|
|
469
|
+
for (; cls > 138; cls -= 138) w(32754);
|
|
470
|
+
if (cls > 2) {
|
|
471
|
+
w(cls > 10 ? cls - 11 << 5 | 28690 : cls - 3 << 5 | 12305);
|
|
472
|
+
cls = 0;
|
|
473
|
+
}
|
|
474
|
+
} else if (cls > 3) {
|
|
475
|
+
w(cln), --cls;
|
|
476
|
+
for (; cls > 6; cls -= 6) w(8304);
|
|
477
|
+
if (cls > 2) w(cls - 3 << 5 | 8208), cls = 0;
|
|
478
|
+
}
|
|
479
|
+
while (cls--) w(cln);
|
|
480
|
+
cls = 1;
|
|
481
|
+
cln = c[i];
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
return {
|
|
485
|
+
c: cl.subarray(0, cli),
|
|
486
|
+
n: s
|
|
487
|
+
};
|
|
488
|
+
};
|
|
489
|
+
// calculate the length of output from tree, code lengths
|
|
490
|
+
var clen = function (cf, cl) {
|
|
491
|
+
var l = 0;
|
|
492
|
+
for (var i = 0; i < cl.length; ++i) l += cf[i] * cl[i];
|
|
493
|
+
return l;
|
|
494
|
+
};
|
|
495
|
+
// writes a fixed block
|
|
496
|
+
// returns the new bit pos
|
|
497
|
+
var wfblk = function (out, pos, dat) {
|
|
498
|
+
// no need to write 00 as type: TypedArray defaults to 0
|
|
499
|
+
var s = dat.length;
|
|
500
|
+
var o = shft(pos + 2);
|
|
501
|
+
out[o] = s & 255;
|
|
502
|
+
out[o + 1] = s >> 8;
|
|
503
|
+
out[o + 2] = out[o] ^ 255;
|
|
504
|
+
out[o + 3] = out[o + 1] ^ 255;
|
|
505
|
+
for (var i = 0; i < s; ++i) out[o + i + 4] = dat[i];
|
|
506
|
+
return (o + 4 + s) * 8;
|
|
507
|
+
};
|
|
508
|
+
// writes a block
|
|
509
|
+
var wblk = function (dat, out, final, syms, lf, df, eb, li, bs, bl, p) {
|
|
510
|
+
wbits(out, p++, final);
|
|
511
|
+
++lf[256];
|
|
512
|
+
var _a = hTree(lf, 15),
|
|
513
|
+
dlt = _a.t,
|
|
514
|
+
mlb = _a.l;
|
|
515
|
+
var _b = hTree(df, 15),
|
|
516
|
+
ddt = _b.t,
|
|
517
|
+
mdb = _b.l;
|
|
518
|
+
var _c = lc(dlt),
|
|
519
|
+
lclt = _c.c,
|
|
520
|
+
nlc = _c.n;
|
|
521
|
+
var _d = lc(ddt),
|
|
522
|
+
lcdt = _d.c,
|
|
523
|
+
ndc = _d.n;
|
|
524
|
+
var lcfreq = new u16(19);
|
|
525
|
+
for (var i = 0; i < lclt.length; ++i) ++lcfreq[lclt[i] & 31];
|
|
526
|
+
for (var i = 0; i < lcdt.length; ++i) ++lcfreq[lcdt[i] & 31];
|
|
527
|
+
var _e = hTree(lcfreq, 7),
|
|
528
|
+
lct = _e.t,
|
|
529
|
+
mlcb = _e.l;
|
|
530
|
+
var nlcc = 19;
|
|
531
|
+
for (; nlcc > 4 && !lct[clim[nlcc - 1]]; --nlcc);
|
|
532
|
+
var flen = bl + 5 << 3;
|
|
533
|
+
var ftlen = clen(lf, flt) + clen(df, fdt) + eb;
|
|
534
|
+
var dtlen = clen(lf, dlt) + clen(df, ddt) + eb + 14 + 3 * nlcc + clen(lcfreq, lct) + 2 * lcfreq[16] + 3 * lcfreq[17] + 7 * lcfreq[18];
|
|
535
|
+
if (bs >= 0 && flen <= ftlen && flen <= dtlen) return wfblk(out, p, dat.subarray(bs, bs + bl));
|
|
536
|
+
var lm, ll, dm, dl;
|
|
537
|
+
wbits(out, p, 1 + (dtlen < ftlen)), p += 2;
|
|
538
|
+
if (dtlen < ftlen) {
|
|
539
|
+
lm = hMap(dlt, mlb, 0), ll = dlt, dm = hMap(ddt, mdb, 0), dl = ddt;
|
|
540
|
+
var llm = hMap(lct, mlcb, 0);
|
|
541
|
+
wbits(out, p, nlc - 257);
|
|
542
|
+
wbits(out, p + 5, ndc - 1);
|
|
543
|
+
wbits(out, p + 10, nlcc - 4);
|
|
544
|
+
p += 14;
|
|
545
|
+
for (var i = 0; i < nlcc; ++i) wbits(out, p + 3 * i, lct[clim[i]]);
|
|
546
|
+
p += 3 * nlcc;
|
|
547
|
+
var lcts = [lclt, lcdt];
|
|
548
|
+
for (var it = 0; it < 2; ++it) {
|
|
549
|
+
var clct = lcts[it];
|
|
550
|
+
for (var i = 0; i < clct.length; ++i) {
|
|
551
|
+
var len = clct[i] & 31;
|
|
552
|
+
wbits(out, p, llm[len]), p += lct[len];
|
|
553
|
+
if (len > 15) wbits(out, p, clct[i] >> 5 & 127), p += clct[i] >> 12;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
} else {
|
|
557
|
+
lm = flm, ll = flt, dm = fdm, dl = fdt;
|
|
558
|
+
}
|
|
559
|
+
for (var i = 0; i < li; ++i) {
|
|
560
|
+
var sym = syms[i];
|
|
561
|
+
if (sym > 255) {
|
|
562
|
+
var len = sym >> 18 & 31;
|
|
563
|
+
wbits16(out, p, lm[len + 257]), p += ll[len + 257];
|
|
564
|
+
if (len > 7) wbits(out, p, sym >> 23 & 31), p += fleb[len];
|
|
565
|
+
var dst = sym & 31;
|
|
566
|
+
wbits16(out, p, dm[dst]), p += dl[dst];
|
|
567
|
+
if (dst > 3) wbits16(out, p, sym >> 5 & 8191), p += fdeb[dst];
|
|
568
|
+
} else {
|
|
569
|
+
wbits16(out, p, lm[sym]), p += ll[sym];
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
wbits16(out, p, lm[256]);
|
|
573
|
+
return p + ll[256];
|
|
574
|
+
};
|
|
575
|
+
// deflate options (nice << 13) | chain
|
|
576
|
+
var deo = /*#__PURE__*/new i32([65540, 131080, 131088, 131104, 262176, 1048704, 1048832, 2114560, 2117632]);
|
|
577
|
+
// empty
|
|
578
|
+
var et = /*#__PURE__*/new u8(0);
|
|
579
|
+
// compresses data into a raw DEFLATE buffer
|
|
580
|
+
var dflt = function (dat, lvl, plvl, pre, post, st) {
|
|
581
|
+
var s = st.z || dat.length;
|
|
582
|
+
var o = new u8(pre + s + 5 * (1 + Math.ceil(s / 7000)) + post);
|
|
583
|
+
// writing to this writes to the output buffer
|
|
584
|
+
var w = o.subarray(pre, o.length - post);
|
|
585
|
+
var lst = st.l;
|
|
586
|
+
var pos = (st.r || 0) & 7;
|
|
587
|
+
if (lvl) {
|
|
588
|
+
if (pos) w[0] = st.r >> 3;
|
|
589
|
+
var opt = deo[lvl - 1];
|
|
590
|
+
var n = opt >> 13,
|
|
591
|
+
c = opt & 8191;
|
|
592
|
+
var msk_1 = (1 << plvl) - 1;
|
|
593
|
+
// prev 2-byte val map curr 2-byte val map
|
|
594
|
+
var prev = st.p || new u16(32768),
|
|
595
|
+
head = st.h || new u16(msk_1 + 1);
|
|
596
|
+
var bs1_1 = Math.ceil(plvl / 3),
|
|
597
|
+
bs2_1 = 2 * bs1_1;
|
|
598
|
+
var hsh = function (i) {
|
|
599
|
+
return (dat[i] ^ dat[i + 1] << bs1_1 ^ dat[i + 2] << bs2_1) & msk_1;
|
|
600
|
+
};
|
|
601
|
+
// 24576 is an arbitrary number of maximum symbols per block
|
|
602
|
+
// 424 buffer for last block
|
|
603
|
+
var syms = new i32(25000);
|
|
604
|
+
// length/literal freq distance freq
|
|
605
|
+
var lf = new u16(288),
|
|
606
|
+
df = new u16(32);
|
|
607
|
+
// l/lcnt exbits index l/lind waitdx blkpos
|
|
608
|
+
var lc_1 = 0,
|
|
609
|
+
eb = 0,
|
|
610
|
+
i = st.i || 0,
|
|
611
|
+
li = 0,
|
|
612
|
+
wi = st.w || 0,
|
|
613
|
+
bs = 0;
|
|
614
|
+
for (; i + 2 < s; ++i) {
|
|
615
|
+
// hash value
|
|
616
|
+
var hv = hsh(i);
|
|
617
|
+
// index mod 32768 previous index mod
|
|
618
|
+
var imod = i & 32767,
|
|
619
|
+
pimod = head[hv];
|
|
620
|
+
prev[imod] = pimod;
|
|
621
|
+
head[hv] = imod;
|
|
622
|
+
// We always should modify head and prev, but only add symbols if
|
|
623
|
+
// this data is not yet processed ("wait" for wait index)
|
|
624
|
+
if (wi <= i) {
|
|
625
|
+
// bytes remaining
|
|
626
|
+
var rem = s - i;
|
|
627
|
+
if ((lc_1 > 7000 || li > 24576) && (rem > 423 || !lst)) {
|
|
628
|
+
pos = wblk(dat, w, 0, syms, lf, df, eb, li, bs, i - bs, pos);
|
|
629
|
+
li = lc_1 = eb = 0, bs = i;
|
|
630
|
+
for (var j = 0; j < 286; ++j) lf[j] = 0;
|
|
631
|
+
for (var j = 0; j < 30; ++j) df[j] = 0;
|
|
632
|
+
}
|
|
633
|
+
// len dist chain
|
|
634
|
+
var l = 2,
|
|
635
|
+
d = 0,
|
|
636
|
+
ch_1 = c,
|
|
637
|
+
dif = imod - pimod & 32767;
|
|
638
|
+
if (rem > 2 && hv == hsh(i - dif)) {
|
|
639
|
+
var maxn = Math.min(n, rem) - 1;
|
|
640
|
+
var maxd = Math.min(32767, i);
|
|
641
|
+
// max possible length
|
|
642
|
+
// not capped at dif because decompressors implement "rolling" index population
|
|
643
|
+
var ml = Math.min(258, rem);
|
|
644
|
+
while (dif <= maxd && --ch_1 && imod != pimod) {
|
|
645
|
+
if (dat[i + l] == dat[i + l - dif]) {
|
|
646
|
+
var nl = 0;
|
|
647
|
+
for (; nl < ml && dat[i + nl] == dat[i + nl - dif]; ++nl);
|
|
648
|
+
if (nl > l) {
|
|
649
|
+
l = nl, d = dif;
|
|
650
|
+
// break out early when we reach "nice" (we are satisfied enough)
|
|
651
|
+
if (nl > maxn) break;
|
|
652
|
+
// now, find the rarest 2-byte sequence within this
|
|
653
|
+
// length of literals and search for that instead.
|
|
654
|
+
// Much faster than just using the start
|
|
655
|
+
var mmd = Math.min(dif, nl - 2);
|
|
656
|
+
var md = 0;
|
|
657
|
+
for (var j = 0; j < mmd; ++j) {
|
|
658
|
+
var ti = i - dif + j & 32767;
|
|
659
|
+
var pti = prev[ti];
|
|
660
|
+
var cd = ti - pti & 32767;
|
|
661
|
+
if (cd > md) md = cd, pimod = ti;
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
// check the previous match
|
|
666
|
+
imod = pimod, pimod = prev[imod];
|
|
667
|
+
dif += imod - pimod & 32767;
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
// d will be nonzero only when a match was found
|
|
671
|
+
if (d) {
|
|
672
|
+
// store both dist and len data in one int32
|
|
673
|
+
// Make sure this is recognized as a len/dist with 28th bit (2^28)
|
|
674
|
+
syms[li++] = 268435456 | revfl[l] << 18 | revfd[d];
|
|
675
|
+
var lin = revfl[l] & 31,
|
|
676
|
+
din = revfd[d] & 31;
|
|
677
|
+
eb += fleb[lin] + fdeb[din];
|
|
678
|
+
++lf[257 + lin];
|
|
679
|
+
++df[din];
|
|
680
|
+
wi = i + l;
|
|
681
|
+
++lc_1;
|
|
682
|
+
} else {
|
|
683
|
+
syms[li++] = dat[i];
|
|
684
|
+
++lf[dat[i]];
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
for (i = Math.max(i, wi); i < s; ++i) {
|
|
689
|
+
syms[li++] = dat[i];
|
|
690
|
+
++lf[dat[i]];
|
|
691
|
+
}
|
|
692
|
+
pos = wblk(dat, w, lst, syms, lf, df, eb, li, bs, i - bs, pos);
|
|
693
|
+
if (!lst) {
|
|
694
|
+
st.r = pos & 7 | w[pos / 8 | 0] << 3;
|
|
695
|
+
// shft(pos) now 1 less if pos & 7 != 0
|
|
696
|
+
pos -= 7;
|
|
697
|
+
st.h = head, st.p = prev, st.i = i, st.w = wi;
|
|
698
|
+
}
|
|
699
|
+
} else {
|
|
700
|
+
for (var i = st.w || 0; i < s + lst; i += 65535) {
|
|
701
|
+
// end
|
|
702
|
+
var e = i + 65535;
|
|
703
|
+
if (e >= s) {
|
|
704
|
+
// write final block
|
|
705
|
+
w[pos / 8 | 0] = lst;
|
|
706
|
+
e = s;
|
|
707
|
+
}
|
|
708
|
+
pos = wfblk(w, pos + 1, dat.subarray(i, e));
|
|
709
|
+
}
|
|
710
|
+
st.i = s;
|
|
711
|
+
}
|
|
712
|
+
return slc(o, 0, pre + shft(pos) + post);
|
|
713
|
+
};
|
|
714
|
+
// deflate with opts
|
|
715
|
+
var dopt = function (dat, opt, pre, post, st) {
|
|
716
|
+
if (!st) {
|
|
717
|
+
st = {
|
|
718
|
+
l: 1
|
|
719
|
+
};
|
|
720
|
+
if (opt.dictionary) {
|
|
721
|
+
var dict = opt.dictionary.subarray(-32768);
|
|
722
|
+
var newDat = new u8(dict.length + dat.length);
|
|
723
|
+
newDat.set(dict);
|
|
724
|
+
newDat.set(dat, dict.length);
|
|
725
|
+
dat = newDat;
|
|
726
|
+
st.w = dict.length;
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
return dflt(dat, opt.level == null ? 6 : opt.level, opt.mem == null ? st.l ? Math.ceil(Math.max(8, Math.min(13, Math.log(dat.length))) * 1.5) : 20 : 12 + opt.mem, pre, post, st);
|
|
730
|
+
};
|
|
731
|
+
/**
|
|
732
|
+
* Compresses data with DEFLATE without any wrapper
|
|
733
|
+
* @param data The data to compress
|
|
734
|
+
* @param opts The compression options
|
|
735
|
+
* @returns The deflated version of the data
|
|
736
|
+
*/
|
|
737
|
+
function deflateSync(data, opts) {
|
|
738
|
+
return dopt(data, opts || {}, 0, 0);
|
|
739
|
+
}
|
|
740
|
+
/**
|
|
741
|
+
* Expands DEFLATE data with no wrapper
|
|
742
|
+
* @param data The data to decompress
|
|
743
|
+
* @param opts The decompression options
|
|
744
|
+
* @returns The decompressed version of the data
|
|
745
|
+
*/
|
|
746
|
+
function inflateSync(data, opts) {
|
|
747
|
+
return inflt(data, {
|
|
748
|
+
i: 2
|
|
749
|
+
}, opts, opts);
|
|
750
|
+
}
|
|
751
|
+
// text encoder
|
|
752
|
+
var te = typeof TextEncoder != 'undefined' && /*#__PURE__*/new TextEncoder();
|
|
753
|
+
// text decoder
|
|
754
|
+
var td = typeof TextDecoder != 'undefined' && /*#__PURE__*/new TextDecoder();
|
|
755
|
+
try {
|
|
756
|
+
td.decode(et, {
|
|
757
|
+
stream: true
|
|
758
|
+
});
|
|
759
|
+
} catch (e) {}
|
|
760
|
+
// decode UTF8
|
|
761
|
+
var dutf8 = function (d) {
|
|
762
|
+
for (var r = '', i = 0;;) {
|
|
763
|
+
var c = d[i++];
|
|
764
|
+
var eb = (c > 127) + (c > 223) + (c > 239);
|
|
765
|
+
if (i + eb > d.length) return {
|
|
766
|
+
s: r,
|
|
767
|
+
r: slc(d, i - 1)
|
|
768
|
+
};
|
|
769
|
+
if (!eb) r += String.fromCharCode(c);else if (eb == 3) {
|
|
770
|
+
c = ((c & 15) << 18 | (d[i++] & 63) << 12 | (d[i++] & 63) << 6 | d[i++] & 63) - 65536, r += String.fromCharCode(55296 | c >> 10, 56320 | c & 1023);
|
|
771
|
+
} else if (eb & 1) r += String.fromCharCode((c & 31) << 6 | d[i++] & 63);else r += String.fromCharCode((c & 15) << 12 | (d[i++] & 63) << 6 | d[i++] & 63);
|
|
772
|
+
}
|
|
773
|
+
};
|
|
774
|
+
/**
|
|
775
|
+
* Converts a string into a Uint8Array for use with compression/decompression methods
|
|
776
|
+
* @param str The string to encode
|
|
777
|
+
* @param latin1 Whether or not to interpret the data as Latin-1. This should
|
|
778
|
+
* not need to be true unless decoding a binary string.
|
|
779
|
+
* @returns The string encoded in UTF-8/Latin-1 binary
|
|
780
|
+
*/
|
|
781
|
+
function strToU8(str, latin1) {
|
|
782
|
+
var i;
|
|
783
|
+
if (te) return te.encode(str);
|
|
784
|
+
var l = str.length;
|
|
785
|
+
var ar = new u8(str.length + (str.length >> 1));
|
|
786
|
+
var ai = 0;
|
|
787
|
+
var w = function (v) {
|
|
788
|
+
ar[ai++] = v;
|
|
789
|
+
};
|
|
790
|
+
for (var i = 0; i < l; ++i) {
|
|
791
|
+
if (ai + 5 > ar.length) {
|
|
792
|
+
var n = new u8(ai + 8 + (l - i << 1));
|
|
793
|
+
n.set(ar);
|
|
794
|
+
ar = n;
|
|
795
|
+
}
|
|
796
|
+
var c = str.charCodeAt(i);
|
|
797
|
+
if (c < 128 || latin1) w(c);else if (c < 2048) w(192 | c >> 6), w(128 | c & 63);else if (c > 55295 && c < 57344) c = 65536 + (c & 1023 << 10) | str.charCodeAt(++i) & 1023, w(240 | c >> 18), w(128 | c >> 12 & 63), w(128 | c >> 6 & 63), w(128 | c & 63);else w(224 | c >> 12), w(128 | c >> 6 & 63), w(128 | c & 63);
|
|
798
|
+
}
|
|
799
|
+
return slc(ar, 0, ai);
|
|
800
|
+
}
|
|
801
|
+
/**
|
|
802
|
+
* Converts a Uint8Array to a string
|
|
803
|
+
* @param dat The data to decode to string
|
|
804
|
+
* @param latin1 Whether or not to interpret the data as Latin-1. This should
|
|
805
|
+
* not need to be true unless encoding to binary string.
|
|
806
|
+
* @returns The original UTF-8/Latin-1 string
|
|
807
|
+
*/
|
|
808
|
+
function strFromU8(dat, latin1) {
|
|
809
|
+
var r; if (td) {
|
|
810
|
+
return td.decode(dat);
|
|
811
|
+
} else {
|
|
812
|
+
var _a = dutf8(dat),
|
|
813
|
+
s = _a.s,
|
|
814
|
+
r = _a.r;
|
|
815
|
+
if (r.length) err(8);
|
|
816
|
+
return s;
|
|
817
|
+
}
|
|
818
|
+
}const RAW_PREFIX = 'storage:raw:';
|
|
819
|
+
const DEFLATE_PREFIX = 'storage:deflate:v1:';
|
|
820
|
+
const isProbablyJson = value => {
|
|
821
|
+
if (!value) return false;
|
|
822
|
+
const c = value.charCodeAt(0);
|
|
823
|
+
// { [ " digits, t/f/n (true/false/null)
|
|
824
|
+
return c === 123 || c === 91 || c === 34 || c >= 48 && c <= 57 || c === 45 || c === 116 || c === 102 || c === 110;
|
|
825
|
+
};
|
|
826
|
+
const u8ToBase64 = bytes => {
|
|
827
|
+
let binary = '';
|
|
828
|
+
const chunkSize = 0x8000;
|
|
829
|
+
for (let i = 0; i < bytes.length; i += chunkSize) {
|
|
830
|
+
binary += String.fromCharCode(...bytes.subarray(i, i + chunkSize));
|
|
831
|
+
}
|
|
832
|
+
return btoa(binary);
|
|
833
|
+
};
|
|
834
|
+
const base64ToU8 = base64 => {
|
|
835
|
+
const binary = atob(base64);
|
|
836
|
+
const bytes = new Uint8Array(binary.length);
|
|
837
|
+
for (let i = 0; i < binary.length; i++) {
|
|
838
|
+
bytes[i] = binary.charCodeAt(i);
|
|
839
|
+
}
|
|
840
|
+
return bytes;
|
|
841
|
+
};
|
|
842
|
+
const createCompressedStorage = (base, options = {}) => {
|
|
843
|
+
const {
|
|
844
|
+
minSizeToCompress = 1024,
|
|
845
|
+
deflateLevel = 1,
|
|
846
|
+
writeDebounceMs = 50
|
|
847
|
+
} = options;
|
|
848
|
+
const pendingWrites = new Map();
|
|
849
|
+
let flushTimer;
|
|
850
|
+
let lifecycleHooksInstalled = false;
|
|
851
|
+
const flush = () => {
|
|
852
|
+
flushTimer = undefined;
|
|
853
|
+
for (const [key, value] of pendingWrites) {
|
|
854
|
+
try {
|
|
855
|
+
if (value.length < minSizeToCompress) {
|
|
856
|
+
base.setItem(key, RAW_PREFIX + value);
|
|
857
|
+
continue;
|
|
858
|
+
}
|
|
859
|
+
const input = strToU8(value);
|
|
860
|
+
const compressed = deflateSync(input, {
|
|
861
|
+
level: deflateLevel
|
|
862
|
+
});
|
|
863
|
+
base.setItem(key, DEFLATE_PREFIX + u8ToBase64(compressed));
|
|
864
|
+
} catch (error) {
|
|
865
|
+
console.error('Error setting item:', error);
|
|
866
|
+
try {
|
|
867
|
+
base.setItem(key, RAW_PREFIX + value);
|
|
868
|
+
} catch (_a) {
|
|
869
|
+
// ignore
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
pendingWrites.clear();
|
|
874
|
+
};
|
|
875
|
+
const scheduleFlush = () => {
|
|
876
|
+
if (flushTimer != null) return;
|
|
877
|
+
if (!lifecycleHooksInstalled && typeof window !== 'undefined') {
|
|
878
|
+
lifecycleHooksInstalled = true;
|
|
879
|
+
window.addEventListener('beforeunload', flush);
|
|
880
|
+
document.addEventListener('visibilitychange', () => {
|
|
881
|
+
if (document.visibilityState === 'hidden') flush();
|
|
882
|
+
});
|
|
883
|
+
}
|
|
884
|
+
flushTimer = globalThis.setTimeout(flush, writeDebounceMs);
|
|
885
|
+
};
|
|
886
|
+
return {
|
|
887
|
+
getItem: key => {
|
|
888
|
+
try {
|
|
889
|
+
const stored = base.getItem(key);
|
|
890
|
+
if (!stored) return null;
|
|
891
|
+
if (stored.startsWith(RAW_PREFIX)) {
|
|
892
|
+
return stored.slice(RAW_PREFIX.length);
|
|
893
|
+
}
|
|
894
|
+
if (stored.startsWith(DEFLATE_PREFIX)) {
|
|
895
|
+
const b64 = stored.slice(DEFLATE_PREFIX.length);
|
|
896
|
+
const bytes = base64ToU8(b64);
|
|
897
|
+
const decompressed = inflateSync(bytes);
|
|
898
|
+
return strFromU8(decompressed);
|
|
899
|
+
}
|
|
900
|
+
// Back-compat: older versions may have stored raw JSON without any prefix
|
|
901
|
+
if (isProbablyJson(stored)) return stored;
|
|
902
|
+
return null;
|
|
903
|
+
} catch (error) {
|
|
904
|
+
console.error('Error getting item:', error);
|
|
905
|
+
return null;
|
|
906
|
+
}
|
|
907
|
+
},
|
|
908
|
+
setItem: (key, value) => {
|
|
909
|
+
try {
|
|
910
|
+
// Some upstream serializers can return `undefined` (e.g. JSON.stringify(undefined)).
|
|
911
|
+
const rawValue = value;
|
|
912
|
+
if (rawValue == null) {
|
|
913
|
+
pendingWrites.delete(key);
|
|
914
|
+
base.removeItem(key);
|
|
915
|
+
return;
|
|
916
|
+
}
|
|
917
|
+
const stringValue = typeof rawValue === 'string' ? rawValue : String(rawValue);
|
|
918
|
+
pendingWrites.set(key, stringValue);
|
|
919
|
+
scheduleFlush();
|
|
920
|
+
} catch (error) {
|
|
921
|
+
console.error('Error setting item:', error);
|
|
922
|
+
}
|
|
923
|
+
},
|
|
924
|
+
removeItem: key => {
|
|
925
|
+
try {
|
|
926
|
+
pendingWrites.delete(key);
|
|
927
|
+
base.removeItem(key);
|
|
928
|
+
} catch (error) {
|
|
929
|
+
console.error('Error removing item:', error);
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
};
|
|
933
|
+
};
|
|
934
|
+
const baseStorage = {
|
|
935
|
+
getItem: key => {
|
|
936
|
+
if (typeof localStorage === 'undefined') return null;
|
|
937
|
+
return localStorage.getItem(key);
|
|
938
|
+
},
|
|
939
|
+
setItem: (key, value) => {
|
|
940
|
+
if (typeof localStorage === 'undefined') return;
|
|
941
|
+
localStorage.setItem(key, value);
|
|
942
|
+
},
|
|
943
|
+
removeItem: key => {
|
|
944
|
+
if (typeof localStorage === 'undefined') return;
|
|
945
|
+
localStorage.removeItem(key);
|
|
946
|
+
}
|
|
947
|
+
};
|
|
948
|
+
exports.storage = createCompressedStorage(baseStorage);
|
|
949
|
+
const setCustomStorage = newStorage => {
|
|
950
|
+
exports.storage = newStorage;
|
|
951
|
+
};function hasInitialValue(atom) {
|
|
952
|
+
return "init" in atom;
|
|
953
|
+
}
|
|
954
|
+
function isActuallyWritableAtom(atom) {
|
|
955
|
+
return !!atom.write;
|
|
956
|
+
}
|
|
957
|
+
function isAtomStateInitialized(atomState) {
|
|
958
|
+
return "v" in atomState || "e" in atomState;
|
|
959
|
+
}
|
|
960
|
+
function returnAtomValue(atomState) {
|
|
961
|
+
if ("e" in atomState) {
|
|
962
|
+
throw atomState.e;
|
|
963
|
+
}
|
|
964
|
+
if ((undefined ? undefined.MODE : void 0) !== "production" && !("v" in atomState)) {
|
|
965
|
+
throw new Error("[Bug] atom state is not initialized");
|
|
966
|
+
}
|
|
967
|
+
return atomState.v;
|
|
968
|
+
}
|
|
969
|
+
const promiseStateMap = /* @__PURE__ */ new WeakMap();
|
|
970
|
+
function isPendingPromise(value) {
|
|
971
|
+
var _a;
|
|
972
|
+
return isPromiseLike$1(value) && !!((_a = promiseStateMap.get(value)) == null ? void 0 : _a[0]);
|
|
973
|
+
}
|
|
974
|
+
function abortPromise(promise) {
|
|
975
|
+
const promiseState = promiseStateMap.get(promise);
|
|
976
|
+
if (promiseState == null ? void 0 : promiseState[0]) {
|
|
977
|
+
promiseState[0] = false;
|
|
978
|
+
promiseState[1].forEach((fn) => fn());
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
function registerAbortHandler(promise, abortHandler) {
|
|
982
|
+
let promiseState = promiseStateMap.get(promise);
|
|
983
|
+
if (!promiseState) {
|
|
984
|
+
promiseState = [true, /* @__PURE__ */ new Set()];
|
|
985
|
+
promiseStateMap.set(promise, promiseState);
|
|
986
|
+
const settle = () => {
|
|
987
|
+
promiseState[0] = false;
|
|
988
|
+
};
|
|
989
|
+
promise.then(settle, settle);
|
|
990
|
+
}
|
|
991
|
+
promiseState[1].add(abortHandler);
|
|
992
|
+
}
|
|
993
|
+
function isPromiseLike$1(p) {
|
|
994
|
+
return typeof (p == null ? void 0 : p.then) === "function";
|
|
995
|
+
}
|
|
996
|
+
function addPendingPromiseToDependency(atom, promise, dependencyAtomState) {
|
|
997
|
+
if (!dependencyAtomState.p.has(atom)) {
|
|
998
|
+
dependencyAtomState.p.add(atom);
|
|
999
|
+
const cleanup = () => dependencyAtomState.p.delete(atom);
|
|
1000
|
+
promise.then(cleanup, cleanup);
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
function getMountedOrPendingDependents(atom, atomState, mountedMap) {
|
|
1004
|
+
var _a;
|
|
1005
|
+
const dependents = /* @__PURE__ */ new Set();
|
|
1006
|
+
for (const a of ((_a = mountedMap.get(atom)) == null ? void 0 : _a.t) || []) {
|
|
1007
|
+
if (mountedMap.has(a)) {
|
|
1008
|
+
dependents.add(a);
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
for (const atomWithPendingPromise of atomState.p) {
|
|
1012
|
+
dependents.add(atomWithPendingPromise);
|
|
1013
|
+
}
|
|
1014
|
+
return dependents;
|
|
1015
|
+
}
|
|
1016
|
+
const atomRead = (_store, atom, ...params) => atom.read(...params);
|
|
1017
|
+
const atomWrite = (_store, atom, ...params) => atom.write(...params);
|
|
1018
|
+
const atomOnInit = (store, atom) => {
|
|
1019
|
+
var _a;
|
|
1020
|
+
return (_a = atom.unstable_onInit) == null ? void 0 : _a.call(atom, store);
|
|
1021
|
+
};
|
|
1022
|
+
const atomOnMount = (_store, atom, setAtom) => {
|
|
1023
|
+
var _a;
|
|
1024
|
+
return (_a = atom.onMount) == null ? void 0 : _a.call(atom, setAtom);
|
|
1025
|
+
};
|
|
1026
|
+
const ensureAtomState = (store, atom) => {
|
|
1027
|
+
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
1028
|
+
const atomStateMap = buildingBlocks[0];
|
|
1029
|
+
const atomOnInit2 = buildingBlocks[9];
|
|
1030
|
+
if ((undefined ? undefined.MODE : void 0) !== "production" && !atom) {
|
|
1031
|
+
throw new Error("Atom is undefined or null");
|
|
1032
|
+
}
|
|
1033
|
+
let atomState = atomStateMap.get(atom);
|
|
1034
|
+
if (!atomState) {
|
|
1035
|
+
atomState = { d: /* @__PURE__ */ new Map(), p: /* @__PURE__ */ new Set(), n: 0 };
|
|
1036
|
+
atomStateMap.set(atom, atomState);
|
|
1037
|
+
atomOnInit2 == null ? void 0 : atomOnInit2(store, atom);
|
|
1038
|
+
}
|
|
1039
|
+
return atomState;
|
|
1040
|
+
};
|
|
1041
|
+
const flushCallbacks = (store) => {
|
|
1042
|
+
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
1043
|
+
const mountedMap = buildingBlocks[1];
|
|
1044
|
+
const changedAtoms = buildingBlocks[3];
|
|
1045
|
+
const mountCallbacks = buildingBlocks[4];
|
|
1046
|
+
const unmountCallbacks = buildingBlocks[5];
|
|
1047
|
+
const storeHooks = buildingBlocks[6];
|
|
1048
|
+
const recomputeInvalidatedAtoms2 = buildingBlocks[13];
|
|
1049
|
+
const errors = [];
|
|
1050
|
+
const call = (fn) => {
|
|
1051
|
+
try {
|
|
1052
|
+
fn();
|
|
1053
|
+
} catch (e) {
|
|
1054
|
+
errors.push(e);
|
|
1055
|
+
}
|
|
1056
|
+
};
|
|
1057
|
+
do {
|
|
1058
|
+
if (storeHooks.f) {
|
|
1059
|
+
call(storeHooks.f);
|
|
1060
|
+
}
|
|
1061
|
+
const callbacks = /* @__PURE__ */ new Set();
|
|
1062
|
+
const add = callbacks.add.bind(callbacks);
|
|
1063
|
+
changedAtoms.forEach((atom) => {
|
|
1064
|
+
var _a;
|
|
1065
|
+
return (_a = mountedMap.get(atom)) == null ? void 0 : _a.l.forEach(add);
|
|
1066
|
+
});
|
|
1067
|
+
changedAtoms.clear();
|
|
1068
|
+
unmountCallbacks.forEach(add);
|
|
1069
|
+
unmountCallbacks.clear();
|
|
1070
|
+
mountCallbacks.forEach(add);
|
|
1071
|
+
mountCallbacks.clear();
|
|
1072
|
+
callbacks.forEach(call);
|
|
1073
|
+
if (changedAtoms.size) {
|
|
1074
|
+
recomputeInvalidatedAtoms2(store);
|
|
1075
|
+
}
|
|
1076
|
+
} while (changedAtoms.size || unmountCallbacks.size || mountCallbacks.size);
|
|
1077
|
+
if (errors.length) {
|
|
1078
|
+
throw new AggregateError(errors);
|
|
1079
|
+
}
|
|
1080
|
+
};
|
|
1081
|
+
const recomputeInvalidatedAtoms = (store) => {
|
|
1082
|
+
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
1083
|
+
const mountedMap = buildingBlocks[1];
|
|
1084
|
+
const invalidatedAtoms = buildingBlocks[2];
|
|
1085
|
+
const changedAtoms = buildingBlocks[3];
|
|
1086
|
+
const ensureAtomState2 = buildingBlocks[11];
|
|
1087
|
+
const readAtomState2 = buildingBlocks[14];
|
|
1088
|
+
const mountDependencies2 = buildingBlocks[17];
|
|
1089
|
+
const topSortedReversed = [];
|
|
1090
|
+
const visiting = /* @__PURE__ */ new WeakSet();
|
|
1091
|
+
const visited = /* @__PURE__ */ new WeakSet();
|
|
1092
|
+
const stack = Array.from(changedAtoms);
|
|
1093
|
+
while (stack.length) {
|
|
1094
|
+
const a = stack[stack.length - 1];
|
|
1095
|
+
const aState = ensureAtomState2(store, a);
|
|
1096
|
+
if (visited.has(a)) {
|
|
1097
|
+
stack.pop();
|
|
1098
|
+
continue;
|
|
1099
|
+
}
|
|
1100
|
+
if (visiting.has(a)) {
|
|
1101
|
+
if (invalidatedAtoms.get(a) === aState.n) {
|
|
1102
|
+
topSortedReversed.push([a, aState]);
|
|
1103
|
+
} else if ((undefined ? undefined.MODE : void 0) !== "production" && invalidatedAtoms.has(a)) {
|
|
1104
|
+
throw new Error("[Bug] invalidated atom exists");
|
|
1105
|
+
}
|
|
1106
|
+
visited.add(a);
|
|
1107
|
+
stack.pop();
|
|
1108
|
+
continue;
|
|
1109
|
+
}
|
|
1110
|
+
visiting.add(a);
|
|
1111
|
+
for (const d of getMountedOrPendingDependents(a, aState, mountedMap)) {
|
|
1112
|
+
if (!visiting.has(d)) {
|
|
1113
|
+
stack.push(d);
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
for (let i = topSortedReversed.length - 1; i >= 0; --i) {
|
|
1118
|
+
const [a, aState] = topSortedReversed[i];
|
|
1119
|
+
let hasChangedDeps = false;
|
|
1120
|
+
for (const dep of aState.d.keys()) {
|
|
1121
|
+
if (dep !== a && changedAtoms.has(dep)) {
|
|
1122
|
+
hasChangedDeps = true;
|
|
1123
|
+
break;
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
if (hasChangedDeps) {
|
|
1127
|
+
readAtomState2(store, a);
|
|
1128
|
+
mountDependencies2(store, a);
|
|
1129
|
+
}
|
|
1130
|
+
invalidatedAtoms.delete(a);
|
|
1131
|
+
}
|
|
1132
|
+
};
|
|
1133
|
+
const readAtomState = (store, atom) => {
|
|
1134
|
+
var _a, _b;
|
|
1135
|
+
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
1136
|
+
const mountedMap = buildingBlocks[1];
|
|
1137
|
+
const invalidatedAtoms = buildingBlocks[2];
|
|
1138
|
+
const changedAtoms = buildingBlocks[3];
|
|
1139
|
+
const storeHooks = buildingBlocks[6];
|
|
1140
|
+
const atomRead2 = buildingBlocks[7];
|
|
1141
|
+
const ensureAtomState2 = buildingBlocks[11];
|
|
1142
|
+
const flushCallbacks2 = buildingBlocks[12];
|
|
1143
|
+
const recomputeInvalidatedAtoms2 = buildingBlocks[13];
|
|
1144
|
+
const readAtomState2 = buildingBlocks[14];
|
|
1145
|
+
const writeAtomState2 = buildingBlocks[16];
|
|
1146
|
+
const mountDependencies2 = buildingBlocks[17];
|
|
1147
|
+
const atomState = ensureAtomState2(store, atom);
|
|
1148
|
+
if (isAtomStateInitialized(atomState)) {
|
|
1149
|
+
if (mountedMap.has(atom) && invalidatedAtoms.get(atom) !== atomState.n) {
|
|
1150
|
+
return atomState;
|
|
1151
|
+
}
|
|
1152
|
+
if (Array.from(atomState.d).every(
|
|
1153
|
+
([a, n]) => (
|
|
1154
|
+
// Recursively, read the atom state of the dependency, and
|
|
1155
|
+
// check if the atom epoch number is unchanged
|
|
1156
|
+
readAtomState2(store, a).n === n
|
|
1157
|
+
)
|
|
1158
|
+
)) {
|
|
1159
|
+
return atomState;
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
atomState.d.clear();
|
|
1163
|
+
let isSync = true;
|
|
1164
|
+
function mountDependenciesIfAsync() {
|
|
1165
|
+
if (mountedMap.has(atom)) {
|
|
1166
|
+
mountDependencies2(store, atom);
|
|
1167
|
+
recomputeInvalidatedAtoms2(store);
|
|
1168
|
+
flushCallbacks2(store);
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
function getter(a) {
|
|
1172
|
+
var _a2;
|
|
1173
|
+
if (a === atom) {
|
|
1174
|
+
const aState2 = ensureAtomState2(store, a);
|
|
1175
|
+
if (!isAtomStateInitialized(aState2)) {
|
|
1176
|
+
if (hasInitialValue(a)) {
|
|
1177
|
+
setAtomStateValueOrPromise(store, a, a.init);
|
|
1178
|
+
} else {
|
|
1179
|
+
throw new Error("no atom init");
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
return returnAtomValue(aState2);
|
|
1183
|
+
}
|
|
1184
|
+
const aState = readAtomState2(store, a);
|
|
1185
|
+
try {
|
|
1186
|
+
return returnAtomValue(aState);
|
|
1187
|
+
} finally {
|
|
1188
|
+
atomState.d.set(a, aState.n);
|
|
1189
|
+
if (isPendingPromise(atomState.v)) {
|
|
1190
|
+
addPendingPromiseToDependency(atom, atomState.v, aState);
|
|
1191
|
+
}
|
|
1192
|
+
(_a2 = mountedMap.get(a)) == null ? void 0 : _a2.t.add(atom);
|
|
1193
|
+
if (!isSync) {
|
|
1194
|
+
mountDependenciesIfAsync();
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
let controller;
|
|
1199
|
+
let setSelf;
|
|
1200
|
+
const options = {
|
|
1201
|
+
get signal() {
|
|
1202
|
+
if (!controller) {
|
|
1203
|
+
controller = new AbortController();
|
|
1204
|
+
}
|
|
1205
|
+
return controller.signal;
|
|
1206
|
+
},
|
|
1207
|
+
get setSelf() {
|
|
1208
|
+
if ((undefined ? undefined.MODE : void 0) !== "production" && !isActuallyWritableAtom(atom)) {
|
|
1209
|
+
console.warn("setSelf function cannot be used with read-only atom");
|
|
1210
|
+
}
|
|
1211
|
+
if (!setSelf && isActuallyWritableAtom(atom)) {
|
|
1212
|
+
setSelf = (...args) => {
|
|
1213
|
+
if ((undefined ? undefined.MODE : void 0) !== "production" && isSync) {
|
|
1214
|
+
console.warn("setSelf function cannot be called in sync");
|
|
1215
|
+
}
|
|
1216
|
+
if (!isSync) {
|
|
1217
|
+
try {
|
|
1218
|
+
return writeAtomState2(store, atom, ...args);
|
|
1219
|
+
} finally {
|
|
1220
|
+
recomputeInvalidatedAtoms2(store);
|
|
1221
|
+
flushCallbacks2(store);
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
};
|
|
1225
|
+
}
|
|
1226
|
+
return setSelf;
|
|
1227
|
+
}
|
|
1228
|
+
};
|
|
1229
|
+
const prevEpochNumber = atomState.n;
|
|
1230
|
+
try {
|
|
1231
|
+
const valueOrPromise = atomRead2(store, atom, getter, options);
|
|
1232
|
+
setAtomStateValueOrPromise(store, atom, valueOrPromise);
|
|
1233
|
+
if (isPromiseLike$1(valueOrPromise)) {
|
|
1234
|
+
registerAbortHandler(valueOrPromise, () => controller == null ? void 0 : controller.abort());
|
|
1235
|
+
valueOrPromise.then(mountDependenciesIfAsync, mountDependenciesIfAsync);
|
|
1236
|
+
}
|
|
1237
|
+
(_a = storeHooks.r) == null ? void 0 : _a.call(storeHooks, atom);
|
|
1238
|
+
return atomState;
|
|
1239
|
+
} catch (error) {
|
|
1240
|
+
delete atomState.v;
|
|
1241
|
+
atomState.e = error;
|
|
1242
|
+
++atomState.n;
|
|
1243
|
+
return atomState;
|
|
1244
|
+
} finally {
|
|
1245
|
+
isSync = false;
|
|
1246
|
+
if (prevEpochNumber !== atomState.n && invalidatedAtoms.get(atom) === prevEpochNumber) {
|
|
1247
|
+
invalidatedAtoms.set(atom, atomState.n);
|
|
1248
|
+
changedAtoms.add(atom);
|
|
1249
|
+
(_b = storeHooks.c) == null ? void 0 : _b.call(storeHooks, atom);
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
};
|
|
1253
|
+
const invalidateDependents = (store, atom) => {
|
|
1254
|
+
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
1255
|
+
const mountedMap = buildingBlocks[1];
|
|
1256
|
+
const invalidatedAtoms = buildingBlocks[2];
|
|
1257
|
+
const ensureAtomState2 = buildingBlocks[11];
|
|
1258
|
+
const stack = [atom];
|
|
1259
|
+
while (stack.length) {
|
|
1260
|
+
const a = stack.pop();
|
|
1261
|
+
const aState = ensureAtomState2(store, a);
|
|
1262
|
+
for (const d of getMountedOrPendingDependents(a, aState, mountedMap)) {
|
|
1263
|
+
const dState = ensureAtomState2(store, d);
|
|
1264
|
+
invalidatedAtoms.set(d, dState.n);
|
|
1265
|
+
stack.push(d);
|
|
1266
|
+
}
|
|
1267
|
+
}
|
|
1268
|
+
};
|
|
1269
|
+
const writeAtomState = (store, atom, ...args) => {
|
|
1270
|
+
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
1271
|
+
const changedAtoms = buildingBlocks[3];
|
|
1272
|
+
const storeHooks = buildingBlocks[6];
|
|
1273
|
+
const atomWrite2 = buildingBlocks[8];
|
|
1274
|
+
const ensureAtomState2 = buildingBlocks[11];
|
|
1275
|
+
const flushCallbacks2 = buildingBlocks[12];
|
|
1276
|
+
const recomputeInvalidatedAtoms2 = buildingBlocks[13];
|
|
1277
|
+
const readAtomState2 = buildingBlocks[14];
|
|
1278
|
+
const invalidateDependents2 = buildingBlocks[15];
|
|
1279
|
+
const mountDependencies2 = buildingBlocks[17];
|
|
1280
|
+
let isSync = true;
|
|
1281
|
+
const getter = (a) => returnAtomValue(readAtomState2(store, a));
|
|
1282
|
+
const setter = (a, ...args2) => {
|
|
1283
|
+
var _a;
|
|
1284
|
+
const aState = ensureAtomState2(store, a);
|
|
1285
|
+
try {
|
|
1286
|
+
if (a === atom) {
|
|
1287
|
+
if (!hasInitialValue(a)) {
|
|
1288
|
+
throw new Error("atom not writable");
|
|
1289
|
+
}
|
|
1290
|
+
const prevEpochNumber = aState.n;
|
|
1291
|
+
const v = args2[0];
|
|
1292
|
+
setAtomStateValueOrPromise(store, a, v);
|
|
1293
|
+
mountDependencies2(store, a);
|
|
1294
|
+
if (prevEpochNumber !== aState.n) {
|
|
1295
|
+
changedAtoms.add(a);
|
|
1296
|
+
(_a = storeHooks.c) == null ? void 0 : _a.call(storeHooks, a);
|
|
1297
|
+
invalidateDependents2(store, a);
|
|
1298
|
+
}
|
|
1299
|
+
return void 0;
|
|
1300
|
+
} else {
|
|
1301
|
+
return writeAtomState(store, a, ...args2);
|
|
1302
|
+
}
|
|
1303
|
+
} finally {
|
|
1304
|
+
if (!isSync) {
|
|
1305
|
+
recomputeInvalidatedAtoms2(store);
|
|
1306
|
+
flushCallbacks2(store);
|
|
1307
|
+
}
|
|
1308
|
+
}
|
|
1309
|
+
};
|
|
1310
|
+
try {
|
|
1311
|
+
return atomWrite2(store, atom, getter, setter, ...args);
|
|
1312
|
+
} finally {
|
|
1313
|
+
isSync = false;
|
|
1314
|
+
}
|
|
1315
|
+
};
|
|
1316
|
+
const mountDependencies = (store, atom) => {
|
|
1317
|
+
var _a;
|
|
1318
|
+
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
1319
|
+
const mountedMap = buildingBlocks[1];
|
|
1320
|
+
const changedAtoms = buildingBlocks[3];
|
|
1321
|
+
const storeHooks = buildingBlocks[6];
|
|
1322
|
+
const ensureAtomState2 = buildingBlocks[11];
|
|
1323
|
+
const invalidateDependents2 = buildingBlocks[15];
|
|
1324
|
+
const mountAtom2 = buildingBlocks[18];
|
|
1325
|
+
const unmountAtom2 = buildingBlocks[19];
|
|
1326
|
+
const atomState = ensureAtomState2(store, atom);
|
|
1327
|
+
const mounted = mountedMap.get(atom);
|
|
1328
|
+
if (mounted && !isPendingPromise(atomState.v)) {
|
|
1329
|
+
for (const [a, n] of atomState.d) {
|
|
1330
|
+
if (!mounted.d.has(a)) {
|
|
1331
|
+
const aState = ensureAtomState2(store, a);
|
|
1332
|
+
const aMounted = mountAtom2(store, a);
|
|
1333
|
+
aMounted.t.add(atom);
|
|
1334
|
+
mounted.d.add(a);
|
|
1335
|
+
if (n !== aState.n) {
|
|
1336
|
+
changedAtoms.add(a);
|
|
1337
|
+
(_a = storeHooks.c) == null ? void 0 : _a.call(storeHooks, a);
|
|
1338
|
+
invalidateDependents2(store, a);
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
for (const a of mounted.d || []) {
|
|
1343
|
+
if (!atomState.d.has(a)) {
|
|
1344
|
+
mounted.d.delete(a);
|
|
1345
|
+
const aMounted = unmountAtom2(store, a);
|
|
1346
|
+
aMounted == null ? void 0 : aMounted.t.delete(atom);
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
};
|
|
1351
|
+
const mountAtom = (store, atom) => {
|
|
1352
|
+
var _a;
|
|
1353
|
+
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
1354
|
+
const mountedMap = buildingBlocks[1];
|
|
1355
|
+
const mountCallbacks = buildingBlocks[4];
|
|
1356
|
+
const storeHooks = buildingBlocks[6];
|
|
1357
|
+
const atomOnMount2 = buildingBlocks[10];
|
|
1358
|
+
const ensureAtomState2 = buildingBlocks[11];
|
|
1359
|
+
const flushCallbacks2 = buildingBlocks[12];
|
|
1360
|
+
const recomputeInvalidatedAtoms2 = buildingBlocks[13];
|
|
1361
|
+
const readAtomState2 = buildingBlocks[14];
|
|
1362
|
+
const writeAtomState2 = buildingBlocks[16];
|
|
1363
|
+
const atomState = ensureAtomState2(store, atom);
|
|
1364
|
+
let mounted = mountedMap.get(atom);
|
|
1365
|
+
if (!mounted) {
|
|
1366
|
+
readAtomState2(store, atom);
|
|
1367
|
+
for (const a of atomState.d.keys()) {
|
|
1368
|
+
const aMounted = mountAtom(store, a);
|
|
1369
|
+
aMounted.t.add(atom);
|
|
1370
|
+
}
|
|
1371
|
+
mounted = {
|
|
1372
|
+
l: /* @__PURE__ */ new Set(),
|
|
1373
|
+
d: new Set(atomState.d.keys()),
|
|
1374
|
+
t: /* @__PURE__ */ new Set()
|
|
1375
|
+
};
|
|
1376
|
+
mountedMap.set(atom, mounted);
|
|
1377
|
+
(_a = storeHooks.m) == null ? void 0 : _a.call(storeHooks, atom);
|
|
1378
|
+
if (isActuallyWritableAtom(atom)) {
|
|
1379
|
+
const processOnMount = () => {
|
|
1380
|
+
let isSync = true;
|
|
1381
|
+
const setAtom = (...args) => {
|
|
1382
|
+
try {
|
|
1383
|
+
return writeAtomState2(store, atom, ...args);
|
|
1384
|
+
} finally {
|
|
1385
|
+
if (!isSync) {
|
|
1386
|
+
recomputeInvalidatedAtoms2(store);
|
|
1387
|
+
flushCallbacks2(store);
|
|
1388
|
+
}
|
|
1389
|
+
}
|
|
1390
|
+
};
|
|
1391
|
+
try {
|
|
1392
|
+
const onUnmount = atomOnMount2(store, atom, setAtom);
|
|
1393
|
+
if (onUnmount) {
|
|
1394
|
+
mounted.u = () => {
|
|
1395
|
+
isSync = true;
|
|
1396
|
+
try {
|
|
1397
|
+
onUnmount();
|
|
1398
|
+
} finally {
|
|
1399
|
+
isSync = false;
|
|
1400
|
+
}
|
|
1401
|
+
};
|
|
1402
|
+
}
|
|
1403
|
+
} finally {
|
|
1404
|
+
isSync = false;
|
|
1405
|
+
}
|
|
1406
|
+
};
|
|
1407
|
+
mountCallbacks.add(processOnMount);
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
return mounted;
|
|
1411
|
+
};
|
|
1412
|
+
const unmountAtom = (store, atom) => {
|
|
1413
|
+
var _a;
|
|
1414
|
+
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
1415
|
+
const mountedMap = buildingBlocks[1];
|
|
1416
|
+
const unmountCallbacks = buildingBlocks[5];
|
|
1417
|
+
const storeHooks = buildingBlocks[6];
|
|
1418
|
+
const ensureAtomState2 = buildingBlocks[11];
|
|
1419
|
+
const unmountAtom2 = buildingBlocks[19];
|
|
1420
|
+
const atomState = ensureAtomState2(store, atom);
|
|
1421
|
+
let mounted = mountedMap.get(atom);
|
|
1422
|
+
if (mounted && !mounted.l.size && !Array.from(mounted.t).some((a) => {
|
|
1423
|
+
var _a2;
|
|
1424
|
+
return (_a2 = mountedMap.get(a)) == null ? void 0 : _a2.d.has(atom);
|
|
1425
|
+
})) {
|
|
1426
|
+
if (mounted.u) {
|
|
1427
|
+
unmountCallbacks.add(mounted.u);
|
|
1428
|
+
}
|
|
1429
|
+
mounted = void 0;
|
|
1430
|
+
mountedMap.delete(atom);
|
|
1431
|
+
(_a = storeHooks.u) == null ? void 0 : _a.call(storeHooks, atom);
|
|
1432
|
+
for (const a of atomState.d.keys()) {
|
|
1433
|
+
const aMounted = unmountAtom2(store, a);
|
|
1434
|
+
aMounted == null ? void 0 : aMounted.t.delete(atom);
|
|
1435
|
+
}
|
|
1436
|
+
return void 0;
|
|
1437
|
+
}
|
|
1438
|
+
return mounted;
|
|
1439
|
+
};
|
|
1440
|
+
const setAtomStateValueOrPromise = (store, atom, valueOrPromise) => {
|
|
1441
|
+
const ensureAtomState2 = getInternalBuildingBlocks(store)[11];
|
|
1442
|
+
const atomState = ensureAtomState2(store, atom);
|
|
1443
|
+
const hasPrevValue = "v" in atomState;
|
|
1444
|
+
const prevValue = atomState.v;
|
|
1445
|
+
if (isPromiseLike$1(valueOrPromise)) {
|
|
1446
|
+
for (const a of atomState.d.keys()) {
|
|
1447
|
+
addPendingPromiseToDependency(
|
|
1448
|
+
atom,
|
|
1449
|
+
valueOrPromise,
|
|
1450
|
+
ensureAtomState2(store, a)
|
|
1451
|
+
);
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
atomState.v = valueOrPromise;
|
|
1455
|
+
delete atomState.e;
|
|
1456
|
+
if (!hasPrevValue || !Object.is(prevValue, atomState.v)) {
|
|
1457
|
+
++atomState.n;
|
|
1458
|
+
if (isPromiseLike$1(prevValue)) {
|
|
1459
|
+
abortPromise(prevValue);
|
|
1460
|
+
}
|
|
1461
|
+
}
|
|
1462
|
+
};
|
|
1463
|
+
const storeGet = (store, atom) => {
|
|
1464
|
+
const readAtomState2 = getInternalBuildingBlocks(store)[14];
|
|
1465
|
+
return returnAtomValue(readAtomState2(store, atom));
|
|
1466
|
+
};
|
|
1467
|
+
const storeSet = (store, atom, ...args) => {
|
|
1468
|
+
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
1469
|
+
const flushCallbacks2 = buildingBlocks[12];
|
|
1470
|
+
const recomputeInvalidatedAtoms2 = buildingBlocks[13];
|
|
1471
|
+
const writeAtomState2 = buildingBlocks[16];
|
|
1472
|
+
try {
|
|
1473
|
+
return writeAtomState2(store, atom, ...args);
|
|
1474
|
+
} finally {
|
|
1475
|
+
recomputeInvalidatedAtoms2(store);
|
|
1476
|
+
flushCallbacks2(store);
|
|
1477
|
+
}
|
|
1478
|
+
};
|
|
1479
|
+
const storeSub = (store, atom, listener) => {
|
|
1480
|
+
const buildingBlocks = getInternalBuildingBlocks(store);
|
|
1481
|
+
const flushCallbacks2 = buildingBlocks[12];
|
|
1482
|
+
const mountAtom2 = buildingBlocks[18];
|
|
1483
|
+
const unmountAtom2 = buildingBlocks[19];
|
|
1484
|
+
const mounted = mountAtom2(store, atom);
|
|
1485
|
+
const listeners = mounted.l;
|
|
1486
|
+
listeners.add(listener);
|
|
1487
|
+
flushCallbacks2(store);
|
|
1488
|
+
return () => {
|
|
1489
|
+
listeners.delete(listener);
|
|
1490
|
+
unmountAtom2(store, atom);
|
|
1491
|
+
flushCallbacks2(store);
|
|
1492
|
+
};
|
|
1493
|
+
};
|
|
1494
|
+
const buildingBlockMap = /* @__PURE__ */ new WeakMap();
|
|
1495
|
+
const getInternalBuildingBlocks = (store) => {
|
|
1496
|
+
const buildingBlocks = buildingBlockMap.get(store);
|
|
1497
|
+
if ((undefined ? undefined.MODE : void 0) !== "production" && !buildingBlocks) {
|
|
1498
|
+
throw new Error(
|
|
1499
|
+
"Store must be created by buildStore to read its building blocks"
|
|
1500
|
+
);
|
|
1501
|
+
}
|
|
1502
|
+
return buildingBlocks;
|
|
1503
|
+
};
|
|
1504
|
+
function buildStore(...buildArgs) {
|
|
1505
|
+
const store = {
|
|
1506
|
+
get(atom) {
|
|
1507
|
+
const storeGet2 = getInternalBuildingBlocks(store)[21];
|
|
1508
|
+
return storeGet2(store, atom);
|
|
1509
|
+
},
|
|
1510
|
+
set(atom, ...args) {
|
|
1511
|
+
const storeSet2 = getInternalBuildingBlocks(store)[22];
|
|
1512
|
+
return storeSet2(store, atom, ...args);
|
|
1513
|
+
},
|
|
1514
|
+
sub(atom, listener) {
|
|
1515
|
+
const storeSub2 = getInternalBuildingBlocks(store)[23];
|
|
1516
|
+
return storeSub2(store, atom, listener);
|
|
1517
|
+
}
|
|
1518
|
+
};
|
|
1519
|
+
const buildingBlocks = [
|
|
1520
|
+
// store state
|
|
1521
|
+
/* @__PURE__ */ new WeakMap(),
|
|
1522
|
+
// atomStateMap
|
|
1523
|
+
/* @__PURE__ */ new WeakMap(),
|
|
1524
|
+
// mountedMap
|
|
1525
|
+
/* @__PURE__ */ new WeakMap(),
|
|
1526
|
+
// invalidatedAtoms
|
|
1527
|
+
/* @__PURE__ */ new Set(),
|
|
1528
|
+
// changedAtoms
|
|
1529
|
+
/* @__PURE__ */ new Set(),
|
|
1530
|
+
// mountCallbacks
|
|
1531
|
+
/* @__PURE__ */ new Set(),
|
|
1532
|
+
// unmountCallbacks
|
|
1533
|
+
{},
|
|
1534
|
+
// storeHooks
|
|
1535
|
+
// atom interceptors
|
|
1536
|
+
atomRead,
|
|
1537
|
+
atomWrite,
|
|
1538
|
+
atomOnInit,
|
|
1539
|
+
atomOnMount,
|
|
1540
|
+
// building-block functions
|
|
1541
|
+
ensureAtomState,
|
|
1542
|
+
flushCallbacks,
|
|
1543
|
+
recomputeInvalidatedAtoms,
|
|
1544
|
+
readAtomState,
|
|
1545
|
+
invalidateDependents,
|
|
1546
|
+
writeAtomState,
|
|
1547
|
+
mountDependencies,
|
|
1548
|
+
mountAtom,
|
|
1549
|
+
unmountAtom,
|
|
1550
|
+
setAtomStateValueOrPromise,
|
|
1551
|
+
storeGet,
|
|
1552
|
+
storeSet,
|
|
1553
|
+
storeSub,
|
|
1554
|
+
void 0
|
|
1555
|
+
].map((fn, i) => buildArgs[i] || fn);
|
|
1556
|
+
buildingBlockMap.set(store, Object.freeze(buildingBlocks));
|
|
1557
|
+
return store;
|
|
1558
|
+
}let keyCount = 0;
|
|
1559
|
+
function atom(read, write) {
|
|
1560
|
+
const key = `atom${++keyCount}`;
|
|
1561
|
+
const config = {
|
|
1562
|
+
toString() {
|
|
1563
|
+
return (undefined ? undefined.MODE : void 0) !== "production" && this.debugLabel ? key + ":" + this.debugLabel : key;
|
|
1564
|
+
}
|
|
1565
|
+
};
|
|
1566
|
+
if (typeof read === "function") {
|
|
1567
|
+
config.read = read;
|
|
1568
|
+
} else {
|
|
1569
|
+
config.init = read;
|
|
1570
|
+
config.read = defaultRead;
|
|
1571
|
+
config.write = defaultWrite;
|
|
1572
|
+
}
|
|
1573
|
+
if (write) {
|
|
1574
|
+
config.write = write;
|
|
1575
|
+
}
|
|
1576
|
+
return config;
|
|
1577
|
+
}
|
|
1578
|
+
function defaultRead(get) {
|
|
1579
|
+
return get(this);
|
|
1580
|
+
}
|
|
1581
|
+
function defaultWrite(get, set, arg) {
|
|
1582
|
+
return set(
|
|
1583
|
+
this,
|
|
1584
|
+
typeof arg === "function" ? arg(get(this)) : arg
|
|
1585
|
+
);
|
|
1586
|
+
}
|
|
1587
|
+
function createStore() {
|
|
1588
|
+
return buildStore();
|
|
1589
|
+
}
|
|
1590
|
+
let defaultStore;
|
|
1591
|
+
function getDefaultStore() {
|
|
1592
|
+
if (!defaultStore) {
|
|
1593
|
+
defaultStore = createStore();
|
|
1594
|
+
if ((undefined ? undefined.MODE : void 0) !== "production") {
|
|
1595
|
+
globalThis.__JOTAI_DEFAULT_STORE__ || (globalThis.__JOTAI_DEFAULT_STORE__ = defaultStore);
|
|
1596
|
+
if (globalThis.__JOTAI_DEFAULT_STORE__ !== defaultStore) {
|
|
1597
|
+
console.warn(
|
|
1598
|
+
"Detected multiple Jotai instances. It may cause unexpected behavior with the default store. https://github.com/pmndrs/jotai/discussions/2044"
|
|
1599
|
+
);
|
|
1600
|
+
}
|
|
1601
|
+
}
|
|
1602
|
+
}
|
|
1603
|
+
return defaultStore;
|
|
1604
|
+
}const StoreContext = React.createContext(
|
|
1605
|
+
void 0
|
|
1606
|
+
);
|
|
1607
|
+
function useStore(options) {
|
|
1608
|
+
const store = React.useContext(StoreContext);
|
|
1609
|
+
return store || getDefaultStore();
|
|
1610
|
+
}
|
|
1611
|
+
|
|
1612
|
+
const isPromiseLike = (x) => typeof (x == null ? void 0 : x.then) === "function";
|
|
1613
|
+
const attachPromiseStatus = (promise) => {
|
|
1614
|
+
if (!promise.status) {
|
|
1615
|
+
promise.status = "pending";
|
|
1616
|
+
promise.then(
|
|
1617
|
+
(v) => {
|
|
1618
|
+
promise.status = "fulfilled";
|
|
1619
|
+
promise.value = v;
|
|
1620
|
+
},
|
|
1621
|
+
(e) => {
|
|
1622
|
+
promise.status = "rejected";
|
|
1623
|
+
promise.reason = e;
|
|
1624
|
+
}
|
|
1625
|
+
);
|
|
1626
|
+
}
|
|
1627
|
+
};
|
|
1628
|
+
const use = React.use || // A shim for older React versions
|
|
1629
|
+
((promise) => {
|
|
1630
|
+
if (promise.status === "pending") {
|
|
1631
|
+
throw promise;
|
|
1632
|
+
} else if (promise.status === "fulfilled") {
|
|
1633
|
+
return promise.value;
|
|
1634
|
+
} else if (promise.status === "rejected") {
|
|
1635
|
+
throw promise.reason;
|
|
1636
|
+
} else {
|
|
1637
|
+
attachPromiseStatus(promise);
|
|
1638
|
+
throw promise;
|
|
1639
|
+
}
|
|
1640
|
+
});
|
|
1641
|
+
const continuablePromiseMap = /* @__PURE__ */ new WeakMap();
|
|
1642
|
+
const createContinuablePromise = (promise, getValue) => {
|
|
1643
|
+
let continuablePromise = continuablePromiseMap.get(promise);
|
|
1644
|
+
if (!continuablePromise) {
|
|
1645
|
+
continuablePromise = new Promise((resolve, reject) => {
|
|
1646
|
+
let curr = promise;
|
|
1647
|
+
const onFulfilled = (me) => (v) => {
|
|
1648
|
+
if (curr === me) {
|
|
1649
|
+
resolve(v);
|
|
1650
|
+
}
|
|
1651
|
+
};
|
|
1652
|
+
const onRejected = (me) => (e) => {
|
|
1653
|
+
if (curr === me) {
|
|
1654
|
+
reject(e);
|
|
1655
|
+
}
|
|
1656
|
+
};
|
|
1657
|
+
const onAbort = () => {
|
|
1658
|
+
try {
|
|
1659
|
+
const nextValue = getValue();
|
|
1660
|
+
if (isPromiseLike(nextValue)) {
|
|
1661
|
+
continuablePromiseMap.set(nextValue, continuablePromise);
|
|
1662
|
+
curr = nextValue;
|
|
1663
|
+
nextValue.then(onFulfilled(nextValue), onRejected(nextValue));
|
|
1664
|
+
registerAbortHandler(nextValue, onAbort);
|
|
1665
|
+
} else {
|
|
1666
|
+
resolve(nextValue);
|
|
1667
|
+
}
|
|
1668
|
+
} catch (e) {
|
|
1669
|
+
reject(e);
|
|
1670
|
+
}
|
|
1671
|
+
};
|
|
1672
|
+
promise.then(onFulfilled(promise), onRejected(promise));
|
|
1673
|
+
registerAbortHandler(promise, onAbort);
|
|
1674
|
+
});
|
|
1675
|
+
continuablePromiseMap.set(promise, continuablePromise);
|
|
1676
|
+
}
|
|
1677
|
+
return continuablePromise;
|
|
1678
|
+
};
|
|
1679
|
+
function useAtomValue(atom, options) {
|
|
1680
|
+
const { delay, unstable_promiseStatus: promiseStatus = !React.use } = {};
|
|
1681
|
+
const store = useStore();
|
|
1682
|
+
const [[valueFromReducer, storeFromReducer, atomFromReducer], rerender] = React.useReducer(
|
|
1683
|
+
(prev) => {
|
|
1684
|
+
const nextValue = store.get(atom);
|
|
1685
|
+
if (Object.is(prev[0], nextValue) && prev[1] === store && prev[2] === atom) {
|
|
1686
|
+
return prev;
|
|
1687
|
+
}
|
|
1688
|
+
return [nextValue, store, atom];
|
|
1689
|
+
},
|
|
1690
|
+
void 0,
|
|
1691
|
+
() => [store.get(atom), store, atom]
|
|
1692
|
+
);
|
|
1693
|
+
let value = valueFromReducer;
|
|
1694
|
+
if (storeFromReducer !== store || atomFromReducer !== atom) {
|
|
1695
|
+
rerender();
|
|
1696
|
+
value = store.get(atom);
|
|
1697
|
+
}
|
|
1698
|
+
React.useEffect(() => {
|
|
1699
|
+
const unsub = store.sub(atom, () => {
|
|
1700
|
+
if (promiseStatus) {
|
|
1701
|
+
try {
|
|
1702
|
+
const value2 = store.get(atom);
|
|
1703
|
+
if (isPromiseLike(value2)) {
|
|
1704
|
+
attachPromiseStatus(
|
|
1705
|
+
createContinuablePromise(value2, () => store.get(atom))
|
|
1706
|
+
);
|
|
1707
|
+
}
|
|
1708
|
+
} catch (e) {
|
|
1709
|
+
}
|
|
1710
|
+
}
|
|
1711
|
+
if (typeof delay === "number") {
|
|
1712
|
+
setTimeout(rerender, delay);
|
|
1713
|
+
return;
|
|
1714
|
+
}
|
|
1715
|
+
rerender();
|
|
1716
|
+
});
|
|
1717
|
+
rerender();
|
|
1718
|
+
return unsub;
|
|
1719
|
+
}, [store, atom, delay, promiseStatus]);
|
|
1720
|
+
React.useDebugValue(value);
|
|
1721
|
+
if (isPromiseLike(value)) {
|
|
1722
|
+
const promise = createContinuablePromise(value, () => store.get(atom));
|
|
1723
|
+
if (promiseStatus) {
|
|
1724
|
+
attachPromiseStatus(promise);
|
|
1725
|
+
}
|
|
1726
|
+
return use(promise);
|
|
1727
|
+
}
|
|
1728
|
+
return value;
|
|
1729
|
+
}
|
|
1730
|
+
|
|
1731
|
+
function useSetAtom(atom, options) {
|
|
1732
|
+
const store = useStore();
|
|
1733
|
+
const setAtom = React.useCallback(
|
|
1734
|
+
(...args) => {
|
|
1735
|
+
if ((undefined ? undefined.MODE : void 0) !== "production" && !("write" in atom)) {
|
|
1736
|
+
throw new Error("not writable atom");
|
|
1737
|
+
}
|
|
1738
|
+
return store.set(atom, ...args);
|
|
1739
|
+
},
|
|
1740
|
+
[store, atom]
|
|
1741
|
+
);
|
|
1742
|
+
return setAtom;
|
|
1743
|
+
}
|
|
1744
|
+
|
|
1745
|
+
function useAtom(atom, options) {
|
|
1746
|
+
return [
|
|
1747
|
+
useAtomValue(atom),
|
|
1748
|
+
// We do wrong type assertion here, which results in throwing an error.
|
|
1749
|
+
useSetAtom(atom)
|
|
1750
|
+
];
|
|
1751
|
+
}const RESET = Symbol(
|
|
1752
|
+
(undefined ? undefined.MODE : void 0) !== "production" ? "RESET" : ""
|
|
1753
|
+
);
|
|
1754
|
+
|
|
1755
|
+
const isPromiseLike$3 = (x) => typeof (x == null ? void 0 : x.then) === "function";
|
|
1756
|
+
function createJSONStorage(getStringStorage = () => {
|
|
1757
|
+
try {
|
|
1758
|
+
return window.localStorage;
|
|
1759
|
+
} catch (e) {
|
|
1760
|
+
if ((undefined ? undefined.MODE : void 0) !== "production") {
|
|
1761
|
+
if (typeof window !== "undefined") {
|
|
1762
|
+
console.warn(e);
|
|
1763
|
+
}
|
|
1764
|
+
}
|
|
1765
|
+
return void 0;
|
|
1766
|
+
}
|
|
1767
|
+
}, options) {
|
|
1768
|
+
var _a;
|
|
1769
|
+
let lastStr;
|
|
1770
|
+
let lastValue;
|
|
1771
|
+
const storage = {
|
|
1772
|
+
getItem: (key, initialValue) => {
|
|
1773
|
+
var _a2, _b;
|
|
1774
|
+
const parse = (str2) => {
|
|
1775
|
+
str2 = str2 || "";
|
|
1776
|
+
if (lastStr !== str2) {
|
|
1777
|
+
try {
|
|
1778
|
+
lastValue = JSON.parse(str2, void 0 );
|
|
1779
|
+
} catch (e) {
|
|
1780
|
+
return initialValue;
|
|
1781
|
+
}
|
|
1782
|
+
lastStr = str2;
|
|
1783
|
+
}
|
|
1784
|
+
return lastValue;
|
|
1785
|
+
};
|
|
1786
|
+
const str = (_b = (_a2 = getStringStorage()) == null ? void 0 : _a2.getItem(key)) != null ? _b : null;
|
|
1787
|
+
if (isPromiseLike$3(str)) {
|
|
1788
|
+
return str.then(parse);
|
|
1789
|
+
}
|
|
1790
|
+
return parse(str);
|
|
1791
|
+
},
|
|
1792
|
+
setItem: (key, newValue) => {
|
|
1793
|
+
var _a2;
|
|
1794
|
+
return (_a2 = getStringStorage()) == null ? void 0 : _a2.setItem(
|
|
1795
|
+
key,
|
|
1796
|
+
JSON.stringify(newValue, void 0 )
|
|
1797
|
+
);
|
|
1798
|
+
},
|
|
1799
|
+
removeItem: (key) => {
|
|
1800
|
+
var _a2;
|
|
1801
|
+
return (_a2 = getStringStorage()) == null ? void 0 : _a2.removeItem(key);
|
|
1802
|
+
}
|
|
1803
|
+
};
|
|
1804
|
+
const createHandleSubscribe = (subscriber2) => (key, callback, initialValue) => subscriber2(key, (v) => {
|
|
1805
|
+
let newValue;
|
|
1806
|
+
try {
|
|
1807
|
+
newValue = JSON.parse(v || "");
|
|
1808
|
+
} catch (e) {
|
|
1809
|
+
newValue = initialValue;
|
|
1810
|
+
}
|
|
1811
|
+
callback(newValue);
|
|
1812
|
+
});
|
|
1813
|
+
let subscriber;
|
|
1814
|
+
try {
|
|
1815
|
+
subscriber = (_a = getStringStorage()) == null ? void 0 : _a.subscribe;
|
|
1816
|
+
} catch (e) {
|
|
1817
|
+
}
|
|
1818
|
+
if (!subscriber && typeof window !== "undefined" && typeof window.addEventListener === "function" && window.Storage) {
|
|
1819
|
+
subscriber = (key, callback) => {
|
|
1820
|
+
if (!(getStringStorage() instanceof window.Storage)) {
|
|
1821
|
+
return () => {
|
|
1822
|
+
};
|
|
1823
|
+
}
|
|
1824
|
+
const storageEventCallback = (e) => {
|
|
1825
|
+
if (e.storageArea === getStringStorage() && e.key === key) {
|
|
1826
|
+
callback(e.newValue);
|
|
1827
|
+
}
|
|
1828
|
+
};
|
|
1829
|
+
window.addEventListener("storage", storageEventCallback);
|
|
1830
|
+
return () => {
|
|
1831
|
+
window.removeEventListener("storage", storageEventCallback);
|
|
1832
|
+
};
|
|
1833
|
+
};
|
|
1834
|
+
}
|
|
1835
|
+
if (subscriber) {
|
|
1836
|
+
storage.subscribe = createHandleSubscribe(subscriber);
|
|
1837
|
+
}
|
|
1838
|
+
return storage;
|
|
1839
|
+
}
|
|
1840
|
+
const defaultStorage = createJSONStorage();
|
|
1841
|
+
function atomWithStorage(key, initialValue, storage = defaultStorage, options) {
|
|
1842
|
+
const baseAtom = atom(
|
|
1843
|
+
initialValue
|
|
1844
|
+
);
|
|
1845
|
+
if ((undefined ? undefined.MODE : void 0) !== "production") {
|
|
1846
|
+
baseAtom.debugPrivate = true;
|
|
1847
|
+
}
|
|
1848
|
+
baseAtom.onMount = (setAtom) => {
|
|
1849
|
+
setAtom(storage.getItem(key, initialValue));
|
|
1850
|
+
let unsub;
|
|
1851
|
+
if (storage.subscribe) {
|
|
1852
|
+
unsub = storage.subscribe(key, setAtom, initialValue);
|
|
1853
|
+
}
|
|
1854
|
+
return unsub;
|
|
1855
|
+
};
|
|
1856
|
+
const anAtom = atom(
|
|
1857
|
+
(get) => get(baseAtom),
|
|
1858
|
+
(get, set, update) => {
|
|
1859
|
+
const nextValue = typeof update === "function" ? update(get(baseAtom)) : update;
|
|
1860
|
+
if (nextValue === RESET) {
|
|
1861
|
+
set(baseAtom, initialValue);
|
|
1862
|
+
return storage.removeItem(key);
|
|
1863
|
+
}
|
|
1864
|
+
if (isPromiseLike$3(nextValue)) {
|
|
1865
|
+
return nextValue.then((resolvedValue) => {
|
|
1866
|
+
set(baseAtom, resolvedValue);
|
|
1867
|
+
return storage.setItem(key, resolvedValue);
|
|
1868
|
+
});
|
|
1869
|
+
}
|
|
1870
|
+
set(baseAtom, nextValue);
|
|
1871
|
+
return storage.setItem(key, nextValue);
|
|
1872
|
+
}
|
|
1873
|
+
);
|
|
1874
|
+
return anAtom;
|
|
1875
|
+
}// Implementazione
|
|
1876
|
+
function atomStateGenerator({
|
|
1877
|
+
key,
|
|
1878
|
+
defaultValue,
|
|
1879
|
+
persist = false,
|
|
1880
|
+
storage: customStorage
|
|
1881
|
+
}) {
|
|
1882
|
+
const resolvedStorage = customStorage || exports.storage;
|
|
1883
|
+
// Usa atomWithStorage solo se persist è true, altrimenti atom normale
|
|
1884
|
+
const jotaiAtom = persist ? atomWithStorage(key, defaultValue, createJSONStorage(() => resolvedStorage)) : atom(defaultValue);
|
|
1885
|
+
const useValue = () => {
|
|
1886
|
+
const [value] = useAtom(jotaiAtom);
|
|
1887
|
+
return value;
|
|
1888
|
+
};
|
|
1889
|
+
const useState = () => {
|
|
1890
|
+
const $ = compilerRuntime.c(3);
|
|
1891
|
+
const [value, setValue] = useAtom(jotaiAtom);
|
|
1892
|
+
let t0;
|
|
1893
|
+
if ($[0] !== setValue || $[1] !== value) {
|
|
1894
|
+
t0 = [value, setValue];
|
|
1895
|
+
$[0] = setValue;
|
|
1896
|
+
$[1] = value;
|
|
1897
|
+
$[2] = t0;
|
|
1898
|
+
} else {
|
|
1899
|
+
t0 = $[2];
|
|
1900
|
+
}
|
|
1901
|
+
return t0;
|
|
1902
|
+
};
|
|
1903
|
+
const useReset = () => {
|
|
1904
|
+
const $ = compilerRuntime.c(2);
|
|
1905
|
+
const [, setValue] = useAtom(jotaiAtom);
|
|
1906
|
+
let t0;
|
|
1907
|
+
if ($[0] !== setValue) {
|
|
1908
|
+
t0 = () => {
|
|
1909
|
+
setValue(defaultValue);
|
|
1910
|
+
if (persist) {
|
|
1911
|
+
resolvedStorage.removeItem(key);
|
|
1912
|
+
}
|
|
1913
|
+
};
|
|
1914
|
+
$[0] = setValue;
|
|
1915
|
+
$[1] = t0;
|
|
1916
|
+
} else {
|
|
1917
|
+
t0 = $[1];
|
|
1918
|
+
}
|
|
1919
|
+
return t0;
|
|
1920
|
+
};
|
|
1921
|
+
return {
|
|
1922
|
+
atom: jotaiAtom,
|
|
1923
|
+
useValue,
|
|
1924
|
+
useState,
|
|
1925
|
+
useReset
|
|
1926
|
+
};
|
|
1927
|
+
}exports.atomStateGenerator=atomStateGenerator;exports.setCustomStorage=setCustomStorage;//# sourceMappingURL=index.js.map
|