Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
137
rated 0 times [  141] [ 4]  / answers: 1 / hits: 18825  / 10 Years ago, tue, august 19, 2014, 12:00:00

In Lua I've made my own Event system and I wanted to convert and use it in JavaScript, here's the code in Lua:



TriggerEvent = function(Name, ...)
local EventID = nil;

for Event2ID, Table in pairs(Event) do
if(Table.Active and Table.Name == Name) then
EventID = Event2ID;
break;
end
end

if(EventID == nil) then
return false;
end

for ListenerID, Table in pairs(Event[EventID].Listener) do
if(Table.Active) then
Table.Function(...);
end
end
return true;
end


Which works flawlessly.
Here's what I have for JavaScript:



TriggerEvent = function(Name, ...) {
var
EventID = undefined
;

for(Event2ID = 0, Length = Event.length; Event2ID < Length; Event2ID++) {
if(Event[Event2ID].Active && Event[Event2ID].Name == Name) {
EventID = Event2ID;
break;
}
}

if(EventID == undefined) {
return false;
}

for(ListenerID = 0, Length = Event[EventID].Listener.length; ListenerID < Length; ListenerID++) {
if(Event[EventID].Listener[ListenerID].Active) {
Event[EventID].Listener[ListenerID].Function(...);
}
}
return true;
}


Which doesn't work at all.



Example usage:



WebsiteConnected = function(Website, Test) {
Website.SetTitle(Hello World);

console.log(Website.GetTitle() + connected! + Test);
return true;
}

CreateEvent(WebsiteConnected); // Moved down.
AddEventListener(WebsiteConnected, WebsiteConnected);
TriggerEvent(WebsiteConnected, (Website || {}), Test); // Moved down.

More From » function

 Answers
116

I made a work around for what I was previously requesting, here's my system:



// ------------------------------------------------------------------------------------------------- Event Class
Event = function() {
var
Public = this,
Private = {}
;

Public.Create = function(Name) {
if(Private.Created) {
return false;
}

Private.Created = true;
Private.Name = Name;
Private.Function = [];
return true;
}

Public.Delete = function() {
if(! Private.Created) {
return false;
}

Private = {};
Private.Created = false;
return true;
}

Public.IsCreated = function() {
if(! Private.Created) {
return false;
}
return true;
}

Public.SetName = function(Name) {
if(! Private.Created) {
return false;
}

Private.Name = Name;
return true;
}

Public.GetName = function() {
var
Name =
;

if(! Private.Created) {
return Name;
}

Name = Private.Name;
return Name;
}

Public.AddFunction = function(Function) {
if(! Private.Created) {
return false;
}

for(var FunctionID = 0, Length = Private.Function.length; FunctionID < Length; FunctionID++) {
if(Private.Function[FunctionID] == Function) {
return false;
}
}

Private.Function[Private.Function.length] = Function;
return true;
}

Public.HasFunction = function(Function) {
if(! Private.Created) {
return false;
}

for(var FunctionID = 0, Length = Private.Function.length; FunctionID < Length; FunctionID++) {
if(Private.Function[FunctionID] == Function) {
return true;
}
}
return false;
}

Public.Call = function() {
if(! Private.Created) {
return false;
}

var
Arguments =
;

for(var argumentID = 0, Length = arguments.length; argumentID < Length; argumentID++) {
Arguments += arguments[ + argumentID + ];

if(argumentID != (arguments.length - 1)) {
Arguments += , ;
}
}

for(var FunctionID = 0, Length = Private.Function.length; FunctionID < Length; FunctionID++) {
eval(Private.Function[FunctionID]( + Arguments + ));
}
return false;
}

Public.Create(arguments[0]);
return Public;
}

var
TestEvent = new Event(TestEvent)
;

TestEvent.AddFunction(
function() {
console.log(TestEvent called.);
}
);

TestEvent.AddFunction(
function(String, String1) {
console.log(String1 + String);
}
);

TestEvent.Call(world!, Hello );


This works flawlessly. I've ran many benchmark tests.


[#69724] Sunday, August 17, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
freddiej

Total Points: 294
Total Questions: 95
Total Answers: 97

Location: Saudi Arabia
Member since Sat, Aug 20, 2022
2 Years ago
;