Skip to content

Commit

Permalink
Simplify initial data object
Browse files Browse the repository at this point in the history
  • Loading branch information
smithki committed Apr 13, 2019
1 parent b8e36de commit 3f2ba95
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,19 @@ function createProxy<TStorageDefinitions extends any>(

initDataStorage(namespace, storageTarget);

const storageProxyMetadata = {
const data = {
...getDataFromStorage(namespace, storageTarget),
[namespaceSymbol]: namespace,
[isStorageProxy]: true,
[storageTargetSymbol]: storageTarget,
};

const data = getDataFromStorage(namespace, storageTarget);
const proxyData = onChange(data, (path, value, prevValue) => {
if (value === prevValue) return;
window[storageTarget].setItem(namespace, JSON.stringify(proxyData));
});

return new Proxy(proxyData, {
get: (target, prop, receiver) => {
if (prop in storageProxyMetadata) return (storageProxyMetadata as any)[prop];
if (typeof proxyData[prop as any] === 'undefined') return null;
return proxyData[prop as any];
},
Expand Down

0 comments on commit 3f2ba95

Please sign in to comment.