|
| 1 | +import * as Parse from 'parse'; |
| 2 | + |
| 3 | +import { CachedResults } from './CachedResults'; |
| 4 | + |
| 5 | +export class ParseOffline { |
| 6 | + /** |
| 7 | + * Save query results in the localStorage for any class. |
| 8 | + * With this, given a query, you could fetch its results and |
| 9 | + * save them in the localStorage for later usage. |
| 10 | + * |
| 11 | + * Let's say you have a query for items of the class |
| 12 | + * GamePoints, they are saved as JSON in the local storage as |
| 13 | + * _cache_GamePoints. |
| 14 | + * |
| 15 | + * Nowadays, localStorage is widely supported |
| 16 | + * https://caniuse.com/#search=localStorage |
| 17 | + * |
| 18 | + * @param query |
| 19 | + * @param localStorageKey |
| 20 | + */ |
| 21 | + static async saveResultsToLocalStorage(params:{ |
| 22 | + query: Parse.Query, |
| 23 | + localStorageKey?: string, |
| 24 | + options?: Parse.Query.FindOptions, |
| 25 | + }): Promise<Parse.Object[]> { |
| 26 | + const { query, localStorageKey, options } = params; |
| 27 | + |
| 28 | + // Generate the cache key |
| 29 | + const cacheKey: string = localStorageKey || |
| 30 | + ParseOffline.getLocalStorageKeyForClassName(query.className); |
| 31 | + |
| 32 | + // Get the results |
| 33 | + const results = await query.find(options); |
| 34 | + |
| 35 | + // Save the items in the localStorage as JSON |
| 36 | + localStorage.setItem( |
| 37 | + cacheKey, |
| 38 | + JSON.stringify(new CachedResults(results).toJSON()), |
| 39 | + ); |
| 40 | + |
| 41 | + // Return the results |
| 42 | + return results; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Given a query, gets the results |
| 47 | + * @param query |
| 48 | + */ |
| 49 | + static getResultsFromTheLocalStorage(params: { |
| 50 | + className: string, |
| 51 | + localStorageKey?: string, |
| 52 | + }): CachedResults { |
| 53 | + const { className, localStorageKey } = params; |
| 54 | + |
| 55 | + const previousItems = localStorage.getItem( |
| 56 | + localStorageKey || // from an arbitrary key |
| 57 | + ParseOffline.getLocalStorageKeyForClassName(className), // for an autogenerated key |
| 58 | + ); |
| 59 | + |
| 60 | + // return if there were no previous items |
| 61 | + if (!previousItems) return; |
| 62 | + |
| 63 | + // parse the previous results |
| 64 | + const parsedJSON: any = JSON.parse(previousItems); |
| 65 | + |
| 66 | + // Return the Parse Objects |
| 67 | + return CachedResults.fromJSON(parsedJSON); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Returns the key to be used for saving the results |
| 72 | + * of a given query to the localStorage |
| 73 | + * @param query |
| 74 | + */ |
| 75 | + static getLocalStorageKeyForClassName(className: string): string { |
| 76 | + return `_cache_${className}`; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Gets a className, given a localStorage key used to |
| 81 | + * save an array of items |
| 82 | + * @param key |
| 83 | + */ |
| 84 | + static getClassNameFromLocalStorageKey(key: string): string { |
| 85 | + return key.replace('_cache_', ''); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Finds the results for the given query, |
| 90 | + * saves them to the localStorage and gives you |
| 91 | + * back the results. |
| 92 | + * |
| 93 | + * In case that the browser is not onLine, tries |
| 94 | + * to give you back the results that were cached |
| 95 | + * in the localStorage |
| 96 | + * |
| 97 | + * @param query |
| 98 | + */ |
| 99 | + static async findWithFallbackAndCache(params: { |
| 100 | + query: Parse.Query, |
| 101 | + localStorageKey?: string, |
| 102 | + options?: Parse.Query.FindOptions, |
| 103 | + maxAge?: number, |
| 104 | + }): Promise<Parse.Object[]> { |
| 105 | + const { query, localStorageKey, options, maxAge } = params; |
| 106 | + |
| 107 | + // Get the previous items |
| 108 | + const previousItems = localStorage.getItem( |
| 109 | + localStorageKey || |
| 110 | + ParseOffline.getLocalStorageKeyForClassName(query.className), |
| 111 | + ); |
| 112 | + |
| 113 | + // If offline, and results were cached, |
| 114 | + // then parse and return them |
| 115 | + if (previousItems && !navigator.onLine) { |
| 116 | + const cachedResults = ParseOffline.getResultsFromTheLocalStorage({ |
| 117 | + localStorageKey, |
| 118 | + className: query.className, |
| 119 | + }); |
| 120 | + |
| 121 | + if (maxAge && !ParseOffline.areCachedResultsValid(cachedResults, maxAge)) { |
| 122 | + throw new Error('Cached results are not valid anymore'); |
| 123 | + } |
| 124 | + |
| 125 | + return cachedResults.toParseObjs(query.className); |
| 126 | + } |
| 127 | + |
| 128 | + // Else, return the items from the network and cache them |
| 129 | + return ParseOffline.saveResultsToLocalStorage({ |
| 130 | + query, |
| 131 | + localStorageKey, |
| 132 | + options, |
| 133 | + }); |
| 134 | + } |
| 135 | + |
| 136 | + static areCachedResultsValid( |
| 137 | + cachedResults: CachedResults, maxAge: number, |
| 138 | + ): boolean { |
| 139 | + return cachedResults.createdAt.getTime() > (new Date().getTime() - maxAge); |
| 140 | + } |
| 141 | +} |
0 commit comments