1818 * limitations under the License.
1919 */
2020
21- /**
21+ /**
2222 * @module behavior
2323 * @requires db
2424 * @requires helper
2525 * @requires channel
26- * @description This module manages the behaviors of all components.
27- * A behavior is a mecanism that allow users to add actions that will be executed
26+ * @description This module manages the behaviors of all components.
27+ * A behavior is a mecanism that allow users to add actions that will be executed
2828 * when a specific state of a component will change.
2929 */
3030
3333var $db = require ( './db.js' ) ;
3434var $helper = require ( './helper.js' ) ;
3535
36-
3736/* Private properties */
3837
39-
4038var store = { } ;
4139
42-
4340/* Private methods */
4441
45-
46-
4742/**
4843 * @method createFunction
49- * @param {String } name default name of the function
44+ * @param {String } name default name of the function
5045 * @param {String } func a stringified function
5146 * @param {Boolean } core if true, the behavior will be treated as a System Runtime core behavior.
5247 * In that case, the behavior can not be exported in a system (default false)
@@ -81,7 +76,10 @@ function createFunction(name, func, core, useCoreAPI) {
8176 header = header . replace ( '=>' , '' ) ;
8277
8378 if ( header . indexOf ( '(' ) !== - 1 ) {
84- funcParams = header . split ( '(' ) [ 1 ] . replace ( ')' , '' ) . trim ( ) ;
79+ funcParams = header
80+ . split ( '(' ) [ 1 ]
81+ . replace ( ')' , '' )
82+ . trim ( ) ;
8583 } else {
8684 funcParams = header . trim ( ) ;
8785 }
@@ -108,7 +106,10 @@ function createFunction(name, func, core, useCoreAPI) {
108106 beginBody = func . indexOf ( '{' ) ;
109107 header = func . substring ( 0 , beginBody ) ;
110108
111- funcParams = header . split ( '(' ) [ 1 ] . replace ( ')' , '' ) . trim ( ) ;
109+ funcParams = header
110+ . split ( '(' ) [ 1 ]
111+ . replace ( ')' , '' )
112+ . trim ( ) ;
112113
113114 params = funcParams . split ( ',' ) ;
114115 params . forEach ( function ( param ) {
@@ -133,23 +134,35 @@ function createFunction(name, func, core, useCoreAPI) {
133134 }
134135
135136 if ( params [ 0 ] !== '' ) {
136- action = new Function ( '__body' , "return function " + name + " (" + params . join ( ',' ) + ") { return new Function('" + params . join ( "','" ) + "', __body).apply(this, arguments) };" ) ( funcBody ) ;
137+ action = new Function (
138+ '__body' ,
139+ 'return function ' +
140+ name +
141+ ' (' +
142+ params . join ( ',' ) +
143+ ") { return new Function('" +
144+ params . join ( "','" ) +
145+ "', __body).apply(this, arguments) };"
146+ ) ( funcBody ) ;
137147 } else {
138- action = new Function ( '__body' , "return function " + name + " () { return new Function(__body).apply(this, arguments) };" ) ( funcBody ) ;
148+ action = new Function (
149+ '__body' ,
150+ 'return function ' +
151+ name +
152+ ' () { return new Function(__body).apply(this, arguments) };'
153+ ) ( funcBody ) ;
139154 }
140155
141156 return action ;
142157}
143158
144-
145159/* Public methods */
146160
147-
148161/**
149162 * @method add
150163 * @param {String } id id of the component
151- * @param {Object } state the state on which the action will be executed
152- * @param {Object } action the action to execute when the component will have a specific state
164+ * @param {Object } state the state on which the action will be executed
165+ * @param {Object } action the action to execute when the component will have a specific state
153166 * @param {Boolean } useCoreAPI if true, System Runtime core modules will be injected as parameters of the action (default false)
154167 * @param {Boolean } core if true, behavior can not be exported
155168 * @returns {String } id of the behavior created in System Runtime database
@@ -171,18 +184,17 @@ exports.add = function add(id, state, action, useCoreAPI, core) {
171184 store [ behaviorId ] = action ;
172185
173186 $db . _Behavior . insert ( {
174- ' _id' : behaviorId ,
175- ' component' : id ,
176- ' state' : state ,
177- ' action' : strAction ,
178- ' useCoreAPI' : useCoreAPI ,
179- ' core' : core
187+ _id : behaviorId ,
188+ component : id ,
189+ state : state ,
190+ action : strAction ,
191+ useCoreAPI : useCoreAPI ,
192+ core : core
180193 } ) ;
181194
182195 return behaviorId ;
183196} ;
184197
185-
186198/**
187199 * @method remove
188200 * @param {Object } params
@@ -203,20 +215,20 @@ exports.remove = function remove(params) {
203215 if ( params . componentId ) {
204216 if ( params . behaviorId ) {
205217 $db . _Behavior . remove ( {
206- ' _id' : params . behaviorId ,
207- ' component' : params . componentId ,
208- ' state' : params . state
218+ _id : params . behaviorId ,
219+ component : params . componentId ,
220+ state : params . state
209221 } ) ;
210222 delete store [ params . behaviorId ] ;
211223 } else {
212224 if ( params . state ) {
213225 result = $db . _Behavior . remove ( {
214- ' component' : params . componentId ,
215- ' state' : params . state
226+ component : params . componentId ,
227+ state : params . state
216228 } ) ;
217229 } else {
218230 result = $db . _Behavior . remove ( {
219- ' component' : params . componentId
231+ component : params . componentId
220232 } ) ;
221233 }
222234 result . forEach ( function ( id ) {
@@ -226,7 +238,6 @@ exports.remove = function remove(params) {
226238 }
227239} ;
228240
229-
230241/**
231242 * @method removeFromMemory
232243 * @param {String } id id of the component
@@ -236,7 +247,6 @@ exports.removeFromMemory = function removeFromMemory(id) {
236247 delete store [ id ] ;
237248} ;
238249
239-
240250/**
241251 * @method getActions
242252 * @param {String } id id of the component
@@ -250,26 +260,30 @@ exports.getActions = function getActions(id, state) {
250260 var action = null ;
251261
252262 dbResult = $db . _Behavior . find ( {
253- ' component' : id ,
254- ' state' : state
263+ component : id ,
264+ state : state
255265 } ) ;
256266
257267 dbResult . forEach ( function ( behavior ) {
258268 action = store [ behavior . _id ] ;
259269 if ( typeof action === 'undefined' ) {
260- action = createFunction ( behavior . state , behavior . action , behavior . core , behavior . useCoreAPI ) ;
270+ action = createFunction (
271+ behavior . state ,
272+ behavior . action ,
273+ behavior . core ,
274+ behavior . useCoreAPI
275+ ) ;
261276 store [ behavior . _id ] = action ;
262277 }
263278 result . push ( {
264- ' useCoreAPI' : behavior . useCoreAPI ,
265- ' action' : action
279+ useCoreAPI : behavior . useCoreAPI ,
280+ action : action
266281 } ) ;
267282 } ) ;
268283
269284 return result ;
270285} ;
271286
272-
273287/**
274288 * @method clear
275289 * @description Remove all the behaviors stored in memory
@@ -278,7 +292,6 @@ exports.clear = function clear() {
278292 store = { } ;
279293} ;
280294
281-
282295/**
283296 * @method get
284297 * @param {String } id id of the behavior
0 commit comments