ExLiveUrl.Operation (ExLiveUrl v0.2.0)

ExLiveUrl.Operation structs represent url operations. An operation consists of a :url which the operation targets, a :mode which indicates how the server should get to the target (updating the current view, mounting a new view, or going to an external url), and a :stack_operation which indicates how applying the operation should update the browser's history stack (pushing a history entry, replacing the current history entry, or updating window.location entirely).

Link to this section Summary

Functions

Apply an operation to the given socket.

Build a push navigate operation.

Build a push patch operation.

Build a redirect operation.

Link to this section Types

Link to this type

t()

(since 0.2.0)
@type t() :: %ExLiveUrl.Operation{
  mode: :intra_view | :inter_view | :external,
  stack_operation: :push | :replace | :redirect,
  url: ExLiveUrl.Url.t()
}

Link to this section Functions

Link to this function

apply(socket, operation)

(since 0.2.0)

Apply an operation to the given socket.

iex> socket = %Phoenix.LiveView.Socket{
...>   assigns: %{
...>     :__changed__ => %{},
...>     ExLiveUrl => ExLiveUrl.Url.from_string("http://apple.com")
...>   }
...> }
iex> ExLiveUrl.Operation.apply(socket, ExLiveUrl.Operation.redirect(external: "https://google.com"))
%Phoenix.LiveView.Socket{
  assigns: %{
    :__changed__ => %{},
    ExLiveUrl => %ExLiveUrl.Url{
      scheme: :http,
      host: "apple.com",
      port: 80,
      path: "/",
      params: %{}
    }
  },
  redirected: {:redirect, %{external: "https://google.com:443/?"}}
}
Link to this function

push_navigate(opts)

(since 0.2.0)
@spec push_navigate(
  [{:to, ExLiveUrl.Url.t()}]
  | [to: ExLiveUrl.Url.t(), replace: true]
) :: t()

Build a push navigate operation.

iex> ExLiveUrl.Operation.push_navigate(to: ExLiveUrl.Url.from_string("https://google.com"))
%ExLiveUrl.Operation{
  url: %ExLiveUrl.Url{
    scheme: :https,
    host: "google.com",
    port: 443,
    path: "/",
    params: %{}
  },
  mode: :inter_view,
  stack_operation: :push
}

iex> ExLiveUrl.Operation.push_navigate(to: ExLiveUrl.Url.from_string("https://google.com"), replace: true)
%ExLiveUrl.Operation{
  url: %ExLiveUrl.Url{
    scheme: :https,
    host: "google.com",
    port: 443,
    path: "/",
    params: %{}
  },
  mode: :inter_view,
  stack_operation: :replace
}
Link to this function

push_patch(opts)

(since 0.2.0)
@spec push_patch([{:to, ExLiveUrl.Url.t()}] | [to: ExLiveUrl.Url.t(), replace: true]) ::
  t()

Build a push patch operation.

iex> ExLiveUrl.Operation.push_patch(to: ExLiveUrl.Url.from_string("https://google.com"))
%ExLiveUrl.Operation{
  url: %ExLiveUrl.Url{
    scheme: :https,
    host: "google.com",
    port: 443,
    path: "/",
    params: %{}
  },
  mode: :intra_view,
  stack_operation: :push
}

iex> ExLiveUrl.Operation.push_patch(to: ExLiveUrl.Url.from_string("https://google.com"), replace: true)
%ExLiveUrl.Operation{
  url: %ExLiveUrl.Url{
    scheme: :https,
    host: "google.com",
    port: 443,
    path: "/",
    params: %{}
  },
  mode: :intra_view,
  stack_operation: :replace
}
Link to this function

redirect(opts)

(since 0.2.0)
@spec redirect(
  [{:to, ExLiveUrl.Url.t()}]
  | [{:external, String.t() | ExLiveUrl.Url.t()}]
) :: t()

Build a redirect operation.

iex> ExLiveUrl.Operation.redirect(to: ExLiveUrl.Url.from_string("https://google.com"))
%ExLiveUrl.Operation{
  url: %ExLiveUrl.Url{
    scheme: :https,
    host: "google.com",
    port: 443,
    path: "/",
    params: %{}
  },
  mode: :inter_view,
  stack_operation: :redirect
}

iex> ExLiveUrl.Operation.redirect(external: "https://google.com")
%ExLiveUrl.Operation{
  url: %ExLiveUrl.Url{
    scheme: :https,
    host: "google.com",
    port: 443,
    path: "/",
    params: %{}
  },
  mode: :external,
  stack_operation: :redirect
}

iex> ExLiveUrl.Operation.redirect(external: ExLiveUrl.Url.from_string("https://google.com"))
%ExLiveUrl.Operation{
  url: %ExLiveUrl.Url{
    scheme: :https,
    host: "google.com",
    port: 443,
    path: "/",
    params: %{}
  },
  mode: :external,
  stack_operation: :redirect
}