|
| 1 | +module Angular.Cache |
| 2 | + ( Cache(..) |
| 3 | + , CacheFactory(..) |
| 4 | + , CACHE(..) |
| 5 | + , Key(..) |
| 6 | + , Name(..) |
| 7 | + , Options(..) |
| 8 | + , injCacheFactory |
| 9 | + , cache |
| 10 | + , put |
| 11 | + , get |
| 12 | + , remove |
| 13 | + , removeAll |
| 14 | + , destroy |
| 15 | + , info |
| 16 | + ) where |
| 17 | + |
| 18 | +import Control.Monad.Eff |
| 19 | +import Data.Maybe |
| 20 | + |
| 21 | +import Angular.Injector (InjectDependency(..)) |
| 22 | + |
| 23 | +foreign import data CACHE :: ! |
| 24 | + |
| 25 | +foreign import data Cache :: * |
| 26 | + |
| 27 | +foreign import data CacheFactory :: * |
| 28 | + |
| 29 | +type Key = String |
| 30 | + |
| 31 | +type Name = String |
| 32 | + |
| 33 | +type Options a = { capacity :: Number | a } |
| 34 | + |
| 35 | +foreign import injCacheFactory |
| 36 | + " function injCacheFactory(){ \ |
| 37 | + \ var $injector = angular.element(document).injector(); \ |
| 38 | + \ return $injector.get('$cacheFactory'); \ |
| 39 | + \ } " |
| 40 | + :: forall e. Eff (nginj :: InjectDependency | e) CacheFactory |
| 41 | + |
| 42 | +foreign import cache |
| 43 | + " function cache(name){ \ |
| 44 | + \ return function(opts){ \ |
| 45 | + \ return function($cacheFactory){ \ |
| 46 | + \ return function(){ \ |
| 47 | + \ return $cacheFactory(name, opts.values[0]); \ |
| 48 | + \ }; \ |
| 49 | + \ }; \ |
| 50 | + \ }; \ |
| 51 | + \ } " |
| 52 | + :: forall e a. Name -> Maybe (Options a) -> CacheFactory -> Eff (ngcache :: CACHE | e) Cache |
| 53 | + |
| 54 | +foreign import put |
| 55 | + " function put(key){ \ |
| 56 | + \ return function(value){ \ |
| 57 | + \ return function(cache){ \ |
| 58 | + \ return function(){ \ |
| 59 | + \ return cache.put(key, value); \ |
| 60 | + \ }; \ |
| 61 | + \ }; \ |
| 62 | + \ }; \ |
| 63 | + \ } " |
| 64 | + :: forall e a. Key -> a -> Cache -> Eff (ngcache :: CACHE | e) a |
| 65 | + |
| 66 | +foreign import get |
| 67 | + " function get(key){ \ |
| 68 | + \ return function(cache){ \ |
| 69 | + \ return function(){ \ |
| 70 | + \ return cache.get(key); \ |
| 71 | + \ }; \ |
| 72 | + \ }; \ |
| 73 | + \ } " |
| 74 | + :: forall e a. Key -> Cache -> Eff (ngcache :: CACHE | e) a |
| 75 | + |
| 76 | +foreign import remove |
| 77 | + " function remove(key){ \ |
| 78 | + \ return function(cache){ \ |
| 79 | + \ return function(){ \ |
| 80 | + \ return cache.remove(key); \ |
| 81 | + \ }; \ |
| 82 | + \ }; \ |
| 83 | + \ } " |
| 84 | + :: forall e a. Key -> Cache -> Eff (ngcache :: CACHE | e) Unit |
| 85 | + |
| 86 | +foreign import removeAll |
| 87 | + " function removeAll(cache){ \ |
| 88 | + \ return function(){ \ |
| 89 | + \ return cache.removeAll(); \ |
| 90 | + \ }; \ |
| 91 | + \ } " |
| 92 | + :: forall e. Cache -> Eff (ngcache :: CACHE | e) Unit |
| 93 | + |
| 94 | +foreign import destroy |
| 95 | + " function destroy(cache){ \ |
| 96 | + \ return function(){ \ |
| 97 | + \ return cache.destroy(); \ |
| 98 | + \ }; \ |
| 99 | + \ } " |
| 100 | + :: forall e. Cache -> Eff (ngcache :: CACHE | e) Unit |
| 101 | + |
| 102 | +foreign import info |
| 103 | + " function info(cache){ \ |
| 104 | + \ return function(){ \ |
| 105 | + \ return cache.info(); \ |
| 106 | + \ }; \ |
| 107 | + \ } " |
| 108 | + :: forall e a. Cache -> Eff (ngcache :: CACHE | e) { id :: String, size :: Number | a } |
0 commit comments