aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/LoaderRef.purs
blob: 2567b1e77e0749a4a665a90318bb0b8de0a4f2ff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module PursLoader.LoaderRef
  ( LoaderRef()
  , Loader()
  , async
  , cacheable
  , query
  ) where

import Control.Monad.Eff (Eff())
import Control.Monad.Eff.Exception (Error())

import Data.Function (Fn3(), runFn3)
import Data.Maybe (Maybe(), fromMaybe, isJust)

data LoaderRef

foreign import data Loader :: !

foreign import asyncFn """
function asyncFn(isJust, fromMaybe, ref){
  return function(){
    var callback = ref.async();
    return function(error){
      return function(value){
        return function(){
          return isJust(error) ? callback(fromMaybe(new Error())(error))
                               : callback(null, value);
        };
      };
    };
  };
}""" :: forall eff a. Fn3 (Maybe Error -> Boolean)
                          (Error -> Maybe Error -> Error)
                          LoaderRef
                          (Eff (loader :: Loader | eff) (Maybe Error -> a -> Eff (loader :: Loader | eff) Unit))

async :: forall eff a. LoaderRef -> Eff (loader :: Loader | eff) (Maybe Error -> a -> Eff (loader :: Loader | eff) Unit)
async ref = runFn3 asyncFn isJust fromMaybe ref

foreign import cacheable """
function cacheable(ref){
  return function(){
    return ref.cacheable && ref.cacheable();
  };
}""" :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) Unit

foreign import query """
function query(ref){
  return ref.query;
}""" :: LoaderRef -> String