Skip to content

Commit 679c1c1

Browse files
author
Tom Kirkpatrick
committed
Inject state machine methods directly onto model instances
1 parent 4e5fad0 commit 679c1c1

File tree

2 files changed

+10
-63
lines changed

2 files changed

+10
-63
lines changed

lib/mixins/state-machine.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,11 @@ module.exports = function StateMachine(Model, settings) {
109109

110110
debug('Initialized events: %o', Model.__FSM_CONFIG__.events)
111111
debug('Initialized handlers: %o', handlers)
112+
113+
// Add event methods to the prototype.
114+
Model.getEventNames(settings.events).forEach(eventName => {
115+
Model.prototype[eventName] = function event(...args) {
116+
return Model.app.getStateMachine(this)[eventName](this, ...args)
117+
}
118+
})
112119
}
Lines changed: 3 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,9 @@
11
const FcSubscription = require('../providers/subscription')
2-
const assert = require('assert')
3-
const log = require('fullcube-logger')
4-
const createPromiseCallback = require('loopback-datasource-juggler/lib/utils').createPromiseCallback
52

63
module.exports = function SubscriptionModel(Subscription) {
7-
Subscription.observe('fsm:oncancel', ctx => {
8-
log.debug('fsm:oncancel')
9-
return new FcSubscription(ctx.instance).cancel()
10-
.then(() => ctx)
11-
})
4+
Subscription.observe('fsm:oncancel', ctx => new FcSubscription(ctx.instance).cancel().then(() => ctx))
125

13-
Subscription.observe('fsm:onreactivate', ctx => {
14-
log.debug('fsm:onreactivate')
15-
return new FcSubscription(ctx.instance).reactivate()
16-
.then(() => ctx)
17-
})
6+
Subscription.observe('fsm:onreactivate', ctx => new FcSubscription(ctx.instance).reactivate().then(() => ctx))
187

19-
Subscription.observe('fsm:onexpire', ctx => {
20-
log.debug('fsm:onexpire')
21-
return new FcSubscription(ctx.instance).expire()
22-
.then(() => ctx)
23-
})
24-
25-
/**
26-
* Cancel a subscription
27-
* @param {Function(Error)} callback
28-
*/
29-
Subscription.prototype.cancel = function cancel(cb) {
30-
cb = cb || createPromiseCallback()
31-
assert(typeof cb === 'function', 'The cb argument must be a function')
32-
33-
Subscription.app.getStateMachine(this).cancel(this)
34-
.then(res => cb(null, res))
35-
.catch(cb)
36-
37-
return cb.promise
38-
}
39-
40-
/**
41-
* Reactive a canceled subscription
42-
* @param {Function(Error, )} callback
43-
*/
44-
Subscription.prototype.reactivate = function reactivate(cb) {
45-
cb = cb || createPromiseCallback()
46-
assert(typeof cb === 'function', 'The cb argument must be a function')
47-
48-
Subscription.app.getStateMachine(this).reactivate(this)
49-
.then(res => cb(null, res))
50-
.catch(cb)
51-
52-
return cb.promise
53-
}
54-
55-
/**
56-
* Expire a subscription
57-
* @param {Function(Error, )} callback
58-
*/
59-
Subscription.prototype.expire = function reactivate(cb) {
60-
cb = cb || createPromiseCallback()
61-
assert(typeof cb === 'function', 'The cb argument must be a function')
62-
63-
Subscription.app.getStateMachine(this).expire(this)
64-
.then(res => cb(null, res))
65-
.catch(cb)
66-
67-
return cb.promise
68-
}
8+
Subscription.observe('fsm:onexpire', ctx => new FcSubscription(ctx.instance).expire().then(() => ctx))
699
}

0 commit comments

Comments
 (0)