Bug 1350613 - Don't repeatedly create DateTimeFormat instances for the exact same set of options, but instead use cached formatters. r?markh draft
authorJeff Walden <jwalden@mit.edu>
Fri, 14 Apr 2017 13:29:02 -0700
changeset 563761 d606cd4e928a4c6dbc6ee701b559303c8d2a55f5
parent 563757 2b6a66a98e253ba158f3960f1c68ad49b2ebcdb4
child 624573 725b31c5b29d5c3110b201e7d690059e5e11d8f5
push id54416
push userbmo:eoger@fastmail.com
push dateMon, 17 Apr 2017 19:41:10 +0000
reviewersmarkh
bugs1350613
milestone55.0a1
Bug 1350613 - Don't repeatedly create DateTimeFormat instances for the exact same set of options, but instead use cached formatters. r?markh MozReview-Commit-ID: 6FteRerCH2x
browser/base/content/browser-syncui.js
browser/components/customizableui/test/browser_synced_tabs_menu.js
browser/components/syncedtabs/test/browser/browser_sidebar_syncedtabslist.js
browser/components/syncedtabs/test/xpcshell/test_SyncedTabsListStore.js
browser/modules/test/browser/browser_BrowserUITelemetry_syncedtabs.js
--- a/browser/base/content/browser-syncui.js
+++ b/browser/base/content/browser-syncui.js
@@ -34,16 +34,18 @@ var gSyncUI = {
          "weave:engine:sync:finish"
   ],
 
   _unloaded: false,
   // The last sync start time. Used to calculate the leftover animation time
   // once syncing completes (bug 1239042).
   _syncStartTime: 0,
   _syncAnimationTimer: 0,
+  _withinLastWeekFormat: null,
+  _oneWeekOrOlderFormat: null,
 
   init() {
     // Proceed to set up the UI if Sync has already started up.
     // Otherwise we'll do it when Sync is firing up.
     if (this.weaveService.ready) {
       this.initUI();
       return;
     }
@@ -362,31 +364,41 @@ var gSyncUI = {
       if (tooltiptext) {
         broadcaster.setAttribute("tooltiptext", tooltiptext);
       } else {
         broadcaster.removeAttribute("tooltiptext");
       }
     }
   }),
 
