Reusing BodyHandler instance - vert.x

I'm trying to create something like
router.route("foo").handler(BodyHandler.create().setBodyLimit(1024*1024))
router.route("bar").handler(BodyHandler.create().setBodyLimit(1024*1024))
router.route("baz").handler(BodyHandler.create().setBodyLimit(1024*1024))
Would it be ok to call BodyHandler.create().setBodyLimit(1024*1024) only once in the app, and reuse it all over the place I need it?

From what I can tell, you should be able to share BodyHandler between multiple routes. In fact, it is usually shared by setting it on all routes, as was correctly pointed out in another answer.
You can take a look at the code and see that it doesn't mutate anything internaly, so should be safe.
var defaultBodyHandler = BodyHandler.create().setBodyLimit(1000L);
router.route("foo").handler(defaultBodyHandler).handler((ctx) -> {});
router.route("bar").handler(defaultBodyHandler).handler((ctx) -> {});

Yes, you can re-use it. But it might make sense, according to what is provided in the question, to define a global BodyHandler:
router.route().handler(BodyHandler.create().setBodyLimit(1024*1024))
router.route("foo")...
router.route("bar")...
router.route("baz")...

Related

Should you call clear() on a list inside setState or set the list to an empty array?

I'm wondering how I should clear a list that the UI depends on. Here are two options:
//first implementation
setState(() {
list.clear();
});
//second implementation
setState(() {
list = [];
});
Which one is better? Thanks
Generally speaking I would say look at the clear Method implementation to find out what the method does under the hood. You probably will find arguments why you should use the method.
An easy answer is to generally use provided methods because chanches are that the method is not only for convenience and nice naming but also takes care of some things u are not aware of right now.
Either one is fine but you should know .clear() "throws an UnsupportedError, and retains all objects, if this is a fixed-length list." Source

How to call a Mojolicious controller method after failing on template

I have a Mojolicious problem that I suspect has an easy solution, but I can't swim through all of its code.
$r->get(
'/:controller/:action',
sub {
my $c = shift;
$c->render_maybe && return;
# No template. Either call controller->action() or dispatch as POST
}
);
$r->post('/:controller/:action');
As you can see, I have two routes that use the same URL, one for GET and one for POST. The POST is straightforward. It renders after finding the controller and action method and isn't concerned with a template. I have the GET method working where a template exists, ignoring the controller, by using a callback with render_maybe(). My issue is, if there isn't a template, I want to go ahead and run the controller's method.
One solution would be to simply identify my controller and call the action. Since I'm using placeholders in my route, I can't simply hard code this. So, is there a Mojolicious way of getting my controller class, or actual code that will get the class and call the method? I have both controller and action defined in stash, so this really shouldn't be a big deal. Mojo knows how to do this internally.
Another option would be to convert this to a POST method and run it as normal. I don't know if it's best to either come up with the URL, or find the defined POST route, or just convert my GET to a POST. I'm not sure how to accomplish any of these.
Thanks.
I'm not sure if this is the intended use case for under(), but it seems to be a decent solution to my problem.
$r->under( '/:controller/:action', sub { !shift->render_maybe } )->get('/');
$r->post('/:controller/:action');
Chaining under()->get() will create a nested route. The first in the stack will render a template, if it exists, from the render_maybe, stopping there. If the template doesn't exist, it will go to the standard get(), which will first check for a controller action. This is exactly what I was wanting.

How to get an NPP instance in a Firebreath plugin?

From within a class derived from FB::PluginCore (or FB::JSAPIAuto), for example in onPluginReady() or a JS method handler, I'd like to have access to the NPP instance. What is the best practice for getting this pointer?
The underlying goal is to be able to call NPN_SetValueForURL, to set cookies.
You can call any of the NPN functions on the NpapiBrowserHost object, which is what the BrowserHost actually is.
FB::Npapi::NpapiBrowserHostPtr npapiHost = FB::ptr_cast<FB::Npapi::NpapiBrowserHost>(m_host);
I think it has SetValueForURL on it, but if it's missing you can always add it and submit a pull request; I'll accept it as long as it's reasonable.

Does IMP imp = imp_implementationWithBlock((void*) objc_unretainedPointer(^(id me, BOOL selected)

The following provides a convenient way to add methods to a class at runtime:
imp_implementationWithBlock((void*) objc_unretainedPointer(^(id me, BOOL selected)
The method can then be added using class_addMethod(). Will these implementations eventually become cached and use the fast-track method dispatching system?
My gut feeling would be yes, because doing otherwise would complicate the delicate, consistent, and beautiful Objective-C runtime :)
Also this link -> http://kevin.sb.org/2006/11/16/objective-c-caching-and-method-swizzling/
Seems pretty confident. They're all Method's in the Class after you call class_addMethod. As far as I can tell, please correct me if I'm wrong, there's no way to distinguish them from ones which were compiled in.

How to load data into Ext.tree.TreePanel (not through ajax)

in example here
http://docs.sencha.com/ext-js/3-4/#!/api/Ext.tree.TreePanel
i can see property dataUrl: 'get-nodes.php',
but how i can change data from inside my code? there is no store property or setSource method
EDIT:
as i found, one of the possibilities is to create my own Ext.tree.TreeLoader that would have setSource method and use it instead of default
maybe there is more variants?
EDIT:
seems there is already a solution for this
http://www.sencha.com/forum/showthread.php?4595-Creating-Ext.tree.TreePanel-from-static-JSON-data-in-one-shot
seems there is already a solution for this
http://www.sencha.com/forum/showthread.php?4595-Creating-Ext.tree.TreePanel-from-static-JSON-data-in-one-shot