How to share data between ExtJS grids - perl

I am working with a PERL Miner::Base and JavaScript platform (.pm and .js).
My JavaScript contains 3 ExtJS GridPanels.
Now, here's my problem:
I have a function that takes ~1 minute to run and returns a list with references to the data needed for all 3 grids. Let's call this function "foo".
Currently, in order to load each of my grids' store, I call a function that calls "foo" and returns only the relevant reference out of the list returned by "foo".
I need a way to call "foo" only once and share the data it returns between my 3 stores.
Is there such a way?
Thanks.
Here is the basic structure of my code:
my_code.js:
gridA with storeA (calls my_code.pm::get_A_data)
gridB with storeB (calls my_code.pm::get_B_data)
gridC with storeC (calls my_code.pm::get_C_data)
my_code.pm:
get_A_data (calls foo and returns the first reference returned from foo)
get_B_data (calls foo and returns the second reference returned from foo)
get_C_data (calls foo and returns the third reference returned from foo)

If you have three grids you effectively would need to have three stores. However you can do one request, get a store object and then clone it into other two local copies. Search for cloning ExtJs store

Related

Getting unique reference to a module passed as parameter in PACT

I would like to obtain a unique key referencing a module. A unique has or the contract address would be both ok. The reason is that I want to use that string as key in a database, storing some data referenced to that token.
I am passing an interface as parameter as follows:
(defun key:string (token:module{myInterface})
; TODO
)
I've tried the hash function (hash token) but it changes on every call. Also, the describe-module function has the hash property what could be nice, but this cannot be called within a module.
I could store the hash when I load the module and pass it to the function but it is not useful because I would like to be able to call some methods in the interface.

Get elements by class name with Elm.Browser.Dom

I know I can get an Element by id with Browser.Dom.getElement.
But how can I get a List Element by classname?
As of Elm 0.19, the browser package doesn't expose any other helper functions to query the DOM. The getElement function itself is directly calling a kernel function:
getElement : String -> Task Error Element
getElement =
Elm.Kernel.Browser.getElement
Depending on what you want to do specifically, you might want to write a JavaScript function that queries the elements, reads the interesting bits, and makes the result accessible to your Elm application through the ports system.
For instance, take a look at the elm-dom-ports package for inspiration. It exposes document.querySelectorAll() function as a port and you can catch its result by subscribing to querySelectorAllResponse.

How to make internal/helper functions testable?

Suppose that I have some function Foo that uses two internal helper functions bar and baz.
Is there a way to organize the code so that bar and baz remain "out of sight", but at the same time can be unit-tested? (Preferably, the unit tests for bar and baz would be in the same suite as the unit tests for the main function Foo.)
There are a few options to achieve this.
First, does Foo need to be a function? If it is a class then you can implement bar and baz as Hidden and Access='protected' which is pretty tightly locked down. Then you can create a test specific subclass that accesses bar and baz for testing. You can also scope the access to the test to further lock it from others view if desired.
If you do want Foo to be a function then you still have options. One of those is to somehow get a function handle to the private local functions to your tests. You can do this through some special calling syntax of Foo that returns these functions to a test when called. This however, modifies the production code in a potentially confusing way, and it essentially inserts test logic into production. I would prefer hiding these functions by putting them into a package so that they are not in the global namespace. The name of the package can indicate that they are off limits and not part of your supported interface.
Finally, one option is to simply use the public interface in order to test these functions. Clearly they are called from the function in some scenario. You may want to consider writing a test through the front door of your interface. One benefit of this is that you would then be able to change the implementation of your local function structure easily without modifying your test. The private functions are private because by definition they are part of your implementation, not your interface. If you find it complex enough to require its own test independent from the interface of Foo then it should probably just be broken out into another package function or class as described above in order to unit test it.
Not the most elegant way to do it but then Matlab is not the most practical language to build modules in a single code file.
One way to do it is to have a higher level level function which transfer the input arguments to the function you choose:
function out = fooModule( FunctionCalled , varargin)
%// This main function only transfer the input argument to the function chosen
switch FunctionCalled
case 'main'
out = foo(varargin) ;
case 'bar'
out = mBar(varargin) ;
case 'baz'
out = mBaz(varargin) ;
end
end
function outFoo = foo(varargin)
%// your main FOO code
%// which can call the helper function if necessary
end
function outbar = mBar(varargin)
%// your code
end
function outbaz = mBaz(varargin)
%// your code
end
You can even embed the FunctionCalled parameter into varargin if you want to compact things. This would also allow to test the type of the first argument. So for example if the first argument is not a string (calling one of the helper function) then forward the execution directly to the main foo function without having to call it explicitly (so the helper functions remain 'out of sight' if not called explicitly).
Otherwise, a completely different approach, you could consider writing a class.

Lisp Function Interpretation

I am reading a book and I am confused on what the following code does:
(defmethod execute ((o ORDER) (l SIMUL) (e MARKETUPDATE))
(values
(list (make-TRADE :timestamp (timestamp e)
:price (price e)
:quantity (orderquantity o)))
NIL))
The source to which I got this function says that it returns two values. My question is what the body does. From my understanding, the line 3-5 creates a list with :timestamp, :price, :quantity. Am I correct? What about values, the second line? Does it return this variable too? Any summary would help. Thanks
This is a method for a generic function, specializing on arguments of types order, simul, and marketupdate.
It returns 2 values:
A list of length 1 created by the eponymous function list, which contains a single object of, presumably, type trade (probably - but not necessarily - created by a defstruct), which has slots timestamp, price, and quantity.
Symbol nil.
You can probably access the slots of the trade using functions trade-timestamp &c (unless the defstruct form is non-trivial or trade is not defined by a defstruct at all).
Why the result of make-trade is wrapped in a list is hard to guess without more context, but I'd guess that an execute can be split into N trades in some scenarios.
I suspect your confusion arises almost entire because this is the first time you have encountered a use of values. Common Lisp allows functions to return multiple values. That's slightly similar to how any language allows functions to receive multiple parameters.
These multiple return values are quite efficiently implemented. Most newbies encounter multiple values for the first time on the integer division functions, which will return a remainder as their second value. Hash table look ups will return a second value to indicate if the key was actually in the table, since the value stored for the key might be nil.
In your example the second value is NIL, presumably other execute methods might return something more interesting - for example where in the update Q the order was place, or an error code if something goes wrong. Of course checking out the manual for values will be fraught with educational values(sic).
This function is a method returning two values by using the keyword values. Have a look at CLOS to better understand object orientation and at "values" for the way of returning more than one value.

Function returning 2 types based on input in Perl. Is this a good approach?

i have designed a function which can return 2 different types based on the input parameters
ex: &Foo(12,"count") -> returns record count from DB for value 12
&Foo(12,"details") -> returns resultset from DB for value 12 in hash format
My question is is this a good approach? in C# i can do it with function overload.
Please think what part of your code gets easier by saying
Foo(12, "count")
instead of
Foo_count(12)
The only case I can think of is when the function name ("count") itself is input data. And even then do you probably want to perform some validation on that, maybe by means of a function table lookup.
Unless this is for an intermediate layer that just takes a command name and passes it on, I'd go with two separate functions.
Also, the implementation of the Foo function would look at the command name and then just split into a private function for every command anyway, right?
additionally you might consider the want to make foo return the details if you wanted a list.
return wantarray ? ($num, #details) : $num;