+  getWithinLastWeekFormat() {
+    return this._withinLastWeekFormat ||
+           (this._withinLastWeekFormat =
+             new Intl.DateTimeFormat(undefined, {weekday: "long", hour: "numeric", minute: "numeric"}));
+  },
+
+  getOneWeekOrOlderFormat() {
+    return this._oneWeekOrOlderFormat ||
+           (this._oneWeekOrOlderFormat =
+             new Intl.DateTimeFormat(undefined, {month: "long", day: "numeric"}));
+  },
+
   formatLastSyncDate(date) {
-    let dateFormat;
     let sixDaysAgo = (() => {
       let tempDate = new Date();
       tempDate.setDate(tempDate.getDate() - 6);
       tempDate.setHours(0, 0, 0, 0);
       return tempDate;
     })();
-    // It may be confusing for the user to see "Last Sync: Monday" when the last sync was a indeed a Monday but 3 weeks ago
-    if (date < sixDaysAgo) {
-      dateFormat = {month: "long", day: "numeric"};
-    } else {
-      dateFormat = {weekday: "long", hour: "numeric", minute: "numeric"};
-    }
-    let lastSyncDateString = date.toLocaleDateString(undefined, dateFormat);
+
+    // It may be confusing for the user to see "Last Sync: Monday" when the last
+    // sync was indeed a Monday, but 3 weeks ago.
+    let dateFormat = date < sixDaysAgo ? this.getOneWeekOrOlderFormat() : this.getWithinLastWeekFormat();
+
+    let lastSyncDateString = dateFormat.format(date);
     return this._stringBundle.formatStringFromName("lastSync2.label", [lastSyncDateString], 1);
   },
 
   onClientsSynced() {
     let broadcaster = document.getElementById("sync-syncnow-state");
     if (broadcaster) {
       if (Weave.Service.clientsEngine.stats.numClients > 1) {
         broadcaster.setAttribute("devices-status", "multi");
--- a/browser/components/customizableui/test/browser_synced_tabs_menu.js
+++ b/browser/components/customizableui/test/browser_synced_tabs_menu.js
@@ -249,22 +249,24 @@ add_task(function* () {
   // Tell the widget there are tabs available - we have 3 clients, one with no
   // tabs.
   mockedInternal.getTabClients = () => {
     return Promise.resolve([
       {
         id: "guid_mobile",
         type: "client",
         name: "My Phone",
+        lastModified: 1492201200,
         tabs: [],
       },
       {
         id: "guid_desktop",
         type: "client",
         name: "My Desktop",
+        lastModified: 1492201200,
         tabs: [
           {
             title: "http://example.com/10",
             lastUsed: 10, // the most recent
           },
           {
             title: "http://example.com/1",
             lastUsed: 1, // the least recent.
@@ -273,16 +275,17 @@ add_task(function* () {
             title: "http://example.com/5",
             lastUsed: 5,
           },
         ],
       },
       {
         id: "guid_second_desktop",
         name: "My Other Desktop",
+        lastModified: 1492201200,
         tabs: [
           {
             title: "http://example.com/6",
             lastUsed: 6,
           }
         ],
       },
     ]);
@@ -346,16 +349,17 @@ add_task(function* () {
 // Test the pagination capabilities (Show More/All tabs)
 add_task(function* () {
   mockedInternal.getTabClients = () => {
     return Promise.resolve([
       {
         id: "guid_desktop",
         type: "client",
         name: "My Desktop",
+        lastModified: 1492201200,
         tabs: function() {
           let allTabsDesktop = [];
           // We choose 77 tabs, because TABS_PER_PAGE is 25, which means
           // on the second to last page we should have 22 items shown
           // (because we have to show at least NEXT_PAGE_MIN_TABS=5 tabs on the last page)
           for (let i = 1; i <= 77; i++) {
             allTabsDesktop.push({ title: "Tab #" + i });
           }
--- a/browser/components/syncedtabs/test/browser/browser_sidebar_syncedtabslist.js
+++ b/browser/components/syncedtabs/test/browser/browser_sidebar_syncedtabslist.js
@@ -1,30 +1,32 @@
 "use strict";
 
 const FIXTURE = [
   {
     "id": "7cqCr77ptzX3",
     "type": "client",
+    "lastModified": 1492201200,
     "name": "zcarter's Nightly on MacBook-Pro-25",
     "isMobile": false,
     "tabs": [
       {
         "type": "tab",
         "title": "Firefox for Android — Mobile Web browser — More ways to customize and protect your privacy — Mozilla",
         "url": "https://www.mozilla.org/en-US/firefox/android/?utm_source=firefox-browser&utm_medium=firefox-browser&utm_campaign=synced-tabs-sidebar",
         "icon": "chrome://mozapps/skin/places/defaultFavicon.png",
         "client": "7cqCr77ptzX3",
         "lastUsed": 1452124677
       }
     ]
   },
   {
     "id": "2xU5h-4bkWqA",
     "type": "client",
+    "lastModified": 1492201200,
     "name": "laptop",
     "isMobile": false,
     "tabs": [
       {
         "type": "tab",
         "title": "Firefox for iOS — Mobile Web browser for your iPhone, iPad and iPod touch — Mozilla",
         "url": "https://www.mozilla.org/en-US/firefox/ios/?utm_source=firefox-browser&utm_medium=firefox-browser&utm_campaign=synced-tabs-sidebar",
         "icon": "moz-anno:favicon:https://www.mozilla.org/media/img/firefox/favicon.dc6635050bf5.ico",
@@ -48,16 +50,17 @@ const FIXTURE = [
         "client": "2xU5h-4bkWqA",
         "lastUsed": 1451519725
       }
     ]
   },
   {
     "id": "OL3EJCsdb2JD",
     "type": "client",
+    "lastModified": 1492201200,
     "name": "desktop",
     "isMobile": false,
     "tabs": []
   }
 ];
 
 let originalSyncedTabsInternal = null;
 
--- a/browser/components/syncedtabs/test/xpcshell/test_SyncedTabsListStore.js
+++ b/browser/components/syncedtabs/test/xpcshell/test_SyncedTabsListStore.js
@@ -2,16 +2,17 @@
 
 let { SyncedTabs } = Cu.import("resource://services-sync/SyncedTabs.jsm", {});
 let { SyncedTabsListStore } = Cu.import("resource:///modules/syncedtabs/SyncedTabsListStore.js", {});
 
 const FIXTURE = [
   {
     "id": "2xU5h-4bkWqA",
     "type": "client",
+    "lastModified": 1492201200,
     "name": "laptop",
     "isMobile": false,
     "tabs": [
       {
         "type": "tab",
         "title": "Firefox for iOS — Mobile Web browser for your iPhone, iPad and iPod touch — Mozilla",
         "url": "https://www.mozilla.org/en-US/firefox/ios/?utm_source=firefox-browser&utm_medium=firefox-browser&utm_campaign=synced-tabs-sidebar",
         "icon": "moz-anno:favicon:https://www.mozilla.org/media/img/firefox/favicon.dc6635050bf5.ico",
@@ -26,16 +27,17 @@ const FIXTURE = [
         "client": "2xU5h-4bkWqA",
         "lastUsed": 1451519420
       }
     ]
   },
   {
     "id": "OL3EJCsdb2JD",
     "type": "client",
+    "lastModified": 1492201200,
     "name": "desktop",
     "isMobile": false,
     "tabs": []
   }
 ];
 
 add_task(function* testGetDataEmpty() {
   let store = new SyncedTabsListStore(SyncedTabs);
--- a/browser/modules/test/browser/browser_BrowserUITelemetry_syncedtabs.js
+++ b/browser/modules/test/browser/browser_BrowserUITelemetry_syncedtabs.js
@@ -9,16 +9,17 @@ function mockSyncedTabs() {
   let mockedInternal = {
     get isConfiguredToSyncTabs() { return true; },
     getTabClients() {
       return Promise.resolve([
         {
           id: "guid_desktop",
           type: "client",
           name: "My Desktop",
+          lastModified: 1492201200,
           tabs: [
             {
               title: "http://example.com/10",
               lastUsed: 10, // the most recent
             },
           ],
         }
       ]);