mirror of
https://github.com/ilyakooo0/purescript-indexedDB.git
synced 2024-09-11 06:55:48 +03:00
get hands around FFI in PureScript -> open function done
This commit is contained in:
parent
2c8601441e
commit
57a377b1ef
23
src/Core.js
Normal file
23
src/Core.js
Normal file
@ -0,0 +1,23 @@
|
||||
exports.noOp = function noOp() {
|
||||
return function eff() {
|
||||
// Nothing
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
exports.noOp2 = function noOp2() {
|
||||
return exports.noOp;
|
||||
};
|
||||
|
||||
|
||||
exports.errorHandler = function errorHandler(cb) {
|
||||
return function _handler(e) {
|
||||
cb(new Error(e.errorCode));
|
||||
};
|
||||
};
|
||||
|
||||
exports.eventHandler = function eventHandler(cb) {
|
||||
return function _handler(e) {
|
||||
cb(e.target.result)();
|
||||
};
|
||||
};
|
19
src/Core.purs
Normal file
19
src/Core.purs
Normal file
@ -0,0 +1,19 @@
|
||||
module Core where
|
||||
|
||||
import Prelude
|
||||
import Data.Maybe(Maybe)
|
||||
import Control.Monad.Aff(Aff)
|
||||
import Control.Monad.Eff(kind Effect, Eff)
|
||||
|
||||
|
||||
foreign import data INDEXED_DB :: Effect
|
||||
|
||||
|
||||
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)
|
||||
}
|
27
src/IDBDatabase.js
Normal file
27
src/IDBDatabase.js
Normal file
@ -0,0 +1,27 @@
|
||||
const Maybe = require('Data.Maybe');
|
||||
const Core = require('Core/foreign');
|
||||
|
||||
const noOp2 = Core.noOp2;
|
||||
const errorHandler = Core.errorHandler;
|
||||
const eventHandler = Core.eventHandler;
|
||||
|
||||
exports._open = function _open(name, mver, req) {
|
||||
const ver = Maybe.fromMaybe(undefined)(mver);
|
||||
|
||||
return function callback(success, error) {
|
||||
const request = indexedDB.open(name, ver);
|
||||
|
||||
request.onerror = errorHandler(error);
|
||||
request.onblocked = eventHandler(Maybe.fromMaybe(noOp2)(req.onBlocked));
|
||||
request.onsuccess = eventHandler(Maybe.fromMaybe(noOp2)(req.onSuccess));
|
||||
request.onupgradeneeded = eventHandler(Maybe.fromMaybe(noOp2)(req.onUpgradeNeeded));
|
||||
};
|
||||
};
|
||||
|
||||
exports.name = function name(db) {
|
||||
return db.name;
|
||||
};
|
||||
|
||||
exports.version = function version(db) {
|
||||
return db.version;
|
||||
};
|
30
src/IDBDatabase.purs
Normal file
30
src/IDBDatabase.purs
Normal file
@ -0,0 +1,30 @@
|
||||
module IDBDatabase where
|
||||
|
||||
import Prelude
|
||||
|
||||
import Data.Maybe(Maybe)
|
||||
import Control.Monad.Eff.Console as Console
|
||||
import Control.Monad.Aff(Aff)
|
||||
import Control.Monad.Eff(kind Effect, Eff)
|
||||
import Data.Function.Uncurried as Fn
|
||||
import Data.Function.Uncurried(Fn3)
|
||||
|
||||
import Core
|
||||
|
||||
|
||||
foreign import _open
|
||||
:: forall eff. Fn3
|
||||
String
|
||||
(Maybe Int)
|
||||
(IDBOpenRequest eff)
|
||||
(Aff (idb :: INDEXED_DB | eff) Unit)
|
||||
|
||||
open :: forall eff. String -> Maybe Int -> (IDBOpenRequest eff) -> Aff (idb :: INDEXED_DB | eff) Unit
|
||||
open name mver req =
|
||||
Fn.runFn3 _open name mver req
|
||||
|
||||
|
||||
foreign import name :: IDBDatabase -> String
|
||||
|
||||
|
||||
foreign import version :: IDBDatabase -> Int
|
43
src/Main.js
Normal file
43
src/Main.js
Normal file
@ -0,0 +1,43 @@
|
||||
const Maybe = require('Data.Maybe');
|
||||
|
||||
const noOp = function noOp() {
|
||||
return function eff() {
|
||||
// Nothing
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
const noOp2 = function noOp2() {
|
||||
return noOp;
|
||||
};
|
||||
|
||||
|
||||
const errorHandler = function errorHandler(cb) {
|
||||
return function _handler(e) {
|
||||
cb(new Error(e.errorCode));
|
||||
};
|
||||
};
|
||||
|
||||
const eventHandler = function eventHandler(cb) {
|
||||
return function _handler(e) {
|
||||
cb(e.target.result)();
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
exports._open = function _open(name, mver, req) {
|
||||
const ver = Maybe.fromMaybe(undefined)(mver);
|
||||
|
||||
return function callback(success, error) {
|
||||
const request = indexedDB.open('library', ver);
|
||||
|
||||
request.onerror = errorHandler(error);
|
||||
request.onblocked = eventHandler(Maybe.fromMaybe(noOp2)(req.onBlocked));
|
||||
request.onsuccess = eventHandler(Maybe.fromMaybe(noOp2)(req.onSuccess));
|
||||
request.onupgradeneeded = eventHandler(Maybe.fromMaybe(noOp2)(req.onUpgradeNeeded));
|
||||
};
|
||||
};
|
||||
|
||||
exports.idbDatabaseName = function idbDatabaseName(db) {
|
||||
return db.name;
|
||||
};
|
23
src/Main.purs
Normal file
23
src/Main.purs
Normal file
@ -0,0 +1,23 @@
|
||||
module Main where
|
||||
|
||||
import Prelude
|
||||
|
||||
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 Core
|
||||
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
|
||||
, onUpgradeNeeded : Nothing
|
||||
}
|
||||
pure unit
|
Loading…
Reference in New Issue
Block a user