|
| 1 | +-- | A low-level primitive for building asynchronous code. |
| 2 | +module Control.Monad.Aff.Var |
| 3 | + ( AffVar() |
| 4 | + , Var() |
| 5 | + , VarF() |
| 6 | + , killVar |
| 7 | + , makeVar |
| 8 | + , putVar |
| 9 | + , takeVar |
| 10 | + ) where |
| 11 | + |
| 12 | + import Control.Monad.Aff |
| 13 | + import Control.Monad.Eff.Exception(Error()) |
| 14 | + |
| 15 | + foreign import data VarF :: ! |
| 16 | + |
| 17 | + foreign import data Var :: * -> * |
| 18 | + |
| 19 | + type AffVar e a = Aff (var :: VarF | e) a |
| 20 | + |
| 21 | + -- | Makes a new asynchronous variable. |
| 22 | + foreign import makeVar """ |
| 23 | + function makeVar(error) { |
| 24 | + return function(success) { |
| 25 | + return function() { |
| 26 | + success({ |
| 27 | + consumers: [], |
| 28 | + producers: [], |
| 29 | + error: undefined |
| 30 | + }); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + """ :: forall e a. AffVar e (Var a) |
| 35 | + |
| 36 | + -- | Takes the next value from the asynchronous variable. |
| 37 | + foreign import takeVar """ |
| 38 | + function takeVar(avar) { |
| 39 | + return function(error) { |
| 40 | + return function(success) { |
| 41 | + return function() { |
| 42 | + if (avar.error !== undefined) { |
| 43 | + error(avar.error)(); |
| 44 | + } else if (avar.producers.length > 0) { |
| 45 | + var producer = avar.producers.shift(); |
| 46 | +
|
| 47 | + producer(error, success); |
| 48 | + } else { |
| 49 | + avar.consumers.push({error: error, success: success}); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + """ :: forall e a. Var a -> AffVar e a |
| 56 | + |
| 57 | + -- | Puts a new value into the asynchronous variable. If the variable has |
| 58 | + -- | been killed, this will result in an error. |
| 59 | + foreign import putVar """ |
| 60 | + function putVar(avar) { |
| 61 | + return function(a) { |
| 62 | + return function(error) { |
| 63 | + return function(success) { |
| 64 | + return function() { |
| 65 | + if (avar.error !== undefined) { |
| 66 | + error(avar.error)(); |
| 67 | + } else if (avar.consumers.length == 0) { |
| 68 | + avar.producer.push(function(error, success) { |
| 69 | + success(a)(); |
| 70 | + }); |
| 71 | + } else { |
| 72 | + var consumer = avar.shift(); |
| 73 | +
|
| 74 | + try { |
| 75 | + consumer.success(a)(); |
| 76 | +
|
| 77 | + success({})(); |
| 78 | + } catch (e) { |
| 79 | + error(e)(); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + """ :: forall e a. Var a -> a -> AffVar e Unit |
| 88 | + |
| 89 | + -- | Kills an asynchronous variable. |
| 90 | + foreign import killVar """ |
| 91 | + function killVar(avar) { |
| 92 | + return function(e) { |
| 93 | + return function(error) { |
| 94 | + return function(success) { |
| 95 | + return function() { |
| 96 | + if (avar.error !== undefined) { |
| 97 | + error(avar.error); |
| 98 | + } else { |
| 99 | + var errors = []; |
| 100 | +
|
| 101 | + avar.error = e; |
| 102 | +
|
| 103 | + while (avar.consumers.length > 0) { |
| 104 | + var consumer = avar.shift(); |
| 105 | +
|
| 106 | + try { |
| 107 | + consumer.error(e)(); |
| 108 | + } catch (e) { |
| 109 | + errors.push(e); |
| 110 | + } |
| 111 | + } |
| 112 | +
|
| 113 | + if (errors.length > 0) error(errors[0])(); |
| 114 | + else success({})(); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + """ :: forall e a. Var a -> Error -> AffVar e Unit |
| 122 | + |
0 commit comments