haskell-gi-base-0.26.9: Foundation for libraries generated by haskell-gi

Safe Haskell | Safe-Inferred
Language | Haskell2010

Data.GI.Base.DynVal

Description

This is an experimental module that introduces support for dynamic values: these are functions from a record `model` to some type `a` which keep track of which selectors of `model` does the result depend on. For example, for a record of the form

    data Example = Example {
        first  :: Int,
        second :: Bool,
        third  :: Float
    }

a `DynVal Example String` could be constructed, assuming that you are given a `record` `DynVal` representing the full record, using:

    let format = \f s -> "First is " <> f <> " and second is " <> s
        formatted = format <$> record.first <*> record.second :: DynVal Example String

Here we are showcasing two properties of `DynVal`s: they can be conveniently constructed using `OverloadedRecordDot`, and they provide an `Applicative` instance. The resulting `formatted` `DynVal` keeps track of the fact that it depends on the `first` and `second` record selectors.

Synopsis

- data DynVal model a = DynVal DVKey (model -> a)
- data DVKey
  - = DVKeyDirect [Text]
  - | DVKeyDerived (Set [Text])
- data ModelProxy model = ModelProxy (IO model) (DVKey -> (model -> IO ()) -> IO ()) ([Text] -> (model -> Maybe model) -> IO ())
- dvKeys :: DynVal model a -> DVKey
- dvRead :: DynVal model a -> model -> a
- modelProxyCurrentValue :: ModelProxy model -> IO model
- modelProxyRegisterHandler :: ModelProxy model -> DVKey -> (model -> IO ()) -> IO ()
- modelProxyUpdate :: ModelProxy model -> [Text] -> (model -> Maybe model) -> IO ()

# Documentation

data DynVal model a Source #

A `DynVal` is a way of extracting values of type `a` from `model`, which keeps track of which fields (parameterised by `dvKeys`) in `model` are needed for computing the `DynVal`.

Constructors

DynVal DVKey (model -> a) |

#### Instances

Instances details

(HasField fieldName field a, KnownSymbol fieldName) => HasField (fieldName :: Symbol) (DynVal model field) (DynVal model a) Source # |
Instance details Defined in Data.GI.Base.DynVal Methods getField :: DynVal model field -> DynVal model a Source #
Applicative (DynVal model) Source # |
Instance details Defined in Data.GI.Base.DynVal Methods pure :: a -> DynVal model a Source # (<*>) :: DynVal model (a -> b) -> DynVal model a -> DynVal model b Source # liftA2 :: (a -> b -> c) -> DynVal model a -> DynVal model b -> DynVal model c Source # (*>) :: DynVal model a -> DynVal model b -> DynVal model b Source # (<*) :: DynVal model a -> DynVal model b -> DynVal model a Source #
Functor (DynVal model) Source # |
Instance details Defined in Data.GI.Base.DynVal Methods fmap :: (a -> b) -> DynVal model a -> DynVal model b Source # (<$) :: a -> DynVal model b -> DynVal model a Source #
IsString (DynVal model Text) Source # |
Instance details Defined in Data.GI.Base.DynVal Methods fromString :: String -> DynVal model Text Source #

data DVKey Source #

Constructors

DVKeyDirect [Text] | Direct access to subfields: for example writing `record.field.subfield` (using the `HasField` instance) would lead to ``DVKeyDirect` ["field", "subfield"]`
DVKeyDerived (Set [Text]) | Value derived from a direct key, by acting with the functor or applicative instances.

#### Instances

Instances details

Semigroup DVKey Source # | Joining of keys always produces derived ones.
Instance details Defined in Data.GI.Base.DynVal Methods (<>) :: DVKey -> DVKey -> DVKey Source # sconcat :: NonEmpty DVKey -> DVKey Source # stimes :: Integral b => b -> DVKey -> DVKey Source #
Show DVKey Source # |
Instance details Defined in Data.GI.Base.DynVal Methods showsPrec :: Int -> DVKey -> ShowS Source # show :: DVKey -> String Source # showList :: [DVKey] -> ShowS Source #
Eq DVKey Source # |
Instance details Defined in Data.GI.Base.DynVal Methods (==) :: DVKey -> DVKey -> Bool Source # (/=) :: DVKey -> DVKey -> Bool Source #
Ord DVKey Source # |
Instance details Defined in Data.GI.Base.DynVal Methods compare :: DVKey -> DVKey -> Ordering Source # (<) :: DVKey -> DVKey -> Bool Source # (<=) :: DVKey -> DVKey -> Bool Source # (>) :: DVKey -> DVKey -> Bool Source # (>=) :: DVKey -> DVKey -> Bool Source # max :: DVKey -> DVKey -> DVKey Source # min :: DVKey -> DVKey -> DVKey Source #

data ModelProxy model Source #

A `ModelProxy` is a way of obtaining records of type `model`, which allows for registering for notifications whenever certain keys (typically associated to record fields) get modified, and allows to modify fields of the model.

Constructors

ModelProxy (IO model) (DVKey -> (model -> IO ()) -> IO ()) ([Text] -> (model -> Maybe model) -> IO ()) |

dvKeys :: DynVal model a -> DVKey Source #

Keys to fields in the model that this `DynVal` depends on.

dvRead :: DynVal model a -> model -> a Source #

Compute the actual value given a model.

modelProxyCurrentValue :: ModelProxy model -> IO model Source #

Obtain the current value of the model.

modelProxyRegisterHandler :: ModelProxy model -> DVKey -> (model -> IO ()) -> IO () Source #

Register a handler that will be executed whenever any of the fields in the model pointed to by the keys is modified.

modelProxyUpdate :: ModelProxy model -> [Text] -> (model -> Maybe model) -> IO () Source #

Modify the given keys in the proxy, using the given update function, of type (model -> Maybe model). If this function returns Nothing no modification will be performed, otherwise the modified model will be stored in the ModelProxy, and any listeners will be notified of a change.