Module

FRP.Event

Package
purescript-hyrule
Repository
mikesol/purescript-hyrule

#fix Source

fix :: forall m s i o. MonadST s m => Monad m => (AnEvent m i -> { input :: AnEvent m i, output :: AnEvent m o }) -> AnEvent m o

Compute a fixed point

#fold Source

fold :: forall m s a b. MonadST s m => (a -> b -> b) -> AnEvent m a -> b -> AnEvent m b

Fold over values received from some Event, creating a new Event.

#keepLatest Source

keepLatest :: forall m s a. MonadST s m => AnEvent m (AnEvent m a) -> AnEvent m a

Flatten a nested Event, reporting values only from the most recent inner Event.

#sampleOn Source

sampleOn :: forall m s a b. MonadST s m => Applicative m => AnEvent m a -> AnEvent m (a -> b) -> AnEvent m b

Create an Event which samples the latest values from the first event at the times when the second event fires.

#AnEvent Source

newtype AnEvent :: (Type -> Type) -> Type -> Typenewtype AnEvent m a

An Event represents a collection of discrete occurrences with associated times. Conceptually, an Event is a (possibly-infinite) list of values-and-times:

type Event a = List { value :: a, time :: Time }

Events are created from real events like timers or mouse clicks, and then combined using the various functions and instances provided in this module.

Events are consumed by providing a callback using the subscribe function.

Instances

#Event Source

type Event a = AnEvent Effect a

Instances

#EventIO Source

type EventIO a = { event :: Event a, push :: a -> Effect Unit }

#create Source

create :: forall m1 m2 s a. MonadST s m1 => MonadST s m2 => m1 (AnEventIO m2 a)

Create an event and a function which supplies a value to that event.

#makeEvent Source

makeEvent :: forall m a. ((a -> m Unit) -> m (m Unit)) -> AnEvent m a

Make an Event from a function which accepts a callback and returns an unsubscription function.

Note: you probably want to use create instead, unless you need explicit control over unsubscription.

#subscribe Source

subscribe :: forall m a. AnEvent m a -> (a -> m Unit) -> m (m Unit)

Subscribe to an Event by providing a callback.

subscribe returns a canceller function.

#bang Source

bang :: forall m a. Applicative m => a -> AnEvent m a

#bus Source

bus :: forall m s r a. MonadST s m => ((a -> m Unit) -> AnEvent m a -> r) -> AnEvent m r

Creates an event bus within a closure.

#memoize Source

memoize :: forall m s r a. MonadST s m => AnEvent m a -> (AnEvent m a -> r) -> AnEvent m r

Takes an event and memoizes it within a closure. All interactions with the event in the closure will not trigger a fresh subscription. Outside the closure does, however, trigger a fresh subscription.

#hot Source

hot :: forall m s a. MonadST s m => AnEvent m a -> m { event :: AnEvent m a, unsubscribe :: m Unit }

Makes an event hot, meaning that it will start firing on left-bind. This means that bang should never be used with hot as it will be lost. Use this for loops, for example.

#mailboxed Source

mailboxed :: forall m s r a. Ord a => MonadST s m => AnEvent m a -> ((a -> AnEvent m Unit) -> r) -> AnEvent m r

Takes the entire domain of a and allows for ad-hoc specialization.

#delay Source

delay :: forall a. Int -> Event a -> Event a

#ToEvent Source

type ToEvent :: (Type -> Type) -> Type -> Typetype ToEvent m a = Always (Endo Function (Effect Unit)) (Endo Function (m Unit)) => Always (m (m Unit)) (Effect (Effect Unit)) => Applicative m => a

#toEvent Source

toEvent :: forall m. ToEvent m ((AnEvent m) ~> Event)

#FromEvent Source

type FromEvent :: (Type -> Type) -> Type -> Typetype FromEvent m a = Always (m Unit) (Effect Unit) => Always (Endo Function (Effect (Effect Unit))) (Endo Function (m (m Unit))) => Applicative m => a

