-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
better error handling for local storage operations (#44)
* better error handling for local storage operations * lint * fix obsolete comments
- Loading branch information
1 parent
0444a31
commit 6edc3f6
Showing
10 changed files
with
223 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * as utils from './utils'; | ||
|
||
export default function PersistentFlagStore(storage, environment, hash, ident) { | ||
const store = {}; | ||
|
||
function getFlagsKey() { | ||
let key = ''; | ||
const user = ident.getUser(); | ||
if (user) { | ||
key = hash || utils.btoa(JSON.stringify(user)); | ||
} | ||
return 'ld:' + environment + ':' + key; | ||
} | ||
|
||
// Returns a Promise which will be resolved with a parsed JSON value if a stored value was available, | ||
// or resolved with null if there was no value or if storage was not available. | ||
store.loadFlags = () => | ||
storage.get(getFlagsKey()).then(dataStr => { | ||
if (dataStr === null || dataStr === undefined) { | ||
return null; | ||
} | ||
try { | ||
let data = JSON.parse(dataStr); | ||
if (data) { | ||
const schema = data.$schema; | ||
if (schema === undefined || schema < 1) { | ||
data = utils.transformValuesToVersionedValues(data); | ||
} else { | ||
delete data['$schema']; | ||
} | ||
} | ||
return data; | ||
} catch (ex) { | ||
return store.clearFlags().then(() => null); | ||
} | ||
}); | ||
|
||
// Resolves with true if successful, or false if storage is unavailable. Never rejects. | ||
store.saveFlags = flags => { | ||
const data = utils.extend({}, flags, { $schema: 1 }); | ||
return storage.set(getFlagsKey(), JSON.stringify(data)); | ||
}; | ||
|
||
// Resolves with true if successful, or false if storage is unavailable. Never rejects. | ||
store.clearFlags = () => storage.clear(getFlagsKey()); | ||
|
||
return store; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import * as messages from './messages'; | ||
|
||
// The localStorageProvider is provided by the platform object. It should have the following | ||
// methods, each of which should return a Promise: | ||
// - get(key): Gets the string value, if any, for the given key | ||
// - set(key, value): Stores a string value for the given key | ||
// - remove(key): Removes the given key | ||
// | ||
// Storage is just a light wrapper of the localStorageProvider, adding error handling and | ||
// ensuring that we don't call it if it's unavailable. The get method will simply resolve | ||
// with an undefined value if there is an error or if there is no localStorageProvider. | ||
// None of the promises returned by Storage will ever be rejected. | ||
// | ||
// It is always possible that the underlying platform storage mechanism might fail or be | ||
// disabled. If so, it's likely that it will keep failing, so we will only log one warning | ||
// instead of repetitive warnings. | ||
export default function PersistentStorage(localStorageProvider, logger) { | ||
const storage = {}; | ||
let loggedError = false; | ||
|
||
const logError = err => { | ||
if (!loggedError) { | ||
loggedError = true; | ||
logger.warn(messages.localStorageUnavailable(err)); | ||
} | ||
}; | ||
|
||
storage.isEnabled = () => !!localStorageProvider; | ||
|
||
// Resolves with a value, or undefined if storage is unavailable. Never rejects. | ||
storage.get = key => | ||
new Promise(resolve => { | ||
if (!localStorageProvider) { | ||
resolve(undefined); | ||
return; | ||
} | ||
localStorageProvider | ||
.get(key) | ||
.then(resolve) | ||
.catch(err => { | ||
logError(err); | ||
resolve(undefined); | ||
}); | ||
}); | ||
|
||
// Resolves with true if successful, or false if storage is unavailable. Never rejects. | ||
storage.set = (key, value) => | ||
new Promise(resolve => { | ||
if (!localStorageProvider) { | ||
resolve(false); | ||
return; | ||
} | ||
localStorageProvider | ||
.set(key, value) | ||
.then(() => resolve(true)) | ||
.catch(err => { | ||
logError(err); | ||
resolve(false); | ||
}); | ||
}); | ||
|
||
// Resolves with true if successful, or false if storage is unavailable. Never rejects. | ||
storage.clear = key => | ||
new Promise(resolve => { | ||
if (!localStorageProvider) { | ||
resolve(false); | ||
return; | ||
} | ||
localStorageProvider | ||
.clear(key) | ||
.then(() => resolve(true)) | ||
.catch(err => { | ||
logError(err); | ||
resolve(false); | ||
}); | ||
}); | ||
|
||
return storage; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.