Skip to content

Commit

Permalink
use cached storage
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Jan 7, 2020
1 parent 2ee7ae9 commit 377ae05
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 19 deletions.
35 changes: 25 additions & 10 deletions packages/moon/dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -739,37 +739,52 @@
driver: driver$2
};

/*
* Current storage
*/
var storage = {};

for (var key in localStorage) {
if (localStorage.hasOwnProperty(key)) {
storage[key] = localStorage[key];
}
}
/**
* Storage driver
*
* The storage driver allows applications to receive input from local storage
* and persist string key/value pairs in local storage.
*/


var driver$3 = {
input: function input() {
// Return the local storage as input.
return localStorage;
return storage;
},
output: function output(localStorageNew) {
output: function output(storageNew) {
// Update the local storage when it is an output.
for (var keyNew in localStorageNew) {
var valueNew = localStorageNew[keyNew];
for (var keyNew in storageNew) {
var valueNew = storageNew[keyNew];

if (localStorage[keyNew] !== valueNew) {
if (storage[keyNew] !== valueNew) {
localStorage[keyNew] = valueNew;
}
} // Remove any items that aren't in the new local storage.


for (var keyOld in localStorage) {
if (!(keyOld in localStorageNew)) {
for (var keyOld in storage) {
if (!(keyOld in storageNew)) {
delete localStorage[keyOld];
}
}
} // Update the global storage reference.


storage = storageNew;
}
};

var storage = {
var storage$1 = {
driver: driver$3
};

Expand Down Expand Up @@ -921,7 +936,7 @@
http: http,
route: route$1,
run: run,
storage: storage,
storage: storage$1,
time: time,
use: use,
version: "1.0.0-beta.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/moon/dist/moon.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 21 additions & 7 deletions packages/moon/src/storage/driver.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/*
* Current storage
*/
let storage = {};

for (const key in localStorage) {
if (localStorage.hasOwnProperty(key)) {
storage[key] = localStorage[key];
}
}

/**
* Storage driver
*
Expand All @@ -7,23 +18,26 @@
export default {
input() {
// Return the local storage as input.
return localStorage;
return storage;
},
output(localStorageNew) {
output(storageNew) {
// Update the local storage when it is an output.
for (const keyNew in localStorageNew) {
const valueNew = localStorageNew[keyNew];
for (const keyNew in storageNew) {
const valueNew = storageNew[keyNew];

if (localStorage[keyNew] !== valueNew) {
if (storage[keyNew] !== valueNew) {
localStorage[keyNew] = valueNew;
}
}

// Remove any items that aren't in the new local storage.
for (const keyOld in localStorage) {
if (!(keyOld in localStorageNew)) {
for (const keyOld in storage) {
if (!(keyOld in storageNew)) {
delete localStorage[keyOld];
}
}

// Update the global storage reference.
storage = storageNew;
}
};
10 changes: 9 additions & 1 deletion packages/moon/test/storage.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import Moon from "moon/src/index";
localStorage.bar = "baz";
jest.resetModules();

const Moon = require("moon/src/index").default;

test("sets storage initially", () => {
Moon.use({ storage: Moon.storage.driver });
Expand All @@ -10,6 +13,7 @@ test("sets storage initially", () => {

Moon.run(() => ({ storage }));
expect(JSON.parse(JSON.stringify(localStorage))).toEqual(storage);
Moon.run(input => { expect(input.storage).toEqual(storage) });
});

test("updates storage as needed", () => {
Expand All @@ -22,6 +26,7 @@ test("updates storage as needed", () => {

Moon.run(() => ({ storage }));
expect(JSON.parse(JSON.stringify(localStorage))).toEqual(storage);
Moon.run(input => { expect(input.storage).toEqual(storage) });

storage = {
foo: "bar",
Expand All @@ -30,6 +35,7 @@ test("updates storage as needed", () => {

Moon.run(() => ({ storage }));
expect(JSON.parse(JSON.stringify(localStorage))).toEqual(storage);
Moon.run(input => { expect(input.storage).toEqual(storage) });
});

test("removes storage as needed", () => {
Expand All @@ -42,11 +48,13 @@ test("removes storage as needed", () => {

Moon.run(() => ({ storage }));
expect(JSON.parse(JSON.stringify(localStorage))).toEqual(storage);
Moon.run(input => { expect(input.storage).toEqual(storage) });

storage = {
moon: "titan"
};

Moon.run(() => ({ storage }));
expect(JSON.parse(JSON.stringify(localStorage))).toEqual(storage);
Moon.run(input => { expect(input.storage).toEqual(storage) });
});

0 comments on commit 377ae05

Please sign in to comment.