aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/LoaderRef.purs
diff options
context:
space:
mode:
Diffstat (limited to 'src/LoaderRef.purs')
-rw-r--r--src/LoaderRef.purs75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/LoaderRef.purs b/src/LoaderRef.purs
new file mode 100644
index 0000000..2d62754
--- /dev/null
+++ b/src/LoaderRef.purs
@@ -0,0 +1,75 @@
1module PursLoader.LoaderRef
2 ( LoaderRef()
3 , Loader()
4 , async
5 , cacheable
6 , clearDependencies
7 , resourcePath
8 , addDependency
9 , query
10 ) where
11
12import Control.Monad.Eff (Eff())
13import Control.Monad.Eff.Exception (Error())
14
15import Data.Foreign (Foreign())
16import Data.Function (Fn3(), runFn3)
17import Data.Maybe (Maybe(), fromMaybe, isJust)
18
19data LoaderRef
20
21foreign import data Loader :: !
22
23foreign import asyncFn """
24function asyncFn(isJust, fromMaybe, ref){
25 return function(){
26 var callback = ref.async();
27 return function(error){
28 return function(value){
29 return function(){
30 return isJust(error) ? callback(fromMaybe(new Error())(error))
31 : callback(null, value);
32 };
33 };
34 };
35 };
36}""" :: forall eff a. Fn3 (Maybe Error -> Boolean)
37 (Error -> Maybe Error -> Error)
38 LoaderRef
39 (Eff (loader :: Loader | eff) (Maybe Error -> a -> Eff (loader :: Loader | eff) Unit))
40
41async :: forall eff a. LoaderRef -> Eff (loader :: Loader | eff) (Maybe Error -> a -> Eff (loader :: Loader | eff) Unit)
42async ref = runFn3 asyncFn isJust fromMaybe ref
43
44foreign import cacheable """
45function cacheable(ref){
46 return function(){
47 return ref.cacheable && ref.cacheable();
48 };
49}""" :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) Unit
50
51foreign import clearDependencies """
52function clearDependencies(ref){
53 return function(){
54 return ref.clearDependencies();
55 };
56}""" :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) Unit
57
58foreign import resourcePath """
59function resourcePath(ref){
60 return ref.resourcePath;
61}""" :: LoaderRef -> String
62
63foreign import addDependency """
64function addDependency(ref){
65 return function(dep){
66 return function(){
67 return ref.addDependency(dep);
68 };
69 };
70}""" :: forall eff. LoaderRef -> String -> Eff (loader :: Loader | eff) Unit
71
72foreign import query """
73function query(ref){
74 return ref.query;
75}""" :: LoaderRef -> String