mirror of
https://github.com/ilyakooo0/purescript-indexedDB.git
synced 2024-09-11 06:55:48 +03:00
implement binding for IDBCursor
NOTE: source needs some testing about how to determine whether a source is a IDBObjectStore or a IDBIndex
This commit is contained in:
parent
5454bfa567
commit
85e6e996fd
74
src/Database/IndexedDB/IDBCursor.js
Normal file
74
src/Database/IndexedDB/IDBCursor.js
Normal file
@ -0,0 +1,74 @@
|
||||
const $Core = require('Database.IndexedDB.Core/foreign');
|
||||
|
||||
const errorHandler = $Core.errorHandler;
|
||||
const successHandler = $Core.successHandler;
|
||||
|
||||
|
||||
exports._advance = function _advance(cursor, count) {
|
||||
return function eff() {
|
||||
try {
|
||||
cursor.advance(count);
|
||||
} catch (e) {
|
||||
throw new Error(e.name);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
exports._continue = function _continue(cursor, key) {
|
||||
return function eff() {
|
||||
try {
|
||||
cursor.continue(key || undefined);
|
||||
} catch (e) {
|
||||
throw new Error(e.name);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
exports._continuePrimaryKey = function _continuePrimaryKey(cursor, key, primaryKey) {
|
||||
return function eff() {
|
||||
try {
|
||||
cursor.continuePrimaryKey(key, primaryKey);
|
||||
} catch (e) {
|
||||
throw new Error(e.name);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
exports._delete = function _delete(cursor) {
|
||||
return function aff(success, error) {
|
||||
const request = cursor.delete();
|
||||
request.onsuccess = successHandler(success);
|
||||
request.onerror = errorHandler(error);
|
||||
};
|
||||
};
|
||||
|
||||
exports._direction = function _direction(fromString, cursor) {
|
||||
return fromString(cursor.direction);
|
||||
};
|
||||
|
||||
exports._key = function _key(cursor) {
|
||||
return cursor.key;
|
||||
};
|
||||
|
||||
exports._primaryKey = function _primaryKey(cursor) {
|
||||
return cursor.primaryKey;
|
||||
};
|
||||
|
||||
exports._source = function _source(IDBObjectStore, IDBIndex, cursor) {
|
||||
console.log(typeof cursor.source);
|
||||
console.log(Object.getPrototypeOf(cursor.source));
|
||||
console.log(cursor.source);
|
||||
throw new Error('TODO');
|
||||
};
|
||||
|
||||
exports._update = function _update(cursor, value) {
|
||||
return function aff(success, error) {
|
||||
const request = cursor.update(value);
|
||||
request.onsuccess = successHandler(success);
|
||||
request.onerror = errorHandler(error);
|
||||
};
|
||||
};
|
||||
|
||||
exports._value = function _value(cursor) {
|
||||
return cursor.value;
|
||||
};
|
92
src/Database/IndexedDB/IDBCursor.purs
Normal file
92
src/Database/IndexedDB/IDBCursor.purs
Normal file
@ -0,0 +1,92 @@
|
||||
module Database.IndexedDB.IDBCursor
|
||||
( class IDBCursor, advance, continue, continuePrimaryKey, delete, direction, key, primaryKey, source, update
|
||||
, class IDBCursorWithValue, value
|
||||
) where
|
||||
|
||||
import Prelude (Unit, (>>>))
|
||||
|
||||
import Control.Monad.Aff (Aff)
|
||||
import Control.Monad.Eff (Eff)
|
||||
import Control.Monad.Eff.Exception (EXCEPTION)
|
||||
import Data.Function.Uncurried as Fn
|
||||
import Data.Function.Uncurried (Fn2, Fn3)
|
||||
import Data.Maybe (Maybe)
|
||||
import Data.Nullable (Nullable, toNullable)
|
||||
import Data.Foreign (Foreign, toForeign, unsafeFromForeign)
|
||||
|
||||
import Database.IndexedDB.Core
|
||||
import Database.IndexedDB.IDBCursorDirection as IDBCursorDirection
|
||||
|
||||
|
||||
class IDBCursor cursor where
|
||||
advance :: forall eff. cursor -> Int -> Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) Unit
|
||||
continue :: forall eff. cursor -> Maybe IDBKey -> Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) Unit
|
||||
continuePrimaryKey :: forall eff. cursor -> IDBKey -> IDBKey -> Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) Unit
|
||||
delete :: forall eff. cursor -> Aff (idb ::INDEXED_DB, exception :: EXCEPTION | eff) Unit
|
||||
direction :: cursor -> IDBCursorDirection
|
||||
key :: cursor -> IDBKey
|
||||
primaryKey :: cursor -> IDBKey
|
||||
source :: cursor -> IDBCursorSource
|
||||
update :: forall val eff. cursor -> val -> Aff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) IDBKey
|
||||
|
||||
|
||||
class IDBCursor cursor <= IDBCursorWithValue cursor where
|
||||
value :: forall val. cursor -> val
|
||||
|
||||
|
||||
instance keyCursorKeyCursor :: IDBCursor IDBKeyCursor where
|
||||
advance = Fn.runFn2 _advance
|
||||
continue c mk = Fn.runFn2 _continue c (toNullable mk)
|
||||
continuePrimaryKey = Fn.runFn3 _continuePrimaryKey
|
||||
delete = _delete
|
||||
direction = Fn.runFn2 _direction (IDBCursorDirection.fromString >>> toNullable)
|
||||
key = _key
|
||||
primaryKey = _primaryKey
|
||||
source = Fn.runFn3 _source IDBObjectStore IDBIndex
|
||||
update c = toForeign >>> Fn.runFn2 _update c
|
||||
|
||||
|
||||
instance valueCursorKeyCursor :: IDBCursor IDBValueCursor where
|
||||
advance = Fn.runFn2 _advance
|
||||
continue c mk = Fn.runFn2 _continue c (toNullable mk)
|
||||
continuePrimaryKey = Fn.runFn3 _continuePrimaryKey
|
||||
delete = _delete
|
||||
direction = Fn.runFn2 _direction (IDBCursorDirection.fromString >>> toNullable)
|
||||
key = _key
|
||||
primaryKey = _primaryKey
|
||||
source = Fn.runFn3 _source IDBObjectStore IDBIndex
|
||||
update c = toForeign >>> Fn.runFn2 _update c
|
||||
|
||||
|
||||
instance valueCursorWithValueCursor :: IDBCursorWithValue IDBValueCursor where
|
||||
value = _value >>> unsafeFromForeign
|
||||
|
||||
|
||||
foreign import _advance :: forall cursor eff. Fn2 cursor Int (Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) Unit)
|
||||
|
||||
|
||||
foreign import _continue :: forall cursor eff. Fn2 cursor (Nullable IDBKey) (Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) Unit)
|
||||
|
||||
|
||||
foreign import _continuePrimaryKey :: forall cursor eff. Fn3 cursor IDBKey IDBKey (Eff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) Unit)
|
||||
|
||||
|
||||
foreign import _delete :: forall cursor eff. cursor -> (Aff (idb ::INDEXED_DB, exception :: EXCEPTION | eff) Unit)
|
||||
|
||||
|
||||
foreign import _direction :: forall cursor. Fn2 (String -> Nullable IDBCursorDirection) cursor IDBCursorDirection
|
||||
|
||||
|
||||
foreign import _key :: forall cursor. cursor -> IDBKey
|
||||
|
||||
|
||||
foreign import _primaryKey :: forall cursor. cursor -> IDBKey
|
||||
|
||||
|
||||
foreign import _source :: forall cursor. Fn3 (IDBObjectStore -> IDBCursorSource) (IDBIndex -> IDBCursorSource) cursor IDBCursorSource
|
||||
|
||||
|
||||
foreign import _update :: forall cursor eff. Fn2 cursor Foreign (Aff (idb :: INDEXED_DB, exception :: EXCEPTION | eff) IDBKey)
|
||||
|
||||
|
||||
foreign import _value :: forall cursor val. cursor -> val
|
Loading…
Reference in New Issue
Block a user