miso
Copyright(C) 2016-2026 David M. Johnson
LicenseBSD3-style (see the file LICENSE)
MaintainerDavid M. Johnson <code@dmj.io>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Miso.Subscription.History

Description

Overview

Miso.Subscription.History wraps the browser's History API and popstate event, providing both a reactive subscription and imperative navigation helpers.

Subscriptions

uriSub fires whenever the URL changes — through browser back/forward buttons or any of the imperative helpers below:

subs :: [Sub Action]
subs = [ uriSub UrlChanged ]

routerSub is a convenience wrapper that decodes the URI via a Router instance before delivering it as an action:

subs = [ routerSub (RouteChanged . fromRight NotFound) ]

Imperative navigation

These functions push or replace entries on the browser history stack and simultaneously fire a synthetic popstate event so that uriSub and routerSub are notified automatically:

update GoHome     = io_ (pushURI (toURI Home))
update GoProfile  = io_ (pushRoute (User (Capture 42)))
update ReplaceUrl = io_ (replaceURI newUri)
update GoBack     = io_ back
update GoForward  = io_ forward
update (Jump n)   = io_ (go n)

getURI reads the current URL from window.location without subscribing:

update Init = io (GotURI <$> getURI)

See also

Synopsis

Subscription

uriSub Source #

Arguments

:: (URI -> action)

Callback fired with the new URI on every URL change

-> Sub action 

Subscription for URI changes, uses the History API.

This returns a new URI whenever go, back, forward, pushState or replaceState have been called.

routerSub Source #

Arguments

:: Router route 
=> (Either RoutingError route -> action)

Callback fired with the decoded route (or RoutingError) on every URL change

-> Sub action 

Subscription for popstate events, from the History API, mapped to a user-defined Router.

Functions

getURI :: IO URI Source #

Retrieves the current relative URI by inspecting pathname, search and hash.

pushURI Source #

Arguments

:: URI

The URI to push onto the history stack

-> IO () 

Pushes a new URI onto the History stack. Also raises a popstate event.

pushRoute Source #

Arguments

:: Router route 
=> route

The route to push onto the history stack (converted to a URI internally)

-> IO () 

Pushes a new Route onto the History stack. Also raises a popstate event.

Converts the Route to a URI internally.

replaceURI Source #

Arguments

:: URI

The URI to replace the current history entry with

-> IO () 

Replaces current URI on stack. Also raises a popstate event.

back :: IO () Source #

Navigates backwards.

forward :: IO () Source #

Navigates forwards.

go Source #

Arguments

:: Int

Number of steps to jump; positive = forward, negative = backward

-> IO () 

Jumps to a specific position in history.

Types

data URI Source #

URI type. See the official specification

Constructors

URI 

Fields

Instances

Instances details
Generic URI Source # 
Instance details

Defined in Miso.Types

Associated Types

type Rep URI 
Instance details

Defined in Miso.Types

Methods

from :: URI -> Rep URI x #

to :: Rep URI x -> URI #

Show URI Source # 
Instance details

Defined in Miso.Types

Methods

showsPrec :: Int -> URI -> ShowS #

show :: URI -> String #

showList :: [URI] -> ShowS #

Eq URI Source # 
Instance details

Defined in Miso.Types

Methods

(==) :: URI -> URI -> Bool #

(/=) :: URI -> URI -> Bool #

ToJSVal URI Source # 
Instance details

Defined in Miso.Types

Methods

toJSVal :: URI -> IO JSVal Source #

ToObject URI Source # 
Instance details

Defined in Miso.Types

Methods

toObject :: URI -> IO Object Source #

FromJSON URI Source # 
Instance details

Defined in Miso.Router

ToJSON URI Source # 
Instance details

Defined in Miso.Types

Methods

toJSON :: URI -> Value Source #

toJSONList :: [URI] -> Value

FromMisoString URI Source # 
Instance details

Defined in Miso.Router

ToMisoString URI Source # 
Instance details

Defined in Miso.Types

type Rep URI Source # 
Instance details

Defined in Miso.Types