-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.js
127 lines (112 loc) · 2.81 KB
/
service.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
/**
* @license MIT http://troopjs.mit-license.org/
*/
define([
"troopjs-hub/component",
"troopjs-hub/emitter"
], function (Component, hub) {
"use strict";
var UNDEFINED;
var DICTIONARY = "dictionary";
/**
* Provides localization as a service
* @class l10n.service
* @extend hub.component
* @alias service.l10n
*/
/**
* Triggered when the localization service starts
* @event hub/l10n/start
*/
/**
* Triggered when the localization service stops
* @event hub/l10n/stop
*/
/**
* Triggered when a localization GET is performed
* @event hub/l10n/get
* @param {String} key Localization key
* @param {String} [value] Default value
*/
/**
* Triggered when a localization FETCH is performed
* @event hub/l10n/fetch
* @inheritdoc #event-hub/l10n/get
*/
/**
* Triggered when a localization PUT is performed
* @event hub/l10n/put
* @param {String} key Localization key
* @param {String} value Localization value
*/
/**
* Triggered when a localization UPDATE is performed
* @event hub/l10n/update
* @inheritdoc #event-hub/l10n/put
*/
/**
* @method constructor
*/
return Component.extend(function () {
this[DICTIONARY] = {};
}, {
"displayName": "l10n/service",
/**
* @handler
* @inheritdoc #event-sig/start
* @localdoc Notifies interested parties about the service start
* @fires hub/l10n/start
*/
"sig/start": function () {
return hub.emit("l10n/start");
},
/**
* @handler
* @inheritdoc #event-sig/stop
* @localdoc Notifies interested parties about the service stop
* @fires hub/l10n/stop
*/
"sig/stop": function () {
return hub.emit("l10n/stop");
},
/**
* @handler
* @inheritdoc #event-hub/l10n/get
* @localdoc Gets localization
* @fires hub/l10n/fetch
* @fires hub/l10n/put
* @return {Array}
*/
"hub/l10n/get": function (key, value) {
var me = this;
var dictionary = me[DICTIONARY];
return me.task(function (resolve, reject) {
if (dictionary.hasOwnProperty(key)) {
resolve([ key, dictionary[key] ]);
}
else {
hub
.emit("l10n/fetch", key, UNDEFINED)
.spread(function (_key, _value) {
return _value === UNDEFINED
? [ key, value ]
: hub.emit("l10n/put", _key, _value);
})
.then(resolve, reject);
}
});
},
/**
* @handler
* @inheritdoc #event-hub/l10n/put
* @localdoc Puts localization
* @fires hub/l10n/update
* @return {Array}
*/
"hub/l10n/put": function (key, value) {
return hub
.emit("l10n/update", key, this[DICTIONARY][key] = value)
.yield([ key, value ]);
}
});
});