Skip to content

Commit 4ee0573

Browse files
committed
Use just regular JavaScript events instead of ContextRoot
1 parent 532cbb0 commit 4ee0573

File tree

7 files changed

+75
-80
lines changed

7 files changed

+75
-80
lines changed

package-lock.json

Lines changed: 4 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
}
6363
},
6464
"dependencies": {
65-
"@floating-ui/dom": "^1.6.13",
66-
"@lit/context": "^1.1.3"
65+
"@floating-ui/dom": "^1.6.13"
6766
}
6867
}

src/application.js

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { CSSResult } from "lit";
22

3+
import {
4+
EVENT_READTHEDOCS_ADDONS_DATA_READY,
5+
EVENT_READTHEDOCS_URL_CHANGED,
6+
} from "./events";
37
import { getReadTheDocsConfig } from "./readthedocs-config";
48
import * as notification from "./notification";
59
import * as analytics from "./analytics";
@@ -24,25 +28,14 @@ import {
2428
} from "./utils";
2529

2630
import doctoolsStyleSheet from "./doctools.css";
27-
import { html, nothing, render, LitElement } from "lit";
28-
import { ContextConsumer } from "@lit/context";
29-
import { configContext } from "./context.js";
30-
31-
export class AddonsAppElement extends LitElement {
32-
static elementName = "readthedocs-application";
33-
34-
// `_config` is the context we are going to consume when it's updated.
35-
_config = new ContextConsumer(this, {
36-
context: configContext,
37-
subscribe: true,
38-
});
3931

32+
export class AddonsApplication {
4033
constructor() {
41-
super();
34+
this.config = null;
4235

4336
console.log(
44-
"Addons Application _config (from constructor() method)",
45-
this._config.value,
37+
"Addons Application config (from constructor() method)",
38+
this.config,
4639
);
4740

4841
this.addons = [
@@ -67,29 +60,29 @@ export class AddonsAppElement extends LitElement {
6760

6861
setupLogging();
6962
setupHistoryEvents();
63+
this.addDoctoolData();
7064
getReadTheDocsConfig(this.sendUrlParam()); // .then(() => console.log("Finished"));;
7165
}
7266

73-
render() {
74-
console.log(
75-
"Addons Application _config (from render() method)",
76-
this._config.value,
77-
);
67+
reload(config) {
68+
console.log("Addons Application config (from reload() method)", config);
7869

79-
if (!this._config.value) {
80-
return nothing;
70+
if (!config) {
71+
return null;
8172
}
8273

83-
if (this._config.value && !this.loadWhenEmbedded()) {
84-
return nothing;
74+
this.config = config;
75+
76+
if (this.config && !this.loadWhenEmbedded()) {
77+
return null;
8578
}
8679

8780
let promises = [];
8881
for (const addon of this.addons) {
89-
if (addon.isEnabled(this._config.value, this.httpStatus)) {
82+
if (addon.isEnabled(this.config, this.httpStatus)) {
9083
promises.push(
9184
new Promise((resolve) => {
92-
resolve(new addon(this._config.value));
85+
resolve(new addon(this.config));
9386
}),
9487
);
9588
}
@@ -99,12 +92,12 @@ export class AddonsAppElement extends LitElement {
9992
console.error(err);
10093
});
10194

102-
return nothing;
95+
return null;
10396
}
10497

10598
loadWhenEmbedded() {
10699
const loadWhenEmbedded = objectPath.get(
107-
this._config.value,
100+
this.config,
108101
"addons.options.load_when_embedded",
109102
false,
110103
);
@@ -152,5 +145,4 @@ export class AddonsAppElement extends LitElement {
152145
}
153146
}
154147

155-
customElements.define("readthedocs-application", AddonsAppElement);
156-
render(new AddonsAppElement(), document.body);
148+
export const addonsApplication = new AddonsApplication();

src/context.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/events.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { ADDONS_API_VERSION, getMetadataValue } from "./utils";
2+
import { addonsApplication } from "./application";
3+
import { getReadTheDocsConfig } from "./readthedocs-config";
24

35
export const EVENT_READTHEDOCS_SEARCH_SHOW = "readthedocs-search-show";
46
export const EVENT_READTHEDOCS_SEARCH_HIDE = "readthedocs-search-hide";
@@ -14,14 +16,23 @@ export const EVENT_READTHEDOCS_FLYOUT_HIDE = "readthedocs-flyout-hide";
1416
export const EVENT_READTHEDOCS_URL_CHANGED = "readthedocs-url-changed";
1517

1618
/**
17-
* Event triggered when the Read the Docs data is ready to be consumed.
19+
* Event triggered when the Read the Docs data is ready to be consumed by users.
1820
*
1921
* This is the event users subscribe to to make usage of Read the Docs data.
2022
* The object received is `ReadTheDocsEventData`.
2123
*/
22-
export const EVENT_READTHEDOCS_ADDONS_DATA_READY =
24+
export const EVENT_READTHEDOCS_ADDONS_USER_DATA_READY =
2325
"readthedocs-addons-data-ready";
2426

27+
/**
28+
* Event triggered when the Read the Docs data is ready to be consumed internally.
29+
*
30+
* This is the event users subscribe to to make usage of Read the Docs data.
31+
* The object received is `ReadTheDocsEventData`.
32+
*/
33+
export const EVENT_READTHEDOCS_ADDONS_INTERNAL_DATA_READY =
34+
"readthedocs-addons-internal-data-ready";
35+
2536
/**
2637
* Event triggered when any addons modifies the root DOM.
2738
*
@@ -79,3 +90,26 @@ export class ReadTheDocsEventData {
7990
return httpStatus;
8091
}
8192
}
93+
94+
/**
95+
* Subscribe to `EVENT_READTHEDOCS_ADDONS_DATA_READY` to reload all the events
96+
* with Addons API data once it's ready.
97+
*
98+
*/
99+
document.addEventListener(
100+
EVENT_READTHEDOCS_ADDONS_INTERNAL_DATA_READY,
101+
(event) => {
102+
addonsApplication.reload(event.detail.data(true));
103+
},
104+
);
105+
106+
/**
107+
* Subscribe to `EVENT_READTHEDOCS_URL_CHANGED` to trigger a new request to
108+
* Addons API to fetch fresh data.
109+
*
110+
*/
111+
window.addEventListener(EVENT_READTHEDOCS_URL_CHANGED, (event) => {
112+
console.debug("URL Change detected. Triggering a new API call", event);
113+
// TODO: find a way to get access to the `AddonApplication.sendUrlParam()` method.
114+
getReadTheDocsConfig(true);
115+
});

src/readthedocs-config.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { default as fetch } from "unfetch";
22
import {
3-
EVENT_READTHEDOCS_ADDONS_DATA_READY,
3+
EVENT_READTHEDOCS_ADDONS_USER_DATA_READY,
4+
EVENT_READTHEDOCS_ADDONS_INTERNAL_DATA_READY,
45
ReadTheDocsEventData,
56
} from "./events";
67
import {
@@ -108,6 +109,14 @@ export function getReadTheDocsConfig(sendUrlParam) {
108109
return response.json();
109110
})
110111
.then((data) => {
112+
// Trigger the addons data ready CustomEvent to with the data the user is expecting.
113+
console.log("Dispatching the event...");
114+
return dispatchEvent(
115+
EVENT_READTHEDOCS_ADDONS_INTERNAL_DATA_READY,
116+
document,
117+
new ReadTheDocsEventData(data),
118+
);
119+
111120
// Trigger a new task here to hit the API again in case the version
112121
// request missmatchs the one the user expects.
113122
getReadTheDocsUserConfig(sendUrlParam).then((dataUser) => {
@@ -116,7 +125,7 @@ export function getReadTheDocsConfig(sendUrlParam) {
116125

117126
// Trigger the addons data ready CustomEvent to with the data the user is expecting.
118127
return dispatchEvent(
119-
EVENT_READTHEDOCS_ADDONS_DATA_READY,
128+
EVENT_READTHEDOCS_ADDONS_USER_DATA_READY,
120129
document,
121130
new ReadTheDocsEventData(dataEvent),
122131
);

src/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export class AddonBase {
176176
*/
177177
export function setupHistoryEvents() {
178178
// Let's ensure that the history will be patched only once, so we create a Symbol to check by
179-
const patchKey = Symbol.for("addons_history");
179+
const patchKey = Symbol.for("addons-history");
180180

181181
if (
182182
typeof history !== "undefined" &&

0 commit comments

Comments
 (0)