2 | import Control.Monad.Either
27 | data Attribute : (alwaysReturns : Bool)
28 | -> (f : Type -> Type)
36 | Attr : (get : JSIO a) -> (set : a -> JSIO ()) -> Attribute True Prelude.id a
40 | (get : JSIO (Maybe a))
41 | -> (set : Maybe a -> JSIO ())
42 | -> Attribute False Maybe a
46 | (get : JSIO (Optional a))
47 | -> (set : Optional a -> JSIO ())
49 | -> Attribute True Optional a
52 | OptionalAttrNoDefault :
53 | (get : JSIO (Optional a))
54 | -> (set : Optional a -> JSIO ())
55 | -> Attribute False Optional a
64 | get : (o : obj) -> (attr : obj -> Attribute b f a) -> JSIO $
f a
65 | get o g = case g o of
67 | NullableAttr gt _ => gt
68 | OptionalAttr gt _ _ => gt
69 | OptionalAttrNoDefault gt _ => gt
73 | over : (a -> b) -> (attr : Attribute x f a) -> JSIO $
f b
74 | over f (Attr gt _) = map f gt
75 | over f (NullableAttr gt _) = map f <$> gt
76 | over f (OptionalAttr gt _ _) = map f <$> gt
77 | over f (OptionalAttrNoDefault gt _) = map f <$> gt
86 | to : (attr : obj -> Attribute b f a) -> (o : obj) -> JSIO $
f a
92 | set' : (attr : Attribute b f a) -> f a -> JSIO ()
94 | set' (NullableAttr _ s) = s
95 | set' (OptionalAttr _ s _) = s
96 | set' (OptionalAttrNoDefault _ s) = s
102 | getDef : (o : obj) -> (attr : obj -> Attribute True f a) -> JSIO a
103 | getDef o g = case g o of
105 | OptionalAttr gt _ def => map (fromOptional def) gt
112 | overDef : (a -> b) -> (attr : Attribute True f a) -> JSIO b
113 | overDef f a = f <$> getDef () (const a)
117 | toDef : (attr : obj -> Attribute True f a) -> (o : obj) -> JSIO a
118 | toDef = flip getDef
126 | set : Attribute b f a -> a -> JSIO ()
127 | set (Attr _ s) y = s y
128 | set (NullableAttr _ s) y = s (Just y)
129 | set (OptionalAttr _ s _) y = s (Def y)
130 | set (OptionalAttrNoDefault _ s) y = s (Def y)
137 | mod : Attribute b f a -> (a -> a) -> JSIO ()
138 | mod (Attr g s) f = g >>= s . f
139 | mod (NullableAttr g s) f = g >>= s . map f
140 | mod (OptionalAttr g s _) f = g >>= s . map f
141 | mod (OptionalAttrNoDefault g s) f = g >>= s . map f
145 | unset : Alternative f => (o : obj) -> (obj -> Attribute b f a) -> JSIO ()
146 | unset o g = set' (g o) empty
148 | export infix 1 .=
, =.
, %=
, =%
156 | (.=) : Attribute b f a -> a -> JSIO ()
169 | (%=) : Attribute b f a -> (a -> a) -> JSIO ()
182 | (=.) : (obj -> Attribute b f a) -> a -> obj -> JSIO ()
183 | (=.) f v o = set (f o) v
195 | (=%) : (obj -> Attribute b f a) -> (a -> a) -> obj -> JSIO ()
196 | (=%) f g o = mod (f o) g
198 | export infixr 0 !>
, ?>
206 | (!>) : Callback a fun => Attribute b f a -> fun -> JSIO ()
207 | a !> cb = callback cb >>= set a
216 | (?>) : Callback a (x -> y) => Attribute b f a -> y -> JSIO ()
217 | a ?> v = a !> const v
225 | {auto _ : ToFFI a b}
226 | -> {auto _ : FromFFI a b}
228 | -> (obj -> PrimIO b)
229 | -> (obj -> b -> PrimIO ())
231 | -> Attribute True Prelude.id a
232 | fromPrim msg g s o =
233 | Attr (tryJS msg $
g o) (\a => primJS $
s o (toFFI a))
237 | {auto _ : ToFFI a b}
238 | -> {auto _ : FromFFI a b}
240 | -> (obj -> PrimIO $
Nullable b)
241 | -> (obj -> Nullable b -> PrimIO ())
243 | -> Attribute False Maybe a
244 | fromNullablePrim msg g s o =
245 | NullableAttr (tryJS msg $
g o) (\a => primJS $
s o (toFFI a))
249 | {auto _ : ToFFI a b}
250 | -> {auto _ : FromFFI a b}
252 | -> (obj -> PrimIO $
UndefOr b)
253 | -> (obj -> UndefOr b -> PrimIO ())
256 | -> Attribute True Optional a
257 | fromUndefOrPrim msg g s def o =
258 | OptionalAttr (tryJS msg $
g o) (\a => primJS $
s o (toFFI a)) def
261 | fromUndefOrPrimNoDefault :
262 | {auto _ : ToFFI a b}
263 | -> {auto _ : FromFFI a b}
265 | -> (obj -> PrimIO $
UndefOr b)
266 | -> (obj -> UndefOr b -> PrimIO ())
268 | -> Attribute False Optional a
269 | fromUndefOrPrimNoDefault msg g s o =
270 | OptionalAttrNoDefault (tryJS msg $
g o) (\a => primJS $
s o (toFFI a))