/README.rst

https://github.com/taiste/data-flow · ReStructuredText · 106 lines · 87 code · 19 blank · 0 comment · 0 complexity · d50760c9c185a6a5377c3a48f3db3ed5 MD5 · raw file

  1. Dataflow
  2. ========
  3. Dataflow is a library that can be used to represent object data manipulation as a concise, jQuery-style series of
  4. function invocations. It supports both synchronous and asynchronous operations so the programming style is the same in
  5. both cases.
  6. Example
  7. -------
  8. To illustrate this concept with an example, consider the following:
  9. ::
  10. var names = ["Dedekind", "Riemann", "Cauchy"];
  11. new taiste.DataFlow([
  12. { test: 123, foo: "bar", users: [0, 2],
  13. votes: { 0: true, 1: false, 2: true } },
  14. { test: 456, foo: "barbar", users: [1, 0], votes: { 0: false }}
  15. ])
  16. .pluck('users')
  17. .asyncMapEach(function() {
  18. _.delay(_.bind(function() {
  19. this.setData( { name: names[this.data]} );
  20. this.next();
  21. }, this), 250);
  22. }).end()
  23. .pluck('votes')
  24. .each(function(value, key) {
  25. var context = this;
  26. new taiste.DataFlow(value, undefined, undefined, true)
  27. .each(function(vote, user) {
  28. context.data[key][user] = {
  29. vote: vote,
  30. user: context.parent.users[key][user]
  31. };
  32. });
  33. }).end()
  34. .asyncOperation(
  35. function() {
  36. console.log("Running async op");
  37. _.delay(_.bind(function() {
  38. this.next();
  39. }, this), 250);
  40. })
  41. .then(function() {
  42. var context = this;
  43. _.delay(function() {
  44. context.next();
  45. });
  46. return false;
  47. })
  48. .then(function() {
  49. console.log(this);
  50. });
  51. The most important methods to understand are ``then``, ``next``, ``pluck``, ``asyncMap``, ``asyncEach``, ``map``, ``each`` and ``mapEach``.
  52. ``then`` adds a new function to the asynchronously executed pqueue. The execution can be made asynchronous by returning
  53. ``false`` from the ``then`` function. The context contains ``next`` function that is required to be executed when the
  54. operation finishes.
  55. The data is contained in the context's ``data`` property, that can be manipulated.
  56. The queue is executed in the order the items are added to it. The ``pluck`` function can be used to separate a new
  57. "stack item" so that the following functions before ``end`` are operating with the plucked data. Pluck also accepts
  58. wildcards and nested plucking with ``*`` and ``.``. The ``end`` can be then used to add the transformed data back to the
  59. master object. The root object is always accessible using the ``root`` function on the context.
  60. The ``getJSON``-method can be used to add data to the queue.
  61. External requirements
  62. ---------------------
  63. * jQuery_
  64. * Underscore.js_
  65. * `Taiste underscore extensions`_
  66. .. _Underscore.js: http://github.com/documentcloud/underscore/
  67. .. _`Taiste underscore extensions`: http://github.com/taiste/underscore-extensions/
  68. .. _jQuery: http://jquery.org
  69. Licence
  70. -------
  71. Licenced under MIT::
  72. Copyright (C) 2011 by Taiste Oy
  73. Permission is hereby granted, free of charge, to any person obtaining a copy
  74. of this software and associated documentation files (the "Software"), to deal
  75. in the Software without restriction, including without limitation the rights
  76. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  77. copies of the Software, and to permit persons to whom the Software is
  78. furnished to do so, subject to the following conditions:
  79. The above copyright notice and this permission notice shall be included in
  80. all copies or substantial portions of the Software.
  81. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  82. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  83. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  84. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  85. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  86. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  87. THE SOFTWARE.