Skip to content

Commit

Permalink
feat(core): create event manager
Browse files Browse the repository at this point in the history
  • Loading branch information
agviegas committed Oct 17, 2024
1 parent 1e98323 commit 4bf8ff2
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@thatopen/components",
"description": "Collection of core functionalities to author BIM apps.",
"version": "2.4.0-alpha.2",
"version": "2.4.0-alpha.3",
"author": "That Open Company",
"contributors": [
"Antonio Gonzalez Viegas (https://github.com/agviegas)",
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/core/Types/src/async-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
* Simple event handler by [Jason Kleban](https://gist.github.com/JasonKleban/50cee44960c225ac1993c922563aa540). Keep in mind that if you want to remove it later, you might want to declare the callback as an object. If you want to maintain the reference to `this`, you will need to declare the callback as an arrow function.
*/
export class AsyncEvent<T> {
/**
* Whether this event is active or not. If not, it won't trigger.
*/
enabled = true;

/**
* Add a callback to this event instance.
* @param handler - the callback to be added to this event.
Expand All @@ -28,6 +33,9 @@ export class AsyncEvent<T> {

/** Triggers all the callbacks assigned to this event. */
trigger = async (data?: T) => {
if (!this.enabled) {
return;
}
const handlers = this.handlers.slice(0);
for (const handler of handlers) {
await handler(data as any);
Expand Down
50 changes: 50 additions & 0 deletions packages/core/src/core/Types/src/event-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Event } from "./event";
import { AsyncEvent } from "./async-event";

/**
* Simple class to easily toggle and reset event lists.
*/
export class EventManager {
/**
* The list of events managed by this instance.
*/
list = new Set<Event<any> | AsyncEvent<any>>();

/**
* Adds events to this manager.
* @param events the events to add.
*/
add(events: Iterable<Event<any> | AsyncEvent<any>>) {
for (const event of events) {
this.list.add(event);
}
}
/**
* Removes events from this manager.
* @param events the events to remove.
*/
remove(events: Iterable<Event<any> | AsyncEvent<any>>) {
for (const event of events) {
this.list.delete(event);
}
}

/**
* Sets all the events managed by this instance as enabled or disabled.
* @param active whether to turn on or off the events.
*/
set(active: boolean) {
for (const event of this.list) {
event.enabled = active;
}
}

/**
* Resets all the events managed by this instance.
*/
reset() {
for (const event of this.list) {
event.reset();
}
}
}
8 changes: 8 additions & 0 deletions packages/core/src/core/Types/src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
* Simple event handler by [Jason Kleban](https://gist.github.com/JasonKleban/50cee44960c225ac1993c922563aa540). Keep in mind that if you want to remove it later, you might want to declare the callback as an object. If you want to maintain the reference to `this`, you will need to declare the callback as an arrow function.
*/
export class Event<T> {
/**
* Whether this event is active or not. If not, it won't trigger.
*/
enabled = true;

/**
* Add a callback to this event instance.
* @param handler - the callback to be added to this event.
Expand All @@ -20,6 +25,9 @@ export class Event<T> {

/** Triggers all the callbacks assigned to this event. */
trigger = (data?: T) => {
if (!this.enabled) {
return;
}
const handlers = this.handlers.slice(0);
for (const handler of handlers) {
handler(data as any);
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/core/Types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export * from "./data-set";
export * from "./data-map";
export * from "./component-with-ui";
export * from "./config-types";
export * from "./event-manager";
11 changes: 11 additions & 0 deletions packages/core/src/core/Types/src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as THREE from "three";
import CameraControls from "camera-controls";
import { Event } from "./event";
import { EventManager } from "./event-manager";

/**
* Whether this component has to be manually destroyed once you are done with it to prevent [memory leaks](https://threejs.org/docs/#manual/en/introduction/How-to-dispose-of-objects). This also ensures that the DOM events created by that component will be cleaned up.
Expand Down Expand Up @@ -125,3 +126,13 @@ export interface CameraControllable {
*/
controls: CameraControls;
}

/**
* Whether it has events or not.
*/
export interface Eventable {
/**
* The object in charge of managing all the events.
*/
eventManager: EventManager;
}

0 comments on commit 4bf8ff2

Please sign in to comment.