Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

perf: don't serialize data to IDB (#3824)

authored by

Cas_ and committed by
GitHub
d845f88b 046c6e5e

+11 -52
+5
.changeset/orange-rabbits-poke.md
··· 1 + --- 2 + '@urql/exchange-graphcache': major 3 + --- 4 + 5 + Don't serialize data to IDB. This invalidates all existing data, but greatly improves performance of read/write operations.
+6 -52
exchanges/graphcache/src/default-storage/index.ts
··· 78 78 req.result.createObjectStore(METADATA_STORE_NAME); 79 79 }; 80 80 81 - const serializeEntry = (entry: string): string => entry.replace(/:/g, '%3a'); 82 - 83 - const deserializeEntry = (entry: string): string => 84 - entry.replace(/%3a/g, ':'); 85 - 86 - const serializeBatch = (): string => { 87 - let data = ''; 88 - for (const key in batch) { 89 - const value = batch[key]; 90 - data += serializeEntry(key); 91 - data += ':'; 92 - if (value) data += serializeEntry(value); 93 - data += ':'; 94 - } 95 - 96 - return data; 97 - }; 98 - 99 - const deserializeBatch = (input: string) => { 100 - const data = {}; 101 - let char = ''; 102 - let key = ''; 103 - let entry = ''; 104 - let mode = 0; 105 - let index = 0; 106 - while (index < input.length) { 107 - entry = ''; 108 - while ((char = input[index++]) !== ':' && char) { 109 - entry += char; 110 - } 111 - 112 - if (mode) { 113 - data[key] = deserializeEntry(entry) || undefined; 114 - mode = 0; 115 - } else { 116 - key = deserializeEntry(entry); 117 - mode = 1; 118 - } 119 - } 120 - 121 - return data; 122 - }; 123 - 124 81 return { 125 82 clear() { 126 83 return database$.then(database => { ··· 175 132 database 176 133 .transaction(ENTRIES_STORE_NAME, 'readwrite') 177 134 .objectStore(ENTRIES_STORE_NAME) 178 - .put(serializeBatch(), timestamp) 135 + .put(batch, timestamp) 179 136 ); 180 137 }) 181 138 .then(toUndefined, toUndefined); 182 139 }, 183 140 184 141 readData(): Promise<SerializedEntries> { 185 - const chunks: string[] = []; 142 + const data: SerializedEntries = {}; 186 143 return database$ 187 144 .then(database => { 188 145 const transaction = database.transaction( ··· 200 157 store.delete(key); 201 158 } else { 202 159 const request = store.get(key); 203 - const index = chunks.length; 204 - chunks.push(''); 205 160 request.onsuccess = () => { 206 - const result = '' + request.result; 207 - if (key === timestamp) 208 - Object.assign(batch, deserializeBatch(result)); 209 - chunks[index] = result; 161 + const result = request.result; 162 + if (key === timestamp) Object.assign(batch, result); 163 + Object.assign(data, result); 210 164 }; 211 165 } 212 166 ··· 217 171 return getTransactionPromise(transaction); 218 172 }) 219 173 .then( 220 - () => deserializeBatch(chunks.join('')), 174 + () => data, 221 175 () => batch 222 176 ); 223 177 },