Skip to content

Commit

Permalink
Let vite refreshes page if Elm's initial state is changed
Browse files Browse the repository at this point in the history
  • Loading branch information
hmsk committed Jun 20, 2021
1 parent 458197e commit 9c910a2
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ if (import.meta.hot) {
const id = getId()
const instance = {
id, path, domNode, flags, portSubscribes, portSends,
lastState: null
lastState: null,
initialState: null
}
return instances[id] = instance
}
Expand Down Expand Up @@ -277,6 +278,11 @@ if (import.meta.hot) {
let oldModel = swappingInstance.lastState
const newModel = initialStateTuple.a
if (JSON.stringify(newModel.state.a) !== swappingInstance.initialState) {
console.log("[vite-plugin-elm] Initial state seems to be updated. Refreshes page")
import.meta.hot.invalidate()
}
if (typeof elmSymbol("elm$browser$Browser$application") !== "undefined" && typeof elmSymbol("elm$browser$Browser$Navigation") !== "undefined") {
const oldKeyLoc = findNavKey(oldModel)
const newKeyLoc = findNavKey(newModel)
Expand Down Expand Up @@ -304,6 +310,7 @@ if (import.meta.hot) {
initialStateTuple.b = elmSymbol("elm$core$Platform$Cmd$none")
} else {
initializingInstance.lastState = initialStateTuple.a
initializingInstance.initialState = JSON.stringify(initialStateTuple.a.state.a)
}
return initialStateTuple
Expand Down

0 comments on commit 9c910a2

Please sign in to comment.