|
127 | 127 | }; |
128 | 128 |
|
129 | 129 | SQLitePlugin.prototype.startNextTransaction = function() { |
130 | | - var self; |
131 | | - self = this; |
132 | | - nextTick((function(_this) { |
133 | | - return function() { |
| 130 | + var _this = this; |
| 131 | + nextTick(function() { |
134 | 132 | var txLock; |
135 | 133 | if (!(_this.dbname in _this.openDBs) || _this.openDBs[_this.dbname].state !== DB_STATE_OPEN) { |
136 | 134 | console.log('cannot start next transaction: database not open'); |
137 | 135 | return; |
138 | 136 | } |
139 | | - txLock = txLocks[self.dbname]; |
| 137 | + txLock = txLocks[_this.dbname]; |
140 | 138 | if (!txLock) { |
141 | 139 | console.log('cannot start next transaction: database connection is lost'); |
142 | 140 | return; |
|
158 | 156 | } |
159 | 157 | } |
160 | 158 | } |
161 | | - }; |
162 | | - })(this)); |
| 159 | + }); |
163 | 160 | }; |
164 | 161 |
|
165 | 162 | SQLitePlugin.prototype.abortAllPendingTransactions = function() { |
|
178 | 175 |
|
179 | 176 | SQLitePlugin.prototype.open = function(success, error) { |
180 | 177 | var openerrorcb, opensuccesscb; |
| 178 | + var _this = this; |
| 179 | + |
181 | 180 | if (this.dbname in this.openDBs) { |
182 | 181 | console.log('database already open: ' + this.dbname); |
183 | | - nextTick((function(_this) { |
184 | | - return function() { |
| 182 | + nextTick(function() { |
185 | 183 | success(_this); |
186 | | - }; |
187 | | - })(this)); |
| 184 | + }); |
188 | 185 | } else { |
189 | | - var maxConnections = 4; |
| 186 | + |
| 187 | + var maxConnections = 4, okConnections = 0; |
| 188 | + var failed = false; |
| 189 | + var successOnce = function(sqlitePlugin) { |
| 190 | + okConnections++; |
| 191 | + if (!failed && okConnections == maxConnections) { |
| 192 | + success(sqlitePlugin); |
| 193 | + } |
| 194 | + } |
190 | 195 |
|
191 | 196 | console.log('OPEN database: ' + this.dbname); |
192 | | - opensuccesscb = (function(_this) { |
193 | | - return function(connectionName) { |
| 197 | + opensuccesscb = function(connectionName) { |
194 | 198 | return function() { |
195 | 199 | var txLock; |
196 | 200 | console.log('OPEN database: ' + _this.dbname + ', connection: ' + connectionName + ' - OK'); |
|
204 | 208 |
|
205 | 209 | var callSuccessAndStartTransaction = function () { |
206 | 210 | if (!!success) { |
207 | | - success(_this); |
| 211 | + successOnce(_this); |
208 | 212 | } |
209 | 213 | txLock = txLocks[_this.dbname]; |
210 | 214 | if (!!txLock && txLock.queue.length > 0) { |
|
221 | 225 |
|
222 | 226 | }; |
223 | 227 | }; |
224 | | - })(this); |
225 | | - openerrorcb = (function(_this) { |
226 | | - return function() { |
| 228 | + openerrorcb = function() { |
227 | 229 | console.log('OPEN database: ' + _this.dbname + ' FAILED, aborting any pending transactions'); |
228 | | - if (!!error) { |
| 230 | + if (!!error && !failed) { |
229 | 231 | error(newSQLError('Could not open database')); |
230 | 232 | } |
| 233 | + failed = true; |
231 | 234 | delete _this.openDBs[_this.dbname]; |
232 | 235 | _this.abortAllPendingTransactions(); |
233 | 236 | }; |
234 | | - })(this); |
235 | 237 | this.openDBs[this.dbname] = { |
236 | 238 | state: DB_STATE_INIT, |
237 | 239 | connections: [] |
238 | 240 | }; |
239 | 241 |
|
240 | | - var options = {}; |
241 | | - for (var key in this.openargs) { |
242 | | - options[key] = this.openargs[key]; |
| 242 | + var copyOptions = function() { |
| 243 | + var options = {}; |
| 244 | + var optionNames = Object.keys(_this.openargs); |
| 245 | + for (var i = 0; i < optionNames.length; i++) { |
| 246 | + options[optionNames[i]] = _this.openargs[optionNames[i]]; |
| 247 | + } |
| 248 | + return options; |
243 | 249 | } |
244 | 250 |
|
245 | | - for (var i = 0; i < maxConnections; i++) { |
246 | | - var connectionName = 'connection' + i; |
| 251 | + var openNext = function openNext(connNumber) { |
| 252 | + |
| 253 | + connNumber++; |
| 254 | + |
| 255 | + var options = copyOptions(); |
| 256 | + var connectionName = 'connection' + connNumber; |
| 257 | + var onSuccess = opensuccesscb(connectionName); |
247 | 258 | options['connectionName'] = connectionName; |
248 | 259 |
|
249 | | - cordova.exec(opensuccesscb(connectionName), openerrorcb, "SQLitePlugin", "open", [options]); |
250 | | - } |
| 260 | + cordova.exec(function() { |
| 261 | + onSuccess(); |
| 262 | + if (connNumber < maxConnections) { |
| 263 | + openNext(connNumber); |
| 264 | + } |
| 265 | + }, openerrorcb, "SQLitePlugin", "open", [options]); |
| 266 | + }; |
| 267 | + |
| 268 | + openNext(0); |
251 | 269 | } |
252 | 270 | }; |
253 | 271 |
|
|
0 commit comments