The weird world of observable keys ๐Ÿ”‘๐Ÿ‘€

The get that keeps on getting.


Given a bus:

import Foundation
struct Value : Event {
  let key: String
  let val: Any?
protocol Store {
  func get(_ key: String)
  func set(_ key: String, _ val: Any?)
class LocalStore : Store {
  let db: UserDefaults
  let bus: Bus
  init(db: UserDefaults, bus: Bus){
    self.db = db
    self.bus = bus
  func get(_ key: String) {
    let val = db.object(forKey: key) key, val: val))
  func set(_ key: String, _ val: Any?){
    db.set(val, forKey: key) key, val: val))
let bus = Bus()
let local = LocalStore(db: UserDefaults.standard, bus: bus)
local.set("foo", "bar")

A couple features I like:

  • The bus provides a consistent interface for adding and removing subscriptions
  • There’s a straightforward get method to flush a value into the bus


If rationalizing events bubbling up from UI and disparate data sources is challenging, normalizing to a single bus interface may be helpful.