{"name":"chainit","version":"2.0.3","description":"Turn an asynchronous JavaScript api into an asynchronous chainable JavaScript api.","main":"index.js","directories":{"example":"examples"},"scripts":{"test":"zuul -- test/chain.js"},"author":{"name":"Vincent Voyer","email":"vincent@zeroload.net"},"license":"MIT/X11","devDependencies":{"mocha":"~1.18.2","zuul":"~1.6.3"},"dependencies":{"queue":"~1.0.2"},"keywords":["chain","async","queue","flow control","chainable","chainify","chainit","chain api"],"repository":{"type":"git","url":"git://github.com/vvo/chainit.git"},"bugs":{"url":"https://github.com/vvo/chainit/issues"},"homepage":"https://github.com/vvo/chainit","_id":"chainit@2.0.3","_shasum":"6d61d878712a1d0a23b1b3682992d5644948ba6e","_from":".","_npmVersion":"1.4.7","_npmUser":{"name":"christian-bromann","email":"contact@christian-bromann.com"},"maintainers":[{"name":"vvo","email":"vincent.voyer@gmail.com"},{"name":"christian-bromann","email":"contact@christian-bromann.com"}],"dist":{"shasum":"6d61d878712a1d0a23b1b3682992d5644948ba6e","tarball":"http://registry-npm.ywwl.com/chainit/download/chainit-2.0.3.tgz","integrity":"sha512-R92S7RYdEzVwJwt19Ka0NgxYWtYSyi+WDTB/3vACXO3Iz6H4ohTVaqPJDLBEYLd/gbsNhjeQY6a26oMGCf4suQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQD89r2r5dCZx+NwemCOgWLz5Ri4BqcIy+6z2nEpdpj7swIhAKbsHE0MeDCJ7KjLzzVZ2j6gwZuo0jI4IHc6MvFvkVnt"}],"size":6891,"noattachment":false,"key":"/chainit/-/chainit-2.0.3.tgz"},"deprecated":"Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.","readme":"# chainit3\n\nTurn an asynchronous JavaScript api into an asynchronous\n[chainable](http://en.wikipedia.org/wiki/Method_chaining) JavaScript api.\n\nThis is a fork of [chainit](https://github.com/vvo/chainit), with more\nwork into getting a well-defined behavior of the transformation.\n\n\n## usage\n\n```js\nfunction MyApi() {}\nMyApi.prototype.method1 = function(cb) {cb()}\nMyApi.prototype.method2 = function(cb) {cb()}\n\nvar chainit = require('chainit');\nvar MyChainApi = chainit(MyApi);\nvar obj = new MyChainApi();\nobj\n  .method1()                      // 1st call\n  .method2()                      // 2nd call\n  .method1(function(/* args */) { // 3rd call\n    this.method1();               // 4th call\n  })\n  .method2();                     // 5th call\n```\n\n\n## Adding or overriding methods\n\nAdding and overriding methods works at both prototype level and instance level.\n\nYou must use `chainit.add(chain, methodName, method)`, since direct assignations\nwon't wrap your method properly.\n\n```js\nfunction MyApi() {}\nMyApi.prototype.method1 = function(cb) {cb()}\nMyApi.prototype.method2 = function(cb) {cb()}\n\nvar chainit = require('chainit');\nvar MyChainApi = chainit(MyApi);\n\nvar obj = new MyChainApi();\n\n// override instance method\nchainit.add(obj, 'method1', function(cb) {\n  cb()\n});\n\nobj\n  .method1() // calls the newly added method1\n  .method2();\n\n// revert original method\nchainit.add(obj, 'method1', MyApi.prototype.method1);\n\n// override prototype method\nchainit.add(MyChainApi, 'method1', function(cb) {\n  cb()\n});\n\nvar obj2 = new MyChainApi();\n\nobj2.method1(); // calls the newly chained prototype `method1`\n```\n\n## Like a state machine\n\nIt is also possible to have a custom function called immediately, in addition\nto the function that is queued:\n\n```js\nchainit.add(obj, 'method1', function queued1(sub, cb) {\n  //... the actual method\n}, function now1(sub) {\n  this.chainState = { one: true };\n});\nchainit.add(obj, 'method2', function queued2(var1, var2, cb) {\n\n}, function now2(var1, var2) {\n  if (!this.chainState || !this.chainState.one) throw new Error(\"method2 must be called after method1\");\n  this.chainState.one = false;\n});\n\n// this works\nobj.method1(a, b).method2(a, b) // ok\nobj.method1(a, b, function() {\n  this.method2(a, b); // ok\n  this.method2(a, b); // not allowed by that simple state machine\n});\n\n```\n\nThe first function is pushed to the queue, the second function is called\nimmediately.\n\nThe second function can set `this.chainState = someObject` and the queued\nfunction will be able to access that object.\n\n`this.chainState` reference is copied to the next queued (and immediate if any)\nfunctions: this behavior allows queued functions to get information about\nwhich functions were queued before or after them.\n\n\n## error handling\n\nUpon error, execution is stopped and the nearest callback is called,\nor the error is thrown:\n\n```js\nobj\n  .method1()\n  .methodError()\n  .notactuallycalled(function(err) {\n    // the error that happened in methodError\n    // but the method \"notactuallycalled\" is not called !\n    console.error(err);\n  });\n```\n\nUncaught errors are also caught and handled the same way - which\nadds some safety to the original API.\n\n\n## variable length arguments\n\nMethods that have a variable number of arguments require special handling.\nIt is advised to define those methods with the smallest number of required\narguments:\n\n```js\nfunction(uri, cb) {\n  var opts = {};\n  if (typeof cb != \"function\") {\n    opts = cb;\n    cb = arguments[2];\n  }\n  // ...\n}\n```\n\nThis automatically excludes the case where the function can accept\n(uri, myfun, cb) with myfun as an optional argument.\nThis is actually a good thing because it prevents undefined behavior.\n\n\n## features\n\nFeatures:\n\n* supports async apis\n* supports (crazy) nested calls\n* supports static and prototype methods\n* preserves nested calls order\n* preserves context in cb()\n* preserves cb(args)\n* stops execution on error\n* propagates error to the nearest callback\n* throws error if no callback can handle it\n* supports method(fun, cb) signatures\n* supports process.nextTick(cb)\n* supports setTimeout(cb)\n* supports methods redifinition\n* supports adding new methods\n* fully tested! local: `npm install -g mocha && mocha`, saucelabs: `npm test`\n\n\n## mixing async/sync apis\n\nThere is no easy way to mix sync/async chainable\napis because there is no way to differenciate sync/async calls.\n\n```js\nobj\n  .asyncMethod()\n  .syncMethod()\n```\n\nWe cannot know that syncMethod is synchronous and that\nwe do not\nneed to wait for a callback to be called to continue.\n\nEither your api is fully asynchronous and every method\ntakes a callback.\n\nEither your api is fully synchronous.\nIf you want synchronous support, make a pull request\nadding `chainit.sync(Constructor)`.\n\n\n## credits\n\nThis module is using [jessetane/queue](https://github.com/jessetane/queue).\n\nA chainable api is queueing methods and reordering calls, so we use a queue.\n\nThis module was built to replace the chainable api from\n[webdriverjs](https://github.com/camme/webdriverjs).\n\nIt is now used by more modules and is maintained with general use in mind now.\n","publish_time":1397837788952,"_cnpm_publish_time":1397837788952,"dist-tags":{"latest":"2.1.1"}}