Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use stateProperty to get instance's state #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = function loopbackComponentFsm(app) {

function getModelName(instance) {
return instance.constructor.definition.name
}
}

function generateCacheKey(instance) {
if (!instance.id) {
Expand Down Expand Up @@ -54,7 +54,9 @@ module.exports = function loopbackComponentFsm(app) {
throw new Error(`${modelName} does not have state machine support.`)
}

config = Object.assign(config, { initial: instance.status })
let stateProperty = config.stateProperty;

config = Object.assign(config, { initial: instance[stateProperty] })
cache = new StateMachine(config)
debug('Created new state machine for instance %s with config %o', instance.id, config)

Expand Down
3 changes: 2 additions & 1 deletion lib/mixins/state-machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = function StateMachine(Model, settings) {

// Initialize the main StateMachine config.
Model.__FSM_CONFIG__ = {
stateProperty: stateProperty,
events: settings.events,
callbacks: {
// When leaving any state, grab the initial model instance and set in options for eacy access.
Expand Down Expand Up @@ -134,7 +135,7 @@ module.exports = function StateMachine(Model, settings) {
const force = _.get(params, 'force')

const origFrom = Model.__FSM_CONFIG__.events[eventNameIndex].from
const newFrom = [ ...origFrom, this.status ]
const newFrom = [ ...origFrom, this[stateProperty] ]

// If force has been requested, override the state machine config.
if (force && (forceFsmSetting || forceEventSetting)) {
Expand Down