Skip to content

POC: Broadcast events #19374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
import type { UmbEntityActionEvent } from '@umbraco-cms/backoffice/entity-action';
import { UmbContextBase } from '@umbraco-cms/backoffice/class-api';
import { UmbContextToken } from '@umbraco-cms/backoffice/context-api';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { Subject } from '@umbraco-cms/backoffice/external/rxjs';

export class UmbActionEventContext extends UmbContextBase {
/* It is not possible to add native event listeners to all events on a event target.
This is a workaround to be able to listen to all events.
It could potentially replace the need for the native events if we want to. */
public readonly events = new Subject<Event>();

constructor(host: UmbControllerHost) {
super(host, UMB_ACTION_EVENT_CONTEXT);
}

/* Override the native dispatchEvent method to also push the event to the subject */
override dispatchEvent(event: UmbEntityActionEvent): boolean {
this.events.next(event);
return super.dispatchEvent(event);
}

// TODO: revisit this. This is currently used to prevent a
public localDispatchEvent(event: UmbEntityActionEvent) {
super.dispatchEvent(event);
}
}

export const UMB_ACTION_EVENT_CONTEXT = new UmbContextToken<UmbActionEventContext, UmbActionEventContext>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ export class UmbEntityActionEvent<
getEventUnique(): string | undefined {
return this._args.eventUnique;
}

toObject(): ArgsType {
return {
...this._args,
type: this.type,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { UmbEntityActionEventArgs } from './entity-action.event.js';
import { UmbEntityActionEvent } from './entity-action.event.js';

interface UmbGenericEntityActionEventArgs extends UmbEntityActionEventArgs {
type: string;
}

export class UmbEntityEvent extends UmbEntityActionEvent {
constructor(args: UmbGenericEntityActionEventArgs) {
super(args.type, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { UmbEntityActionEvent } from '../../entity-action.event.js';
import { UmbEntityEvent } from '../../entity.event.js';
import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import { UMB_ACTION_EVENT_CONTEXT } from '@umbraco-cms/backoffice/action';

export class UmbEntityActionEventBroadcastManager extends UmbControllerBase {
#broadcastChannel = new BroadcastChannel('umbraco-backoffice-entity-action-event');
#actionEventContext?: typeof UMB_ACTION_EVENT_CONTEXT.TYPE;

constructor(host: UmbControllerHost) {
super(host);

this.consumeContext(UMB_ACTION_EVENT_CONTEXT, (context) => {
this.#actionEventContext = context;

// Broadcast all events from the action event context to the broadcast channel
this.observe(this.#actionEventContext?.events, (event) => {
if (event instanceof UmbEntityActionEvent) {
this.#broadcastEvent(event);
}
});
});

this.#broadcastChannel.onmessage = (event: MessageEvent) => {
/* We know when the event is from this channel the data will include
all the args for a UmbEntityEvent */
const entityEvent = new UmbEntityEvent(event.data);
this.#actionEventContext?.localDispatchEvent(entityEvent);
};
}

#broadcastEvent(event: UmbEntityActionEvent) {
const eventObject = event.toObject();
this.#broadcastChannel.postMessage(eventObject);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './entity-action-event-broadcast.manager.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './broadcast/index.js';
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export * from './entity-action.event.js';
export * from './has-children/index.js';
export * from './entity-updated.event.js';
export * from './entity-deleted.event.js';
export * from './entity.event.js';
export * from './event/index.js';

export type * from './types.js';

Expand Down
3 changes: 3 additions & 0 deletions src/Umbraco.Web.UI.Client/src/packages/core/entry-point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { UMB_AUTH_CONTEXT } from './auth/auth.context.token.js';
import { UmbBackofficeNotificationContainerElement, UmbBackofficeModalContainerElement } from './components/index.js';
import { UmbActionEventContext } from './action/action-event.context.js';
import { manifests as coreManifests } from './manifests.js';
import { UmbEntityActionEventBroadcastManager } from './entity-action/index.js';
import { UmbNotificationContext } from '@umbraco-cms/backoffice/notification';
import { UmbModalManagerContext } from '@umbraco-cms/backoffice/modal';
import { UmbExtensionsApiInitializer, type UmbEntryPointOnInit } from '@umbraco-cms/backoffice/extension-api';
Expand All @@ -17,6 +18,8 @@ export const onInit: UmbEntryPointOnInit = (host, extensionRegistry) => {
new UmbExtensionsApiInitializer(host, extensionRegistry, 'treeStore', [host]);
new UmbExtensionsApiInitializer(host, extensionRegistry, 'itemStore', [host]);

new UmbEntityActionEventBroadcastManager(host);

extensionRegistry.registerMany(coreManifests);

const notificationContainerElement = new UmbBackofficeNotificationContainerElement();
Expand Down
Loading