-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
24 lines (20 loc) · 914 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import {Elm} from "./Main.elm"
// Read the settings and high scores from local storage. This is sent into the Elm app as JSON which is decoded using
// Elm. Any errors in decoding (or null values) result in the default settings and high scores being used instead.
const settings = localStorage.getItem("settings")
const highScores = localStorage.getItem("highScores")
const app = Elm.Main.init({
node: document.querySelector("main"),
flags: {
settings: settings ? JSON.parse(settings) : null,
highScores: highScores ? JSON.parse(highScores) : null
}
})
/**
* Persists the settings in local storage.
*/
app.ports.persistSettings.subscribe(settings => localStorage.setItem("settings", JSON.stringify(settings)))
/**
* Persists the high scores in local storage.
*/
app.ports.persistHighScores.subscribe(highScores => localStorage.setItem("highScores", JSON.stringify(highScores)))