Skip to content

Commit 21ff102

Browse files
committed
fixed event emission error
1 parent bc74e45 commit 21ff102

File tree

1 file changed

+78
-81
lines changed

1 file changed

+78
-81
lines changed

OpenScript.js

Lines changed: 78 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11

22
/**
3-
* A Web Frontend Library
43
* The OpenScript Namespace
5-
* @author Levi Kamara Zwannah
6-
* @link [Github](https://github.com/levizwannah)
7-
* @link [Documentation](https://levizwannah.github.io/OpenScript.Js)
84
*/
95
var OpenScript = {
106

@@ -174,7 +170,7 @@ var OpenScript = {
174170
}
175171

176172
this.qs = new URLSearchParams(url.search);
177-
173+
178174
map.get('->')[1]();
179175

180176
this.reset.value = false;
@@ -500,79 +496,7 @@ var OpenScript = {
500496
this.emitter.on(this.EVENTS.rerendered, (th) => th.rerendered = true);
501497
this.emitter.on(this.EVENTS.bound, (th) => th.bound = true);
502498
this.emitter.on(this.EVENTS.mounted, (th) => th.mounted = true);
503-
this.emitter.on(this.EVENTS.visible, (th) => th.visible = true);
504-
505-
let obj = this;
506-
let seen = new Set();
507-
508-
do {
509-
if(!(obj instanceof OpenScript.Component)) break;
510-
511-
for(let method of Object.getOwnPropertyNames(obj)){
512-
if(seen.has(method)) continue;
513-
514-
if(typeof this[method] !== "function") continue;
515-
if(method.length < 3) continue;
516-
517-
if(method[0] !== '$' && method[1] !== "_") continue;
518-
519-
let meta = method.substring(1).split(/\$/g);
520-
521-
let events = meta[0].split(/_/g);
522-
events.shift();
523-
let cmpName = this.name;
524-
525-
let subjects = meta.slice(1);
526-
527-
if(!subjects?.length) subjects = [this.name, 'on'];
528-
529-
let methods = {on: true, onAll: true};
530-
531-
let stack = [];
532-
533-
for(let i = 0; i < subjects.length; i++){
534-
535-
let current = subjects[i];
536-
stack.push(current);
537-
538-
while(stack.length){
539-
i++;
540-
current = subjects[i] ?? null;
541-
542-
if(current && methods[current]){
543-
stack.push(current);
544-
}
545-
else {
546-
stack.push('on');
547-
i--;
548-
}
549-
550-
let m = stack.pop();
551-
let cmp = stack.pop();
552-
553-
for(let j = 0; j < events.length; j++) {
554-
let ev = events[j];
555-
556-
if(!ev.length) continue;
557-
558-
h[m](cmp, ev, (component, event, ...args) => {
559-
560-
try{
561-
h.getComponent(cmpName)[method](h.getComponent(cmpName), component, event, ...args);
562-
}
563-
catch(e){
564-
console.error(e);
565-
}
566-
567-
});
568-
}
569-
}
570-
}
571-
572-
seen.add(method);
573-
}
574-
}
575-
while (obj = Object.getPrototypeOf(obj));
499+
this.emitter.on(this.EVENTS.visible, (th) => th.visible = true);
576500
}
577501

578502
/**
@@ -665,11 +589,84 @@ var OpenScript = {
665589
* @emits pre-mount
666590
*/
667591
async mount() {
592+
h.component(this.name, this);
593+
668594
this.claimListeners();
669595
this.emit(this.EVENTS.premount);
670-
h.component(this.name, this);
671596
await this.bind();
672597
this.emit(this.EVENTS.mounted);
598+
599+
let obj = this;
600+
let seen = new Set();
601+
602+
do {
603+
if(!(obj instanceof OpenScript.Component)) break;
604+
605+
for(let method of Object.getOwnPropertyNames(obj)){
606+
if(seen.has(method)) continue;
607+
608+
if(typeof this[method] !== "function") continue;
609+
if(method.length < 3) continue;
610+
611+
if(method[0] !== '$' && method[1] !== "_") continue;
612+
613+
let meta = method.substring(1).split(/\$/g);
614+
615+
let events = meta[0].split(/_/g);
616+
events.shift();
617+
let cmpName = this.name;
618+
619+
let subjects = meta.slice(1);
620+
621+
if(!subjects?.length) subjects = [this.name, 'on'];
622+
623+
let methods = {on: true, onAll: true};
624+
625+
let stack = [];
626+
627+
for(let i = 0; i < subjects.length; i++){
628+
629+
let current = subjects[i];
630+
stack.push(current);
631+
632+
while(stack.length){
633+
i++;
634+
current = subjects[i] ?? null;
635+
636+
if(current && methods[current]){
637+
stack.push(current);
638+
}
639+
else {
640+
stack.push('on');
641+
i--;
642+
}
643+
644+
let m = stack.pop();
645+
let cmp = stack.pop();
646+
647+
for(let j = 0; j < events.length; j++) {
648+
let ev = events[j];
649+
650+
if(!ev.length) continue;
651+
652+
h[m](cmp, ev, (component, event, ...args) => {
653+
654+
try{
655+
h.getComponent(cmpName)[method](h.getComponent(cmpName), component, event, ...args);
656+
}
657+
catch(e){
658+
console.error(e);
659+
}
660+
661+
});
662+
}
663+
}
664+
}
665+
666+
seen.add(method);
667+
}
668+
}
669+
while (obj = Object.getPrototypeOf(obj));
673670
}
674671

675672
/**
@@ -805,7 +802,7 @@ var OpenScript = {
805802

806803
// check if we have previously emitted this event
807804
listeners.forEach(a => {
808-
if(this.emitter.emitted[event]) a();
805+
if(this.emitter.emitted[event]) a(this, event);
809806

810807
this.emitter.on(event, a);
811808
});
@@ -858,7 +855,7 @@ var OpenScript = {
858855
* @returns {DocumentFragment|HTMLElement|String|Array<DocumentFragment|HTMLElement|String>}
859856
*/
860857
render(...args) {
861-
return h.toElement("<ojs></ojs>");
858+
return h.ojs(...args);
862859
}
863860

864861
/**

0 commit comments

Comments
 (0)