Skip to content

Commit

Permalink
navigation url request interface +
Browse files Browse the repository at this point in the history
  • Loading branch information
lue-bird committed Nov 25, 2023
1 parent 1a41d53 commit 53a3ff9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
4 changes: 4 additions & 0 deletions runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ export function start(config: { ports: ElmPorts, domElement: HTMLElement }) {
on: event => event?.removeAnimationFrameListen,
run: (_config, _sendToElm) => { removeAnimationFrameListen() }
},
{
on: event => event?.addNavigationUrlRequest,
run: (_config, sendToElm) => { sendToElm(window.location.href) }
},
{
on: event => event?.addDocumentEventListen,
run: documentEventListenAdd
Expand Down
34 changes: 34 additions & 0 deletions src/BrowserApp.elm
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ type InterfaceSingle state
| HttpRequest (HttpRequest state)
| WindowEventListen { eventName : String, on : Json.Decode.Value -> state }
| WindowAnimationFrameListen (Time.Posix -> state)
| NavigationUrlRequest (String -> state)
| DocumentEventListen { eventName : String, on : Json.Decode.Value -> state }
| NavigationReplaceUrl String
| NavigationPushUrl String
Expand Down Expand Up @@ -259,6 +260,7 @@ type InterfaceSingleId
| IdHttpRequest HttpRequestId
| IdWindowEventListen String
| IdWindowAnimationFrameListen
| IdNavigationUrlRequest
| IdDocumentEventListen String
| IdNavigationReplaceUrl String
| IdNavigationPushUrl String
Expand Down Expand Up @@ -499,6 +501,9 @@ interfaceSingleMap stateChange =
WindowAnimationFrameListen toState ->
(\event -> toState event |> stateChange) |> WindowAnimationFrameListen

NavigationUrlRequest toState ->
(\event -> toState event |> stateChange) |> NavigationUrlRequest

DocumentEventListen listen ->
{ eventName = listen.eventName, on = \value -> listen.on value |> stateChange }
|> DocumentEventListen
Expand Down Expand Up @@ -678,6 +683,9 @@ interfaceSingleToId =
WindowAnimationFrameListen _ ->
IdWindowAnimationFrameListen

NavigationUrlRequest _ ->
IdNavigationUrlRequest

DocumentEventListen listen ->
IdDocumentEventListen listen.eventName

Expand Down Expand Up @@ -766,6 +774,14 @@ interfaceIdOrder =
_ ->
GT

IdNavigationUrlRequest ->
case b of
IdNavigationUrlRequest ->
EQ

_ ->
GT

IdDocumentEventListen aEventName ->
case b of
IdDocumentEventListen bEventName ->
Expand Down Expand Up @@ -966,6 +982,9 @@ interfaceDiffToCmds =
WindowAnimationFrameListen _ ->
RemoveWindowAnimationFrameListen |> Just

NavigationUrlRequest _ ->
Nothing

DocumentEventListen listen ->
RemoveDocumentEventListen listen.eventName |> Just

Expand Down Expand Up @@ -1016,6 +1035,9 @@ interfaceDiffToCmds =
WindowAnimationFrameListen _ ->
AddWindowAnimationFrameListen |> Just

NavigationUrlRequest _ ->
AddNavigationUrlRequest |> Just

DocumentEventListen listen ->
AddDocumentEventListen listen.eventName |> Just

Expand Down Expand Up @@ -1124,6 +1146,9 @@ interfaceDiffToJson =
RemoveWindowAnimationFrameListen ->
( "removeWindowAnimationFrameListen", Json.Encode.null )

AddNavigationUrlRequest ->
( "addNavigationUrlRequest", Json.Encode.null )

AddDocumentEventListen eventName ->
( "addDocumentEventListen", eventName |> Json.Encode.string )

Expand Down Expand Up @@ -1417,6 +1442,14 @@ eventDataAndConstructStateJsonDecoder interfaceDiff interface =
_ ->
Nothing

NavigationUrlRequest toState ->
case interfaceDiff of
AddNavigationUrlRequest ->
Json.Decode.string |> Json.Decode.map toState |> Just

_ ->
Nothing

DocumentEventListen listen ->
case interfaceDiff of
AddDocumentEventListen addedEventName ->
Expand Down Expand Up @@ -1729,6 +1762,7 @@ type InterfaceDiff
| RemoveWindowEventListen String
| AddWindowAnimationFrameListen
| RemoveWindowAnimationFrameListen
| AddNavigationUrlRequest
| AddDocumentEventListen String
| RemoveDocumentEventListen String
| AddNavigationReplaceUrl String
Expand Down
16 changes: 15 additions & 1 deletion src/BrowserApp/Navigation.elm
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module BrowserApp.Navigation exposing
( forward, back, pushUrl, replaceUrl
( urlRequest
, forward, back, pushUrl, replaceUrl
, load, reload
)

{-| Helpers for `history` interaction as part of an [`Interface`](BrowserApp#Interface)
@docs urlRequest
@docs forward, back, pushUrl, replaceUrl
@docs load, reload
Expand All @@ -14,6 +16,18 @@ import BrowserApp
import Rope


{-| An [`Interface`](BrowserApp#Interface) for getting the current page's url.
Is usually used while starting up the app.
Note: Uses [`window.location.href`](https://developer.mozilla.org/en-US/docs/Web/API/Window/location)
-}
urlRequest : BrowserApp.Interface String
urlRequest =
BrowserApp.NavigationUrlRequest identity
|> Rope.singleton


{-| An [`Interface`](BrowserApp#Interface) that changes the URL,
but neither triggers a page load nor adds a new entry to the browser history.
Expand Down

0 comments on commit 53a3ff9

Please sign in to comment.