240
Prepare a delta of events to send to the client since the last time they
241
asked. The deltas list is prepared nearly the same way depending on Py or Go
242
implentation, but the data within the individual deltas must be structured
243
dependent on the backend. This method is called using `apply` from within
244
the appropriate sandbox so that `this._deltaWhitelist` and
245
`self._getDeltaAttrs` can structure the delta according to the juju type.
247
@method _prepareDelta
248
@return {Array} An array of deltas events.
250
var _prepareDelta = function() {
252
var state = this.get('state');
254
var changes = state.nextChanges();
255
if (changes && changes.error) {
258
var annotations = state.nextAnnotations();
259
if (annotations && annotations.error) {
262
if (changes || annotations) {
263
Y.each(this._deltaWhitelist, function(whitelist, changeType) {
264
var collectionName = changeType + 's';
266
Y.each(changes[collectionName], function(change) {
267
var attrs = self._getDeltaAttrs(change[0], whitelist);
268
var action = change[1] ? 'change' : 'remove';
269
// The unit changeType is actually "serviceUnit" in the Python
270
// stream. Our model code handles either, so we're not modifying
272
var delta = [changeType, action, attrs];
277
Y.each(annotations[changeType + 's'], function(attrs, key) {
278
if (!changes || !changes[key]) {
279
attrs = self._getDeltaAttrs(attrs, whitelist);
280
// Special case environment handling.
281
if (changeType === 'annotation') {
282
changeType = 'annotations';
283
attrs = attrs.annotations;
285
deltas.push([changeType, 'change', attrs]);
295
240
A sandbox Juju environment using the Python API.
336
Prepare a delta of events to send to the client since the last time they
337
asked. The deltas list is prepared nearly the same way depending on Py
338
or Go implentation, but the data within the individual deltas must be
339
structured dependent on the backend. This method is called using `apply`
340
from within the appropriate sandbox so that `this._deltaWhitelist` and
341
`self._getDeltaAttrs` can structure the delta according to the juju type.
343
@method _prepareDelta
344
@return {Array} An array of deltas events.
346
_prepareDelta: function() {
348
var state = this.get('state');
350
var changes = state.nextChanges();
351
if (changes && changes.error) {
354
var annotations = state.nextAnnotations();
355
if (annotations && annotations.error) {
358
if (changes || annotations) {
359
Y.each(this._deltaWhitelist, function(whitelist, changeType) {
360
var collectionName = changeType + 's';
362
Y.each(changes[collectionName], function(change) {
363
var attrs = self._getDeltaAttrs(change[0], whitelist);
364
var action = change[1] ? 'change' : 'remove';
365
// The unit changeType is actually "serviceUnit" in the Python
366
// stream. Our model code handles either, so we're not modifying
368
var delta = [changeType, action, attrs];
373
Y.each(annotations[changeType + 's'], function(attrs, key) {
374
if (!changes || !changes[key]) {
375
attrs = self._getDeltaAttrs(attrs, whitelist);
376
// Special case environment handling.
377
if (changeType === 'annotation') {
378
changeType = 'annotations';
379
attrs = attrs.annotations;
381
deltas.push([changeType, 'change', attrs]);
391
391
Send a delta of events to the client from since the last time they asked.
393
393
@method sendDelta
394
394
@return {undefined} Nothing.
396
396
sendDelta: function() {
397
var deltas = _prepareDelta.apply(this);
397
var deltas = this._prepareDelta();
398
398
if (deltas.length) {
399
399
this.get('client').receiveNow({op: 'delta', result: deltas});
949
Prepare a delta of events to send to the client since the last time they
950
asked. The deltas list is prepared nearly the same way depending on Py
951
or Go implentation, but the data within the individual deltas must be
952
structured dependent on the backend. This method is called using `apply`
953
from within the appropriate sandbox so that `this._deltaWhitelist` and
954
`self._getDeltaAttrs` can structure the delta according to the juju type.
956
@method _prepareDelta
957
@return {Array} An array of deltas events.
959
_prepareDelta: function() {
961
var state = this.get('state');
963
var changes = state.nextChanges();
964
if (changes && changes.error) {
968
Y.each(this._deltaWhitelist, function(whitelist, changeType) {
969
var collectionName = changeType + 's';
971
Y.each(changes[collectionName], function(change) {
972
var attrs = self._getDeltaAttrs(change[0], whitelist);
973
var action = change[1] ? 'change' : 'remove';
974
// The unit changeType is actually "serviceUnit" in the Python
975
// stream. Our model code handles either, so we're not modifying
977
var delta = [changeType, action, attrs];
988
Prepare the juju-core specific version of annotation information.
989
This has its own (core styled) formatting. This is different from
990
the Python version which includes annotations in the normal object
993
@method _prepareAnnotations
994
@return [ {Object} ] Array of annotation deltas.
996
_prepareAnnotations: function() {
997
var state = this.get('state');
999
var annotations = state.nextAnnotations();
1000
if (annotations && annotations.error) {
1004
Y.each(this._deltaWhitelist, function(whitelist, changeType) {
1005
Y.each(annotations[changeType + 's'], function(model, key) {
1006
var attrs = models.getAnnotations(model);
1007
var tag = this.modelToTag(model);
1008
// This form will trigger the annotationInfo handler.
1009
deltas.push(['annotation', 'change', {
1010
Tag: tag, Annotations: attrs}]);
1018
Map from a model to a Tag name as used in juju-core
1021
@param {Object} model
1022
@return {String} tag.
1024
modelToTag: function(model) {
1025
var name = model.name;
1026
if (name === 'serviceUnit') {
1028
} else if (name === 'environment') {
1031
return name + '-' + (model.id || model.get('id'));
945
1037
Send a delta of events to the client from since the last time they asked.
947
1039
@method sendDelta
950
1042
sendDelta: function() {
951
1043
var nextRequestId = this.get('nextRequestId');
952
1044
if (nextRequestId) {
953
var deltas = _prepareDelta.apply(this);
1045
var deltas = this._prepareDelta();
1046
var annotations = this._prepareAnnotations();
1047
if (deltas.length || annotations.length) {
955
1048
this.get('client').receive({
956
1049
RequestId: this.get('nextRequestId'),
957
Response: {Deltas: deltas}
1050
Response: {Deltas: deltas.concat(annotations)}
959
1052
// Prevent sending additional deltas until the Go environment is
960
1053
// ready for them (when the next `Next` message is sent).
1264
1357
@return {undefined} Side effects only.
1266
1359
handleClientSetAnnotations: function(data, client, state) {
1267
var serviceId = /service-([^ ]*)$/.exec(data.Params.Tag)[1];
1268
var result = state.updateAnnotations(serviceId, data.Params.Pairs);
1360
var entityId = /^(service|unit|machine|environment)-([^ ]*)$/.
1361
exec(data.Params.Tag)[2];
1362
var result = state.updateAnnotations(entityId, data.Params.Pairs);
1269
1363
this._basicReceive(data, client, result);