0 | module TyTTP.Core.Stream
 1 |
 2 | import Data.Contravariant
 3 |
 4 | public export
 5 | record Subscriber (m : Type -> Type) (e : Type) (a : Type) where
 6 |   constructor MkSubscriber
 7 |   onNext: a -> m ()
 8 |   onSucceded: () -> m ()
 9 |   onFailed: e -> m ()
10 |
11 | export
12 | Contravariant (Subscriber m e) where
13 |   contramap f subscriber = { onNext := subscriber.onNext . f } subscriber
14 |
15 | public export
16 | record Publisher (m : Type -> Type) (e : Type) (a : Type) where
17 |   constructor MkPublisher
18 |   subscribe : Subscriber m e a -> m ()
19 |
20 | export
21 | Functor (Publisher m e) where
22 |   map f publisher = MkPublisher $ \s => publisher.subscribe $ contramap f s
23 |
24 | export
25 | empty : Publisher m e a
26 | empty = MkPublisher $ \s => s.onSucceded ()
27 |
28 | export
29 | fail : e -> Publisher m e a
30 | fail e = MkPublisher $ \s => s.onFailed e
31 |
32 | export
33 | singleton : Monad m => a -> Publisher m e a
34 | singleton a = MkPublisher $ \s => s.onNext a >>= s.onSucceded
35 |
36 | export
37 | fromList : Monad m => List a -> Publisher m e a
38 | fromList list = MkPublisher $ \s => do
39 |   traverse_ s.onNext list
40 |   s.onSucceded ()
41 |
42 |