Skip to content

Commit

Permalink
Add general purpose notification API for embed
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Nov 25, 2024
1 parent 93deb13 commit 92e18a3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
- Added `Model#legacyPrepare` readonly property to indicate whether the legacy `prepare` method is used or the new `setup()` method
- Updated `struct` view to define the `setStructViewAnnotations` action when using the new `setup()` method, allowing custom annotations to be specified across all `struct` views
- Updated `embed` option of `App` to accept a config
- Added `EmbedApp#publicApi.notify(name, details)` method for sending notifications
- Introduced `onNotify(name, details)` option in the embed extension to define a callback for handling `notification` messages from the embed host
- Added `EmbedApp#publicApi.setLocationSync()` method to simplify sync between the embed app and the host location, preventing potential pitfalls
- Added `ViewModel#enforceScheduledRenders()` to immediately execute scheduled renders
- Changed `ViewModel#scheduleRender()` to use `setTimeout()` instead of `Promise.resolve()` to ensure proper processing of event loop tasks, eliminating unnecessary renders
Expand Down
12 changes: 12 additions & 0 deletions src/extensions/embed-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { loadDataFromPush, loadDataFromStream } from '../core/utils/load-data.js
export type EmbedClientOptions = {
hostId: string;
postponeMessages: EmbedHostToClientPostponeMessage[];
onNotify: (name: string, details: any) => void;
};

export type LoadDataChunkedStatus = LoadDataFromPush & {
Expand Down Expand Up @@ -44,6 +45,9 @@ function createNavItemConfig(rawConfig: unknown, sendMessage: SendMessage, rawCo
function setup(options?: Partial<EmbedClientOptions>) {
const hostId = options?.hostId || randomId();
const postponeMessages = options?.postponeMessages;
const onNotify = typeof options?.onNotify === 'function'
? options.onNotify
: () => {};

return function(host: ViewModel) {
let loadChunkedDataStatus: LoadDataChunkedStatus | null = null;
Expand Down Expand Up @@ -87,6 +91,14 @@ function setup(options?: Partial<EmbedClientOptions>) {

if (id === hostId) {
switch (type) {
case 'notification': {
const { name, details } = payload;

onNotify(name, details);

break;
}

case 'defineAction': {
const name = payload;
host.action.define(name, (...args) =>
Expand Down
4 changes: 4 additions & 0 deletions src/extensions/embed-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ class EmbedApp extends BaseApp<EmbedHostToClientMessage, EmbedAppEvents> {

nav: Object.assign(nav.secondary, nav),

notify(name: string, details: any) {
app.sendMessage('notification', { name, details });
},

defineAction(name: string, fn: (...args: unknown[]) => unknown) {
app.actions.set(name, fn);
app.sendMessage('defineAction', name);
Expand Down
1 change: 1 addition & 0 deletions src/extensions/embed-message.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export type EmbedHostToClientPostponeMessage = Extract<EmbedHostToPreinitMessage

export type EmbedHostToClientMessage = CreateMessageType<{
defineAction: string;
notification: { name: string; details: any; };
setPageHash: { hash: string; replace?: boolean; };
setPage: Omit<PageState, 'hash'> & { replace?: boolean; };
setPageRef: { ref: PageRef; replace?: boolean; };
Expand Down

0 comments on commit 92e18a3

Please sign in to comment.