MCPcopy
hub / github.com/jabbany/CommentCoreLibrary / MetaObject

Class MetaObject

src/scripting/api/Runtime/Runtime.ts:46–107  ·  view source on GitHub ↗

* Meta object that serves only to receive and send events

Source from the content-addressed store, hash-verified

44 * Meta object that serves only to receive and send events
45 */
46 class MetaObject implements RegisterableObject, Listenable {
47 private _name:string;
48 private _listeners:{[name:string]: Array<Function>} = {};
49
50 constructor(name:string) {
51 if (name.slice(0, 2) !== '__') {
52 throw new Error('MetaObject names must start with two underscores.');
53 }
54 this._name = name;
55 }
56
57 public addEventListener(event:string,
58 listener:Function,
59 useCapture:boolean = false,
60 priority:number = 0):void {
61
62 if (!(event in this._listeners)) {
63 this._listeners[event] = [];
64 }
65 this._listeners[event].push(listener);
66 }
67
68 public removeEventListener(event:string,
69 listener:Function,
70 useCapture:boolean = false):void {
71
72 if (!(event in this._listeners)) {
73 return;
74 }
75 var index = this._listeners[event].indexOf(listener)
76 if (index >= 0) {
77 this._listeners[event].splice(index, 1);
78 }
79 }
80
81 public hasEventListener(event:string):boolean {
82 return event in this._listeners && this._listeners[event].length > 0;
83 }
84
85 public dispatchEvent(event:string, data?:any):void {
86 if (!(event in this._listeners)) {
87 return; // Ignore
88 }
89 for (var i:number = 0; i < this._listeners[event].length; i++) {
90 this._listeners[event][i](data);
91 }
92 }
93
94 public getId():string {
95 return this._name;
96 }
97
98 public serialize():Object {
99 return {
100 "class":this._name
101 };
102 }
103

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected