mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-08 02:14:43 +00:00
Bug 1471524 - Add start/end notifications on RemoteSettings.pollChanges() r=glasserc
Add start/end notifications on RemoteSettings.pollChanges() Differential Revision: https://phabricator.services.mozilla.com/D16674 --HG-- extra : moz-landing-system : lando
This commit is contained in:
parent
0c6ab3dd41
commit
51cdff0ef9
@ -150,6 +150,27 @@ And once done:
|
||||
#. Wait for Firefox to pick-up the changes for your settings key
|
||||
|
||||
|
||||
Global Notifications
|
||||
====================
|
||||
|
||||
The polling for changes process sends two notifications that observers can register to:
|
||||
|
||||
* ``remote-settings:changes-poll-start``: Polling for changes is starting. triggered either by the scheduled timer or a push broadcast.
|
||||
* ``remote-settings:changes-poll-end``: Polling for changes has ended
|
||||
|
||||
.. code-block:: javascript
|
||||
|
||||
const observer = {
|
||||
observe(aSubject, aTopic, aData) {
|
||||
Services.obs.removeObserver(this, "remote-settings:changes-poll-start");
|
||||
|
||||
const { expectedTimestamp } = JSON.parse(aData);
|
||||
console.log("Polling started", expectedTimestamp ? "from push broadcast" : "by scheduled trigger");
|
||||
},
|
||||
};
|
||||
Services.obs.addObserver(observer, "remote-settings:changes-poll-start");
|
||||
|
||||
|
||||
Advanced Options
|
||||
================
|
||||
|
||||
|
@ -168,6 +168,8 @@ function remoteSettingsFunction() {
|
||||
}
|
||||
}
|
||||
|
||||
Services.obs.notifyObservers(null, "remote-settings:changes-poll-start", JSON.stringify({ expectedTimestamp }));
|
||||
|
||||
const lastEtag = gPrefs.getCharPref(PREF_SETTINGS_LAST_ETAG, "");
|
||||
|
||||
let pollResult;
|
||||
@ -226,6 +228,7 @@ function remoteSettingsFunction() {
|
||||
// the one in the local database.
|
||||
try {
|
||||
await client.maybeSync(last_modified, { loadDump });
|
||||
|
||||
// Save last time this client was successfully synced.
|
||||
Services.prefs.setIntPref(client.lastCheckTimePref, checkedServerTimeInSeconds);
|
||||
} catch (e) {
|
||||
@ -245,7 +248,7 @@ function remoteSettingsFunction() {
|
||||
gPrefs.setCharPref(PREF_SETTINGS_LAST_ETAG, currentEtag);
|
||||
}
|
||||
|
||||
Services.obs.notifyObservers(null, "remote-settings-changes-polled");
|
||||
Services.obs.notifyObservers(null, "remote-settings:changes-poll-end");
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -60,6 +60,30 @@ function run_test() {
|
||||
|
||||
add_task(clear_state);
|
||||
|
||||
|
||||
add_task(async function test_an_event_is_sent_on_start() {
|
||||
server.registerPathHandler(CHANGES_PATH, (request, response) => {
|
||||
response.write(JSON.stringify({ data: [] }));
|
||||
response.setHeader("ETag", '"42"');
|
||||
response.setHeader("Date", (new Date()).toUTCString());
|
||||
response.setStatusLine(null, 200, "OK");
|
||||
});
|
||||
let notificationObserved = null;
|
||||
const observer = {
|
||||
observe(aSubject, aTopic, aData) {
|
||||
Services.obs.removeObserver(this, "remote-settings:changes-poll-start");
|
||||
notificationObserved = JSON.parse(aData);
|
||||
},
|
||||
};
|
||||
Services.obs.addObserver(observer, "remote-settings:changes-poll-start");
|
||||
|
||||
await RemoteSettings.pollChanges({ expectedTimestamp: 13 });
|
||||
|
||||
Assert.equal(notificationObserved.expectedTimestamp, 13, "start notification should have been observed");
|
||||
});
|
||||
add_task(clear_state);
|
||||
|
||||
|
||||
add_task(async function test_check_success() {
|
||||
const startHistogram = getUptakeTelemetrySnapshot(TELEMETRY_HISTOGRAM_KEY);
|
||||
const serverTime = 8000;
|
||||
@ -86,15 +110,15 @@ add_task(async function test_check_success() {
|
||||
let maybeSyncCalled = false;
|
||||
c.maybeSync = () => { maybeSyncCalled = true; };
|
||||
|
||||
// Ensure that the remote-settings-changes-polled notification works
|
||||
// Ensure that the remote-settings:changes-poll-end notification works
|
||||
let notificationObserved = false;
|
||||
const observer = {
|
||||
observe(aSubject, aTopic, aData) {
|
||||
Services.obs.removeObserver(this, "remote-settings-changes-polled");
|
||||
Services.obs.removeObserver(this, "remote-settings:changes-poll-end");
|
||||
notificationObserved = true;
|
||||
},
|
||||
};
|
||||
Services.obs.addObserver(observer, "remote-settings-changes-polled");
|
||||
Services.obs.addObserver(observer, "remote-settings:changes-poll-end");
|
||||
|
||||
await RemoteSettings.pollChanges();
|
||||
|
||||
@ -131,7 +155,7 @@ add_task(async function test_update_timer_interface() {
|
||||
}]));
|
||||
|
||||
await new Promise((resolve) => {
|
||||
const e = "remote-settings-changes-polled";
|
||||
const e = "remote-settings:changes-poll-end";
|
||||
const changesPolledObserver = {
|
||||
observe(aSubject, aTopic, aData) {
|
||||
Services.obs.removeObserver(this, e);
|
||||
@ -164,15 +188,15 @@ add_task(async function test_check_up_to_date() {
|
||||
|
||||
Services.prefs.setCharPref(PREF_LAST_ETAG, '"1100"');
|
||||
|
||||
// Ensure that the remote-settings-changes-polled notification is sent.
|
||||
// Ensure that the remote-settings:changes-poll-end notification is sent.
|
||||
let notificationObserved = false;
|
||||
const observer = {
|
||||
observe(aSubject, aTopic, aData) {
|
||||
Services.obs.removeObserver(this, "remote-settings-changes-polled");
|
||||
Services.obs.removeObserver(this, "remote-settings:changes-poll-end");
|
||||
notificationObserved = true;
|
||||
},
|
||||
};
|
||||
Services.obs.addObserver(observer, "remote-settings-changes-polled");
|
||||
Services.obs.addObserver(observer, "remote-settings:changes-poll-end");
|
||||
|
||||
// If server has no change, a 304 is received, maybeSync() is not called.
|
||||
let maybeSyncCalled = false;
|
||||
@ -257,6 +281,7 @@ add_task(async function test_client_last_check_is_saved() {
|
||||
});
|
||||
add_task(clear_state);
|
||||
|
||||
|
||||
add_task(async function test_success_with_partial_list() {
|
||||
function partialList(request, response) {
|
||||
const entries = [{
|
||||
@ -353,11 +378,11 @@ add_task(async function test_server_error() {
|
||||
let notificationObserved = false;
|
||||
const observer = {
|
||||
observe(aSubject, aTopic, aData) {
|
||||
Services.obs.removeObserver(this, "remote-settings-changes-polled");
|
||||
Services.obs.removeObserver(this, "remote-settings:changes-poll-end");
|
||||
notificationObserved = true;
|
||||
},
|
||||
};
|
||||
Services.obs.addObserver(observer, "remote-settings-changes-polled");
|
||||
Services.obs.addObserver(observer, "remote-settings:changes-poll-end");
|
||||
Services.prefs.setIntPref(PREF_LAST_UPDATE, 42);
|
||||
|
||||
// pollChanges() fails with adequate error and no notification.
|
||||
|
Loading…
Reference in New Issue
Block a user