{"action":"created","issue":{"url":"https://api.github.com/repos/pouchdb/pouchdb/issues/4317","labels_url":"https://api.github.com/repos/pouchdb/pouchdb/issues/4317/labels{/name}","comments_url":"https://api.github.com/repos/pouchdb/pouchdb/issues/4317/comments","events_url":"https://api.github.com/repos/pouchdb/pouchdb/issues/4317/events","html_url":"https://github.com/pouchdb/pouchdb/issues/4317","id":106116222,"number":4317,"title":"sync()/replication() - A race condition with API design can miss subscribed events","user":{"login":"tohagan","id":301525,"avatar_url":"https://avatars.githubusercontent.com/u/301525?v=3","gravatar_id":"","url":"https://api.github.com/users/tohagan","html_url":"https://github.com/tohagan","followers_url":"https://api.github.com/users/tohagan/followers","following_url":"https://api.github.com/users/tohagan/following{/other_user}","gists_url":"https://api.github.com/users/tohagan/gists{/gist_id}","starred_url":"https://api.github.com/users/tohagan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tohagan/subscriptions","organizations_url":"https://api.github.com/users/tohagan/orgs","repos_url":"https://api.github.com/users/tohagan/repos","events_url":"https://api.github.com/users/tohagan/events{/privacy}","received_events_url":"https://api.github.com/users/tohagan/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-09-12T00:27:48Z","updated_at":"2015-09-12T00:46:00Z","closed_at":null,"body":"I think there is a race condition due to the API design of both `PouchDB.replicate()` and `db.sync()`. \r\n\r\nThis is because you configure and *start* the replication before you get a chance to subscribe to the emitted events. Hence there is the real risk of missing an event like 'completed', 'error', 'denied' or 'change'.  The solution is to introduce a `start()` method that allows the developer to delay emitted events until the subscriptions have completed and only permit the replicate() and sync() methods to configure the replications.\r\n\r\nExample: \r\n\r\n     var rep = PouchDB.replicate('mydb', 'http://localhost:5984/mydb', {\r\n       live: true,\r\n       retry: true\r\n     }).on('change', function (info) {\r\n       // handle change\r\n     }).on('paused', function () {\r\n       // replication paused (e.g. user went offline)\r\n     }).on('active', function () {\r\n       // replicate resumed (e.g. user went back online)\r\n     }).on('denied', function (info) {\r\n       // a document failed to replicate, e.g. due to permissions\r\n     }).on('complete', function (info) {\r\n       // handle complete\r\n     }).on('error', function (err) {\r\n       // handle error\r\n     });\r\n     \r\n     rep.start();   // Start replication only once the on() events are ALL subscribed.\r\n  \r\n     rep.cancel(); // whenever you want to cancel \r\n\r\nSame for the `sync()` example.\r\n     "},"comment":{"url":"https://api.github.com/repos/pouchdb/pouchdb/issues/comments/139693251","html_url":"https://github.com/pouchdb/pouchdb/issues/4317#issuecomment-139693251","issue_url":"https://api.github.com/repos/pouchdb/pouchdb/issues/4317","id":139693251,"user":{"login":"tohagan","id":301525,"avatar_url":"https://avatars.githubusercontent.com/u/301525?v=3","gravatar_id":"","url":"https://api.github.com/users/tohagan","html_url":"https://github.com/tohagan","followers_url":"https://api.github.com/users/tohagan/followers","following_url":"https://api.github.com/users/tohagan/following{/other_user}","gists_url":"https://api.github.com/users/tohagan/gists{/gist_id}","starred_url":"https://api.github.com/users/tohagan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tohagan/subscriptions","organizations_url":"https://api.github.com/users/tohagan/orgs","repos_url":"https://api.github.com/users/tohagan/repos","events_url":"https://api.github.com/users/tohagan/events{/privacy}","received_events_url":"https://api.github.com/users/tohagan/received_events","type":"User","site_admin":false},"created_at":"2015-09-12T00:46:00Z","updated_at":"2015-09-12T00:46:00Z","body":"A short term fix for the current API would be to introduce a timeout delay to starting replication that would cure the problem imperfectly but pragmatically for most current software that depends on the current APIs."}}