#fromEvent Source

fromEvent :: forall m. FromEvent m (Event ~> (AnEvent m))

Re-exports from FRP.Event.Class

#Filterable Source

class Filterable :: (Type -> Type) -> Constraintclass (Compactable f, Functor f) <= Filterable f  where

Filterable represents data structures which can be partitioned/filtered.

  • partitionMap - partition a data structure based on an either predicate.
  • partition - partition a data structure based on boolean predicate.
  • filterMap - map over a data structure and filter based on a maybe.
  • filter - filter a data structure based on a boolean.

Laws:

  • Functor Relation: filterMap identity ≡ compact

  • Functor Identity: filterMap Just ≡ identity

  • Kleisli Composition: filterMap (l <=< r) ≡ filterMap l <<< filterMap r

  • filter ≡ filterMap <<< maybeBool

  • filterMap p ≡ filter (isJust <<< p)

  • Functor Relation: partitionMap identity ≡ separate

  • Functor Identity 1: _.right <<< partitionMap Right ≡ identity

  • Functor Identity 2: _.left <<< partitionMap Left ≡ identity

  • f <<< partition ≡ partitionMap <<< eitherBool where f = \{ no, yes } -> { left: no, right: yes }

  • f <<< partitionMap p ≡ partition (isRight <<< p) where f = \{ left, right } -> { no: left, yes: right}

Default implementations are provided by the following functions:

  • partitionDefault
  • partitionDefaultFilter
  • partitionDefaultFilterMap
  • partitionMapDefault
  • filterDefault
  • filterDefaultPartition
  • filterDefaultPartitionMap
  • filterMapDefault

Members

Instances

#IsEvent Source

class IsEvent :: (Type -> Type) -> Constraintclass (Plus event, Filterable event) <= IsEvent event  where

Functions which an Event type should implement:

  • fold: combines incoming values using the specified function, starting with the specific initial value.
  • keepLatest flattens a nested event, reporting values only from the most recent inner event.
  • sampleOn: samples an event at the times when a second event fires.
  • fix: compute a fixed point, by feeding output events back in as inputs.
  • bang: A one-shot event that happens NOW.

Members

  • fold :: forall a b. (a -> b -> b) -> event a -> b -> event b
  • keepLatest :: forall a. event (event a) -> event a
  • sampleOn :: forall a b. event a -> event (a -> b) -> event b
  • fix :: forall i o. (event i -> { input :: event i, output :: event o }) -> event o

#withLast Source

withLast :: forall event a. IsEvent event => event a -> event { last :: Maybe a, now :: a }

Compute differences between successive event values.

#sampleOn_ Source

sampleOn_ :: forall event a b. IsEvent event => event a -> event b -> event a

Create an Event which samples the latest values from the first event at the times when the second event fires, ignoring the values produced by the second event.

#mapAccum Source

mapAccum :: forall event a b c. IsEvent event => (a -> b -> Tuple b c) -> event a -> b -> event c

Map over an event with an accumulator.

For example, to keep the index of the current event:

mapAccum (\x i -> Tuple (i + 1) (Tuple x i)) 0`.

#gateBy Source

gateBy :: forall a b event. IsEvent event => (Maybe a -> b -> Boolean) -> event a -> event b -> event b

Generalised form of gateBy, allowing for any predicate between the two events. The predicate will not be evaluated until a value from the first event is received.

#gate Source

gate :: forall a event. IsEvent event => event Boolean -> event a -> event a

Sample the events that are fired while a boolean event is true. Note that, until the boolean event fires, it will be assumed to be false, and events will be blocked.

#folded Source

folded :: forall event a. IsEvent event => Monoid a => event a -> event a

Combine subsequent events using a Monoid.

#count Source

count :: forall event a. IsEvent event => event a -> event Int

Count the number of events received.