Skip to content

Commit

Permalink
fix(core/pubsubhub): account for all message types (#3908)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoscaceres authored Dec 18, 2021
1 parent f875034 commit d42cf98
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/core/dfn-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ const CODE_TYPES = new Set([
export function run() {
const index = document.querySelector("section#index");
if (!index) {
// See below...
sub("toc", () => {}, { once: true });
return;
}

Expand Down
3 changes: 0 additions & 3 deletions src/core/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import { addId, getIntlData } from "./utils.js";
import css from "../styles/examples.css.js";
import { html } from "./import-maps.js";
import { pub } from "./pubsubhub.js";

export const name = "core/examples";

Expand Down Expand Up @@ -93,7 +92,6 @@ export function run() {
const id = addId(example, "example", title || String(number));
const selfLink = div.querySelector("a.self-link");
selfLink.href = `#${id}`;
pub("example", report);
} else {
const inAside = !!example.closest("aside");
if (!inAside) ++number;
Expand All @@ -113,7 +111,6 @@ export function run() {
const selfLink = div.querySelector("a.self-link");
selfLink.href = `#${div.id}`;
example.replaceWith(div);
if (!inAside) pub("example", report);
}
});
}
3 changes: 0 additions & 3 deletions src/core/issues-notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import {
} from "./utils.js";
import css from "../styles/issues-notes.css.js";
import { html } from "./import-maps.js";
import { pub } from "./pubsubhub.js";

export const name = "core/issues-notes";

const localizationStrings = {
Expand Down Expand Up @@ -186,7 +184,6 @@ function handleIssues(ins, ghIssues, conf) {
const level = parents(titleParent, "section").length + 2;
titleParent.setAttribute("aria-level", level);
}
pub(report.type, report);
});
makeIssueSectionSummary(issueList);
}
Expand Down
10 changes: 7 additions & 3 deletions src/core/pubsubhub.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ export const name = "core/pubsubhub";

const subscriptions = new Map();

