ice-frontend-react-mobx
Version:
ICE Frontend REACT+MobX
92 lines (78 loc) • 2.38 kB
JavaScript
const debug = require('debug')('ice:models:rack'); // eslint-disable-line no-unused-vars
import { computed, observable, reaction } from 'mobx';
import debounce from '../helpers/debounce';
export const RackDefaults = {
name: null,
allocatedPower: null,
priority: 1,
redundancyStrategy: '2N',
rPdus: [],
defaultRpdus: []
};
export class Rack {
_id = null;
name = RackDefaults.name;
allocatedPower = RackDefaults.allocatedPower;
priority = RackDefaults.priority;
redundancyStrategy = RackDefaults.redundancyStrategy;
rPdus = RackDefaults.rPdus;
rPdusString = '';
defaultRpdus = RackDefaults.defaultRpdus;
defaultRpdusString = '';
io = null;
broadcaster = null;
constructor (io) {
this.io = io;
this.broadcaster = reaction(
() => this.changed,
debounce((json) => {
debug('Rack updated:', json);
this.io.emit('msg', { type: 'rack_updated', data: this.toJson });
})
);
}
get id () {
return this._id;
}
get toJson () {
return {
id: this.id,
name: this.name,
allocatedPower: Number(this.allocatedPower),
priority: this.priority,
redundancyStrategy: this.redundancyStrategy,
rPdus: this.rPdus,
defaultRpdus: this.defaultRpdus
};
}
get changed () {
return {
id: this.id,
name: this.name,
allocatedPower: Number(this.allocatedPower),
priority: this.priority,
redundancyStrategy: this.redundancyStrategy,
rPdus: this.rPdusString,
defaultRpdus: this.defaultRpdusString
};
}
updateFromJson (json) {
this._id = json.id;
this.name = json.name || RackDefaults.name;
this.allocatedPower = json.allocatedPower || RackDefaults.allocatedPower;
this.priority = json.priority || RackDefaults.priority;
this.redundancyStrategy = json.redundancyStrategy || RackDefaults.redundancyStrategy;
if (Array.isArray(json.rPdus)) {
this.rPdus = json.rPdus;
this.rPdusString = json.rPdus.join(',');
}
if (Array.isArray(json.defaultRpdus)) {
this.defaultRpdus = json.defaultRpdus;
this.defaultRpdusString = json.defaultRpdus.join(',');
}
}
dispose () {
this.io.emit('msg', { type: 'rack_deleted', data: this.id });
this.broadcaster();
}
}