diff options
Diffstat (limited to 'src/LoaderRef.purs')
-rw-r--r-- | src/LoaderRef.purs | 75 |
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 @@ | |||
1 | module PursLoader.LoaderRef | ||
2 | ( LoaderRef() | ||
3 | , Loader() | ||
4 | , async | ||
5 | , cacheable | ||
6 | , clearDependencies | ||
7 | , resourcePath | ||
8 | , addDependency | ||
9 | , query | ||
10 | ) where | ||
11 | |||
12 | import Control.Monad.Eff (Eff()) | ||
13 | import Control.Monad.Eff.Exception (Error()) | ||
14 | |||
15 | import Data.Foreign (Foreign()) | ||
16 | import Data.Function (Fn3(), runFn3) | ||
17 | import Data.Maybe (Maybe(), fromMaybe, isJust) | ||
18 | |||
19 | data LoaderRef | ||
20 | |||
21 | foreign import data Loader :: ! | ||
22 | |||
23 | foreign import asyncFn """ | ||
24 | function 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 | |||
41 | async :: forall eff a. LoaderRef -> Eff (loader :: Loader | eff) (Maybe Error -> a -> Eff (loader :: Loader | eff) Unit) | ||
42 | async ref = runFn3 asyncFn isJust fromMaybe ref | ||
43 | |||
44 | foreign import cacheable """ | ||
45 | function cacheable(ref){ | ||
46 | return function(){ | ||
47 | return ref.cacheable && ref.cacheable(); | ||
48 | }; | ||
49 | }""" :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) Unit | ||
50 | |||
51 | foreign import clearDependencies """ | ||
52 | function clearDependencies(ref){ | ||
53 | return function(){ | ||
54 | return ref.clearDependencies(); | ||
55 | }; | ||
56 | }""" :: forall eff. LoaderRef -> Eff (loader :: Loader | eff) Unit | ||
57 | |||
58 | foreign import resourcePath """ | ||
59 | function resourcePath(ref){ | ||
60 | return ref.resourcePath; | ||
61 | }""" :: LoaderRef -> String | ||
62 | |||
63 | foreign import addDependency """ | ||
64 | function 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 | |||
72 | foreign import query """ | ||
73 | function query(ref){ | ||
74 | return ref.query; | ||
75 | }""" :: LoaderRef -> String | ||