drab v0.10.5 Drab.Waiter View Source

Enables Drab Waiter functionality - synchronous wait for browser events in the Commander handler function.

This module is optional and is not loaded by default. You need to explicitly declare it in the commander:

use Drab.Commander, modules: [Drab.Waiter]

Introduces DSL for registering events. Syntax:

waiter(socket) do
  on "selector1", "event_name", fn (sender) ->
  end
  on "selector2", "event_name", fn (sender) ->
  end
  on_timeout 5000, fn -> end
end

Link to this section Summary

Functions

Registers Javascript event_name on selector in the Drab Waiter loop. When the main loop is launched, Drab freezes the current function process and starts waiting for the events. When event occurs, it matches it and runs the corresponding lambda

Register timeout event in Drab Waiter loop. Launches anonymous function after given time (in milliseconds). When no timeout is given, Waiter will wait forever

Main Waiter loop

Link to this section Functions

Link to this macro on(selector, event_name, function) View Source (macro)

Registers Javascript event_name on selector in the Drab Waiter loop. When the main loop is launched, Drab freezes the current function process and starts waiting for the events. When event occurs, it matches it and runs the corresponding lambda.

Example:

ret = waiter(socket) do
  on "#button1", "click", fn(sender) -> sender["text"] end
  on "#input1", "keyup", fn(sender) -> sender["val"] end
end

Lambda receives sender: the same Map as the Event Handler does, known there are dom_sender.

Link to this macro on_timeout(timeout, function) View Source (macro)

Register timeout event in Drab Waiter loop. Launches anonymous function after given time (in milliseconds). When no timeout is given, Waiter will wait forever.

Example:

ret = waiter(socket) do
  on "#button1", "click", fn(sender) -> sender["text"] end
  on_timeout 5000, fn() -> "timed out" end
end
Link to this macro waiter(socket, list) View Source (macro)

Main Waiter loop.

Takes socket as an argument, returns the return value of the function which matched the selector and event.

Inside the do block you may register Browser Events which Waiter will react to. See Drab.Waiter.on.