shamblett / sporran

A PouchDB like browser application in Dart

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AutoSync does not work.

opened this issue · comments

When transitioning from offline to online, it is expected to see the records in local storage saved to couchdb. However, only new records get's inserted in couchdb and the old ones does not sync.

Sounds like a bug, we should do this.

Hmm, cant see this either at the mo, if you run test_scenario_1 you end up with CouchDB holding the same data as your local IndexedDB does, the scenario does this -

/**

  • Start offline
  • Bulk create 3 docs
  • Add two attachments
  • Delete one document
  • Go online
  • Check that sync worked.
  • */

How did you create the problem in the issue, syncing can take time remember, you must leave your Dart script running until syncing is complete.

Created the code below to replicate.

  1. Run this code in Dartium
  2. Go to developer tools to see the record inserted in local db.
  3. Check http://127.0.0.1:5984/_utils/database.html?sporranlab001 and see if the record is present.
  4. Started at 26th minute of the hour and waited for 5 minutes.

Expected : Record should be put in couchdb.
: Existing couchdb records should be fetch and stored in localdb.

Actual : Record did not get uploaded.
Existing records in couch db was not retrieved and stored in local storage.

Configuration :
I used CORS configuration in couchdb.

pubspec

name: sporranlab
description: A sample web application
dependencies:
browser: any
sporran: ">=2.0.0 <3.0.0"

CODE

import 'dart:html'; import 'dart:math'; import 'package:sporran/sporran.dart'; import 'package:json_object/json_object.dart';

Sporran sporran = null;
final String DB_NAME = "sporranlab001";

void main() {
if(sporran == null) {
sporran = new Sporran(DB_NAME, "localhost", true, "5984", "http://", null, null);
}
sporran.autoSync = true;
sporran.onReady.first.then((e) => insertRecords());
}

void insertRecords() {
Person person = new Person();
person.id = generateId().toString();
person.firstName = "Some Guy First Name " + person.id;
person.lastName = "Last Name " + person.id;
print("inserting Record " + person.toString());

JsonObject jsonObject = new JsonObject();
jsonObject.addAll(person.toMap());
print("inserting Record " + person.toString() + " - " + person.hashCode.toString());
sporran.put(person.id, jsonObject, null).catchError((e) => onError);

}

void onError(e) {
window.alert('Oh no! Something went wrong. See the console for details.');
window.console.log('An error occurred: {$e}');
}

int generateId() {
var random = new Random();
return random.nextInt(1<<16);
}

class Person {
String id;
String firstName, lastName;
Map toMap() => {"id":id, "firstName":firstName, "lastName":lastName};
static Person fromMap(Map map) {
Person person = new Person();
person.id = map["id"];
person.firstName = map["firstName"];
person.lastName = map["lastName"];
return person;
}
}

Also while doing some tests, I encountered this exception. But it is not consistent.

Exception: Uncaught Error: SporranException: message = Initialisation Failure, Lawndart is not initialized Stack Trace: #0 _SporranDatabase.updateLocalStorageObject (package:sporran/src/SporranDatabase.dart:539:51) #1 Sporran.put (package:sporran/src/Sporran.dart:252:39) #2 insertRecords (http://localhost:8080/sporranlab.dart:27:14) #3 main. (http://localhost:8080/sporranlab.dart:15:50) #4 _RootZone.runUnary (dart:async/zone.dart:1082) #5 _Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:488) #6 _Future._propagateToListeners (dart:async/future_impl.dart:571) #7 _Future._complete (dart:async/future_impl.dart:317) #8 _cancelAndValue (dart:async/stream_pipe.dart:44) #9 Stream.first. (dart:async/stream.dart:905) #10 _RootZone.runUnaryGuarded (dart:async/zone.dart:1020) #11 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341) #12 _DelayedData.perform (dart:async/stream_impl.dart:595) #13 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:711) #14 _PendingEvents.schedule. (dart:async/stream_impl.dart:671) #15 _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:41) #16 _asyncRunCallback (dart:async/schedule_microtask.dart:48) #17 _handleMutation (dart:html:39006)

Newly opened db sporranlab001 has version 2 and stores [Sporran]
The store Sporran exists in sporranlab001

Ok, only new and updated records(as defined by the CouchDb notification service) are synced, so if you insert a record straight into CouchDb and you should see this reflected back into local storage, we don't replicate the whole Couch database into local storage as such.

Also your constructor should be changed to this :-

sporran = new Sporran(DB_NAME, "localhost", false,,,,,');

as in the manual change notification boolean should be false, not true. If you set this to true you are saying 'I'll do my own syncing', so you have to include a sync() call in your code somewhere to do this. I set this to false, ran the code and then inserted some records into CouchDb manually, local storage ended up like this 👍

"21821"
{status:updated, key:21821,…}
1
"e2d04e087451af06b7f699f92a018ee6"
{status:updated, key:e2d04e087451af06b7f699f92a018ee6,…}
2
"e2d04e087451af06b7f699f92a01916c"
{status:updated, key:e2d04e087451af06b7f699f92a01916c,…}
3
"e2d04e087451af06b7f699f92a019251"
{status:updated, key:e2d04e087451af06b7f699f92a019251,…}

ie the notifications were sent through and processed by Sporran. If this is not clear in the docs I'll update them.

The exception your seeing happened once to me, on the very first invocation of the test, I've updated the code to hopefully stop this.

I got it thanks.

I just tested again with manual change notification as false and it synced.

On Tue, Sep 9, 2014 at 12:34 AM, Steve Hamblett notifications@github.com
wrote:

Ok, only new and updated records(as defined by the CouchDb notification
service) are synced, so if you insert a record straight into CouchDb and
you should see this reflected back into local storage, we don't replicate
the whole Couch database into local storage as such.

Also your constructor should be changed to this :-

sporran = new Sporran(DB_NAME, "localhost", false,,,,,');

as in the manual change notification boolean should be false, not true. If
you set this to true you are saying 'I'll do my own syncing', so you have
to include a sync() call in your code somewhere to do this. I set this to
false, ran the code and then inserted some records into CouchDb manually,
local storage ended up like this [image: 👍]

"21821"
{status:updated, key:21821,…}
1

"e2d04e087451af06b7f699f92a018ee6"
{status:updated, key:e2d04e087451af06b7f699f92a018ee6,…}
2

"e2d04e087451af06b7f699f92a01916c"
{status:updated, key:e2d04e087451af06b7f699f92a01916c,…}
3

"e2d04e087451af06b7f699f92a019251"
{status:updated, key:e2d04e087451af06b7f699f92a019251,…}

ie the notifications were sent through and processed by Sporran. If this
is not clear in the docs I'll update them.

The exception your seeing happened once to me, on the very first
invocation of the test, I've updated the code to hopefully stop this.


Reply to this email directly or view it on GitHub
#5 (comment).

Sporran updated, can be closed