-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbpmn-instances.js
151 lines (123 loc) · 4.64 KB
/
bpmn-instances.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* global Bpmn:true */
import { Mongo } from 'meteor/mongo'
import { Meteor } from 'meteor/meteor'
import { check, Match } from 'meteor/check'
const {EventEmitter} = require('events')
const isString = s => s && typeof s === 'string' && s.length > 0
// //////////////////////////////////////////////////////////////////////////////////////
//
// Define Instances Collection
//
// //////////////////////////////////////////////////////////////////////////////////////
const BpmnEngineInstancesSchema = {
instanceId: String,
}
const collectionName = 'BpmnEngineInstances'
const BpmnEngineInstances = new Mongo.Collection(collectionName)
BpmnEngineInstances.name = collectionName
BpmnEngineInstances.schema = BpmnEngineInstancesSchema
// wipe collection
// at startup
Meteor.startup(() => {
if (Meteor.isServer) {
const allInstances = BpmnEngineInstances.find({}).fetch()
BpmnEngineInstances.remove({})
allInstances.map(entry => entry.instanceId).forEach(instanceId => {
Bpmn.processes.collection.update({instanceId}, {$set: {state: Bpmn.States.stopped}})
})
}
})
// //////////////////////////////////////////////////////////////////////////////////////
//
// Define API
//
// //////////////////////////////////////////////////////////////////////////////////////
const _cache = {}
const instances = {}
instances.ns = 'extensions.instances'
instances.name = 'Instances'
instances.description = 'Manages running in-memory process instances.'
instances.collection = BpmnEngineInstances
instances.methods = {}
instances.get = function get (instanceId) {
check(instanceId, Match.Where(isString))
return _cache[instanceId]
}
instances.size = function size () {
return Object.keys(_cache).length
}
instances.has = function has (instanceId) {
check(instanceId, Match.Where(isString))
return !!BpmnEngineInstances.findOne({instanceId}) && !!_cache[instanceId]
}
instances.add = function add ({instanceId, engine}) {
check(instanceId, Match.Where(isString))
check(engine, Match.Where(en => en && en.constructor === EventEmitter))
if (instances.has(instanceId))
throw new Error('documemnt exists by instanceid')
const insertId = BpmnEngineInstances.insert({instanceId})
if (!insertId)
throw new Error('could not insert instance record by instanceId ' + instanceId)
_cache[instanceId] = engine
if (!instances.has(instanceId))
throw new Error('could not add engine to cache by instanceId ' + instanceId)
return insertId
}
// //////////////////////////////////////////////////////////////////////////////////////
//
// removeInstance
//
// //////////////////////////////////////////////////////////////////////////////////////
instances.remove = function remove ({instanceId}) {
check(instanceId, Match.Where(isString))
if (!instances.has(instanceId))
throw new Error('engine instance not found by id ' + instanceId)
return (delete _cache[instanceId]) && BpmnEngineInstances.remove({instanceId})
}
instances.clear = function () {
BpmnEngineInstances.find({}).fetch().forEach((entry) => {
instances.remove({instanceId: entry.instanceId})
})
return BpmnEngineInstances.find().count() === 0
}
// //////////////////////////////////////////////////////////////////////////////////////
//
// Hooks
//
// //////////////////////////////////////////////////////////////////////////////////////
const instanceHooks = {}
instanceHooks.onExecuteBefore = function (engineFct) {
const engine = engineFct()
engine.on('end', Meteor.bindEnvironment(() => {
const removed = instances.remove({instanceId: engine.instanceId})
if (!removed)
throw new Error('expected instance to be removed by instanceId ', engine.instanceId)
}))
}
instanceHooks.onExecuteAfter = Meteor.bindEnvironment(function (engineFct) {
const engine = engineFct()
instances.add({instanceId: engine.instanceId, engine})
})
instanceHooks.onResumeAfter = Meteor.bindEnvironment(function (engineFct) {
const engine = engineFct()
engine.on('end', Meteor.bindEnvironment(() => {
const removed = instances.remove({instanceId: engine.instanceId})
if (!removed)
throw new Error('expected instance to be removed by instanceId ', engine.instanceId)
}))
instances.add({instanceId: engine.instanceId, engine})
})
instances.hooks = instanceHooks
instances.on = function on () {
Bpmn.hooks.add(instances.ns, instanceHooks)
}
instances.off = function off () {
Bpmn.hooks.remove(instances.ns)
}
// //////////////////////////////////////////////////////////////////////////////////////
//
// Assign
//
// //////////////////////////////////////////////////////////////////////////////////////
Bpmn.instances = instances
Bpmn.extensions.add(instances.ns, instances, true)