Skip to content

Commit

Permalink
experiment with a public stable browser API that extensions can use
Browse files Browse the repository at this point in the history
IDK yet what it will look like, just trying things out
  • Loading branch information
ornicar committed Oct 30, 2024
1 parent 77853e7 commit e1d638a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
15 changes: 15 additions & 0 deletions ui/site/src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Pubsub, PubsubCallback, PubsubEvent } from 'common/pubsub';

const publicEvents = new Set<PubsubEvent>(['content-loaded', 'zen']);

export const api = (ps: Pubsub) => ({
pubsub: {
on(name: PubsubEvent, cb: PubsubCallback): void {
if (!publicEvents.has(name)) throw 'This event is not part of the public API';
ps.on(name, cb);
},
off(name: PubsubEvent, cb: PubsubCallback): void {
ps.off(name, cb);
},
},
});
3 changes: 2 additions & 1 deletion ui/site/src/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import { unload, redirect, reload } from './reload';
import announce from './announce';
import { displayLocale } from 'common/i18n';
import sound from './sound';
import { api } from './api';
import { pubsub } from 'common/pubsub';

const site = window.site;
// site.load is initialized in layout.scala embedded script tags
// site.manifest is fetched
// site.info, site.debug are populated by ui/build
// site.socket, site.quietMode, site.analysis are set elsewhere
(site as any).pubsub = pubsub; // do not declare in index.d.ts. some extensions need this here
(site as any).api = api(pubsub); // do not declare in index.d.ts. some extensions need this here
site.sri = randomToken();
site.displayLocale = displayLocale;
site.blindMode = document.body.classList.contains('blind-mode');
Expand Down

0 comments on commit e1d638a

Please sign in to comment.