1.1.9 • Published 11 years ago

asyncBuilder v1.1.9

Weekly downloads
207
License
-
Repository
github
Last release
11 years ago

asyncBuilder: to build objects… asynchronously

Dealing with nested asynchronous javascript is little fun and is virtually impossible to optimize by hand with any reasonably complicated block of code. Nested sets of functions will run at the speed of their slowest calls combined, regardless of what the actual dependency tree may look like. This little tool is built to help optimize these paths and to give some insight into what's going on.

Setting up and using a builder involves the following 3 steps:

Create a BuilderFactory

A BuilderFactory is used to create a map of functions that produce output and receive a given set of inputs. The add function may be called to add handlers to the BuilderFactory with a name, function, and list of inputs into the function (from other handlers' output or data passed into build() of the BuilderInstance).

var builderFactory = new require("asyncBuilder").BuilderFactory

builderFactory
  .add("firstName", getFirstName, ["user"])
  .add("lastName", getLastName, ["user"])
  .add("fullName", getFullName, ["firstName", "lastName"])

function getFirstName(user, next) {
  if (!user) return next(new Error("User does not exist"))
  next(null, user.firstName)
}

function getLastName(user, next) {
  if (!user) return next(new Error("User does not exist"))
  next(null, user.lastName)
}

function getFullName(firstName, lastName, next) {
  next(null, firstName + ' ' + lastName)
}

As a note: any errors passed into the first parameter of next for a given handler will end the execution of the current build() call (with the exception of any calls in progress which will silently be ignored) and will bubble up to the callback specified to the build step.

You may also provide the name of one handler as the function for another if you wish to provide another accessor:

builderFactory
  .add("shortName", "firstName")

BuilderFactory instances may be cloned as a method of extending them without overriding (or adding to) the handlers on the original factory:

var newBuilderFactory = builderFactory
  .clone()
  .add("fullName", getAbbreviatedName, ["firstName", "lastName"])
  .add("shortName", "lastName")

Create a BuilderInstance

The BuilderFactory can create BuilderInstance objects via the newBuilder method. Each BuilderInstance will have a code path optimized for it's specific set of required output fields. Only the code needed to fulfill a given instance's outputs will ever be ran by a given BuilderInstance.

var myBuilder = builderFactory.newBuilder(["fullName", "shortName"])

Build your object

Your BuilderInstance can now be used to asynchronously generate output via the build method.

myBuilder.build({user: {firstName: "Jeremy", lastName: "Stanley"}}, function (err, data) {
  //data.fullName and data.shortName should both be present
})

If you have a need to further inspect what's happening with the asyncBuilder, a utility method trace() has been exposed on the BuilderInstance and will show timing information and execution steps for a given build() call:

myBuilder
  .trace()
  .build({user: {firstName: "Jeremy", lastName: "Stanley"}}, function (err, data) {
  //data.fullName and data.shortName should both be present
})

Contributing

Questions, comments, bug reports, and pull requests are all welcome. Submit them at the project on GitHub.

Bug reports that include steps-to-reproduce (including code) are the best. Even better, make them in the form of pull requests that update the test suite. Thanks!

Author

Jeremy Stanley supported by The Obvious Corporation.

License

Copyright 2012 The Obvious Corporation.

Licensed under the Apache License, Version 2.0. See the top-level file LICENSE.txt and (http://www.apache.org/licenses/LICENSE-2.0).

1.1.9

11 years ago

1.1.8

11 years ago

1.1.7

11 years ago

1.1.6

11 years ago

1.1.5

11 years ago

1.1.4

11 years ago

1.1.3

11 years ago

1.1.2

11 years ago

1.1.1

11 years ago

1.1.0

11 years ago

1.0.9

11 years ago

1.0.8

12 years ago

1.0.7

12 years ago

1.0.6

12 years ago

1.0.5

12 years ago

1.0.4

12 years ago

1.0.3

12 years ago

1.0.2

12 years ago

1.0.1

12 years ago

1.0.0

12 years ago

0.2.21

12 years ago

0.2.20

12 years ago

0.2.19

12 years ago

0.2.18

12 years ago

0.2.16

12 years ago

0.2.15

12 years ago

0.2.14

12 years ago

0.2.13

12 years ago

0.2.12

12 years ago

0.2.11

12 years ago

0.2.10

12 years ago

0.2.9

12 years ago

0.2.8

12 years ago

0.2.7

12 years ago

0.2.6

12 years ago

0.2.5

12 years ago

0.2.4

12 years ago

0.2.3

12 years ago

0.2.2

12 years ago

0.2.1

12 years ago

0.2.0

12 years ago

0.1.27

12 years ago

0.1.26

12 years ago

0.1.25

12 years ago

0.1.24

12 years ago

0.1.23

12 years ago

0.1.22

12 years ago

0.1.21

12 years ago

0.1.20

12 years ago

0.1.19

12 years ago

0.1.18

12 years ago

0.1.16

12 years ago

0.1.15

12 years ago

0.1.14

12 years ago

0.1.13

12 years ago

0.1.12

12 years ago

0.1.11

12 years ago

0.1.10

12 years ago

0.1.9

12 years ago

0.1.8

12 years ago

0.1.7

12 years ago

0.1.6

12 years ago

0.1.5

12 years ago

0.1.4

12 years ago

0.1.3

12 years ago

0.1.2

12 years ago

0.1.1

12 years ago

0.1.0

12 years ago