{"action":"created","issue":{"url":"https://api.github.com/repos/slightlyoff/BackgroundSync/issues/44","labels_url":"https://api.github.com/repos/slightlyoff/BackgroundSync/issues/44/labels{/name}","comments_url":"https://api.github.com/repos/slightlyoff/BackgroundSync/issues/44/comments","events_url":"https://api.github.com/repos/slightlyoff/BackgroundSync/issues/44/events","html_url":"https://github.com/slightlyoff/BackgroundSync/issues/44","id":57565326,"number":44,"title":"Resolving a duplicate registration","user":{"login":"KenjiBaheux","id":6354698,"avatar_url":"https://avatars.githubusercontent.com/u/6354698?v=3","gravatar_id":"","url":"https://api.github.com/users/KenjiBaheux","html_url":"https://github.com/KenjiBaheux","followers_url":"https://api.github.com/users/KenjiBaheux/followers","following_url":"https://api.github.com/users/KenjiBaheux/following{/other_user}","gists_url":"https://api.github.com/users/KenjiBaheux/gists{/gist_id}","starred_url":"https://api.github.com/users/KenjiBaheux/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/KenjiBaheux/subscriptions","organizations_url":"https://api.github.com/users/KenjiBaheux/orgs","repos_url":"https://api.github.com/users/KenjiBaheux/repos","events_url":"https://api.github.com/users/KenjiBaheux/events{/privacy}","received_events_url":"https://api.github.com/users/KenjiBaheux/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-02-13T06:38:43Z","updated_at":"2015-02-13T15:12:24Z","closed_at":null,"body":"Follow-up on a sidenote in https://github.com/slightlyoff/BackgroundSync/issues/41\r\n\r\nEmphasis added:\r\n> Default id makes it easy to accidentally double register. Imagine the page registers a repeating sync\r\n> each time it's opened. If it had to use the **same id** each time the **registration would reject**.\r\n\r\nI'm not sure this has to be the case. A nice design touch of the ServiceWorker API is that it lets you call register without having to worry about duplicate registrations. It resolves straight with the existing registration if any or gives you a new one otherwise (primary key being [sw script, scope])\r\n\r\nCan we also follow this pattern in BG sync?\r\n\r\n```html\r\n<!DOCTYPE html>\r\n<!-- https://tweet.example.com/index.html -->\r\n<html>\r\n  <head>\r\n    <script>\r\n      navigator.serviceWorker.register(\"/sw.js\");\r\n\r\n      // Registering for sync will fail unless a viable SW is available, so wait\r\n      // for that to happen.\r\n      navigator.serviceWorker.ready.then(function(swRegistration) {\r\n        // Returns a Promise\r\n        swRegistration.syncManager.register(\r\n          {\r\n            id: \"update\"\r\n          })\r\n        .then(function() { // Success (new registration or existing registration)\r\n               },\r\n               function() { // Failure\r\n                 // User/UA denied permission\r\n               });\r\n      });\r\n    </script>\r\n  </head>\r\n  <body> ... </body>\r\n</html>\r\n```\r\n\r\ninstead of\r\n```html\r\n<!DOCTYPE html>\r\n<!-- https://tweet.example.com/index.html -->\r\n<html>\r\n  <head>\r\n    <script>\r\n      navigator.serviceWorker.register(\"/sw.js\");\r\n\r\n      // Registering for sync will fail unless a viable SW is available, so wait\r\n      // for that to happen.\r\n      navigator.serviceWorker.ready.then(function(swRegistration) {\r\n\r\n      // checking if we have to register our update sync\r\n      var updateSyncNotRegistered = true;\r\n      if (swRegistration.syncManager.getRegistrations().then((regs) => {\r\n           for(reg in regs) {\r\n                if(reg.id == \"update\") {\r\n                      updateSyncNotRegistered = false;\r\n                      break;\r\n                }\r\n           }\r\n      });\r\n\r\n      if (updateSyncNotRegistered) {\r\n        // Returns a Promise\r\n        swRegistration.syncManager.register(\r\n          {\r\n            id: \"update\"\r\n          })\r\n        .then(function() { // Success\r\n               },\r\n               function() { // Failure\r\n                 // User/UA denied permission\r\n                 // but not because of duplicate registration since we checked for that\r\n               });\r\n        });\r\n    }\r\n    </script>\r\n  </head>\r\n  <body> ... </body>\r\n</html>\r\n```\r\n\r\nI imagine that we should also allow a mere call to register with the same id to overwrite a previously registered sync request.\r\n   \r\n```JavaScript\r\nfunction setupUpdates(userPrefs) {\r\n   // [...] (getting hold of swRegistration, etc.)\r\n   swRegistration.syncManager.register(\r\n          {\r\n            id: \"update\",\r\n            //...\r\n            allowOnBattery: userPref[\"allowOnBattery\"],\r\n            //...\r\n          })\r\n         .then(function() { // Success (new registration or updated existing registration\r\n               },\r\n               function() { // Failure\r\n                 // User/UA denied permission\r\n               });\r\n      );\r\n}\r\n\r\n//...\r\n   // code where handling changes to user preferences is handled\r\n   // ...\r\n  setupUpdates(userPrefs);\r\n  // ...\r\n```\r\n\r\nInstead of having to:\r\n + look up for the existing registration if any \r\n + unregistering\r\n + and then registering\r\n\r\n\r\nDoes this make sense? Or did I miss something?"},"comment":{"url":"https://api.github.com/repos/slightlyoff/BackgroundSync/issues/comments/74267990","html_url":"https://github.com/slightlyoff/BackgroundSync/issues/44#issuecomment-74267990","issue_url":"https://api.github.com/repos/slightlyoff/BackgroundSync/issues/44","id":74267990,"user":{"login":"annevk","id":1544111,"avatar_url":"https://avatars.githubusercontent.com/u/1544111?v=3","gravatar_id":"","url":"https://api.github.com/users/annevk","html_url":"https://github.com/annevk","followers_url":"https://api.github.com/users/annevk/followers","following_url":"https://api.github.com/users/annevk/following{/other_user}","gists_url":"https://api.github.com/users/annevk/gists{/gist_id}","starred_url":"https://api.github.com/users/annevk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/annevk/subscriptions","organizations_url":"https://api.github.com/users/annevk/orgs","repos_url":"https://api.github.com/users/annevk/repos","events_url":"https://api.github.com/users/annevk/events{/privacy}","received_events_url":"https://api.github.com/users/annevk/received_events","type":"User","site_admin":false},"created_at":"2015-02-13T15:12:24Z","updated_at":"2015-02-13T15:12:24Z","body":"Matching the service worker pattern is probably okay."}}