node.js - using eventemitter throughout a project - events

just wondering what the best approach is for using eventemitter throughout multiple exports without the modules becoming too coupled with each other.
for instance i have module foo which deals with requests, now to break up the code I have a separate module (bar) which performs an action when a specific request comes in. I want to use an eventemitter for this.
any help is appreciated.
thanks.

Well if you want to use it across different modules you either send it as a function param everywhere or set it as a global variable (not recommended).

Related

How to make PestPHP boot Laravel so I can use facades such as Config?

I want to use standard Laravel facades, such as Str and Config, in my PestPHP Feature tests. Is there some concise way I can get PestPHP to do this for all of my tests in my tests/Feature directory?
I thought having uses(Tests\TestCase::class)->in('Feature'); in my Pest.php file would be sufficient, but all I ever get is this error: "A facade root has not been set."
The problem appears to be that Pest does not boot Laravel when it runs your test files, but rather only when you call certain global functions, e.g. the test closure functions test() and it(). So it's not possible to use Pest's "simplified" test script structure to easily initialize class-wide (file wide) values, i.e. attributes. beforeAll() does not work for this. Instead, one has to use beforeEach() in each class where facades will be needed.
See these issues: https://github.com/pestphp/pest/issues/237, https://github.com/pestphp/pest/issues/33#issuecomment-658022624, https://github.com/pestphp/pest/issues/246.

How to use react hooks in effective way?

Currently I am learning react. can anyone tell me what is the main application usage of react hooks and how can we take more advantage of using hooks with react.?
Basically, before if a component had state or needed to utilize a lifecycle method, we had to use a class, which requires a bunch of extra code.
Now that is not the case. With hooks, instead of ever having to use a class, we could just always use a function.
Tyler McGinnis wrote good blog post about it here
https://tylermcginnis.com/why-react-hooks/

Where to put ajax request in Ember tutorial app?

I want to add "Weather: 24C" to the rental-listing component of the super-rentals tutorial app.
Where would be the "best-practices" place to put this ajax request?
Ember.$.getJSON(`http://api.openweathermap.org/data/2.5/weather?q=${location}&APPID=${apiKey}`)
.then(function(json) {
return JSON.parse(json).main.temp;
});
Do I need to add a component, add a model, add a service, add a second adapter, modify the existing adapter? Something else? All of these? Is the problem that the tutorial uses Mirage? I ask this because when I think I'm getting close, I get an error like this:
Mirage: Your Ember app tried to GET
'http://api.openweathermap.org/data/2.5/weather?q=london&APPID=5432',
but there was no route defined to handle this request.
Define a route that matches this path in your
mirage/config.js file. Did you forget to add your namespace?
You need to configure mirage to allow you making calls to outside in case mirage is active; what I mean is using this.passthrough function within mirage/config.js, that is explained in api documentation quite well.
Regarding your question about where to make the remote call is; it depends:
If you need the data from the server to arrive in case a route is about to open; you should prefer putting it within model hook of the corresponding route.
If you intend to develop a component that is to be reused from within different routes or even from within different applications with the same remote call over and over again; you can consider putting the ajax remote call to a component. Even if that is not a very common case usually; it might be the case that a component itself should be wrapped up to fetch the data and display it by itself for reusing in different places; there is nothing that prevents you to do so. However by usually applying data-down action-up principle; generally the remote calls fall into routes or controllers.
Whether using an ember-data model is another thing to consider. If you intend to use ember-data; you should not directly use Ember.$.ajax but rather be using store provided by ember-data and perhaps providing your custom adapter/serializer to convert data to the format ember-data accepts in case the server do not match to the formats that ember-data accepts. In summary; you do not need to use models if you use pure ajax as you do in this question.

Serverless deploying multiple functions

I've recently updated my serverless project, and I've found that many things have changed in the last few updates.
https://serverless.com/
I don't fully understand whats the correct way to have multiple lambda functions and api gateway endpoints related to the same project. With the old serverless I have every lambda and endpoint as a completely seperate function, this worked pretty well for me.
I can't seem to do this anymore, if I try my second lambda function overrides my first, presumably because my "service name" for both is the same. My service name is the same because I want both rest endpoints in the same API in API Gateway. Since serverless creates the API name based on the service name.
So then I tried to add both functions to the same "Service". this worked for the most part, except that now I need to include my custom role statement for all my functions into the same role (because this one role is now being linked to all my functions). Effectively giving more permissions to each individual function than it should have. The other issue is that all my handler files for the different functions are being put into each functions deployment bundle.
So basically, I'm not sure what is the correct approach to have multiple functions that relate to the same project but are separate in functionality. It used to make sense, now doesn't.
If anybody can give me some pointers please
Thanks
I understand your frustration. I had the same feeling until I looked deeper into the new version and formed a better understanding. One thing to note though, is the new version is not completely finished yet. So if something is completely missing, you can file an issue and have it prioritized before 1.0 is out.
You are supposed to define multiple functions under the same service under the functions: section of serverless.yml. To package these functions individually (exclude code for other functions) you will have to set individually: true under package: section. You can then use include and exclude options at the root level and at the function level as well. There's an upcoming change that will let you use glob syntax in your include and exclude options (example **/*-fn.js). You can find more about packaging here https://www.serverless.com/framework/docs/providers/aws/guide/deploying.
Not sure how to use different roles for different functions under the same service.. How did you do it with 0.5?
I was trying to find a solution for individual iam roles per function as well. I couldn't find a way to do it, but while I was looking through the documentation I found the line: "Support for separate IAM Roles per function is coming soon." on this page, so at least we know they are working on it.
The "IAM Roles Per Function" plugin for Serverless allows you to do exactly what it says on the tin: specify roles for each function. You can still use the provider-level roles as well:
By default, function level iamRoleStatements override the provider level definition. It is also possible to inherit the provider level definition by specifying the option iamRoleStatementsInherit: true
EDIT: You can also apply a predefined AWS role at both the provider and function level.

Executing controllers methods from a library in codeigniter

i have a question, i am developing a library for Codeigniter to create Job Queues with workers and delayed queue (Codeigniter-JobQueue). But i have a question...
... How can i perform or execute controller's methods inside this library? It will be awesome to know this.
The library is taking "controller, method, params" to transform after to "http://www.example.com/controller/method/params".
Thanks, and if you want to help me to develop, you are welcome. ;)
Use curl. If you however expect a return variable from controllers method, then C in your MVC is not designed properly.
Curl is a way to go. Controllers should output content and not return a variable. If you are scheduling any job, the library should call the URL itself. You could use the cli mode of codeigniter.
http://ellislab.com/codeigniter/user-guide/general/cli.html

Resources