Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

ActionStack.js 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. /* jshint quotmark:false */
  18. var events = require('./events');
  19. var Q = require('q');
  20. function ActionStack () {
  21. this.stack = [];
  22. this.completed = [];
  23. }
  24. ActionStack.prototype = {
  25. createAction: function (handler, action_params, reverter, revert_params) {
  26. return {
  27. handler: {
  28. run: handler,
  29. params: action_params
  30. },
  31. reverter: {
  32. run: reverter,
  33. params: revert_params
  34. }
  35. };
  36. },
  37. push: function (tx) {
  38. this.stack.push(tx);
  39. },
  40. // Returns a promise.
  41. process: function (platform) {
  42. events.emit('verbose', 'Beginning processing of action stack for ' + platform + ' project...');
  43. while (this.stack.length) {
  44. var action = this.stack.shift();
  45. var handler = action.handler.run;
  46. var action_params = action.handler.params;
  47. try {
  48. handler.apply(null, action_params);
  49. } catch (e) {
  50. events.emit('warn', 'Error during processing of action! Attempting to revert...');
  51. this.stack.unshift(action);
  52. var issue = 'Uh oh!\n';
  53. // revert completed tasks
  54. while (this.completed.length) {
  55. var undo = this.completed.shift();
  56. var revert = undo.reverter.run;
  57. var revert_params = undo.reverter.params;
  58. try {
  59. revert.apply(null, revert_params);
  60. } catch (err) {
  61. events.emit('warn', 'Error during reversion of action! We probably really messed up your project now, sorry! D:');
  62. issue += 'A reversion action failed: ' + err.message + '\n';
  63. }
  64. }
  65. e.message = issue + e.message;
  66. return Q.reject(e);
  67. }
  68. this.completed.push(action);
  69. }
  70. events.emit('verbose', 'Action stack processing complete.');
  71. return Q();
  72. }
  73. };
  74. module.exports = ActionStack;