Skip to content
This repository

(#589) fix phonegap/cordova adapter api not filling bug #670

Merged
merged 5 commits into from about 1 year ago

3 participants

Howon Lee Dale  Harvey R.J. Steinert
Howon Lee

Note that usage should be something like:

var database;
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady(){
    Pouch('listoutfitter', {}, function (err, db) {
        if (err) {
            console.log("Error while creating database: " + err)
            for( var e in err) {
                console.log("Error element: " + e + " -> "  + err[e]);
            }
            return;
        }
        console.log("Successfully made new database");
        database = db;
        db.post({_id: 'doc1', title: 'Cony Island Baby' }, {}, function (err, response) {
            console.log("created doc: " + response.ok);
            console.log("doc id:  " + response.id)
            console.log("rev: " + response.rev)
        })
    });
};

Note that we must create and use the db inside an onDeviceReady() function, because we use websql which only becomes available to us after the deviceready event.

In order to test, you have to stick the tests/ folder inside of a cordova app and wrap all of webrunner.js in a function that is triggered on a deviceready event.

Dale  Harvey
Owner

So I think even the deviceready thing can be hidden behind the api along similiar lines to taskready, but since that would require other setup changes that can be a follow up feature.

However I want to understand the need for this event more properly, as far as I can you should be guaranteed to hit that point (where you send the event) before the transaction function is ever run anyway, the transaction callback is going to be put on the event loop and the rest of the setup is synchronous, ill start testing this some more but it seems like if there is a bug its not just going to be phonegap specific

Dale  Harvey
Owner

As brought up in the mailing list I can see there being problems with us trying to read the metadata table before it may exist, but that wouldnt be affected by these changes

Howon Lee

So there's a problem with having the deviceready thing, because a lot of phonegap apps end up just wrapping absolutely everything in a function that gets called on deviceready, because of course you can't do anything without phonegap having loaded. Like here: it's one big function go() that gets called on deviceready. If we have something inside phonegap that also gets called conditionally on deviceready, then the behavior will become more mysterious, I think.

Dale  Harvey
Owner

ok, agreed with the deviceready thing, probably is a bit far to try and hide that from the user.

But I still really want to understand why this event is needed before its merged, we might need to put that wait in for all adapters if there is a race condition in the setup, there is nothing that is doing that should only effect cordova apps

Howon Lee

Testing:

  1. Android browser: just grunt connect cors-server forever on your development computer then go to 10.0.2.2:8000/tests/test.html on the emulator or on your phone while connected. 10.0.2.2 on the emulator links to 127.0.0.1 on the development computer.
  2. Phonegap: set up an android phonegap app, go to assets/www/ and copy in the whole test suite folder and the version of pouch you want to test. Then, you must make webrunner.js run on deviceready. By this, I mean "wrap the whole sucker in a function that gets called on deviceready."

Then, set the initial page on the phonegap webview (in the .java file) to test.html in the test suite folder, then fire the phonegap app up.

Howon Lee howonlee merged commit 583f6c6 into from April 16, 2013
Howon Lee howonlee closed this April 16, 2013
R.J. Steinert rjsteinert referenced this pull request in open-learning-exchange/BeLL-Apps June 07, 2013
Closed

Wrap up Hubble Local into an Android App #1

R.J. Steinert

@howonlee Thanks for the advice on using Pouch with PhoneGap. Unfortunately I'm running into the issue where allDocs and query are returning zero results. I'm interested in running the tests in a PhoneGap app as you described above but I'm currently seeing so many tests fail when running grunt node-qunit and grunt browser that I'm hesitant to bother. It's the same report for most tests over and over, "failed to open database". Is everyone else seeing that as the current state of tests? Perhaps I'm doing something wrong, I'm new to grunt so that's very possible.

R.J. Steinert

Aha, I missed the fact I was supposed to have CouchDB running on http://127.0.0.1:5984 with admin party.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
82  src/adapters/pouch.websql.js
... ...
@@ -1,5 +1,6 @@
1 1
 /*globals call: false, extend: false, parseDoc: false, Crypto: false */
2 2
 /*globals isLocalId: false, isDeleted: false, Changes: false, filterChange: false */
  3
+/*global isCordova*/
3 4
 
4 5
 'use strict';
5 6
 
@@ -46,42 +47,51 @@ var webSqlPouch = function(opts, callback) {
46 47
     callback(null, api);
47 48
   }
48 49
 
49  
-  db.transaction(function (tx) {
50  
-    var meta = 'CREATE TABLE IF NOT EXISTS ' + META_STORE +
51  
-      ' (update_seq, dbid)';
52  
-    var attach = 'CREATE TABLE IF NOT EXISTS ' + ATTACH_STORE +
53  
-      ' (digest, json, body BLOB)';
54  
-    var doc = 'CREATE TABLE IF NOT EXISTS ' + DOC_STORE +
55  
-      ' (id unique, seq, json, winningseq)';
56  
-    var seq = 'CREATE TABLE IF NOT EXISTS ' + BY_SEQ_STORE +
57  
-      ' (seq INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, doc_id_rev UNIQUE, json)';
58  
-
59  
-    tx.executeSql(attach);
60  
-    tx.executeSql(doc);
61  
-    tx.executeSql(seq);
62  
-    tx.executeSql(meta);
63  
-
64  
-    var updateseq = 'SELECT update_seq FROM ' + META_STORE;
65  
-    tx.executeSql(updateseq, [], function(tx, result) {
66  
-      if (!result.rows.length) {
67  
-        var initSeq = 'INSERT INTO ' + META_STORE + ' (update_seq) VALUES (?)';
68  
-        var newId = Math.uuid();
69  
-        tx.executeSql(initSeq, [0]);
70  
-        return;
71  
-      }
72  
-      update_seq = result.rows.item(0).update_seq;
73  
-    });
74  
-    var dbid = 'SELECT dbid FROM ' + META_STORE;
75  
-    tx.executeSql(dbid, [], function(tx, result) {
76  
-      if (!result.rows.length) {
77  
-        var initDb = 'INSERT INTO ' + META_STORE + ' (dbid) VALUES (?)';
78  
-        var newId = Math.uuid();
79  
-        tx.executeSql(initDb, [newId]);
80  
-        return;
81  
-      }
82  
-      instanceId = result.rows.item(0).dbid;
83  
-    });
84  
-  }, unknownError(callback), dbCreated);
  50
+  function setup(){
  51
+    db.transaction(function (tx) {
  52
+      var meta = 'CREATE TABLE IF NOT EXISTS ' + META_STORE +
  53
+        ' (update_seq, dbid)';
  54
+      var attach = 'CREATE TABLE IF NOT EXISTS ' + ATTACH_STORE +
  55
+        ' (digest, json, body BLOB)';
  56
+      var doc = 'CREATE TABLE IF NOT EXISTS ' + DOC_STORE +
  57
+        ' (id unique, seq, json, winningseq)';
  58
+      var seq = 'CREATE TABLE IF NOT EXISTS ' + BY_SEQ_STORE +
  59
+        ' (seq INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, doc_id_rev UNIQUE, json)';
  60
+
  61
+      tx.executeSql(attach);
  62
+      tx.executeSql(doc);
  63
+      tx.executeSql(seq);
  64
+      tx.executeSql(meta);
  65
+
  66
+      var updateseq = 'SELECT update_seq FROM ' + META_STORE;
  67
+      tx.executeSql(updateseq, [], function(tx, result) {
  68
+        if (!result.rows.length) {
  69
+          var initSeq = 'INSERT INTO ' + META_STORE + ' (update_seq) VALUES (?)';
  70
+          var newId = Math.uuid();
  71
+          tx.executeSql(initSeq, [0]);
  72
+          return;
  73
+        }
  74
+        update_seq = result.rows.item(0).update_seq;
  75
+      });
  76
+      var dbid = 'SELECT dbid FROM ' + META_STORE;
  77
+      tx.executeSql(dbid, [], function(tx, result) {
  78
+        if (!result.rows.length) {
  79
+          var initDb = 'INSERT INTO ' + META_STORE + ' (dbid) VALUES (?)';
  80
+          var newId = Math.uuid();
  81
+          tx.executeSql(initDb, [newId]);
  82
+          return;
  83
+        }
  84
+        instanceId = result.rows.item(0).dbid;
  85
+      });
  86
+    }, unknownError(callback), dbCreated);
  87
+  }
  88
+  if (isCordova()){
  89
+    //to wait until custom api is made in pouch.adapters before doing setup
  90
+    window.addEventListener(name + "_pouch", setup, false);
  91
+  } else {
  92
+    setup();
  93
+  }
  94
+
85 95
 
86 96
   api.type = function() {
87 97
     return 'websql';
5  src/pouch.adapter.js
... ...
@@ -1,5 +1,6 @@
1 1
 /*globals Pouch: true, yankError: false, extend: false, call: false, parseDocId: false, traverseRevTree: false */
2 2
 /*globals arrayFirst: false, rootToLeaf: false, computeHeight: false */
  3
+/*globals cordova, isCordova */
3 4
 
4 5
 "use strict";
5 6
 
@@ -537,6 +538,10 @@ var PouchAdapter = function(opts, callback) {
537 538
     api.taskqueue.execute(api);
538 539
   }
539 540
 
  541
+  if (isCordova()){
  542
+    //to inform websql adapter that we can use api
  543
+    cordova.fireWindowEvent(opts.name + "_pouch", {});
  544
+  }
540 545
   return customApi;
541 546
 };
542 547
 
7  src/pouch.utils.js
@@ -265,6 +265,10 @@ var isChromeApp = function(){
265 265
   return (typeof chrome !== "undefined" && typeof chrome.storage !== "undefined" && typeof chrome.storage.local !== "undefined");
266 266
 };
267 267
 
  268
+var isCordova = function(){
  269
+  return (typeof cordova !== "undefined" || typeof PhoneGap !== "undefined" || typeof phonegap !== "undefined");
  270
+};
  271
+
268 272
 if (typeof module !== 'undefined' && module.exports) {
269 273
   // use node.js's crypto library instead of the Crypto object created by deps/uuid.js
270 274
   var crypto = require('crypto');
@@ -302,7 +306,8 @@ if (typeof module !== 'undefined' && module.exports) {
302 306
     extend: extend,
303 307
     ajax: ajax,
304 308
     rootToLeaf: rootToLeaf,
305  
-    isChromeApp: isChromeApp
  309
+    isChromeApp: isChromeApp,
  310
+    isCordova: isCordova
306 311
   };
307 312
 }
308 313
 
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.