-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'glance/master'
- Loading branch information
Showing
10 changed files
with
144 additions
and
26 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 @@ | ||
* text=auto eol=lf |
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 |
---|---|---|
|
@@ -3,4 +3,5 @@ module.exports = { | |
singleQuote: true, | ||
trailingComma: 'es5', | ||
arrowParens: 'always', | ||
endOfLine: 'lf', | ||
}; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
function callAndEmpty(l) { | ||
while (l.length) { | ||
l.pop()(); | ||
} | ||
} | ||
|
||
export default class Settings { | ||
constructor(keyPrefix = 'settings') { | ||
this.keyPrefix = keyPrefix; | ||
this.storeSettingsMap = {}; | ||
this.store = null; | ||
this.storeWatchers = []; | ||
} | ||
|
||
key(name) { | ||
if (this.keyPrefix) { | ||
return `${this.keyPrefix}.${name}`; | ||
} | ||
return name; | ||
} | ||
|
||
get(name) { | ||
const result = window.localStorage.getItem(this.key(name)); | ||
if (result === null) { | ||
return undefined; | ||
} | ||
return JSON.parse(result); | ||
} | ||
|
||
set(name, value) { | ||
return window.localStorage.setItem(this.key(name), JSON.stringify(value)); | ||
} | ||
|
||
syncWithStore(store, syncInfo) { | ||
callAndEmpty(this.storeWatchers); | ||
this.store = store; | ||
this.storeSettingsMap = syncInfo; | ||
|
||
const settingNames = Object.keys(syncInfo); | ||
// localStorage -> store | ||
settingNames.forEach((name) => { | ||
const value = this.get(name); | ||
if (value !== undefined) { | ||
const { set } = syncInfo[name]; | ||
set(value); | ||
} | ||
}); | ||
|
||
// store -> localStorage | ||
this.storeWatchers = settingNames.map((name) => { | ||
const { get } = syncInfo[name]; | ||
return store.watch(get, (value) => this.set(name, value)); | ||
}); | ||
} | ||
|
||
delete() { | ||
callAndEmpty(this.storeWatchers); | ||
this.storeSettingsMap = {}; | ||
this.store = null; | ||
} | ||
} |
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