/**
*
* @param {EventTopic} topic
* @param {...any} data
*/
export function pub(topic, ...data) {
if (!subscriptions.has(topic)) {
return; // Nothing to do...
throw new Error(`No subscribers for topic "${topic}".`);
}
Array.from(subscriptions.get(topic)).forEach(cb => {
try {
Expand All @@ -37,8 +42,7 @@ export function pub(topic, ...data) {
}
/**
* Subscribes to a message type.
*
* @param {string} topic The topic to subscribe to (e.g., "start-all")
* @param {EventTopic} topic The topic to subscribe to
* @param {Function} cb Callback function
* @param {Object} [opts]
* @param {Boolean} [opts.once] Add prop "once" for single notification.
Expand Down
15 changes: 13 additions & 2 deletions src/type-helper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ type LicenseInfo = {
* The short linking text of license.
*/
short: string;
}
};

type ResourceHintOption = {
/**
Expand Down Expand Up @@ -223,6 +223,16 @@ type PersonExtras = {
href?: string;
};

type EventTopic =
| "amend-user-config"
| "beforesave"
| "end-all"
| "error"
| "plugins-done"
| "start-all"
| "toc"
| "warn";

type DefinitionValidator = (
/** Text to validate. */
text: string,
Expand All @@ -231,4 +241,5 @@ type DefinitionValidator = (
/** The element from which the validation originated. */
element: HTMLElement,
/** The name of the plugin originating the validation. */
pluginName: string) => boolean;
pluginName: string
) => boolean;
7 changes: 7 additions & 0 deletions tests/spec/core/validators-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
validateMimeType,
validateQuotedString,
} from "../../../src/core/dfn-validators.js";
import { sub } from "../../../src/core/pubsubhub.js";

describe("Core - Validators", () => {
describe("validateDOMName", () => {
Expand All @@ -30,6 +31,7 @@ describe("Core - Validators", () => {
it("generates an error if the element name is not valid", () => {
const elements = ["my element", "crypto$", "🪳", "-something", ""];
for (const element of elements) {
sub("error", () => {}, { once: true });
const dfn = document.createElement("dfn");
const context = `element name: ${element}`;
expect(validateDOMName(element, "element", dfn, "foo/bar"))
Expand Down Expand Up @@ -58,6 +60,7 @@ describe("Core - Validators", () => {
it("generates an error if the attribute name is invalid", () => {
const attributes = ["-crossorigin", "-whatever-", "aria-😇"];
for (const attribute of attributes) {
sub("error", () => {}, { once: true });
const context = `attribute name: ${attribute}`;
const dfn = document.createElement("dfn");
expect(validateDOMName(attribute, "attribute", dfn, "foo/bar"))
Expand All @@ -79,6 +82,7 @@ describe("Core - Validators", () => {
"text/plain",
];
for (const mimeType of mimeTypes) {
sub("error", () => {}, { once: true });
const dfn = document.createElement("dfn");
const context = `mimeType: ${mimeType}`;
expect(validateMimeType(mimeType, "mimetype", dfn, "foo/bar"))
Expand Down Expand Up @@ -133,6 +137,7 @@ describe("Core - Validators", () => {
"-something",
];
for (const name of names) {
sub("error", () => {}, { once: true });
const dfn = document.createElement("dfn");
const context = `invalid name: ${name}`;
expect(validateQuotedString(name, "permission", dfn, "foo/bar"))
Expand All @@ -149,6 +154,7 @@ describe("Core - Validators", () => {
it("generates no error if the name is valid", () => {
const names = ["foo", "bar", "baz", "quux"];
for (const name of names) {
sub("error", () => {}, { once: true });
const dfn = document.createElement("dfn");
const context = `name: ${name}`;
expect(validateCommonName(name, "event", dfn, "foo/bar"))
Expand All @@ -163,6 +169,7 @@ describe("Core - Validators", () => {
it("it generates an error if the name is not valid", () => {
const names = ["my event", "crypto$", "🪳", "-something"];
for (const name of names) {
sub("error", () => {}, { once: true });
const dfn = document.createElement("dfn");
const context = `invalid name: ${name}`;
expect(validateCommonName(name, "event", dfn, "foo/bar"))
Expand Down
10 changes: 7 additions & 3 deletions tests/unit/SpecHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ export function makePluginDoc(
plugins,
{ config = {}, head = `<meta charset="UTF-8" />`, body = "" } = {}
) {
plugins = [
"/src/core/base-runner.js",
"/src/core/ui.js", // Needed for "start-all" event
"/src/core/dfn.js", // Needed for "plugins-done" event,
...plugins,
];
return getDoc(`
<!DOCTYPE html>
<html lang="en">
Expand All @@ -24,9 +30,7 @@ export function makePluginDoc(
</script>
<script type="module">
async function run(plugins) {
const allPlugins = ["/src/core/base-runner.js"]
.concat(plugins)
.map(p => "/base" + p);
const allPlugins = plugins.map(p => "/base" + p);
try {
const [baseRunner, ...plugs] = await Promise.all(
allPlugins.map(plug => import(plug))
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/core/show-people-spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { html } from "../../../src/core/import-maps.js";
import showPeople from "../../../src/core/templates/show-people.js";
import { sub } from "../../../src/core/pubsubhub.js";

describe("Core - Templates - Show People", () => {
describe("showPeople", () => {
it("handles empty person", () => {
sub("error", () => {}, { once: true });
const render = html.bind(document.createDocumentFragment());
const person = {};
const result = render`${showPeople({ people: [person] }, "people")}`;
Expand Down Expand Up @@ -96,6 +98,7 @@ describe("Core - Templates - Show People", () => {
});

it("identifies valid and invalid ORCIDs", async () => {
sub("error", () => {}, { once: true });
const people = [
{
name: "Valid 1",
Expand Down Expand Up @@ -136,6 +139,7 @@ describe("Core - Templates - Show People", () => {
});

it("ignores companyURL when company is missing", () => {
sub("warn", () => {}, { once: true });
const render = html.bind(document.createDocumentFragment());
const person = {
name: "name",
Expand Down Expand Up @@ -177,6 +181,7 @@ describe("Core - Templates - Show People", () => {
});

it("filters out editors with invalid extras", () => {
sub("error", () => {}, { once: true });
const render = html.bind(document.createDocumentFragment());
const people = [
{ name: "wrong type", extras: "" },
Expand Down

0 comments on commit d42cf98

Please sign in to comment.