Skip to content

Commit a0ff962

Browse files
moved index to ParseOffline, and used index to export modules
1 parent 94e2a01 commit a0ff962

File tree

2 files changed

+147
-115
lines changed

2 files changed

+147
-115
lines changed

src/ParseOffline.ts

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
}

src/index.ts

Lines changed: 6 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,7 @@
1-
import * as Parse from 'parse';
1+
import { ParseOffline } from './ParseOffline';
2+
import { CachedResults } from './CachedResults';
23

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(
22-
query: Parse.Query,
23-
localStorageKey?: string,
24-
options?: Parse.Query.FindOptions,
25-
): Promise<Parse.Object[]> {
26-
// Generate the cache key
27-
const cacheKey: string = localStorageKey ||
28-
ParseOffline.getLocalStorageKeyForClassName(query.className);
29-
30-
// Get the results
31-
const results = await query.find(options);
32-
33-
// Save the items in the localStorage as JSON
34-
localStorage.setItem(
35-
cacheKey,
36-
JSON.stringify(new CachedResults(results).toJSON()),
37-
);
38-
39-
// Return the results
40-
return results;
41-
}
42-
43-
/**
44-
* Given a query, gets the results
45-
* @param query
46-
*/
47-
static async getResultsFromTheLocalStorage(
48-
className: string,
49-
localStorageKey?: string,
50-
) {
51-
const previousItems = localStorage.getItem(
52-
localStorageKey || // from an arbitrary key
53-
ParseOffline.getLocalStorageKeyForClassName(className), // for an autogenerated key
54-
);
55-
56-
// return if there were no previous items
57-
if (!previousItems) return;
58-
59-
// parse the previous results
60-
const parsedJSON: any = JSON.parse(previousItems);
61-
const cachedResults: CachedResults = CachedResults.fromJSON(parsedJSON);
62-
63-
// Return the Parse Objects
64-
return cachedResults.toParseObjs(className);
65-
}
66-
67-
/**
68-
* Returns the key to be used for saving the results
69-
* of a given query to the localStorage
70-
* @param query
71-
*/
72-
static getLocalStorageKeyForClassName(className: string): string {
73-
return `_cache_${className}`;
74-
}
75-
76-
/**
77-
* Gets a className, given a localStorage key used to
78-
* save an array of items
79-
* @param key
80-
*/
81-
static getClassNameFromLocalStorageKey(key: string): string {
82-
return key.replace('_cache_', '');
83-
}
84-
85-
/**
86-
* Finds the results for the given query,
87-
* saves them to the localStorage and gives you
88-
* back the results.
89-
*
90-
* In case that the browser is not onLine, tries
91-
* to give you back the results that were cached
92-
* in the localStorage
93-
*
94-
* @param query
95-
*/
96-
static async findWithFallback(
97-
query: Parse.Query,
98-
localStorageKey?: string,
99-
options?: Parse.Query.FindOptions,
100-
): Promise<Parse.Object[]> {
101-
// Get the previous items
102-
const previousItems = localStorage.getItem(
103-
localStorageKey ||
104-
ParseOffline.getLocalStorageKeyForClassName(query.className),
105-
);
106-
107-
// If offline, and results were cached,
108-
// then parse and return them
109-
if (previousItems && !navigator.onLine) {
110-
return ParseOffline.getResultsFromTheLocalStorage(query.className, localStorageKey);
111-
}
112-
113-
// Else, return the items from the network and cache them
114-
return ParseOffline.saveResultsToLocalStorage(query);
115-
}
116-
}
4+
export {
5+
ParseOffline,
6+
CachedResults,
7+
};

0 commit comments

Comments
 (0)