The ecosystem · vibrate
A buzz from a handler
Vibrate.pattern!([300, 150, 300]) buzzes the phone twice: 300 ms on, 150 ms off, 300 ms on. The same line works in Chrome on Android and in an app on the Android host.
Use it when a phone should buzz: a timer ran out, a message arrived, a tap should be felt. Call Vibrate.once! or Vibrate.pattern! from a handler. The pomodoro example buzzes when a session ends.
Vibrate is a module both halves of an app compile, so a component that buzzes type-checks on the server and in the browser. Vibrator is a plugin that serves a small script every page loads, which passes the buzz to the browser. An Android app on the native host needs no script: the host runs the buzz itself.
Installing it#
Name the package in the app's header, and add the plugin to the app's plugins:
app [program] { …, pf: platform "../../server/platform/main.roc", vibrate: "../../packages/vibrate/main.roc" }import vibrate.Vibrator
…
program = App.program({
…
plugins: [AudioPlayer.plugin({}), Vibrator.plugin({})],
})Then import vibrate.Vibrate wherever you buzz.
Buzzing#
The whole module is two functions:
import pf.Browser
## The phone's vibration motor, from a handler: `Vibrate.once!(400)` buzzes for 400 ms,
## `Vibrate.pattern!([200, 100, 200])` buzzes, rests and buzzes again (milliseconds on, off, on…,
## as the browser's `navigator.vibrate` takes them).
##
## A buzz is a `Browser.call!("vibrate.pattern", { pattern })`, once the handler is done and the
## screen is patched. In a browser the plugin's script (`Vibrator.plugin({})` in the app's plugins)
## passes it to `navigator.vibrate`: Chrome on Android buzzes; a browser with no motor, or none it
## lets a page use (a computer's, Safari, Firefox), does nothing, and that is no failure. On an
## Android phone (D-063's native host) the phone's handler buzzes, with no script. Only a handler
## buzzes; while computing, and on the server, it fails.
Vibrate := [].{
## Buzzes once, for `ms` milliseconds (at most 10 s).
once! : U64 => {}
once! = |ms| Vibrate.pattern!([ms])
## Buzzes, rests, buzzes…: milliseconds on, off, on…, each at most 10 s.
pattern! : List(U64) => {}
pattern! = |pattern| Browser.call!("vibrate.pattern", { pattern: clamp(pattern) })
}
## A pattern a motor could play: each step at most 10 s, at most 32 steps.
clamp : List(U64) -> List(U64)
clamp = |pattern| pattern.take_first(32).map(|ms| if (ms > 10000) 10000 else ms)
expect clamp([200, 100, 200]) == [200, 100, 200]
expect clamp([20000]) == [10000]
expect clamp(List.repeat(1, 40)).len() == 32Vibrate.once!(400)buzzes once, for 400 milliseconds.Vibrate.pattern!([200, 100, 200])takes durations in milliseconds, alternating on and off, asnavigator.vibratetakes them: buzz, rest, buzz.
A step longer than 10 seconds is cut to 10, and only the first 32 steps play.
A buzz is a Browser.call! underneath, with the same rules. It runs after the handler finishes and the page shows what it changed. Only a handler can buzz: an event's handler, an effect's apply, or a timer's callback. Calling it while the page computes (a hole, a memo or a component's body), or on the server, fails. The pomodoro's timer buzzes from its interval's tick, beside its chime:
import vibrate.Vibrate
…
ended! = || {
Audio.play!("/sounds/done.wav")
Vibrate.pattern!([300, 150, 300])
…
}In a browser#
The plugin serves its script at /rocx/plugins/vibrate/vibrate.js. Every page loads it before start.js, so it is ready before the page hydrates. It registers the browser effect vibrate.pattern on window.rocxEffects, which passes the pattern to navigator.vibrate:
// The vibrate plugin's script (packages/vibrate): every page loads it before start.js, so the
// effect it registers is there before the page wakes: `vibrate.pattern`, which `Vibrate.once!`
// and `Vibrate.pattern!` call (`Browser.call!("vibrate.pattern", { pattern })`), once the handler
// is done and the page is patched. Milliseconds on, off, on…, as `navigator.vibrate` takes them. A
// browser with no motor, or none it lets a page use, does nothing, and that is no failure.
function vibrate({ pattern = [] }) {
if (typeof navigator === "undefined" || typeof navigator.vibrate !== "function") return;
try {
navigator.vibrate(pattern);
} catch {
// A page the reader hasn't touched yet may not buzz.
}
}
window.rocxEffects = { ...window.rocxEffects, "vibrate.pattern": vibrate };Chrome on Android buzzes. Desktop browsers, Safari and Firefox give pages no motor, so there a buzz does nothing, and nothing fails. A browser may also stay still until the reader has touched the page; that isn't an error either.
On a phone#
On the native Android host, a plugin's Browser.call! goes to a native handler of the same name, with no script. The host has two: audio.play (for audio, a MediaPlayer that fetches the sound from the app's server) and vibrate.pattern, which plays the pattern on Android's Vibrator as a waveform. The app's manifest asks for android.permission.VIBRATE, which Android grants at install, so the user sees no prompt.