@webex/webex-core
Version:
Plugin handling for Cisco Webex
209 lines (203 loc) • 6.44 kB
JavaScript
var _Object$defineProperty = require("@babel/runtime-corejs2/core-js/object/define-property");
var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
_Object$defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _keys = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/keys"));
var _apply = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/reflect/apply"));
var _promise = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/promise"));
var _util = _interopRequireDefault(require("util"));
var _ampersandState = _interopRequireDefault(require("ampersand-state"));
var _lodash = require("lodash");
var _storage = require("./storage");
/*!
* Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
*/
/**
* @class
*/
var WebexPlugin = _ampersandState.default.extend({
derived: {
boundedStorage: {
deps: [],
fn: function fn() {
return (0, _storage.makeWebexPluginStore)('bounded', this);
}
},
unboundedStorage: {
deps: [],
fn: function fn() {
return (0, _storage.makeWebexPluginStore)('unbounded', this);
}
},
config: {
// figure out why caching config breaks the refresh integration test
// but not the refresh automation test.
cache: false,
deps: ['webex', 'webex.config'],
fn: function fn() {
if (this.webex && this.webex.config) {
var namespace = this.getNamespace();
if (namespace) {
return this.webex.config[namespace.toLowerCase()];
}
return this.webex.config;
}
return {};
}
},
logger: {
deps: ['webex', 'webex.logger'],
fn: function fn() {
return this.webex.logger || console;
}
},
webex: {
deps: ['parent'],
fn: function fn() {
if (!this.parent && !this.collection) {
throw new Error('Cannot determine `this.webex` without `this.parent` or `this.collection`. Please initialize `this` via `children` or `collection` or set `this.parent` manually');
}
/* eslint consistent-this: [0] */
var parent = this;
while (parent.parent || parent.collection) {
parent = parent.parent || parent.collection;
}
return parent;
}
}
},
session: {
parent: {
type: 'any'
},
/**
* Indicates this plugin is ready to be used. Defaults to true but can be
* overridden by plugins as appropriate. Used by {@link WebexCore#read}
* @instance
* @memberof WebexPlugin
* @type {boolean}
*/
ready: {
default: true,
type: 'boolean'
}
},
/**
* Overrides AmpersandState#clear to make sure we never unset `parent` and
* recursively visits children/collections.
* @instance
* @memberof WebexPlugin
* @param {Object} options
* @returns {WebexPlugin}
*/
clear: function clear(options) {
var _this = this;
(0, _keys.default)(this.attributes).forEach(function (key) {
if (key !== 'parent') {
_this.unset(key, options);
}
});
(0, _keys.default)(this._children).forEach(function (key) {
_this[key].clear();
});
(0, _keys.default)(this._collections).forEach(function (key) {
_this[key].reset();
});
return this;
},
/**
* Initializer
* @private
* @param {Object} attrs
* @param {Object} options
* @returns {undefined}
*/
initialize: function initialize() {
var _this2 = this;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
(0, _apply.default)(_ampersandState.default.prototype.initialize, this, args);
// HACK to deal with the fact that AmpersandState#dataTypes#set is a pure
// function.
this._dataTypes = (0, _lodash.cloneDeep)(this._dataTypes);
(0, _keys.default)(this._dataTypes).forEach(function (key) {
if (_this2._dataTypes[key].set) {
_this2._dataTypes[key].set = _this2._dataTypes[key].set.bind(_this2);
}
});
// END HACK
// Propagate change:[attribute] events from children
this.on('change', function (model, options) {
if (_this2.parent) {
_this2.parent.trigger("change:".concat(_this2.getNamespace().toLowerCase()), _this2.parent, _this2, options);
}
});
},
/**
* @instance
* @memberof WebexPlugin
* @param {number} depth
* @private
* @returns {Object}
*/
inspect: function inspect(depth) {
return _util.default.inspect((0, _lodash.omit)(this.serialize({
props: true,
session: true,
derived: true
}), 'boundedStorage', 'unboundedStorage', 'config', 'logger', 'webex', 'parent'), {
depth: depth
});
},
request: function request() {
var _this$webex;
return (_this$webex = this.webex).request.apply(_this$webex, arguments);
},
upload: function upload() {
var _this$webex2;
return (_this$webex2 = this.webex).upload.apply(_this$webex2, arguments);
},
when: function when(eventName) {
var _this3 = this;
for (var _len2 = arguments.length, rest = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
rest[_key2 - 1] = arguments[_key2];
}
if (rest && rest.length > 0) {
throw new Error('#when() does not accept a callback, you must attach to its promise');
}
return new _promise.default(function (resolve) {
_this3.once(eventName, function () {
for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
args[_key3] = arguments[_key3];
}
return resolve(args);
});
});
},
/**
* Helper function for dealing with both forms of {@link AmpersandState#set()}
* @param {string} key
* @param {mixed} value
* @param {Object} options
* @private
* @returns {Array<Object, Object>}
*/
_filterSetParameters: function _filterSetParameters(key, value, options) {
var attrs;
if ((0, _lodash.isObject)(key) || key === null) {
attrs = key;
options = value;
} else {
attrs = {};
attrs[key] = value;
}
options = options || {};
return [attrs, options];
}
});
var _default = exports.default = WebexPlugin;
//# sourceMappingURL=webex-plugin.js.map
;