Path: blob/trunk/third_party/closure/goog/db/transaction.js
2868 views
// Copyright 2011 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Wrapper for an IndexedDB transaction.16*17*/181920goog.provide('goog.db.Transaction');21goog.provide('goog.db.Transaction.TransactionMode');2223goog.require('goog.async.Deferred');24goog.require('goog.db.Error');25goog.require('goog.db.ObjectStore');26goog.require('goog.events');27goog.require('goog.events.EventHandler');28goog.require('goog.events.EventTarget');29303132/**33* Creates a new transaction. Transactions contain methods for accessing object34* stores and are created from the database object. Should not be created35* directly, open a database and call createTransaction on it.36* @see goog.db.IndexedDb#createTransaction37*38* @param {!IDBTransaction} tx IndexedDB transaction to back this wrapper.39* @param {!goog.db.IndexedDb} db The database that this transaction modifies.40* @constructor41* @extends {goog.events.EventTarget}42* @final43*/44goog.db.Transaction = function(tx, db) {45goog.db.Transaction.base(this, 'constructor');4647/**48* Underlying IndexedDB transaction object.49*50* @type {!IDBTransaction}51* @private52*/53this.tx_ = tx;5455/**56* The database that this transaction modifies.57*58* @type {!goog.db.IndexedDb}59* @private60*/61this.db_ = db;6263/**64* Event handler for this transaction.65*66* @type {!goog.events.EventHandler<!goog.db.Transaction>}67* @private68*/69this.eventHandler_ = new goog.events.EventHandler(this);7071// TODO(user): remove these casts once the externs file is updated to72// correctly reflect that IDBTransaction extends EventTarget73this.eventHandler_.listen(74/** @type {!EventTarget} */ (this.tx_), 'complete',75goog.bind(76this.dispatchEvent, this, goog.db.Transaction.EventTypes.COMPLETE));77this.eventHandler_.listen(78/** @type {!EventTarget} */ (this.tx_), 'abort',79goog.bind(80this.dispatchEvent, this, goog.db.Transaction.EventTypes.ABORT));81this.eventHandler_.listen(82/** @type {!EventTarget} */ (this.tx_), 'error', this.dispatchError_);83};84goog.inherits(goog.db.Transaction, goog.events.EventTarget);858687/**88* Dispatches an error event based on the given event, wrapping the error89* if necessary.90*91* @param {Event} ev The error event given to the underlying IDBTransaction.92* @private93*/94goog.db.Transaction.prototype.dispatchError_ = function(ev) {95if (ev.target instanceof goog.db.Error) {96this.dispatchEvent(97{type: goog.db.Transaction.EventTypes.ERROR, target: ev.target});98} else {99this.dispatchEvent({100type: goog.db.Transaction.EventTypes.ERROR,101target: goog.db.Error.fromRequest(102/** @type {!IDBRequest} */ (ev.target), 'in transaction')103});104}105};106107108/**109* Event types the Transaction can dispatch. COMPLETE events are dispatched110* when the transaction is committed. If a transaction is aborted it dispatches111* both an ABORT event and an ERROR event with the ABORT_ERR code. Error events112* are dispatched on any error.113*114* @enum {string}115*/116goog.db.Transaction.EventTypes = {117COMPLETE: 'complete',118ABORT: 'abort',119ERROR: 'error'120};121122123/**124* @return {goog.db.Transaction.TransactionMode} The transaction's mode.125*/126goog.db.Transaction.prototype.getMode = function() {127return /** @type {goog.db.Transaction.TransactionMode} */ (this.tx_.mode);128};129130131/**132* @return {!goog.db.IndexedDb} The database that this transaction modifies.133*/134goog.db.Transaction.prototype.getDatabase = function() {135return this.db_;136};137138139/**140* Opens an object store to do operations on in this transaction. The requested141* object store must be one that is in this transaction's scope.142* @see goog.db.IndexedDb#createTransaction143*144* @param {string} name The name of the requested object store.145* @return {!goog.db.ObjectStore} The wrapped object store.146* @throws {goog.db.Error} In case of error getting the object store.147*/148goog.db.Transaction.prototype.objectStore = function(name) {149try {150return new goog.db.ObjectStore(this.tx_.objectStore(name));151} catch (ex) {152throw goog.db.Error.fromException(ex, 'getting object store ' + name);153}154};155156157/**158* @return {!goog.async.Deferred} A deferred that will fire once the159* transaction is complete. It fires the errback chain if an error occurs160* in the transaction, or if it is aborted.161*/162goog.db.Transaction.prototype.wait = function() {163var d = new goog.async.Deferred();164goog.events.listenOnce(165this, goog.db.Transaction.EventTypes.COMPLETE, goog.bind(d.callback, d));166var errorKey;167var abortKey = goog.events.listenOnce(168this, goog.db.Transaction.EventTypes.ABORT, function() {169goog.events.unlistenByKey(errorKey);170d.errback(171new goog.db.Error(172goog.db.Error.ErrorCode.ABORT_ERR,173'waiting for transaction to complete'));174});175errorKey = goog.events.listenOnce(176this, goog.db.Transaction.EventTypes.ERROR, function(e) {177goog.events.unlistenByKey(abortKey);178d.errback(e.target);179});180181var db = this.getDatabase();182return d.addCallback(function() { return db; });183};184185186/**187* Aborts this transaction. No pending operations will be applied to the188* database. Dispatches an ABORT event.189*/190goog.db.Transaction.prototype.abort = function() {191this.tx_.abort();192};193194195/** @override */196goog.db.Transaction.prototype.disposeInternal = function() {197goog.db.Transaction.base(this, 'disposeInternal');198this.eventHandler_.dispose();199};200201202/**203* The three possible transaction modes.204* @see http://www.w3.org/TR/IndexedDB/#idl-def-IDBTransaction205*206* @enum {string}207*/208goog.db.Transaction.TransactionMode = {209READ_ONLY: 'readonly',210READ_WRITE: 'readwrite',211VERSION_CHANGE: 'versionchange'212};213214215