Bug 1868676 - Part 1: Allow snapshots of multiple contexts. r=barret,omc-reviewers,aminomancer

`ASRouterTargeting` does _not_ include the experiment details that
supply `activeExperiments`, `activeRollouts`, etc.  Those are combined
from additional contexts.  This patch makes it easy to do that
combination.

N.b.: the use of `Proxy` instances makes iteration tricky, so we avoid
it entirely.

Differential Revision: https://phabricator.services.mozilla.com/D200124
This commit is contained in:
Nick Alexander
2024-02-14 21:31:19 +00:00
parent fea4b15c7e
commit 8de77add68
5 changed files with 67 additions and 13 deletions

View File

@@ -1061,11 +1061,17 @@ export const ASRouterTargeting = {
* Asynchronous getters are handled. Getters that throw or reject
* are ignored.
*
* @param {object} target - the environment to snapshot.
* @return {object} snapshot of target with `environment` object and `version`
* integer.
* Leftward (earlier) targets supercede rightward (later) targets, just like
* `TargetingContext.combineContexts`.
*
* @param {object} options - object containing:
* @param {Array<object>|null} options.targets -
* targeting environments to snapshot; (default: `[ASRouterTargeting.Environment]`)
* @return {object} snapshot of target with `environment` object and `version` integer.
*/
async getEnvironmentSnapshot(target = ASRouterTargeting.Environment) {
async getEnvironmentSnapshot({
targets = [ASRouterTargeting.Environment],
} = {}) {
async function resolve(object) {
if (typeof object === "object" && object !== null) {
if (Array.isArray(object)) {
@@ -1105,7 +1111,13 @@ export const ASRouterTargeting = {
return object;
}
const environment = await resolve(target);
// We would like to use `TargetingContext.combineContexts`, but `Proxy`
// instances complicate iterating with `Object.keys`. Instead, merge by
// hand after resolving.
const environment = {};
for (let target of targets.toReversed()) {
Object.assign(environment, await resolve(target));
}
// Should we need to migrate in the future.
const snapshot = { environment, version: 1 };