Skip to content

Commit

Permalink
Implement EventTarget.
Browse files Browse the repository at this point in the history
Found what appears to be a good working implementation on NPM.

Closes #15.
  • Loading branch information
flatheadmill committed Sep 28, 2020
1 parent ced419b commit 54078ff
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 5 deletions.
28 changes: 28 additions & 0 deletions factory.js
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
module.exports = 1
const path = require('path')

const { DBFactory } = require('./factory')

exports.indexedDB = new DBFactory(path.resolve(__dirname, './test/tmp/indexeddb'))
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"dependencies":
{
"event-target-shim": "5.0.1"
},
"devDependencies":
{
Expand Down
27 changes: 27 additions & 0 deletions request.js
Original file line number Diff line number Diff line change
@@ -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
23 changes: 19 additions & 4 deletions test/indexeddb.t.js
Original file line number Diff line number Diff line change
@@ -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()
})

0 comments on commit 54078ff

Please sign in to comment.