Services

SharedPreferences

SharedPreferences service

Create or retrieve this service with page.shared_preferences(**properties). Ruflet reuses the registered service unless you supply a different id.

Example

status = text(value: "Saving preference…")
page.add(status)
preferences = page.shared_preferences
preferences.set("theme", "dark", on_result: ->(_saved, save_error) {
  next page.update(status, value: save_error.to_s) if save_error

  preferences.get("theme", on_result: ->(value, read_error) {
    page.update(status, value: read_error ? read_error.to_s : "Theme: #{value}")
  })
})

Page accessor

  • page.shared_preferences(**props)

Properties

  • data — Arbitrary value you can attach to the control and read back in handlers.
  • key — Stable identity used to preserve state across rebuilds.

Page proxy methods

Calling page.shared_preferences with no properties returns a focused proxy with these methods:

  • clear(timeout: ..., on_result: ...)
  • contains_key(key, timeout: ..., on_result: ...)
  • get(key, timeout: ..., on_result: ...)
  • get_keys(key_prefix, timeout: ..., on_result: ...)
  • remove(key, timeout: ..., on_result: ...)
  • set(key, value, timeout: ..., on_result: ...)

Result and error handling

Client calls are asynchronous. When a method accepts on_result:, handle both callback values: |result, error|. Availability and returned data can vary by platform.

Common service API

The service also supports the common Ruflet control API: attach_handler(event_name, handler), children, emit(event_name, event), has_handler?(event_name), id, merge_props(values = ...), on(event_name, &block), props, runtime_page, runtime_page=, to_patch, type, wire_id, wire_id=.

Use the named methods above for application code; common control methods mainly support event registration, updates, and runtime integration.

Wire reference

  • Service helper: shared_preferences
  • Wire type: sharedpreferences
  • Aliases: sharedpreferences