Skip to content

Commit 8a1c0b3

Browse files
Add code documentation to wpt-flags.js (#2432)
1 parent 31146ca commit 8a1c0b3

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

webapp/components/wpt-flags.js

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,28 @@
44
* found in the LICENSE file.
55
*/
66

7-
/*
8-
`<wpt-flags>` is a component for checking wpt.fyi feature flags.
9-
*/
7+
/**
8+
* wpt-flags.js defines components for checking wpt.fyi feature flags, which
9+
* are boolean switches primarily used to enable or disable features.
10+
*
11+
* Feature flags in wpt.fyi use two different layers of storage. Firstly, the
12+
* default value for the flag (if any) is recorded in AppEngine DataStore and
13+
* provided to the frontend via the `WPTEnvironmentFlags` dynamic component. If
14+
* no default exists, it is considered to be false. This layer is often
15+
* referred to as 'admin flags', and can be modified from the wpt.fyi UI by
16+
* users with the relevant permissions.
17+
*
18+
* The other layer of storage for feature flags is the browser's localStorage,
19+
* which is used to let users override the default value. Again by default (and
20+
* assuming no underlying admin value) a feature flag is assumed to be false if
21+
* it has no value.
22+
*
23+
* Feature flags are split into client-side features, which only impact the
24+
* wpt.fyi UI, and server-side features, which affect the backend too.
25+
* Server-side features only care about the backing datastore storage layer,
26+
* and do not interact with localStorage.
27+
*/
28+
1029
import '../node_modules/@polymer/paper-checkbox/paper-checkbox.js';
1130
import '../node_modules/@polymer/paper-item/paper-item.js';
1231
import { html, PolymerElement } from '../node_modules/@polymer/polymer/polymer-element.js';
@@ -84,6 +103,9 @@ const makeFeatureProperties = function(target, features, readOnly, useLocalStora
84103
}
85104
};
86105

106+
// FlagsClass defines a shared superclass for reading feature flags. It assumes
107+
// that it will be part of a custom element class chain, as it relies on
108+
// Polymer's 'properties' concept to expose the feature flag values.
87109
wpt.FlagsClass = (superClass, readOnly, useLocalStorage) => class extends superClass {
88110
static get is() {
89111
return 'wpt-flags';
@@ -108,8 +130,24 @@ wpt.FlagsClass = (superClass, readOnly, useLocalStorage) => class extends superC
108130
}
109131
};
110132

133+
// WPTFlags is a 'reader' class function for feature flags. To use it, a custom
134+
// element should include WPTFlags in its extension chain and then access flag
135+
// values via 'this', e.g.:
136+
//
137+
// class MyCustomElement extends WPTFlags(PolymerElement) {
138+
// foo() {
139+
// const featureEnabled = this.myFeatureFlag;
140+
// ...
141+
// }
142+
// }
111143
const WPTFlags = (superClass) => wpt.FlagsClass(superClass, /*readOnly*/ true, /*useLocalStorage*/ true);
112144

145+
// FlagsEditorClass is a 'writer' class function for feature flags. It allows
146+
// both reading values (identically to WPTFlags) and writing to them.
147+
//
148+
// The environmentFlags argument controls whether the class will read/write
149+
// from localStorage (if environmentFlags is false) or the backing datastore
150+
// (if environmentFlags is true).
113151
const FlagsEditorClass = (environmentFlags) =>
114152
class extends wpt.FlagsClass(PolymerElement, /*readOnly*/ false, /*useLocalStorage*/ !environmentFlags) {
115153
ready() {
@@ -155,6 +193,9 @@ const FlagsEditorClass = (environmentFlags) =>
155193
}
156194
};
157195

196+
// WPTFlagsEditor is a Polymer custom element for modifying client-side feature
197+
// flags. It presents a set of checkboxes that the user can select/unselect to
198+
// override the feature flag value at the localStorage layer.
158199
class WPTFlagsEditor extends FlagsEditorClass(/*environmentFlags*/ false) {
159200
static get template() {
160201
return html`
@@ -283,6 +324,10 @@ class WPTFlagsEditor extends FlagsEditorClass(/*environmentFlags*/ false) {
283324
}
284325
window.customElements.define(WPTFlagsEditor.is, WPTFlagsEditor);
285326

327+
// WPTEnvironmentFlagsEditor is a Polymer custom element for modifying the
328+
// default values for both client-side and server-side feature flags. It
329+
// presents a set of checkboxes that an authorized user can select/unselect to
330+
// override the feature flag value at the datastore layer.
286331
class WPTEnvironmentFlagsEditor extends FlagsEditorClass(/*environmentFlags*/ true) {
287332
static get template() {
288333
return html`

0 commit comments

Comments
 (0)