@gaddario98/react-core 2.0.9 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,186 +1,4 @@
1
- import {atom,useAtom,useSetAtom,useAtomValue}from'jotai';import {atomWithStorage,createJSONStorage,selectAtom}from'jotai/utils';import {inflateSync,strFromU8,strToU8,deflateSync}from'fflate';import {c}from'react/compiler-runtime';import {QueryClient,useQuery,useMutation,useQueries,QueryClientProvider,IsRestoringProvider}from'@tanstack/react-query';import axios from'axios';import*as React from'react';import {useMemo,useCallback,useRef,useEffect,useReducer,useState}from'react';import equal from'fast-deep-equal';import {jsx}from'react/jsx-runtime';const RAW_PREFIX = 'storage:raw:';
2
- const DEFLATE_PREFIX = 'storage:deflate:v1:';
3
- const isProbablyJson = value => {
4
- if (!value) return false;
5
- const c = value.charCodeAt(0);
6
- // { [ " digits, t/f/n (true/false/null)
7
- return c === 123 || c === 91 || c === 34 || c >= 48 && c <= 57 || c === 45 || c === 116 || c === 102 || c === 110;
8
- };
9
- const u8ToBase64 = bytes => {
10
- let binary = '';
11
- const chunkSize = 0x8000;
12
- for (let i = 0; i < bytes.length; i += chunkSize) {
13
- binary += String.fromCharCode(...bytes.subarray(i, i + chunkSize));
14
- }
15
- return btoa(binary);
16
- };
17
- const base64ToU8 = base64 => {
18
- const binary = atob(base64);
19
- const bytes = new Uint8Array(binary.length);
20
- for (let i = 0; i < binary.length; i++) {
21
- bytes[i] = binary.charCodeAt(i);
22
- }
23
- return bytes;
24
- };
25
- const createCompressedStorage = (base, options = {}) => {
26
- const {
27
- minSizeToCompress = 1024,
28
- deflateLevel = 1,
29
- writeDebounceMs = 50
30
- } = options;
31
- const pendingWrites = new Map();
32
- let flushTimer;
33
- let lifecycleHooksInstalled = false;
34
- const flush = () => {
35
- flushTimer = undefined;
36
- for (const [key, value] of pendingWrites) {
37
- try {
38
- if (value.length < minSizeToCompress) {
39
- base.setItem(key, RAW_PREFIX + value);
40
- continue;
41
- }
42
- const input = strToU8(value);
43
- const compressed = deflateSync(input, {
44
- level: deflateLevel
45
- });
46
- base.setItem(key, DEFLATE_PREFIX + u8ToBase64(compressed));
47
- } catch (error) {
48
- console.error('Error setting item:', error);
49
- try {
50
- base.setItem(key, RAW_PREFIX + value);
51
- } catch (_a) {
52
- // ignore
53
- }
54
- }
55
- }
56
- pendingWrites.clear();
57
- };
58
- const scheduleFlush = () => {
59
- if (flushTimer != null) return;
60
- if (!lifecycleHooksInstalled && typeof window !== 'undefined') {
61
- lifecycleHooksInstalled = true;
62
- window.addEventListener('beforeunload', flush);
63
- document.addEventListener('visibilitychange', () => {
64
- if (document.visibilityState === 'hidden') flush();
65
- });
66
- }
67
- flushTimer = globalThis.setTimeout(flush, writeDebounceMs);
68
- };
69
- return {
70
- getItem: key => {
71
- try {
72
- const stored = base.getItem(key);
73
- if (!stored) return null;
74
- if (stored.startsWith(RAW_PREFIX)) {
75
- return stored.slice(RAW_PREFIX.length);
76
- }
77
- if (stored.startsWith(DEFLATE_PREFIX)) {
78
- const b64 = stored.slice(DEFLATE_PREFIX.length);
79
- const bytes = base64ToU8(b64);
80
- const decompressed = inflateSync(bytes);
81
- return strFromU8(decompressed);
82
- }
83
- // Back-compat: older versions may have stored raw JSON without any prefix
84
- if (isProbablyJson(stored)) return stored;
85
- return null;
86
- } catch (error) {
87
- console.error('Error getting item:', error);
88
- return null;
89
- }
90
- },
91
- setItem: (key, value) => {
92
- try {
93
- // Some upstream serializers can return `undefined` (e.g. JSON.stringify(undefined)).
94
- const rawValue = value;
95
- if (rawValue == null) {
96
- pendingWrites.delete(key);
97
- base.removeItem(key);
98
- return;
99
- }
100
- const stringValue = typeof rawValue === 'string' ? rawValue : String(rawValue);
101
- pendingWrites.set(key, stringValue);
102
- scheduleFlush();
103
- } catch (error) {
104
- console.error('Error setting item:', error);
105
- }
106
- },
107
- removeItem: key => {
108
- try {
109
- pendingWrites.delete(key);
110
- base.removeItem(key);
111
- } catch (error) {
112
- console.error('Error removing item:', error);
113
- }
114
- }
115
- };
116
- };
117
- const baseStorage = {
118
- getItem: key => {
119
- if (typeof localStorage === 'undefined') return null;
120
- return localStorage.getItem(key);
121
- },
122
- setItem: (key, value) => {
123
- if (typeof localStorage === 'undefined') return;
124
- localStorage.setItem(key, value);
125
- },
126
- removeItem: key => {
127
- if (typeof localStorage === 'undefined') return;
128
- localStorage.removeItem(key);
129
- }
130
- };
131
- let storage = createCompressedStorage(baseStorage);// Implementazione
132
- function atomStateGenerator({
133
- key,
134
- defaultValue,
135
- persist = false,
136
- storage: customStorage
137
- }) {
138
- const resolvedStorage = customStorage || storage;
139
- // Usa atomWithStorage solo se persist è true, altrimenti atom normale
140
- const jotaiAtom = persist ? atomWithStorage(key, defaultValue, createJSONStorage(() => resolvedStorage)) : atom(defaultValue);
141
- const useValue = () => {
142
- const [value] = useAtom(jotaiAtom);
143
- return value;
144
- };
145
- const useState = () => {
146
- const $ = c(3);
147
- const [value, setValue] = useAtom(jotaiAtom);
148
- let t0;
149
- if ($[0] !== setValue || $[1] !== value) {
150
- t0 = [value, setValue];
151
- $[0] = setValue;
152
- $[1] = value;
153
- $[2] = t0;
154
- } else {
155
- t0 = $[2];
156
- }
157
- return t0;
158
- };
159
- const useReset = () => {
160
- const $ = c(2);
161
- const [, setValue] = useAtom(jotaiAtom);
162
- let t0;
163
- if ($[0] !== setValue) {
164
- t0 = () => {
165
- setValue(defaultValue);
166
- if (persist) {
167
- resolvedStorage.removeItem(key);
168
- }
169
- };
170
- $[0] = setValue;
171
- $[1] = t0;
172
- } else {
173
- t0 = $[1];
174
- }
175
- return t0;
176
- };
177
- return {
178
- atom: jotaiAtom,
179
- useValue,
180
- useState,
181
- useReset
182
- };
183
- }// ============================================================================
1
+ import {atom,useSetAtom,useAtomValue}from'jotai';import {atomWithStorage,createJSONStorage,selectAtom}from'jotai/utils';import {storage,atomStateGenerator}from'@gaddario98/react-state';import {QueryClient,useQuery,useMutation,useQueries,QueryClientProvider,IsRestoringProvider}from'@tanstack/react-query';import axios from'axios';import*as React from'react';import {useMemo,useCallback,useRef,useEffect,useReducer,useState}from'react';import {c}from'react/compiler-runtime';import equal from'fast-deep-equal';import {jsx}from'react/jsx-runtime';// ============================================================================
184
2
  // Default Values
185
3
  // ============================================================================
186
4
  const DEFAULT_QUERY_ENTRY = Object.freeze({