Refactoring

This commit is contained in:
Henry 2016-07-13 19:50:58 -07:00
parent 14463d39a8
commit 2baca659ca
4 changed files with 212 additions and 0 deletions

View File

@ -0,0 +1,70 @@
/*****************************************************************************
* Open MCT Web, Copyright (c) 2014-2015, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT Web includes source code licensed under additional open source
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
define(['./TickSource'], function (TickSource) {
/**
* @interface
* @constructor
*/
function PeriodicTickSource ($timeout, interval) {
TickSource.call(this);
this.interval = interval;
this.$timeout = $timeout;
this.timeoutHandle = undefined;
}
PeriodicTickSource.prototype = Object.create(TickSource.prototype);
PeriodicTickSource.prototype.start = function () {
this.timeoutHandle = this.$timeout(this.tick.bind(this), this.interval);
};
PeriodicTickSource.prototype.stop = function () {
if (this.timeoutHandle) {
this.$timeout.cancel(this.timeoutHandle);
}
};
PeriodicTickSource.prototype.tick = function () {
this.listeners.forEach(function (listener){
listener();
});
};
PeriodicTickSource.prototype.listen = function (listener) {
this.listeners.push(listener);
if (this.listeners.length === 1){
this.start();
}
return function () {
this.listeners.splice(listeners.indexOf(listener));
if (this.listeners.length === 0) {
this.stop();
}
}.bind(this);
};
return TimeSystem;
});

View File

@ -0,0 +1,45 @@
/*****************************************************************************
* Open MCT Web, Copyright (c) 2014-2015, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT Web includes source code licensed under additional open source
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
define([], function () {
/**
* A tick source is a timing of timing signals. Usage is simple, a
* listener registers a callback which is invoked when this source 'ticks'.
*
* @interface
* @constructor
*/
function TickSource () {
this.listeners = [];
}
/**
* @param callback Function to be called when this tick source ticks.
* @returns an 'unlisten' function that will remove the callback from
* the registered listeners
*/
TickSource.prototype.listen = function (callback) {
throw new Error('Not implemented');
};
return TimeSystem;
});

View File

@ -0,0 +1,45 @@
/*****************************************************************************
* Open MCT Web, Copyright (c) 2014-2015, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT Web includes source code licensed under additional open source
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
define([], function () {
/**
* @interface
* @constructor
*/
function TimeSystem () {
this.metadata = undefined;
}
TimeSystem.prototype.formats = function () {
throw new Error('Not implemented');
};
TimeSystem.prototype.tickSources = function () {
throw new Error('Not implemented');
};
TimeSystem.prototype.defaultBounds = function () {
throw new Error('Not implemented');
};
return TimeSystem;
});

View File

@ -0,0 +1,52 @@
/*****************************************************************************
* Open MCT Web, Copyright (c) 2014-2015, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT Web includes source code licensed under additional open source
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
define(['./TimeSystem'], function (TimeSystem) {
var FIFTEEN_MINUTES = 15 * 60 * 1000;
/**
* @implements TimeSystem
* @constructor
*/
function UTCTimeSystem () {
TimeSystem.call(this);
this._formats = [];
this._tickSources = [];
}
UTCTimeSystem.prototype = Object.create(TimeSystem.prototype);
UTCTimeSystem.prototype.formats = function () {
return this._formats;
};
UTCTimeSystem.prototype.tickSources = function () {
return this._tickSources;
};
UTCTimeSystem.prototype.defaultBounds = function () {
var now = Math.ceil(Date.now() / 1000) * 1000;
return {start: now - FIFTEEN_MINUTES, end: now};
};
return UTCTimeSystem;
});