-
Notifications
You must be signed in to change notification settings - Fork 200
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
6 changed files
with
128 additions
and
5 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,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]; | ||
} | ||
} | ||
} | ||
}; |
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,5 @@ | ||
import driver from "moon/src/storage/driver"; | ||
|
||
export default { | ||
driver | ||
}; |
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,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); | ||
}); |