Skip to content

Commit 377ae05

Browse files
committed
use cached storage
1 parent 2ee7ae9 commit 377ae05

File tree

4 files changed

+56
-19
lines changed

4 files changed

+56
-19
lines changed

packages/moon/dist/moon.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -739,37 +739,52 @@
739739
driver: driver$2
740740
};
741741

742+
/*
743+
* Current storage
744+
*/
745+
var storage = {};
746+
747+
for (var key in localStorage) {
748+
if (localStorage.hasOwnProperty(key)) {
749+
storage[key] = localStorage[key];
750+
}
751+
}
742752
/**
743753
* Storage driver
744754
*
745755
* The storage driver allows applications to receive input from local storage
746756
* and persist string key/value pairs in local storage.
747757
*/
758+
759+
748760
var driver$3 = {
749761
input: function input() {
750762
// Return the local storage as input.
751-
return localStorage;
763+
return storage;
752764
},
753-
output: function output(localStorageNew) {
765+
output: function output(storageNew) {
754766
// Update the local storage when it is an output.
755-
for (var keyNew in localStorageNew) {
756-
var valueNew = localStorageNew[keyNew];
767+
for (var keyNew in storageNew) {
768+
var valueNew = storageNew[keyNew];
757769

758-
if (localStorage[keyNew] !== valueNew) {
770+
if (storage[keyNew] !== valueNew) {
759771
localStorage[keyNew] = valueNew;
760772
}
761773
} // Remove any items that aren't in the new local storage.
762774

763775

764-
for (var keyOld in localStorage) {
765-
if (!(keyOld in localStorageNew)) {
776+
for (var keyOld in storage) {
777+
if (!(keyOld in storageNew)) {
766778
delete localStorage[keyOld];
767779
}
768-
}
780+
} // Update the global storage reference.
781+
782+
783+
storage = storageNew;
769784
}
770785
};
771786

772-
var storage = {
787+
var storage$1 = {
773788
driver: driver$3
774789
};
775790

@@ -921,7 +936,7 @@
921936
http: http,
922937
route: route$1,
923938
run: run,
924-
storage: storage,
939+
storage: storage$1,
925940
time: time,
926941
use: use,
927942
version: "1.0.0-beta.4",

packages/moon/dist/moon.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
/*
2+
* Current storage
3+
*/
4+
let storage = {};
5+
6+
for (const key in localStorage) {
7+
if (localStorage.hasOwnProperty(key)) {
8+
storage[key] = localStorage[key];
9+
}
10+
}
11+
112
/**
213
* Storage driver
314
*
@@ -7,23 +18,26 @@
718
export default {
819
input() {
920
// Return the local storage as input.
10-
return localStorage;
21+
return storage;
1122
},
12-
output(localStorageNew) {
23+
output(storageNew) {
1324
// Update the local storage when it is an output.
14-
for (const keyNew in localStorageNew) {
15-
const valueNew = localStorageNew[keyNew];
25+
for (const keyNew in storageNew) {
26+
const valueNew = storageNew[keyNew];
1627

17-
if (localStorage[keyNew] !== valueNew) {
28+
if (storage[keyNew] !== valueNew) {
1829
localStorage[keyNew] = valueNew;
1930
}
2031
}
2132

2233
// Remove any items that aren't in the new local storage.
23-
for (const keyOld in localStorage) {
24-
if (!(keyOld in localStorageNew)) {
34+
for (const keyOld in storage) {
35+
if (!(keyOld in storageNew)) {
2536
delete localStorage[keyOld];
2637
}
2738
}
39+
40+
// Update the global storage reference.
41+
storage = storageNew;
2842
}
2943
};

packages/moon/test/storage.test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import Moon from "moon/src/index";
1+
localStorage.bar = "baz";
2+
jest.resetModules();
3+
4+
const Moon = require("moon/src/index").default;
25

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

1114
Moon.run(() => ({ storage }));
1215
expect(JSON.parse(JSON.stringify(localStorage))).toEqual(storage);
16+
Moon.run(input => { expect(input.storage).toEqual(storage) });
1317
});
1418

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

2327
Moon.run(() => ({ storage }));
2428
expect(JSON.parse(JSON.stringify(localStorage))).toEqual(storage);
29+
Moon.run(input => { expect(input.storage).toEqual(storage) });
2530

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

3136
Moon.run(() => ({ storage }));
3237
expect(JSON.parse(JSON.stringify(localStorage))).toEqual(storage);
38+
Moon.run(input => { expect(input.storage).toEqual(storage) });
3339
});
3440

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

4349
Moon.run(() => ({ storage }));
4450
expect(JSON.parse(JSON.stringify(localStorage))).toEqual(storage);
51+
Moon.run(input => { expect(input.storage).toEqual(storage) });
4552

4653
storage = {
4754
moon: "titan"
4855
};
4956

5057
Moon.run(() => ({ storage }));
5158
expect(JSON.parse(JSON.stringify(localStorage))).toEqual(storage);
59+
Moon.run(input => { expect(input.storage).toEqual(storage) });
5260
});

0 commit comments

Comments
 (0)