Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. # kareem
  2. [![Build Status](https://travis-ci.org/vkarpov15/kareem.svg?branch=master)](https://travis-ci.org/vkarpov15/kareem)
  3. [![Coverage Status](https://img.shields.io/coveralls/vkarpov15/kareem.svg)](https://coveralls.io/r/vkarpov15/kareem)
  4. Re-imagined take on the [hooks](http://npmjs.org/package/hooks) module, meant to offer additional flexibility in allowing you to execute hooks whenever necessary, as opposed to simply wrapping a single function.
  5. Named for the NBA's all-time leading scorer Kareem Abdul-Jabbar, known for his mastery of the [hook shot](http://en.wikipedia.org/wiki/Kareem_Abdul-Jabbar#Skyhook)
  6. <img src="http://upload.wikimedia.org/wikipedia/commons/0/00/Kareem-Abdul-Jabbar_Lipofsky.jpg" width="220">
  7. # API
  8. ## pre hooks
  9. Much like [hooks](https://npmjs.org/package/hooks), kareem lets you define
  10. pre and post hooks: pre hooks are called before a given function executes.
  11. Unlike hooks, kareem stores hooks and other internal state in a separate
  12. object, rather than relying on inheritance. Furthermore, kareem exposes
  13. an `execPre()` function that allows you to execute your pre hooks when
  14. appropriate, giving you more fine-grained control over your function hooks.
  15. #### It runs without any hooks specified
  16. ```javascript
  17. hooks.execPre('cook', null, function() {
  18. done();
  19. });
  20. ```
  21. #### It runs basic serial pre hooks
  22. pre hook functions take one parameter, a "done" function that you execute
  23. when your pre hook is finished.
  24. ```javascript
  25. var count = 0;
  26. hooks.pre('cook', function(done) {
  27. ++count;
  28. done();
  29. });
  30. hooks.execPre('cook', null, function() {
  31. assert.equal(1, count);
  32. done();
  33. });
  34. ```
  35. #### It can run multipe pre hooks
  36. ```javascript
  37. var count1 = 0;
  38. var count2 = 0;
  39. hooks.pre('cook', function(done) {
  40. ++count1;
  41. done();
  42. });
  43. hooks.pre('cook', function(done) {
  44. ++count2;
  45. done();
  46. });
  47. hooks.execPre('cook', null, function() {
  48. assert.equal(1, count1);
  49. assert.equal(1, count2);
  50. done();
  51. });
  52. ```
  53. #### It can run fully synchronous pre hooks
  54. If your pre hook function takes no parameters, its assumed to be
  55. fully synchronous.
  56. ```javascript
  57. var count1 = 0;
  58. var count2 = 0;
  59. hooks.pre('cook', function() {
  60. ++count1;
  61. });
  62. hooks.pre('cook', function() {
  63. ++count2;
  64. });
  65. hooks.execPre('cook', null, function(error) {
  66. assert.equal(null, error);
  67. assert.equal(1, count1);
  68. assert.equal(1, count2);
  69. done();
  70. });
  71. ```
  72. #### It properly attaches context to pre hooks
  73. Pre save hook functions are bound to the second parameter to `execPre()`
  74. ```javascript
  75. hooks.pre('cook', function(done) {
  76. this.bacon = 3;
  77. done();
  78. });
  79. hooks.pre('cook', function(done) {
  80. this.eggs = 4;
  81. done();
  82. });
  83. var obj = { bacon: 0, eggs: 0 };
  84. // In the pre hooks, `this` will refer to `obj`
  85. hooks.execPre('cook', obj, function(error) {
  86. assert.equal(null, error);
  87. assert.equal(3, obj.bacon);
  88. assert.equal(4, obj.eggs);
  89. done();
  90. });
  91. ```
  92. #### It can execute parallel (async) pre hooks
  93. Like the hooks module, you can declare "async" pre hooks - these take two
  94. parameters, the functions `next()` and `done()`. `next()` passes control to
  95. the next pre hook, but the underlying function won't be called until all
  96. async pre hooks have called `done()`.
  97. ```javascript
  98. hooks.pre('cook', true, function(next, done) {
  99. this.bacon = 3;
  100. next();
  101. setTimeout(function() {
  102. done();
  103. }, 5);
  104. });
  105. hooks.pre('cook', true, function(next, done) {
  106. next();
  107. var _this = this;
  108. setTimeout(function() {
  109. _this.eggs = 4;
  110. done();
  111. }, 10);
  112. });
  113. hooks.pre('cook', function(next) {
  114. this.waffles = false;
  115. next();
  116. });
  117. var obj = { bacon: 0, eggs: 0 };
  118. hooks.execPre('cook', obj, function() {
  119. assert.equal(3, obj.bacon);
  120. assert.equal(4, obj.eggs);
  121. assert.equal(false, obj.waffles);
  122. done();
  123. });
  124. ```
  125. #### It supports returning a promise
  126. You can also return a promise from your pre hooks instead of calling
  127. `next()`. When the returned promise resolves, kareem will kick off the
  128. next middleware.
  129. ```javascript
  130. hooks.pre('cook', function() {
  131. return new Promise(resolve => {
  132. setTimeout(() => {
  133. this.bacon = 3;
  134. resolve();
  135. }, 100);
  136. });
  137. });
  138. var obj = { bacon: 0 };
  139. hooks.execPre('cook', obj, function() {
  140. assert.equal(3, obj.bacon);
  141. done();
  142. });
  143. ```
  144. ## post hooks
  145. #### It runs without any hooks specified
  146. ```javascript
  147. hooks.execPost('cook', null, [1], function(error, eggs) {
  148. assert.ifError(error);
  149. assert.equal(1, eggs);
  150. done();
  151. });
  152. ```
  153. #### It executes with parameters passed in
  154. ```javascript
  155. hooks.post('cook', function(eggs, bacon, callback) {
  156. assert.equal(1, eggs);
  157. assert.equal(2, bacon);
  158. callback();
  159. });
  160. hooks.execPost('cook', null, [1, 2], function(error, eggs, bacon) {
  161. assert.ifError(error);
  162. assert.equal(1, eggs);
  163. assert.equal(2, bacon);
  164. done();
  165. });
  166. ```
  167. #### It can use synchronous post hooks
  168. ```javascript
  169. var execed = {};
  170. hooks.post('cook', function(eggs, bacon) {
  171. execed.first = true;
  172. assert.equal(1, eggs);
  173. assert.equal(2, bacon);
  174. });
  175. hooks.post('cook', function(eggs, bacon, callback) {
  176. execed.second = true;
  177. assert.equal(1, eggs);
  178. assert.equal(2, bacon);
  179. callback();
  180. });
  181. hooks.execPost('cook', null, [1, 2], function(error, eggs, bacon) {
  182. assert.ifError(error);
  183. assert.equal(2, Object.keys(execed).length);
  184. assert.ok(execed.first);
  185. assert.ok(execed.second);
  186. assert.equal(1, eggs);
  187. assert.equal(2, bacon);
  188. done();
  189. });
  190. ```
  191. ## wrap()
  192. #### It wraps pre and post calls into one call
  193. ```javascript
  194. hooks.pre('cook', true, function(next, done) {
  195. this.bacon = 3;
  196. next();
  197. setTimeout(function() {
  198. done();
  199. }, 5);
  200. });
  201. hooks.pre('cook', true, function(next, done) {
  202. next();
  203. var _this = this;
  204. setTimeout(function() {
  205. _this.eggs = 4;
  206. done();
  207. }, 10);
  208. });
  209. hooks.pre('cook', function(next) {
  210. this.waffles = false;
  211. next();
  212. });
  213. hooks.post('cook', function(obj) {
  214. obj.tofu = 'no';
  215. });
  216. var obj = { bacon: 0, eggs: 0 };
  217. var args = [obj];
  218. args.push(function(error, result) {
  219. assert.ifError(error);
  220. assert.equal(null, error);
  221. assert.equal(3, obj.bacon);
  222. assert.equal(4, obj.eggs);
  223. assert.equal(false, obj.waffles);
  224. assert.equal('no', obj.tofu);
  225. assert.equal(obj, result);
  226. done();
  227. });
  228. hooks.wrap(
  229. 'cook',
  230. function(o, callback) {
  231. assert.equal(3, obj.bacon);
  232. assert.equal(4, obj.eggs);
  233. assert.equal(false, obj.waffles);
  234. assert.equal(undefined, obj.tofu);
  235. callback(null, o);
  236. },
  237. obj,
  238. args);
  239. ```
  240. ## createWrapper()
  241. #### It wraps wrap() into a callable function
  242. ```javascript
  243. hooks.pre('cook', true, function(next, done) {
  244. this.bacon = 3;
  245. next();
  246. setTimeout(function() {
  247. done();
  248. }, 5);
  249. });
  250. hooks.pre('cook', true, function(next, done) {
  251. next();
  252. var _this = this;
  253. setTimeout(function() {
  254. _this.eggs = 4;
  255. done();
  256. }, 10);
  257. });
  258. hooks.pre('cook', function(next) {
  259. this.waffles = false;
  260. next();
  261. });
  262. hooks.post('cook', function(obj) {
  263. obj.tofu = 'no';
  264. });
  265. var obj = { bacon: 0, eggs: 0 };
  266. var cook = hooks.createWrapper(
  267. 'cook',
  268. function(o, callback) {
  269. assert.equal(3, obj.bacon);
  270. assert.equal(4, obj.eggs);
  271. assert.equal(false, obj.waffles);
  272. assert.equal(undefined, obj.tofu);
  273. callback(null, o);
  274. },
  275. obj);
  276. cook(obj, function(error, result) {
  277. assert.ifError(error);
  278. assert.equal(3, obj.bacon);
  279. assert.equal(4, obj.eggs);
  280. assert.equal(false, obj.waffles);
  281. assert.equal('no', obj.tofu);
  282. assert.equal(obj, result);
  283. done();
  284. });
  285. ```
  286. ## clone()
  287. #### It clones a Kareem object
  288. ```javascript
  289. var k1 = new Kareem();
  290. k1.pre('cook', function() {});
  291. k1.post('cook', function() {});
  292. var k2 = k1.clone();
  293. assert.deepEqual(['cook'], Object.keys(k2._pres));
  294. assert.deepEqual(['cook'], Object.keys(k2._posts));
  295. ```
  296. ## merge()
  297. #### It pulls hooks from another Kareem object
  298. ```javascript
  299. var k1 = new Kareem();
  300. var test1 = function() {};
  301. k1.pre('cook', test1);
  302. k1.post('cook', function() {});
  303. var k2 = new Kareem();
  304. var test2 = function() {};
  305. k2.pre('cook', test2);
  306. var k3 = k2.merge(k1);
  307. assert.equal(k3._pres['cook'].length, 2);
  308. assert.equal(k3._pres['cook'][0].fn, test2);
  309. assert.equal(k3._pres['cook'][1].fn, test1);
  310. assert.equal(k3._posts['cook'].length, 1);
  311. ```