Skip to content
This repository has been archived by the owner on May 12, 2022. It is now read-only.

Commit

Permalink
Fixes #7
Browse files Browse the repository at this point in the history
  • Loading branch information
dilan-dio4 committed Nov 15, 2021
1 parent a76341b commit 7b3082f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 31 deletions.
66 changes: 36 additions & 30 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import Storage from './storage';

const storage = new Storage({ storageBackend: window.localStorage });
const storage = window !== undefined ? new Storage({ storageBackend: window.localStorage }) : undefined;

// https://github.com/sunnylqm/react-native-storage
export async function getCacheTokens(cookieName: string): Promise<Record<string, any>> {
let cacheToken = false;
let cacheRefreshToken = false;
let cacheSession = false;

try {
cacheToken = await storage.load({ key: cookieName + "token" });
} catch (_) {}
if (storage) {
try {
cacheToken = await storage.load({ key: cookieName + "token" });
} catch (_) { }

try {
cacheRefreshToken = await storage.load({ key: cookieName + "refreshToken" });
} catch (_) {}
try {
cacheRefreshToken = await storage.load({ key: cookieName + "refreshToken" });
} catch (_) { }

try {
cacheSession = await storage.load({ key: cookieName + "session" });
} catch (_) {}
try {
cacheSession = await storage.load({ key: cookieName + "session" });
} catch (_) { }
}

return {
cacheToken,
Expand All @@ -28,27 +30,31 @@ export async function getCacheTokens(cookieName: string): Promise<Record<string,
}

export async function clearCacheTokens(cookieName: string) {
await storage.remove({ key: cookieName + "token" });
await storage.remove({ key: cookieName + "refreshToken" });
await storage.remove({ key: cookieName + "session" });
if (storage) {
await storage.remove({ key: cookieName + "token" });
await storage.remove({ key: cookieName + "refreshToken" });
await storage.remove({ key: cookieName + "session" });
}
}

export async function setCacheTokens(g: any, cookieName: string) {
await storage.save({
key: cookieName + "token",
data: g.token,
expires: 3600 * 1000 * 24
});

await storage.save({
key: cookieName + "refreshToken",
data: g.refreshToken,
expires: 3600 * 1000 * 24
});

await storage.save({
key: cookieName + "session",
data: g.session,
expires: null
});
if (storage) {
await storage.save({
key: cookieName + "token",
data: g.token,
expires: 3600 * 1000 * 24
});

await storage.save({
key: cookieName + "refreshToken",
data: g.refreshToken,
expires: 3600 * 1000 * 24
});

await storage.save({
key: cookieName + "session",
data: g.session,
expires: null
});
}
}
6 changes: 5 additions & 1 deletion src/types/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ export interface ContextValue {
/**
* Custom stateful hook to an instance of `db().return`. Other local changes will automatically re-fetch the query
* as detailed in the passed-in db.
* ```jsx
*
* ```
*
* const { frame } = useReturn(() => db('MYTABLE').return().where(e.gt('rating', ratingState)).limit(10), [ratingState])
*
* const onButtonClick = (_key) => {
Expand All @@ -300,7 +302,9 @@ export interface ContextValue {
*
* // Stays fresh after call to `.delete()`
* return (<div>{ frame.map(ele => <Card {...ele} />) }</div>)
*
* ```
*
* @param {function():SQW} dbInstance Function returning an instance of `db().return` without having called `.all` or `.one`
* @param {React.DependencyList} deps If present, instance will be reloaded if the values in the list change.
* @return {UseReturnValue} Object with the required values to statefully access an array that is subscribed to local executions to the corresponding db instance.
Expand Down

0 comments on commit 7b3082f

Please sign in to comment.