Introducing needjs, a dependency manager for nodejs functions.

Introducing needjs, a dependency manager for nodejs functions.

needjs was released last week. needjs is a dependency manager for nodejs functions. But what does that mean?

In needjs, we define a set of Needs and then tell the system to handle them in the order it sees fit. Let’s see how it works by writing a small program here. I assume you have a working npm and nodejs installation.

First thing we need to do import needjs

Here sys is an instance of needjs. Now we have to declare our needs. Assume that we want to make an application that reserves or queries flights. The first thing we need to do is set up our input system. I’ll use readline and get input directly from stdin.

So what did we do here?

First, we defined our new need, called input, it has a post function that is called when something needs this input. The function post uses the readline module to get input from the user; then it will return the answer using this.done(...); after the need is created, it is registered into the system using sys.register

Now we need to define our needs for destination, time and origin. Let’s jump in:

Okay, so we see a few new faces around. The req parameter in Need, tells the system that before calling my post function you have to have the answer to these Needs, i.e this need needs those needs. The pre function is the reverse, it tells the system that before trying to satisfy my reqs do this.

So what does this.ok() means?

In a pre function, you have access to done, fail andok we have seen the former and the later means that I don’t fail but at the same time I don’t finish, I have to see what my needs offer me and then you can check with the post function.

You might ask so how do I see what my needs have done before? If you look closely you’ll see that the post and pre functions have an input called data, this includes all the values of the previous Needs. So to access the input value in post function, we use data['input'] which will give us what the user typed while input was running.

The last piece of the puzzle is sys.forget('input'), which tells the system to forget everything about the input, which will help the system to re-evaluate input when another need asks for it.

With these in mind, let’s try to write our needs for querying and reserving.

Let’s look at reserve first. The last requirement of reserve is called reserve_check, which will fail if the user has not written the phrase reserve in his input. If this need fails, then all of its parents will fail as well, so the reserve won’t run. The same thing happens inside querySo how do we use this exactly, if the system is going to fail entirely, you might ask. Here is how:

There are two new elements here, Need.OneOf and sys.trigger.

Need.OneOf tells the system that running one of these is enough for me. So if one of them fails, the system will try to run the other. This task will fail if all of those inside Need.OneOf fail.

Finally, sys.trigger tells the system to run the main need. That’s it, let’s see the results:


Leave a Reply

Your email address will not be published. Required fields are marked *