-
Notifications
You must be signed in to change notification settings - Fork 942
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix #339 by normalizing localStorage and browser.storage.local usage #395
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,10 +10,47 @@ exports.formatArgs = formatArgs; | |
exports.save = save; | ||
exports.load = load; | ||
exports.useColors = useColors; | ||
exports.storage = 'undefined' != typeof chrome | ||
&& 'undefined' != typeof chrome.storage | ||
? chrome.storage.local | ||
: localstorage(); | ||
|
||
exports.storage = (function() { | ||
var browser = window.browser || window.chrome; | ||
|
||
if (typeof browser !== 'undefined' && | ||
typeof browser.storage !== 'undefined' && | ||
typeof browser.storage.local !== 'undefined') { | ||
|
||
// maintain compatibility with localStorage | ||
return { | ||
get: function(key, cb) { | ||
browser.storage.local.get(key, function(items) { | ||
cb(items[key]); | ||
}); | ||
}, | ||
|
||
set: function(key, value) { | ||
var items = {}; | ||
items[key] = value; | ||
browser.storage.local.set(items); | ||
}, | ||
|
||
remove: browser.storage.local.remove | ||
}; | ||
} | ||
|
||
if (window.localStorage) { | ||
return { | ||
get: function(key, cb) { | ||
var value = window.localStorage.getItem(key); | ||
cb(value); | ||
}, | ||
set: function(key, value) { | ||
window.localStorage.setItem(key, value); | ||
}, | ||
remove: function(key) { | ||
window.localStorage.removeItem(key); | ||
} | ||
}; | ||
} | ||
})(); | ||
|
||
/** | ||
* Colors. | ||
|
@@ -131,13 +168,11 @@ function log() { | |
*/ | ||
|
||
function save(namespaces) { | ||
try { | ||
if (null == namespaces) { | ||
exports.storage.removeItem('debug'); | ||
} else { | ||
exports.storage.debug = namespaces; | ||
} | ||
} catch(e) {} | ||
if (null == namespaces) { | ||
exports.storage.remove('debug'); | ||
} else { | ||
exports.storage.set('debug', namespaces); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -148,35 +183,19 @@ function save(namespaces) { | |
*/ | ||
|
||
function load() { | ||
try { | ||
return exports.storage.debug; | ||
} catch(e) {} | ||
|
||
// If debug isn't set in LS, and we're in Electron, try to load $DEBUG | ||
if (typeof process !== 'undefined' && 'env' in process) { | ||
return process.env.DEBUG; | ||
if (exports.storage) { | ||
exports.storage.get('debug', exports.enable); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another problem I am seeing is that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't follow you here at all. There's no There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I'll elaborate:
Not in this file, no. We need to jump over to
import debug from 'debug';
const log = debug('foo');
log('Hello!'); // <-- Never logs because debug was not enabled when `debug('foo')` was called
setTimeout(() => {
debug('bar')('Hi!'); // <-- (Probably) runs because the promise has resolved and the callback fired
}, 1000); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In other words, you're pointing out point no. 2 in the description of this PR
Comments:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is correct, I am indeed pointing that out. Basically what you are arguing is that this PR is making the situation better, as Let's make it clear that I'm not saying it's a deal breaker. It would be awesome to import debug from 'debug';
// const log = debug('FooComponent'); // <-- DO NOT DO THIS
export default function FooComponent() {
debug('FooComponent')('Rendering FooComponent');
// log('Rendering FooComponent'); // <-- DO NOT DO THIS
return <h1>Foo</h1>;
} |
||
} else { | ||
if (typeof process !== 'undefined' && | ||
typeof process.env !== 'undefined' && | ||
typeof process.env.DEBUG !== 'undefined') { | ||
exports.enable(process.env.DEBUG); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Enable namespaces listed in `localStorage.debug` initially. | ||
* Enable namespaces listed in the storage `debug`` initially. | ||
*/ | ||
|
||
exports.enable(load()); | ||
|
||
/** | ||
* Localstorage attempts to return the localstorage. | ||
* | ||
* This is necessary because safari throws | ||
* when a user disables cookies/localstorage | ||
* and you attempt to access it. | ||
* | ||
* @return {LocalStorage} | ||
* @api private | ||
*/ | ||
|
||
function localstorage() { | ||
try { | ||
return window.localStorage; | ||
} catch (e) {} | ||
} | ||
exports.load(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Firefox:
Error: Incorrect argument types for storage.StorageArea.set.