Skip to content

Commit

Permalink
add storage driver
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Jan 6, 2020
1 parent 4bdd7b1 commit 2ee7ae9
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 5 deletions.
43 changes: 39 additions & 4 deletions packages/moon/dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,40 @@
driver: driver$2
};

/**
* 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;
},
output: function output(localStorageNew) {
// Update the local storage when it is an output.
for (var keyNew in localStorageNew) {
var valueNew = localStorageNew[keyNew];

if (localStorage[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)) {
delete localStorage[keyOld];
}
}
}
};

var storage = {
driver: driver$3
};

/*
* Current global response
*/
Expand All @@ -758,7 +792,7 @@
* implemented with another HTTP request once a response is received.
*/

var driver$3 = {
var driver$4 = {
input: function input() {
// Return the response as output.
return response;
Expand Down Expand Up @@ -824,7 +858,7 @@
};

var http = {
driver: driver$3
driver: driver$4
};

/**
Expand All @@ -840,7 +874,7 @@
* based on the current route.
*/

var driver$4 = {
var driver$5 = {
input: function input() {
// Return the current route as input.
return route;
Expand Down Expand Up @@ -878,7 +912,7 @@
}

var route$1 = {
driver: driver$4,
driver: driver$5,
router: router
};

Expand All @@ -887,6 +921,7 @@
http: http,
route: route$1,
run: run,
storage: storage,
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.

2 changes: 2 additions & 0 deletions packages/moon/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import run from "moon/src/run";
import data from "moon/src/data/index";
import view from "moon/src/view/index";
import time from "moon/src/time/index";
import storage from "moon/src/storage/index";
import http from "moon/src/http/index";
import route from "moon/src/route/index";

Expand All @@ -11,6 +12,7 @@ export default {
http,
route,
run,
storage,
time,
use,
version: process.env.MOON_VERSION,
Expand Down
29 changes: 29 additions & 0 deletions packages/moon/src/storage/driver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Storage driver
*
* The storage driver allows applications to receive input from local storage
* and persist string key/value pairs in local storage.
*/
export default {
input() {
// Return the local storage as input.
return localStorage;
},
output(localStorageNew) {
// Update the local storage when it is an output.
for (const keyNew in localStorageNew) {
const valueNew = localStorageNew[keyNew];

if (localStorage[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)) {
delete localStorage[keyOld];
}
}
}
};
5 changes: 5 additions & 0 deletions packages/moon/src/storage/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import driver from "moon/src/storage/driver";

export default {
driver
};
52 changes: 52 additions & 0 deletions packages/moon/test/storage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import Moon from "moon/src/index";

test("sets storage initially", () => {
Moon.use({ storage: Moon.storage.driver });

const storage = {
foo: "bar",
moon: "titan"
};

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

test("updates storage as needed", () => {
Moon.use({ storage: Moon.storage.driver });

let storage = {
foo: "bar",
moon: "titan"
};

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

storage = {
foo: "bar",
moon: "europa"
};

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

test("removes storage as needed", () => {
Moon.use({ storage: Moon.storage.driver });

let storage = {
foo: "bar",
moon: "titan"
};

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

storage = {
moon: "titan"
};

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

0 comments on commit 2ee7ae9

Please sign in to comment.