| Conditions | 3 |
| Paths | 4 |
| Total Lines | 154 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | var SDK = require('@ama-team/voxengine-sdk') |
||
| 64 | function Transition (options) { |
||
| 65 | var self = this |
||
| 66 | if (!options || !options.target) { |
||
| 67 | throw new IllegalStateError('Target state not provided') |
||
| 68 | } |
||
| 69 | var loggerName = 'ama-team.vsf.execution.transition' |
||
| 70 | var origin = options.origin |
||
| 71 | var target = options.target |
||
| 72 | var originId = (origin && origin.id) || null |
||
| 73 | var hints = options.hints |
||
| 74 | options.logger = options.logger || {} |
||
| 75 | options.logger.mdc = options.logger.mdc || {} |
||
| 76 | options.logger.mdc['origin'] = originId |
||
| 77 | options.logger.mdc['target'] = target.id |
||
| 78 | var logger = Slf4j.factory(options.logger, loggerName) |
||
| 79 | |||
| 80 | var token = new CancellationToken() |
||
| 81 | var completion = new Future() |
||
| 82 | var status = Status.Idle |
||
| 83 | |||
| 84 | var launchedAt = null |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param {Transition.Status} next |
||
| 88 | */ |
||
| 89 | function setStatus (next) { |
||
| 90 | logger.trace('Changing status from {} to {}', status.id, next.id) |
||
| 91 | status = next |
||
| 92 | } |
||
| 93 | |||
| 94 | function executionOptions (name) { |
||
| 95 | return { |
||
| 96 | name: name, |
||
| 97 | handler: target[name], |
||
| 98 | logger: options.logger, |
||
| 99 | executor: options.executor |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Returns function that will generate ITransitionResult from provided |
||
| 105 | * value. |
||
| 106 | * |
||
| 107 | * @param {Status} status |
||
| 108 | * @return {Function} |
||
| 109 | */ |
||
| 110 | function factory (status) { |
||
| 111 | return function (value) { |
||
| 112 | return { |
||
| 113 | value: value, |
||
| 114 | status: status, |
||
| 115 | duration: (new Date()).getTime() - launchedAt.getTime() |
||
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Completes transition with provided result, if |
||
| 122 | * |
||
| 123 | * @param {TTransitionResult} result |
||
| 124 | * @param {Status} [requiredStatus] |
||
| 125 | */ |
||
| 126 | function complete (result, requiredStatus) { |
||
| 127 | if (requiredStatus && status !== requiredStatus) { |
||
| 128 | return |
||
| 129 | } |
||
| 130 | setStatus(result.status) |
||
| 131 | logger.debug('Transition has ended with status {} and value {} in {} ms', |
||
| 132 | status.id, result.value, result.duration) |
||
| 133 | completion.resolve(result) |
||
| 134 | } |
||
| 135 | |||
| 136 | function run () { |
||
| 137 | if (status !== Status.Idle) { |
||
| 138 | throw new IllegalStateError('Tried to run ' + self + ' twice') |
||
| 139 | } |
||
| 140 | logger.debug('Running transition') |
||
| 141 | launchedAt = new Date() |
||
| 142 | setStatus(Status.Executing) |
||
| 143 | var opts = executionOptions('transition') |
||
| 144 | var execution = new Branch(opts) |
||
| 145 | execution |
||
| 146 | .run(originId, hints, token) |
||
| 147 | .then(factory(Status.Executed), factory(Status.ExecutionFailure)) |
||
| 148 | .then(function (result) { |
||
| 149 | var level = result.status.successful ? 'debug' : 'warn' |
||
| 150 | var term = result.status.successful ? 'success' : 'error' |
||
| 151 | logger[level]('Transition run has finished with {}: {}', term, |
||
| 152 | result.value) |
||
| 153 | complete(result, Status.Executing) |
||
| 154 | }) |
||
| 155 | return completion |
||
| 156 | } |
||
| 157 | |||
| 158 | function abort () { |
||
| 159 | if (status !== Status.Executing) { |
||
| 160 | throw new IllegalStateError('Tried to abort ' + self) |
||
| 161 | } |
||
| 162 | setStatus(Status.Aborting) |
||
| 163 | var options = executionOptions('abort') |
||
| 164 | var execution = new Branch(options) |
||
| 165 | execution |
||
| 166 | .run(originId, hints) |
||
| 167 | .then(factory(Status.Aborted), factory(Status.AbortFailure)) |
||
| 168 | .then(function (result) { |
||
| 169 | var level = result.status.successful ? 'debug' : 'warn' |
||
| 170 | var term = result.status.successful ? 'success' : 'error' |
||
| 171 | logger[level]('Transition abort has finished with {}: {}', term, |
||
| 172 | result.value) |
||
| 173 | complete(result, Status.Aborting) |
||
| 174 | }) |
||
| 175 | return completion |
||
| 176 | } |
||
| 177 | |||
| 178 | this.run = run |
||
| 179 | |||
| 180 | this.abort = abort |
||
| 181 | |||
| 182 | this.getStatus = function () { |
||
| 183 | return status |
||
| 184 | } |
||
| 185 | |||
| 186 | this.toString = function () { |
||
| 187 | return 'Transition ' + originId + ' -> ' + target.id + |
||
| 188 | ' (' + status.id + ')' |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @return {TTransitionDetails} |
||
| 193 | */ |
||
| 194 | this.toDetails = function () { |
||
| 195 | return { |
||
| 196 | origin: originId, |
||
| 197 | target: target.id, |
||
| 198 | hints: hints |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @return {int} |
||
| 204 | */ |
||
| 205 | this.getLaunchedAt = function () { return launchedAt } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @return {TState} |
||
| 209 | */ |
||
| 210 | this.getTarget = function () { return target } |
||
| 211 | |||
| 212 | this.getOrigin = function () { return origin } |
||
| 213 | |||
| 214 | this.getHints = function () { return hints } |
||
| 215 | |||
| 216 | this.getCompletion = function () { return completion } |
||
| 217 | } |
||
| 218 | |||
| 224 |