Skip to content

Commit 8f084d5

Browse files
committed
working on fixing ojs rendering
1 parent 6d03e20 commit 8f084d5

4 files changed

Lines changed: 51 additions & 10 deletions

File tree

src/broker/Broker.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,24 @@ export default class Broker {
6868
}
6969
}
7070

71+
off(events, listener) {
72+
if (Array.isArray(events)) {
73+
for (let event of events) {
74+
this.off(event, listener);
75+
}
76+
77+
return;
78+
}
79+
80+
events = this.parseEvents(events);
81+
82+
for (let event of events) {
83+
event = event.trim();
84+
85+
this.#emitter.off(event, listener);
86+
}
87+
}
88+
7189
verifyEventRegistration(event) {
7290
if (
7391
this.#emitOnlyRegisteredEvents &&

src/broker/BrokerRegistrar.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Component from "../component/Component.js";
12
import { container } from "../core/Container.js";
23
import { isClass } from "../utils/helpers.js";
34

@@ -65,6 +66,12 @@ export default class BrokerRegistrar {
6566
for (let ev of events) {
6667
if (ev.length === 0) continue;
6768
container.resolve("broker").on(ev, listener.bind(object));
69+
70+
if (object instanceof Component) {
71+
object.__brokerEvents__ = object.__brokerEvents__ || {};
72+
object.__brokerEvents__[ev] = object.__brokerEvents__[ev] || [];
73+
object.__brokerEvents__[ev].push(listener);
74+
}
6875
}
6976
}
7077
}

src/component/Component.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ export default class Component {
9595
*/
9696
this.emitter = new Emitter();
9797

98+
/**
99+
* List of events that the component is listening to
100+
* from the broker
101+
*/
102+
this.__brokerEvents__ = {};
103+
98104
this.isAnonymous = false;
99105

100106
this.name = name ?? this.constructor.name;
@@ -167,7 +173,21 @@ export default class Component {
167173
}
168174

169175
const h = container.resolve("h");
170-
return h.getComponent(splitted[0]).method(splitted[1], args);
176+
let cls = h.getComponent(splitted[0]);
177+
178+
if (!cls) {
179+
console.error(`Component ${splitted[0]} not found`);
180+
return;
181+
}
182+
183+
let obj = new cls();
184+
185+
if (!obj.method) {
186+
console.error(`Method ${splitted[1]} not found in ${splitted[0]}`);
187+
return;
188+
}
189+
190+
return obj.method(splitted[1], args);
171191
}
172192

173193
/**
@@ -253,7 +273,6 @@ export default class Component {
253273
* Get all Emitters declared in the component
254274
*/
255275
getDeclaredListeners() {
256-
257276
if (this.__ojsRegistered) {
258277
console.warn(
259278
`Component "${this.name}" is already registered. Skipping duplicate registration.`
@@ -310,7 +329,7 @@ export default class Component {
310329

311330
for (let j = 0; j < events.length; j++) {
312331
let ev = events[j];
313-
332+
314333
if (!ev.length) continue;
315334

316335
h[m](cmp, ev, (component, event, ...args) => {
@@ -524,7 +543,7 @@ export default class Component {
524543

525544
/**
526545
* Ensure that the action will get called
527-
* even if the event was emitted previous
546+
* even if the event was emitted previously
528547
* @param {string} event
529548
* @param {...function} listeners
530549
*/

src/component/MarkupEngine.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ export default class MarkupEngine {
4343
/**
4444
*
4545
* @param {string} name component name
46-
* @param {Component} component OpenScript component for rendering.
47-
*
48-
*
49-
* @return {HTMLElement|Array<HTMLElement|String>}
46+
* @param {class<Component>} component OpenScript component class.
5047
*/
5148
this.component = (name, component) => {
5249
if (!(typeof name === "string")) {
@@ -55,9 +52,9 @@ export default class MarkupEngine {
5552
);
5653
}
5754

58-
if (!(component instanceof Component)) {
55+
if (!(component.prototype instanceof Component)) {
5956
throw new Error(
60-
`MarkupEngine.Exception: The component for ${name} must be an Component component. ${component.constructor.name} given`
57+
`MarkupEngine.Exception: The component for ${name} must be an Component component. ${component.name} given`
6158
);
6259
}
6360

0 commit comments

Comments
 (0)