forked from wpbakery/vc-cake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
105 lines (104 loc) · 2.6 KB
/
index.js
File metadata and controls
105 lines (104 loc) · 2.6 KB
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
// Inner modules
var enVars = require('./config/settings').env
var constants = require('./config/settings').constants
var services = require('./lib/services')
var events = require('./lib/events')
var scopes = require('./lib/scopes')
var state = require('./lib/state')
var storages = require('./lib/storages')
var ModuleAPI = require('./lib/module-api-constructor')
var CakeException = require('./lib/exception')
/**
* @constructor
*/
var App = function () {
}
App.prototype.add = function (scope, fn) {
scopes.add(scope, fn, new ModuleAPI(scope))
if (enVars.get('started')) {
scopes.load()
}
return this
}
App.prototype.getService = function (name) {
return services.get(name)
}
App.prototype.addService = function (name, obj) {
services.add(name, obj)
return this
}
App.prototype.env = function (key, value) {
var returnValue = this
if (key && typeof value !== 'undefined') {
if (constants.START_EVENT === key) {
new CakeException().throw('Error! You can\'t set %s var. This var can be set only by app\'s start method', key)
} else {
enVars.set(key, value)
}
} else if (key) {
returnValue = enVars.get(key)
}
return returnValue
}
App.prototype.start = function (fn) {
try {
if (enVars.get('started') === false) {
if (typeof fn === 'function') {
fn()
}
scopes.load()
enVars.set('started', true)
events.publish('app', 'event', constants.START_EVENT, true, {})
} else {
if (typeof fn === 'function') {
fn()
}
}
} catch (e) {
if (enVars.get('debug')) {
throw e
}
}
return this
}
App.prototype.end = function (fn) {
if (enVars.get('started') === true) {
if (typeof fn === 'function') {
fn()
}
scopes.clear()
services.clear()
enVars.set('started', false)
events.publish('app', 'event', 'end', true, {})
}
return this
}
App.prototype.state = function () {
return enVars.get('started') ? 'running' : 'stopped'
}
App.prototype.remove = function (name) {
scopes.remove(name)
}
App.prototype.getData = function (key) {
return state.get(key)
}
App.prototype.setData = function (key, value) {
state.set(key, value)
return this
}
App.prototype.onDataChange = function (key, fn, options) {
state.onChange(key, fn, options)
}
App.prototype.ignoreDataChange = function (key, fn, options) {
state.ignoreChange(key, fn, options)
}
App.prototype.getDataAll = function () {
return state.info()
}
App.prototype.getStorage = function (name) {
return storages.get(name)
}
App.prototype.addStorage = function (name, fn) {
return storages.add(name, fn)
}
module.exports = new App()