TVar with change notification - Haskell

Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.

Sridhar Ratnakumar

What's a good approach to extending a TVar to support change-notification? i.e., allow another thread to get notified when the TVar changes.

My current approach,

-- A mutable variable with change notification
--
-- one subscriber only (because of TMVar)
data Changing a = Changing
  { -- | Current value
    changingCurrent  :: TMVar a,
    -- | To get notified of whenever the value changes.
    --
    -- Only one reader is supported.
    changingUpdated :: TMVar ()
  }
Sridhar Ratnakumar

https://github.com/srid/ema/blob/69c292f0f326cc572fd3975fb256ab821eb9ab75/src/Ema/Changing.hs

WIP: Static site generator that is change-aware. Contribute to srid/ema development by creating an account on GitHub.
Sridhar Ratnakumar

By the way, for the record - this type has evolved to support multiple subscribers - almost resembling pubsub - but with unit value being published (because actual value is in the other TVar).

https://github.com/srid/ema/blob/593bc9c49e3e77a54ed3f082bfb42e1b174b154e/src/Ema/Changing.hs

Not happy with how it looks, abstraction wise. Looks a bit messy, the code.

WIP: Static site generator that is change-aware. Contribute to srid/ema development by creating an account on GitHub.
Sridhar Ratnakumar

Introducing LVar

https://github.com/srid/ema/blob/master/src/Data/LVar.hs

Should soon be a library.

WIP: Static site generator that is change-aware. Contribute to srid/ema development by creating an account on GitHub.