mirror of
https://github.com/nasa/openmct.git
synced 2025-06-14 21:28:12 +00:00
[Core] Bring in core bundle from sandbox
Bring in bundle platform/core from the sandbox branch, in preparation for clean up, tests, and integration. WTD-573
This commit is contained in:
107
platform/core/src/objects/DomainObject.js
Normal file
107
platform/core/src/objects/DomainObject.js
Normal file
@ -0,0 +1,107 @@
|
||||
/*global define,Promise*/
|
||||
|
||||
/**
|
||||
* Module defining DomainObject. Created by vwoeltje on 11/7/14.
|
||||
*/
|
||||
define(
|
||||
[],
|
||||
function () {
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Construct a new domain object with the specified
|
||||
* identifier, model, and capabilities.
|
||||
*
|
||||
* @param {string} id the object's unique identifier
|
||||
* @param {object} model the "JSONifiable" state of the object
|
||||
* @param {Object.<string, Capability|function} capabilities all
|
||||
* capabilities to be exposed by this object
|
||||
* @constructor
|
||||
*/
|
||||
function DomainObject(id, model, capabilities) {
|
||||
return {
|
||||
/**
|
||||
* Get the unique identifier for this domain object.
|
||||
* @return {string} the domain object's unique identifier
|
||||
* @memberof DomainObject#
|
||||
*/
|
||||
getId: function () {
|
||||
return id;
|
||||
},
|
||||
|
||||
/**
|
||||
* Get the domain object's model. This is useful to
|
||||
* directly look up known properties of an object, but
|
||||
* direct modification of a returned model is generally
|
||||
* discouraged and may result in errors. Instead, an
|
||||
* object's "mutation" capability should be used.
|
||||
*
|
||||
* @return {object} the domain object's persistent state
|
||||
* @memberof DomainObject#
|
||||
*/
|
||||
getModel: function () {
|
||||
return model;
|
||||
},
|
||||
|
||||
/**
|
||||
* Get a capability associated with this object.
|
||||
* Capabilities are looked up by string identifiers;
|
||||
* prior knowledge of a capability's interface is
|
||||
* necessary.
|
||||
*
|
||||
* @return {Capability} the named capability, or undefined
|
||||
* if not present.
|
||||
* @memberof DomainObject#
|
||||
*/
|
||||
getCapability: function (name) {
|
||||
var capability = capabilities[name];
|
||||
return typeof capability === 'function' ?
|
||||
capability(this) : capability;
|
||||
},
|
||||
|
||||
/**g
|
||||
* Check if this domain object supports a capability
|
||||
* with the provided name.
|
||||
*
|
||||
* @param {string} name the name of the capability to
|
||||
* check for
|
||||
* @returns {boolean} true if provided
|
||||
*/
|
||||
hasCapability: function hasCapability(name) {
|
||||
return this.getCapability(name) !== undefined;
|
||||
},
|
||||
|
||||
/**
|
||||
* Use a capability of an object; this is a shorthand
|
||||
* for:
|
||||
*
|
||||
* ```
|
||||
* hasCapability(name) ?
|
||||
* getCapability(name).invoke(args...) :
|
||||
* undefined
|
||||
* ```
|
||||
*
|
||||
* That is, it handles both the check-for-existence and
|
||||
* invocation of the capability, and checks for existence
|
||||
* before invoking the capability.
|
||||
*
|
||||
* @param {string} name the name of the capability to invoke
|
||||
* @param {...*} [arguments] to pass to the invocation
|
||||
* @returns {*}
|
||||
* @memberof DomainObject#
|
||||
*/
|
||||
useCapability: function (name) {
|
||||
// Get tail of args to pass to invoke
|
||||
var args = Array.prototype.slice.apply(arguments, [1]),
|
||||
capability = this.getCapability(name);
|
||||
|
||||
return (capability && capability.invoke) ?
|
||||
capability.invoke.apply(capability, args) :
|
||||
capability;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return DomainObject;
|
||||
}
|
||||
);
|
85
platform/core/src/objects/DomainObjectProvider.js
Normal file
85
platform/core/src/objects/DomainObjectProvider.js
Normal file
@ -0,0 +1,85 @@
|
||||
/*global define,Promise*/
|
||||
|
||||
/**
|
||||
* Module defining DomainObjectProvider. Created by vwoeltje on 11/7/14.
|
||||
*/
|
||||
define(
|
||||
["./DomainObject"],
|
||||
function (DomainObject) {
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Construct a new provider for domain objects.
|
||||
*
|
||||
* @param {ModelService} modelService the service which shall
|
||||
* provide models (persistent state) for domain objects
|
||||
* @param {CapabilityService} capabilityService the service
|
||||
* which provides capabilities (dynamic behavior)
|
||||
* for domain objects.
|
||||
* @constructor
|
||||
*/
|
||||
function DomainObjectProvider(modelService, capabilityService) {
|
||||
// Given a models object (containing key-value id-model pairs)
|
||||
// create a function that will look up from the capability
|
||||
// service based on id; for handy mapping below.
|
||||
function capabilityResolver(models) {
|
||||
return function (id) {
|
||||
var model = models[id];
|
||||
return model ?
|
||||
capabilityService.getCapabilities(model) :
|
||||
undefined;
|
||||
};
|
||||
}
|
||||
|
||||
// Assemble the results from the model service and the
|
||||
// capability service into one value, suitable to return
|
||||
// from this service. Note that ids are matched to capabilities
|
||||
// by index.
|
||||
function assembleResult(ids, models, capabilities) {
|
||||
var result = {};
|
||||
ids.forEach(function (id, index) {
|
||||
if (models[id]) {
|
||||
// Create the domain object
|
||||
result[id] = new DomainObject(
|
||||
id,
|
||||
models[id],
|
||||
capabilities[index]
|
||||
);
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
// Get object instances; this is the useful API exposed by the
|
||||
// domain object provider.
|
||||
function getObjects(ids) {
|
||||
return modelService.getModels(ids).then(function (models) {
|
||||
return Promise.all(
|
||||
ids.map(capabilityResolver(models))
|
||||
).then(function (capabilities) {
|
||||
return assembleResult(ids, models, capabilities);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
/**
|
||||
* Get a set of objects associated with a list of identifiers.
|
||||
* The provided result may contain a subset or a superset of
|
||||
* the total number of objects.
|
||||
*
|
||||
* @param {Array<string>} ids the identifiers for domain objects
|
||||
* of interest.
|
||||
* @return {Promise<object<string, DomainObject>>} a promise
|
||||
* for an object containing key-value pairs, where keys
|
||||
* are string identifiers for domain objects, and
|
||||
* values are the corresponding domain objects themselves.
|
||||
* @memberof module:core/object/object-provider.ObjectProvider#
|
||||
*/
|
||||
getObjects: getObjects
|
||||
};
|
||||
}
|
||||
|
||||
return DomainObjectProvider;
|
||||
}
|
||||
);
|
Reference in New Issue
Block a user