Caching which windows have a command enabled - Printable Version
-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Caching which windows have a command enabled (/showthread.php?tid=87342)
Caching which windows have a command enabled by MrPickle on 11-18-2008 at 08:14 PM
Here's how I thought to do it, but as I don't have a wide knowledge of JavaScript (Only C++) there may be a better way (hence me asking).
Create an object containing the window's handle and a "command on/off" boolean when a window is created, then add the object to an array of the active windows.
When the command is called, eg; /command on, loop through the array of active windows checking the window's handle to the current array object's handle and check if "command on/off" is false, if it is, set it to true.
Same for /command off except changing true, to false.
When the window is destroyed remove which ever object in the array has the same handle.
Another way I thought to do it is using the windows handle as a string in the array but I'm not sure whether JavaScript supports this. I am unsure of the correct term so here's an example:
jscript code: var fruit = new Array();
fruit['banana'] = "Yellow";
fruit['apple'] = "Red";
fruit['tomatoe'] = "Red"
RE: Caching which windows have a command enabled by matty on 11-18-2008 at 08:52 PM
Sure does:
jscript code: // Declare an object to hold our ChatWindows
var oChatWnds = {};
// An event is fired because a chat window was created
function OnEvent_ChatWndCreated(oChatWnd) {
// Create an entry in the object for the chat window
oChatWnds[oChatWnd.Handle] = {};
// Store the chat window's object in our oChatWnds object
oChatWnds[oChatWnd.Handle].objChatWnd = oChatWnd;
// Create a read/write property for the on/off
oChatWnds[oChatWnd.Handle].bCommand = true;
}
// An event is fired because a chat window was closed
function OnEvent_ChatWndDestroyed(oChatWnd) {
// Remove the chat window from the object
delete oChatWnds[oChatWnd.Handle];
}
// An event is fired because we sent a message
function OnEvent_ChatWndSendMessage(oChatWnd, sMessage) {
// Perform a regexp function to split the command and params
if (/^\/([^\s\/]+)\s*([\s\S]*)$/.exec(sMessage) !== null) {
// Declare local variables for the command and the parameters
var Command = RegExp.$1.toLowerCase();
var Param = RegExp.$2;
// Check if the command is one of ours
if (Command === 'command') {
// If it is then change the variable based on what the paramter is set to
oChatWnds[oChatWnd.Handle].bCommand = (Param === 'on' ? true : false);
// Return a null string to the function
return '';
}
// Return the message if we are doing nothing with it
return sMessage;
}
// Called when the user presses / in the chat window
function OnGetScriptCommands() {
// Make sure 'command' is a valid command so Plus! doesn't bark
return '<ScriptCommands><Command><Name>command</Name></Command></ScriptCommands>';
}
RE: Caching which windows have a command enabled by MrPickle on 11-18-2008 at 09:39 PM
Just for example, say that oChatWnd.Handle is 1054, when you do this: oChatWnds[oChatWnd.Handle], it's looking at the 1054th address, what happened to the other 1053? Isn't this going to waste a huge amount of memory?
So what I'm basically asking is, will JavaScript initialize all of the array's adresses, 0 - 1054, and leave any unused empty, or will it simply move 1054 to whatever's the next available slot and then create a pointer in the previous address to the new address?
RE: Caching which windows have a command enabled by Spunky on 11-18-2008 at 10:37 PM
quote: Originally posted by MrPickle
Just for example, say that oChatWnd.Handle is 1054, when you do this: oChatWnds[oChatWnd.Handle], it's looking at the 1054th address, what happened to the other 1053? Isn't this going to waste a huge amount of memory?
So what I'm basically asking is, will JavaScript initialize all of the array's adresses, 0 - 1054, and leave any unused empty, or will it simply move 1054 to whatever's the next available slot and then create a pointer in the previous address to the new address?
JScript uses sparse arrays...
jscript code: for(var s in oChatWnds){
Debug.Trace(oChatWnds[s])
}
will only display 1054... it will not loop through all of them. It wouldn't really be a speed issue that you'd notice if it wasn't this way tbh.
EDIT:
quote: Originally posted by Spunky
what happened to the other 1053?
You mean 1054; JScript arrays are zero-based
RE: Caching which windows have a command enabled by MrPickle on 11-18-2008 at 10:48 PM
I did mean 1053, because I was excluding 1054
If it's sparse arrays then it is wasting memory I think. Well if I'm understanding Wiki correctly then it is wasting memory. http://en.wikipedia.org/wiki/Sparse_array
RE: Caching which windows have a command enabled by matty on 11-18-2008 at 11:07 PM
It isn't an array so you aren't wasting memory.
RE: Caching which windows have a command enabled by MrPickle on 11-18-2008 at 11:19 PM
What is it then, if it's not an array?
Every langauge I've ever worked in oChatWnds[oChatWnd.Handle] would be an array.
Would this be acceptable.
jscript code: var Windows = new Array();
function OnEvent_ChatWndCreated(ChatWnd) {
wnd = {};
wnd.Handle = ChatWnd.Handle;
wnd.bCommand = false;
Windows.push_back(wnd);
}
This would mean you wouldn't have massive gaps but you'd have to loop through the chats.
Going back to one of my first questions, can the value in between the square brackets be a string?
RE: Caching which windows have a command enabled by Spunky on 11-19-2008 at 01:17 AM
quote: Originally posted by MrPickle
can the value in between the square brackets be a string?
Yes.
quote: Originally posted by MrPickle
What is it then, if it's not an array?
I think they're technically objects. The code you posted should work fine (except for the push_back method... I don't think it exists in JScript)
RE: Caching which windows have a command enabled by MrPickle on 11-19-2008 at 08:25 AM
It's ment to be push, not push_back, sorry.
http://www.w3schools.com/jsref/jsref_push.asp
I'm unsure how it will arrange the elements as strings, if it does it nicely I have an alternative method.
Thanks for you helps.
RE: Caching which windows have a command enabled by Matti on 11-19-2008 at 12:44 PM
It's not an array, it's a regular object.
quote: Originally posted by matty
jscript code: // Declare an object to hold our ChatWindows
var oChatWnds = {};
Basically, you're not defining an element in a zero-based numerical indexed array, you're setting an object property of a regular object. If you were going to use:
jscript code: var oChatWnds = [];
and then set and delete the 1053th element, yes, then you'd be wasting memory and processing time. But here, we only define one object property which simply happens to have a numeric property name - no problems at all.
JavaScript is very lax at such kind of stuff, unlike C++ where you can't make arrays with mixed element types. Compare JavaScript's object creation (like in this example) with extending an empty class: you start with nothing and add stuff in it. You won't create more properties in the class than you want (compared to JavaScript arrays), there's no length property or not-enumerable methods like push and slice. You simply extend an empty object.
quote: Originally posted by MrPickle
Would this be acceptable.
...
This would mean you wouldn't have massive gaps but you'd have to loop through the chats.
Correct, but it's much faster and efficient to have them indexed by handle in an object than looping through an array and checking the Handle property. Also, in your example code, there's no way to get access to the ChatWnd object itself through the array, however it could be you simply forgot that.
|