move success callback as a result of the Aff computation

This commit is contained in:
KtorZ 2017-06-22 14:56:43 +02:00
parent 57a377b1ef
commit 25ecf277d2
No known key found for this signature in database
GPG Key ID: 3F72E8BC2894C015
6 changed files with 21 additions and 12 deletions

View File

@ -12,7 +12,13 @@ exports.noOp2 = function noOp2() {
exports.errorHandler = function errorHandler(cb) {
return function _handler(e) {
cb(new Error(e.errorCode));
cb(new Error(e.target.error.name));
};
};
exports.successHandler = function successHandler(cb) {
return function _handler(e) {
cb(e.target.result);
};
};

View File

@ -14,6 +14,5 @@ foreign import data IDBDatabase :: Type
type IDBOpenRequest eff =
{ onBlocked :: Maybe (IDBDatabase -> Eff eff Unit)
, onSuccess :: Maybe (IDBDatabase -> Eff eff Unit)
, onUpgradeNeeded :: Maybe (IDBDatabase -> Eff eff Unit)
}

View File

@ -3,6 +3,7 @@ const Core = require('Core/foreign');
const noOp2 = Core.noOp2;
const errorHandler = Core.errorHandler;
const successHandler = Core.successHandler;
const eventHandler = Core.eventHandler;
exports._open = function _open(name, mver, req) {
@ -12,8 +13,9 @@ exports._open = function _open(name, mver, req) {
const request = indexedDB.open(name, ver);
request.onerror = errorHandler(error);
request.onsuccess = successHandler(success);
request.onblocked = eventHandler(Maybe.fromMaybe(noOp2)(req.onBlocked));
request.onsuccess = eventHandler(Maybe.fromMaybe(noOp2)(req.onSuccess));
request.onupgradeneeded = eventHandler(Maybe.fromMaybe(noOp2)(req.onUpgradeNeeded));
};
};

View File

@ -17,9 +17,9 @@ foreign import _open
String
(Maybe Int)
(IDBOpenRequest eff)
(Aff (idb :: INDEXED_DB | eff) Unit)
(Aff (idb :: INDEXED_DB | eff) IDBDatabase)
open :: forall eff. String -> Maybe Int -> (IDBOpenRequest eff) -> Aff (idb :: INDEXED_DB | eff) Unit
open :: forall eff. String -> Maybe Int -> (IDBOpenRequest eff) -> Aff (idb :: INDEXED_DB | eff) IDBDatabase
open name mver req =
Fn.runFn3 _open name mver req

View File

@ -13,13 +13,14 @@ const noOp2 = function noOp2() {
const errorHandler = function errorHandler(cb) {
return function _handler(e) {
return function _autruche(e) {
console.log("ERROR");
cb(new Error(e.errorCode));
};
};
const eventHandler = function eventHandler(cb) {
return function _handler(e) {
return function _patate(e) {
cb(e.target.result)();
};
};

View File

@ -6,8 +6,9 @@ import Control.Monad.Aff as Aff
import Control.Monad.Eff(kind Effect, Eff)
import Control.Monad.Eff.Console as Console
import Control.Monad.Eff.Console(CONSOLE)
import Control.Monad.Eff.Exception(EXCEPTION)
import Data.Maybe(Maybe(..), fromMaybe)
import Control.Monad.Eff.Exception(EXCEPTION, catchException)
import Data.Maybe(Maybe(..))
import Data.Either(Either(..))
import Core
import IDBDatabase as IDBDatabase
@ -15,9 +16,9 @@ import IDBDatabase as IDBDatabase
main :: Eff (exception :: EXCEPTION, idb :: INDEXED_DB, console :: CONSOLE) Unit
main = do
_ <- Aff.launchAff $ IDBDatabase.open "myDatabase" Nothing
{ onSuccess : Just (IDBDatabase.name >>> Console.log)
, onBlocked : Nothing
_ <- Aff.runAff (show >>> Console.log) (IDBDatabase.name >>> Console.log) $ IDBDatabase.open "myDatabase" Nothing
{ onBlocked : Nothing
, onUpgradeNeeded : Nothing
}
pure unit