diff --git a/factory.js b/factory.js new file mode 100644 index 0000000..f0c7e39 --- /dev/null +++ b/factory.js @@ -0,0 +1,28 @@ +const { DBOpenDBRequest } = require('./request') + +function createEvent(type, bubbles, cancelable, detail) { + return { + type, + timeStamp: Date.now(), + bubbles: bubbles, + cancelable: cancelable, + detail: detail || null, + } +} + +class DBFactory { + constructor (path) { + console.log(path) + } + + open (name, version = 0) { + const request = new DBOpenDBRequest() + setImmediate(() => { + request.dispatchEvent(createEvent('upgradeneeded', false, false)) + request.dispatchEvent(createEvent('success', false, false)) + }) + return request + } +} + +exports.DBFactory = DBFactory diff --git a/index.js b/index.js index e8f7328..f248fd7 100644 --- a/index.js +++ b/index.js @@ -1 +1,5 @@ -module.exports = 1 +const path = require('path') + +const { DBFactory } = require('./factory') + +exports.indexedDB = new DBFactory(path.resolve(__dirname, './test/tmp/indexeddb')) diff --git a/package.json b/package.json index a6263cc..5f23e48 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ }, "dependencies": { + "event-target-shim": "5.0.1" }, "devDependencies": { diff --git a/request.js b/request.js new file mode 100644 index 0000000..9e21d3e --- /dev/null +++ b/request.js @@ -0,0 +1,27 @@ +const { EventTarget, defineEventAttribute } = require('event-target-shim') + +class DBRequest extends EventTarget { + constructor () { + super() + this.source = null + this.transaction = null + this.readyState = 'pending' + this.onsuccess = null + this.onerror = null + } +} + +defineEventAttribute(DBRequest.prototype, 'error') +defineEventAttribute(DBRequest.prototype, 'success') + +class DBOpenDBRequest extends DBRequest { + constructor () { + super() + } +} + +defineEventAttribute(DBOpenDBRequest.prototype, 'blocked') +defineEventAttribute(DBOpenDBRequest.prototype, 'upgradeneeded') + +exports.DBRequest = DBRequest +exports.DBOpenDBRequest = DBOpenDBRequest diff --git a/test/indexeddb.t.js b/test/indexeddb.t.js index 4a70557..694c50b 100644 --- a/test/indexeddb.t.js +++ b/test/indexeddb.t.js @@ -1,5 +1,20 @@ -require('proof')(1, prove) +require('proof')(1, (okay, callback) => { + const { indexedDB } = require('..') + okay(indexedDB, 'required') + const request = indexedDB.open('test', 3) + request.onupgradeneeded = function () { + console.log('upgrading') + return + const db = request.result + const store = db.createObjectStore('books', { keyPath: 'isbn' }) + store.createIndex('by_title', 'title', { unique: true }) -function prove (ok) { - ok(require('..'), 'require') -} + store.put({ title: 'Quarry Memories', author: 'Fred', isbn: 123456 }) + store.put({ title: 'Water Buffaloes', author: 'Fred', isbn: 234567 }) + store.put({ title: 'Bedrock Nights', author: 'Barney', isbn: 345678 }) + } + request.onsuccess = function () { + console.log('succeeded') + } + callback() +})