-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 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,45 @@ | ||
//54145a.github.io global.js | ||
const UPDATE_MIN_INTERVAL = 1000; | ||
|
||
function createDeepProxy(target, handler) { | ||
const deepHandler = Object.assign({}, handler); | ||
deepHandler.get = (target, property) => { | ||
const value = Reflect.get(target, property); | ||
if (typeof value === "object" && value !== null) { | ||
return createDeepProxy(value, handler); | ||
} else { | ||
if (handler.get) { | ||
return handler.get(target, property, void 0); | ||
} else { | ||
return value; | ||
} | ||
} | ||
}; | ||
return new Proxy(target, deepHandler); | ||
} | ||
function createStorageObject(defaultValue, updator) { | ||
let scheduledUpdate = false; | ||
let cache = Object.assign({}, defaultValue); | ||
return createDeepProxy(cache, { | ||
set(target, property, value) { | ||
if (!scheduledUpdate) { | ||
scheduledUpdate = true; | ||
setTimeout(async () => { | ||
scheduledUpdate = false; | ||
await updator(cache); | ||
}, UPDATE_MIN_INTERVAL); | ||
} | ||
return Reflect.set(target, property, value); | ||
} | ||
}); | ||
} | ||
function getStorage(name) { | ||
const defaultValue = localStorage.getItem(name); | ||
return createStorageObject( | ||
defaultValue ? JSON.parse(defaultValue) : {}, | ||
async data => { | ||
localStorage[name] = JSON.stringify(data); | ||
} | ||
); | ||
} | ||
export { getStorage }; |