@qiwi/cyclone
Advanced tools
Comparing version 2.4.0 to 2.5.0
@@ -0,1 +1,8 @@ | ||
# [2.5.0](https://github.com/qiwi/cyclone/compare/v2.4.0...v2.5.0) (2019-01-22) | ||
### Features | ||
* **Machine:** add conditions to `#prev`method ([73c2d90](https://github.com/qiwi/cyclone/commit/73c2d90)), closes [#1](https://github.com/qiwi/cyclone/issues/1) [#15](https://github.com/qiwi/cyclone/issues/15) | ||
# [2.4.0](https://github.com/qiwi/cyclone/compare/v2.3.0...v2.4.0) (2019-01-21) | ||
@@ -2,0 +9,0 @@ |
@@ -7,2 +7,3 @@ "use strict"; | ||
exports.LOCK_VIOLATION = 'Lock violation'; | ||
exports.UNREACHABLE_STATE = 'Unreachable state'; | ||
var MachineError = /** @class */ (function (_super) { | ||
@@ -9,0 +10,0 @@ tslib_1.__extends(MachineError, _super); |
@@ -81,3 +81,3 @@ "use strict"; | ||
: condition; | ||
return this.history.reverse().find(filter); | ||
return this.history.slice().reverse().find(filter); | ||
}; | ||
@@ -89,5 +89,2 @@ /** | ||
Machine.prototype.prev = function (state) { | ||
if (state) { | ||
console.log('Not implemented: https://github.com/qiwi/cyclone/issues/1'); | ||
} | ||
if (this.key) { | ||
@@ -97,5 +94,14 @@ throw new error_1.MachineError(error_1.LOCK_VIOLATION); | ||
if (this.history.length < 2) { | ||
throw new error_1.MachineError(error_1.TRANSITION_VIOLATION); | ||
throw new error_1.MachineError(error_1.UNREACHABLE_STATE); | ||
} | ||
this.history.pop(); | ||
if (state !== undefined) { | ||
var last = this.last(state); | ||
if (!last) { | ||
throw new error_1.MachineError(error_1.UNREACHABLE_STATE); | ||
} | ||
this.history.length = this.history.indexOf(last) + 1; | ||
} | ||
else { | ||
this.history.pop(); | ||
} | ||
return this; | ||
@@ -102,0 +108,0 @@ }; |
export const TRANSITION_VIOLATION = 'Transition violation'; | ||
export const INVALID_UNLOCK_KEY = 'Invalid unlock key'; | ||
export const LOCK_VIOLATION = 'Lock violation'; | ||
export const UNREACHABLE_STATE = 'Unreachable state'; | ||
export class MachineError extends Error { | ||
} | ||
//# sourceMappingURL=error.js.map |
@@ -1,2 +0,2 @@ | ||
import { MachineError, LOCK_VIOLATION, TRANSITION_VIOLATION, INVALID_UNLOCK_KEY } from './error'; | ||
import { MachineError, LOCK_VIOLATION, TRANSITION_VIOLATION, INVALID_UNLOCK_KEY, UNREACHABLE_STATE } from './error'; | ||
import { generateDate, generateId } from './generator'; | ||
@@ -71,3 +71,3 @@ import { log } from './log'; | ||
: condition; | ||
return this.history.reverse().find(filter); | ||
return [...this.history].reverse().find(filter); | ||
} | ||
@@ -79,5 +79,2 @@ /** | ||
prev(state) { | ||
if (state) { | ||
console.log('Not implemented: https://github.com/qiwi/cyclone/issues/1'); | ||
} | ||
if (this.key) { | ||
@@ -87,5 +84,14 @@ throw new MachineError(LOCK_VIOLATION); | ||
if (this.history.length < 2) { | ||
throw new MachineError(TRANSITION_VIOLATION); | ||
throw new MachineError(UNREACHABLE_STATE); | ||
} | ||
this.history.pop(); | ||
if (state !== undefined) { | ||
const last = this.last(state); | ||
if (!last) { | ||
throw new MachineError(UNREACHABLE_STATE); | ||
} | ||
this.history.length = this.history.indexOf(last) + 1; | ||
} | ||
else { | ||
this.history.pop(); | ||
} | ||
return this; | ||
@@ -92,0 +98,0 @@ } |
export const TRANSITION_VIOLATION: string = 'Transition violation' | ||
export const INVALID_UNLOCK_KEY: string = 'Invalid unlock key' | ||
export const LOCK_VIOLATION: string = 'Lock violation' | ||
export const UNREACHABLE_STATE: string = 'Unreachable state' | ||
export class MachineError extends Error {} |
@@ -5,3 +5,4 @@ import { | ||
TRANSITION_VIOLATION, | ||
INVALID_UNLOCK_KEY | ||
INVALID_UNLOCK_KEY, | ||
UNREACHABLE_STATE | ||
} from './error' | ||
@@ -158,3 +159,3 @@ | ||
*/ | ||
public current (): IDigest { | ||
public current (): IHistoryItem { | ||
return { ...this.history[this.history.length - 1] } | ||
@@ -166,3 +167,3 @@ } | ||
*/ | ||
public last (condition?: string | IPredicate): IDigest | void { | ||
public last (condition?: string | IPredicate): IHistoryItem | void { | ||
if (condition === undefined) { | ||
@@ -176,3 +177,3 @@ return this.current() | ||
return this.history.reverse().find(filter) | ||
return [...this.history].reverse().find(filter) | ||
} | ||
@@ -184,7 +185,3 @@ | ||
*/ | ||
public prev (state?: string): IMachine { | ||
if (state) { | ||
console.log('Not implemented: https://github.com/qiwi/cyclone/issues/1') | ||
} | ||
public prev (state?: string | IPredicate): IMachine { | ||
if (this.key) { | ||
@@ -195,6 +192,18 @@ throw new MachineError(LOCK_VIOLATION) | ||
if (this.history.length < 2) { | ||
throw new MachineError(TRANSITION_VIOLATION) | ||
throw new MachineError(UNREACHABLE_STATE) | ||
} | ||
this.history.pop() | ||
if (state !== undefined) { | ||
const last = this.last(state) | ||
if (!last) { | ||
throw new MachineError(UNREACHABLE_STATE) | ||
} | ||
this.history.length = this.history.indexOf(last) + 1 | ||
} else { | ||
this.history.pop() | ||
} | ||
return this | ||
@@ -201,0 +210,0 @@ } |
{ | ||
"name": "@qiwi/cyclone", | ||
"version": "2.4.0", | ||
"version": "2.5.0", | ||
"description": "\"State machine\" for basic purposes", | ||
@@ -5,0 +5,0 @@ "main": "lib/es5/index.js", |
@@ -5,2 +5,3 @@ declare module '@qiwi/cyclone/lib/es5/error' { | ||
export const LOCK_VIOLATION: string; | ||
export const UNREACHABLE_STATE: string; | ||
export class MachineError extends Error { | ||
@@ -98,7 +99,7 @@ } | ||
*/ | ||
current(): IDigest; | ||
current(): IHistoryItem; | ||
/** | ||
* Returns the last state, that satisfies the condition | ||
*/ | ||
last(condition?: string | IPredicate): IDigest | void; | ||
last(condition?: string | IPredicate): IHistoryItem | void; | ||
/** | ||
@@ -108,3 +109,3 @@ * Reverts current state to the previous. | ||
*/ | ||
prev(state?: string): IMachine; | ||
prev(state?: string | IPredicate): IMachine; | ||
/** | ||
@@ -111,0 +112,0 @@ * Locks the machine. Any transitions are prohibited before unlocking. |
@@ -13,2 +13,3 @@ /** | ||
declare export var LOCK_VIOLATION: string; | ||
declare export var UNREACHABLE_STATE: string; | ||
declare export class MachineError mixins Error {} | ||
@@ -118,3 +119,3 @@ } | ||
*/ | ||
current(): IDigest; | ||
current(): IHistoryItem; | ||
@@ -124,3 +125,3 @@ /** | ||
*/ | ||
last(condition?: string | IPredicate): IDigest | void; | ||
last(condition?: string | IPredicate): IHistoryItem | void; | ||
@@ -131,3 +132,3 @@ /** | ||
*/ | ||
prev(state?: string): IMachine; | ||
prev(state?: string | IPredicate): IMachine; | ||
@@ -134,0 +135,0 @@ /** |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60758
1100