1 line
8.0 KiB
Plaintext
1 line
8.0 KiB
Plaintext
{"version":3,"sources":["../yjs/node_modules/browser-pack/_prelude.js","src/IndexedDB.js"],"names":[],"mappings":"AAAA;ACAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA","file":"y-indexeddb.es6","sourceRoot":"/source/","sourcesContent":["(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require==\"function\"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error(\"Cannot find module '\"+o+\"'\");throw f.code=\"MODULE_NOT_FOUND\",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require==\"function\"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})","/* global Y */\r\n'use strict'\r\n\r\nfunction extend (Y) {\r\n class Store {\r\n constructor (transaction, name) {\r\n this.store = transaction.objectStore(name)\r\n }\r\n * find (id) {\r\n return yield this.store.get(id)\r\n }\r\n * put (v) {\r\n yield this.store.put(v)\r\n }\r\n * delete (id) {\r\n yield this.store.delete(id)\r\n }\r\n * findWithLowerBound (start) {\r\n return yield this.store.openCursor(window.IDBKeyRange.lowerBound(start))\r\n }\r\n * findWithUpperBound (end) {\r\n return yield this.store.openCursor(window.IDBKeyRange.upperBound(end), 'prev')\r\n }\r\n * findNext (id) {\r\n return yield* this.findWithLowerBound([id[0], id[1] + 1])\r\n }\r\n * findPrev (id) {\r\n return yield* this.findWithUpperBound([id[0], id[1] - 1])\r\n }\r\n * iterate (t, start, end, gen) {\r\n var range = null\r\n if (start != null && end != null) {\r\n range = window.IDBKeyRange.bound(start, end)\r\n } else if (start != null) {\r\n range = window.IDBKeyRange.lowerBound(start)\r\n } else if (end != null) {\r\n range = window.IDBKeyRange.upperBound(end)\r\n }\r\n var cursorResult = this.store.openCursor(range)\r\n while ((yield cursorResult) != null) {\r\n yield* gen.call(t, cursorResult.result.value)\r\n cursorResult.result.continue()\r\n }\r\n }\r\n\r\n }\r\n class Transaction extends Y.Transaction {\r\n constructor (store) {\r\n super(store)\r\n var transaction = store.db.transaction(['OperationStore', 'StateStore', 'DeleteStore'], 'readwrite')\r\n this.store = store\r\n this.ss = new Store(transaction, 'StateStore')\r\n this.os = new Store(transaction, 'OperationStore')\r\n this.ds = new Store(transaction, 'DeleteStore')\r\n }\r\n }\r\n class OperationStore extends Y.AbstractDatabase {\r\n constructor (y, opts) {\r\n super(y, opts)\r\n if (opts == null) {\r\n opts = {}\r\n }\r\n if (opts.namespace == null || typeof opts.namespace !== 'string') {\r\n throw new Error('IndexedDB: expect a string (opts.namespace)!')\r\n } else {\r\n this.namespace = opts.namespace\r\n }\r\n if (opts.idbVersion != null) {\r\n this.idbVersion = opts.idbVersion\r\n } else {\r\n this.idbVersion = 5\r\n }\r\n var store = this\r\n // initialize database!\r\n this.requestTransaction(function * () {\r\n store.db = yield window.indexedDB.open(opts.namespace, store.idbVersion)\r\n })\r\n if (opts.cleanStart) {\r\n this.requestTransaction(function * () {\r\n yield this.os.store.clear()\r\n yield this.ds.store.clear()\r\n yield this.ss.store.clear()\r\n })\r\n }\r\n var operationsToAdd = []\r\n window.addEventListener('storage', function (event) {\r\n if (event.key === '__YJS__' + store.namespace) {\r\n operationsToAdd.push(event.newValue)\r\n if (operationsToAdd.length === 1) {\r\n store.requestTransaction(function * () {\r\n var add = operationsToAdd\r\n operationsToAdd = []\r\n for (var i in add) {\r\n // don't call the localStorage event twice..\r\n var op = JSON.parse(add[i])\r\n if (op.struct !== 'Delete') {\r\n op = yield* this.getOperation(op.id)\r\n }\r\n yield* this.store.operationAdded(this, op, true)\r\n }\r\n })\r\n }\r\n }\r\n }, false)\r\n }\r\n * operationAdded (transaction, op, noAdd) {\r\n yield* super.operationAdded(transaction, op)\r\n if (!noAdd) {\r\n window.localStorage['__YJS__' + this.namespace] = JSON.stringify(op)\r\n }\r\n }\r\n transact (makeGen) {\r\n var transaction = this.db != null ? new Transaction(this) : null\r\n var store = this\r\n\r\n var gen = makeGen.call(transaction)\r\n handleTransactions(gen.next())\r\n\r\n function handleTransactions (result) {\r\n var request = result.value\r\n if (result.done) {\r\n makeGen = store.getNextRequest()\r\n if (makeGen != null) {\r\n if (transaction == null && store.db != null) {\r\n transaction = new Transaction(store)\r\n }\r\n gen = makeGen.call(transaction)\r\n handleTransactions(gen.next())\r\n } // else no transaction in progress!\r\n return\r\n }\r\n if (request.constructor === window.IDBRequest) {\r\n request.onsuccess = function () {\r\n var res = request.result\r\n if (res != null && res.constructor === window.IDBCursorWithValue) {\r\n res = res.value\r\n }\r\n handleTransactions(gen.next(res))\r\n }\r\n request.onerror = function (err) {\r\n gen.throw(err)\r\n }\r\n } else if (request.constructor === window.IDBCursor) {\r\n request.onsuccess = function () {\r\n handleTransactions(gen.next(request.result != null ? request.result.value : null))\r\n }\r\n request.onerror = function (err) {\r\n gen.throw(err)\r\n }\r\n } else if (request.constructor === window.IDBOpenDBRequest) {\r\n request.onsuccess = function (event) {\r\n var db = event.target.result\r\n handleTransactions(gen.next(db))\r\n }\r\n request.onerror = function () {\r\n gen.throw(\"Couldn't open IndexedDB database!\")\r\n }\r\n request.onupgradeneeded = function (event) {\r\n var db = event.target.result\r\n try {\r\n db.createObjectStore('OperationStore', {keyPath: 'id'})\r\n db.createObjectStore('DeleteStore', {keyPath: 'id'})\r\n db.createObjectStore('StateStore', {keyPath: 'id'})\r\n } catch (e) {\r\n console.log('Store already exists!')\r\n }\r\n }\r\n } else {\r\n gen.throw('You must not yield this type!')\r\n }\r\n }\r\n }\r\n // TODO: implement \"free\"..\r\n * destroy () {\r\n this.db.close()\r\n yield window.indexedDB.deleteDatabase(this.namespace)\r\n }\r\n }\r\n Y.extend('indexeddb', OperationStore)\r\n}\r\n\r\nmodule.exports = extend\r\nif (typeof Y !== 'undefined') {\r\n extend(Y)\r\n}\r\n"]} |