How should I implement a common method in multiple namespaces
-
Let's say I have A B C three articulations, and each with a namespace implementing a RR count (using namespace because it's local to a script).
I want to use the getNextRR() method in the corresponding namespace based on Message.getChannel(), because each is implemented differently. How should I approach this?
The problem is that we can't add namespaces themselves to a list, only methods from namespaces.
I can think of three ways to make this work, but each with drawbacks.
-
The simplest way is to use a switch condition, it works fine but lots of code for each method, plus switch in Javascript is not performance friendly.
-
Keep only the RR count in namespace, and implement another set of objects with corresponding methods calling Namespace.RR. Methods from objects work, but this method is convoluted and I'm not sure how it performs.
-
Implement an object within each namespace itself, then use an const var array for calling based on index, mainly because array[index] is faster than switch in Javascript.
So for each namespace we add a reg methods = {"method1": method1, "method2": method2...}, then in the main script const var articulations = [A.methods, B.methods...], and you call by using articulations[index]"method1".
However this doesn't work all the time lol. It compiles, but calling methods this way doesn't trigger Console.print() in them, and returns the wrong value when the method tries to retrieve values from a global array.
So what should I do about this?
-
-
@rcjach make a separate script to handle rrs and another one to handle articulations. You don't need a namespace per articulation, namespace aren't classes.
-
@d-healey okay thanks for the reminder.
I refactored the script like you said and used a switch condition :face_with_tears_of_joy: