Commit 9d501e76 authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[cacheAPI] add CacheAPI support

parent 680db391
......@@ -4,6 +4,7 @@
"source": ".psc-package/local/.set/packages.json",
"depends": [
"affjax",
"aff-promise",
"argonaut",
"console",
"css",
......@@ -20,6 +21,7 @@
"markdown-smolder",
"math",
"maybe",
"milkis",
"nonempty",
"numbers",
"prelude",
......
......@@ -12,6 +12,16 @@
};
};
"aff-promise" = pkgs.stdenv.mkDerivation {
name = "aff-promise";
version = "v2.1.0";
fetched = pkgs.fetchgit {
url = "https://github.com/nwolverson/purescript-aff-promise.git";
rev = "033d6b90252e0390b0de7845e21de919bc4c3a0e";
sha256 = "0khm53lvxgvc7fbsvcr2h2wlhcgay8vq45755f0w8vpk1441dvww";
};
};
"affjax" = pkgs.stdenv.mkDerivation {
name = "affjax";
version = "v9.0.0";
......@@ -572,6 +582,16 @@
};
};
"milkis" = pkgs.stdenv.mkDerivation {
name = "milkis";
version = "v7.2.0";
fetched = pkgs.fetchgit {
url = "https://github.com/justinwoo/purescript-milkis.git";
rev = "6a55398de664595406e0b5fe7cd3646a4501f1ed";
sha256 = "1i6l8k4clrnq4wxbdzzq08kagk591yr24xip86z1j7xap98y02xg";
};
};
"mmorph" = pkgs.stdenv.mkDerivation {
name = "mmorph";
version = "v5.1.0";
......
......@@ -18,6 +18,9 @@ type MetricsLoadViewProps a = (
| MetricsProps
)
cacheName :: String
cacheName = "metrics"
metricsLoadView :: forall a. Record (MetricsLoadViewProps a) -> R.Element
metricsLoadView p = R.createElement metricsLoadViewCpt p []
......
......@@ -4,7 +4,7 @@ import Affjax (defaultRequest, printResponseFormatError, request)
import Affjax.RequestBody (RequestBody(..), formData, formURLEncoded)
import Affjax.RequestHeader as ARH
import Affjax.ResponseFormat as ResponseFormat
import DOM.Simple.Console (log)
import DOM.Simple.Console (log, log2)
import Data.Argonaut (class DecodeJson, decodeJson, class EncodeJson, encodeJson)
import Data.Either (Either(..))
import Data.Foldable (foldMap)
......@@ -12,13 +12,18 @@ import Data.FormURLEncoded as FormURLEncoded
import Data.HTTP.Method (Method(..))
import Data.Maybe (Maybe(..))
import Data.MediaType.Common (applicationFormURLEncoded, applicationJSON, multipartFormData)
import Data.Tuple (Tuple)
import Data.Tuple (Tuple(..))
import DOM.Simple.Console (log2)
import Effect.Aff (Aff, throwError)
import Effect.Class (liftEffect)
import Effect.Exception (error)
import Milkis as Milkis
import Unsafe.Coerce (unsafeCoerce)
import Web.XHR.FormData as XHRFormData
import Gargantext.Prelude
import Gargantext.Utils.CacheAPI as GUC
import Gargantext.Utils.Reactix as R2
import Web.XHR.FormData as XHRFormData
type Token = String
......@@ -26,7 +31,7 @@ type Token = String
send :: forall a b. EncodeJson a => DecodeJson b =>
Method -> Maybe Token -> String -> Maybe a -> Aff b
send m mtoken url reqbody = do
affResp <- request $ defaultRequest
let req = defaultRequest
{ url = url
, responseFormat = ResponseFormat.json
, method = Left m
......@@ -38,6 +43,16 @@ send m mtoken url reqbody = do
) mtoken
, content = (Json <<< encodeJson) <$> reqbody
}
cache <- GUC.openCache $ GUC.CacheName "test"
let method = unsafeCoerce (show m) :: Milkis.Method
let options = { method, headers: Milkis.makeHeaders {"content-type": "application/json"} }
let req' = GUC.makeRequest (Milkis.URL url) options
res <- GUC.cached cache req'
liftEffect $ log2 "[send] cache res" res
liftEffect $ log2 "[send] res json" $ Milkis.json res
affResp <- request req
case mtoken of
Nothing -> pure unit
Just token -> liftEffect $ do
......
module Gargantext.Hooks.Loader where
import Gargantext.Prelude
import Data.Argonaut (class DecodeJson, class EncodeJson, decodeJson, encodeJson, (.:), (:=), (~>), jsonEmptyObject)
import Data.Argonaut.Core (stringify)
import Data.Argonaut.Parser (jsonParser)
......@@ -14,6 +13,8 @@ import Effect.Class (liftEffect)
import Reactix as R
import Web.Storage.Storage as WSS
import Gargantext.Prelude
import Gargantext.Components.LoadingSpinner (loadingSpinner)
import Gargantext.Utils as GU
import Gargantext.Utils.Reactix as R2
......
exports._makeRequest = function() {
return function(url) {
return function(options) {
console.log('[_makeRequest] url', url);
console.log('[_makeRequest] options', options);
return new Request(url, options);
}
}
}
exports._openCache = function(cacheName) {
return function() {
return caches.open(cacheName);
}
}
exports._cached = function(cache) {
return function(req) {
return function(onError, onSuccess) {
cache.match(req).then(function(res) {
if (res) {
console.log('[_getC] cache hit with', req);
onSuccess(res)
} else {
cache.add(req).then(function(res) {
console.log('[_getC] cache miss with', req);
onSuccess(res);
}, function(err) {
onError(err);
})
}
}, function(err) {
onError(err);
})
return function(cancelError, onCancelerError, onCancelerSuccess) {
onCancelerSuccess();
}
}
}
}
exports._delete = function(cache) {
return function(req) {
return function() {
cache.delete(req);
}
}
}
module Gargantext.Utils.CacheAPI where
import Control.Promise (Promise, toAffE)
import Data.Tuple (Tuple(..))
import Effect (Effect)
import Effect.Aff (Aff)
import Effect.Aff.Compat (EffectFnAff, fromEffectFnAff)
import Milkis as M
import Type.Row (class Union)
import Gargantext.Prelude
foreign import data Cache :: Type
foreign import data Request :: Type
newtype CacheName = CacheName String
makeRequest :: forall options trash. Union options trash M.Options =>
M.URL -> { method :: M.Method | options } -> Request
makeRequest url options = _makeRequest url options
openCache :: CacheName -> Aff Cache
openCache (CacheName cacheName) = toAffE $ _openCache cacheName
cached :: Cache -> Request -> Aff M.Response
cached cache req = fromEffectFnAff $ _cached cache req
delete :: Cache -> Request -> Aff Unit
delete cache req = toAffE $ _delete cache req
foreign import _makeRequest :: forall options trash. Union options trash M.Options =>
M.URL -> { method :: M.Method | options } -> Request
foreign import _openCache :: String -> Effect (Promise Cache)
foreign import _cached :: Cache -> Request -> EffectFnAff M.Response
foreign import _delete :: Cache -> Request -> Effect (Promise Unit)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment