Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bring code up to speed #9

Merged
merged 19 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
example/bundle.js
12 changes: 10 additions & 2 deletions example/hello.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
var random = require('../')('dbname')
var cool = random('cool.txt')
var RAI = require('../')
const storage = RAI('dbname')
var cool = storage('cool.txt')

cool.write(100, Buffer.from('GREETINGS'), function (err) {
if (err) return console.error(err)
cool.read(104, 3, function (err, buf) {
if (err) return console.error(err)
console.log(buf.toString()) // TIN
})

cool.read(100, 9, function (err, buf) {
if (err) return console.error(err)
console.log(buf.toString()) // GREETINGS
})

})
10 changes: 10 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
</head>
<body>
Example: Open dev console to see output<br />
<script src="bundle.js"></script>
</body>
</html>
38 changes: 28 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Abstract = require('random-access-storage')
var RandomAccess = require('random-access-storage')
var inherits = require('inherits')
var nextTick = require('next-tick')
var once = require('once')
Expand All @@ -10,11 +10,13 @@ var DELIM = '\0'

module.exports = function (dbname, xopts) {
if (!xopts) xopts = {}
var idb = xopts.idb || (typeof window !== 'undefined'
? window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB
: !indexedDB
? null
: indexedDB)

var win = typeof window !== 'undefined' ? window
: (typeof self !== 'undefined' ? self : {})

var idb = xopts.idb || (typeof win !== 'undefined'
? win.indexedDB || win.mozIndexedDB || win.webkitIndexedDB || win.msIndexedDB
: null)
if (!idb) throw new Error('indexedDB not present and not given')
var db = null
var dbqueue = []
Expand Down Expand Up @@ -51,15 +53,15 @@ module.exports = function (dbname, xopts) {

function Store (opts) {
if (!(this instanceof Store)) return new Store(opts)
Abstract.call(this)
RandomAccess.call(this)
if (!opts) opts = {}
if (typeof opts === 'string') opts = { name: opts }
this.size = opts.size || 4096
this.name = opts.name
this.length = opts.length || 0
this._getdb = opts.db
}
inherits(Store, Abstract)
inherits(Store, RandomAccess)

Store.prototype._blocks = function (i, j) {
return blocks(this.size, i, j)
Expand Down Expand Up @@ -116,7 +118,7 @@ Store.prototype._write = function (req) {
var o = offsets[i]
var len = o.end - o.start
if (len === self.size) {
block = req.data.slice(j, j + len)
block = bufferFrom(req.data.slice(j, j + len))
} else {
block = buffers[i]
req.data.copy(block, o.start, j, j + len)
Expand All @@ -131,7 +133,9 @@ Store.prototype._write = function (req) {
self.length = length
req.callback(null)
})
store.transaction.addEventListener('error', req.callback)
store.transaction.addEventListener('error', function (err) {
req.callback(err)
})
}
}

Expand Down Expand Up @@ -160,6 +164,20 @@ Store.prototype._open = function (req) {
})
}

Store.prototype._close = function (req) {
this._getdb(function (db) {
//db.close() // TODO: reopen gracefully. Close breaks with corestore, as innercorestore closes the db
req.callback()
})
}

Store.prototype._stat = function (req) {
var self = this
nextTick(function () {
req.callback(null, { size: self.length })
})
}

function backify (r, cb) {
r.addEventListener('success', function (ev) { cb(null, ev) })
r.addEventListener('error', cb)
Expand Down
Loading