(action, options = {})
| 58 | } |
| 59 | |
| 60 | async do_action(action, options = {}) { |
| 61 | let error_during_free = false; |
| 62 | try { |
| 63 | await action.do(); |
| 64 | } catch (error) { |
| 65 | // Action aborted. This is usually expected behavior as actions throw errors if they shouldn't run. |
| 66 | return { status: 'aborted', reason: error }; |
| 67 | } |
| 68 | // Remove all redo actions from history |
| 69 | if (this.action_history_index < this.action_history.length) { |
| 70 | const freed_actions = this.action_history.slice(this.action_history_index, this.action_history.length).reverse(); |
| 71 | this.action_history = this.action_history.slice(0, this.action_history_index); |
| 72 | for (let freed_action of freed_actions) { |
| 73 | try { |
| 74 | await freed_action.free(); |
| 75 | } catch (error) { |
| 76 | error_during_free = true; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | // Add the new action to history |
| 81 | const last_action = this.action_history[this.action_history.length - 1]; |
| 82 | if (options.merge_with_history && last_action) { |
| 83 | if (typeof options.merge_with_history === 'string') { |
| 84 | options.merge_with_history = [options.merge_with_history]; |
| 85 | } |
| 86 | if (options.merge_with_history.includes(last_action.action_id)) { |
| 87 | this.action_history[this.action_history.length - 1] = new app.Actions.Bundle_action( |
| 88 | last_action.action_id, |
| 89 | last_action.action_description, |
| 90 | [last_action, action] |
| 91 | ); |
| 92 | } |
| 93 | } else { |
| 94 | this.action_history.push(action); |
| 95 | if (this.action_history.length > this.action_history_max) { |
| 96 | let action_to_free = this.action_history.shift(); |
| 97 | try { |
| 98 | await action_to_free.free(); |
| 99 | } catch (error) { |
| 100 | error_during_free = true; |
| 101 | } |
| 102 | } else { |
| 103 | this.action_history_index++; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // Chrome arbitrary method to determine memory usage, but most people use Chrome so... |
| 108 | if (window.performance && window.performance.memory) { |
| 109 | if (window.performance.memory.usedJSHeapSize > window.performance.memory.jsHeapSizeLimit * 0.8) { |
| 110 | this.free(window.performance.memory.jsHeapSizeLimit * 0.2); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if (error_during_free) { |
| 115 | alertify.error('A problem occurred while removing undo history. It\'s suggested you save your work and refresh the page in order to free up memory.'); |
| 116 | } |
| 117 | return { status: 'completed' }; |
no test coverage detected