framework4js
Version:
Framework-for-js is a better Javascript's framework
365 lines (360 loc) • 12.5 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _Container2 = _interopRequireDefault(require("./container/Container"));
var _is = require("./spanners/is");
var _Exception = _interopRequireDefault(require("./spanners/Exception"));
var _package = _interopRequireDefault(require("../package.json"));
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
/**
* CoreJs Framework4Js
* Class Application Container
*/
var Application = /*#__PURE__*/function (_Container) {
(0, _inherits2["default"])(Application, _Container);
var _super = _createSuper(Application);
function Application() {
var _this;
(0, _classCallCheck2["default"])(this, Application);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _super.call.apply(_super, [this].concat(args));
// adapters
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "_adapters", new WeakMap());
// providers
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "_providers", []);
// services
(0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "_services", []);
return _this;
}
(0, _createClass2["default"])(Application, [{
key: "version",
get:
/**
* Get version
* @returns
*/
function get() {
return Application.VERSION;
}
/**
* Method _getProviders
* @returns
*/
}, {
key: "_getProviders",
value: function _getProviders() {
return Application.getInstance()._providers;
}
}, {
key: "setProviders",
value:
/**
* Method setProviders
* @param {Array} providers
*/
function setProviders() {
var providers = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
Application.getInstance()._providers = Application.getInstance()._getProviders().concat(providers);
}
}, {
key: "registerProvider",
value:
/**
* Method registerProvider
* @param {Class|Function} Provider Class Provider
* @param {*} payload
*/
function registerProvider(Provider) {
var payload = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
new Provider(Application.getInstance()).register(payload);
}
}, {
key: "registerProviders",
value:
/**
* Method registerProviders
* @param {*} providers Class Provider [...Class]
*/
function registerProviders() {
var providers = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
if (Array.isArray(providers)) {
providers.map(function (Provider) {
Application.getInstance().registerProvider(Provider);
});
} else {
// Single Provider
Application.getInstance().registerProvider(providers);
}
}
}, {
key: "bindAdapter",
value:
// static get
/**
* Method bindAdapter
* @param {*} Adapter
* @param {*} payload
*/
function bindAdapter(Adapter) {
var payload = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
if (!(0, _is.isClass)(Adapter)) {
throw new _Exception["default"]("The argument must be a class", "AdapterError");
}
var adapter = payload ? new Adapter(payload) : new Adapter();
this.setAdapter(Adapter, adapter);
}
}, {
key: "setAdapter",
value:
/**
* Method setAdapter
* @param {*} Adapter
* @param {*} adapter
*/
function setAdapter(Adapter, adapter) {
if (Application.getInstance().getAdapter(Adapter)) {
// Error
throw new _Exception["default"]("The argument must be a class", "AdapterError");
}
Application.getInstance()._adapters.set(Adapter, adapter);
}
}, {
key: "getAdapter",
value:
/**
* Method getAdapter
* @param {*} Adapter
* @returns
*/
function getAdapter(Adapter) {
if (!(0, _is.isClass)(Adapter)) {
throw new _Exception["default"]("The argument must be a class", "AdapterError");
}
return Application.getInstance()._adapters.get(Adapter);
}
}, {
key: "setAppConfig",
value:
/**
* Method setAppConfig
* @param {*} config
*/
function setAppConfig(config) {
this.bind("$config", config);
}
}, {
key: "getAppConfig",
value:
/**
* Method getAppConfig
* @param {*} name
* @returns
*/
function getAppConfig(name) {
var config = this.make("$config") || {};
if (name) {
return config[name];
}
return config || {};
}
}, {
key: "run",
value:
/**
* Application run
* 应用框架运行
* @param {*} callback All services
*/
function run(callback) {
Application.run(callback);
}
/**
* Application Run
* 框架应用运行
* @param callback
*/
}], [{
key: "lifecycle",
value:
// // lifecycle
// _lifecycle = () => {
// return {
// // Method create
// create: () => {
// console.log("create");
// if (!this._step) {
// // Register providers
// Application.getInstance().registerProviders(
// Application.getInstance()._getProviders()
// );
// // Register providers for developer's
// Application.getInstance().registerProviders(
// Application.getInstance().getAppConfig("PROVIDERS")
// );
// this._step = "create";
// } else {
// throw new Exception(
// "An error occurred in the create method",
// "LifecycleError"
// );
// }
// },
// // Method mounted
// mounted: () => {
// console.log("mounted");
// if (this._step === "create") {
// //
// this._step = "mounted";
// } else {
// throw new Exception(
// "An error occurred in the mounted method",
// "LifecycleError"
// );
// }
// },
// // Method unmounted
// unmounted: () => {
// console.log("unmounted");
// if (this._step === "mounted") {
// //
// this._step = "unmounted";
// } else {
// throw new Exception(
// "An error occurred in the unmounted method",
// "LifecycleError"
// );
// }
// },
// };
// };
function lifecycle() {
var startTime = new Date().getTime();
return {
created: function created() {
Application.registerProviders(Application.getInstance()._getProviders());
Application.registerProviders(Application.getInstance().getAppConfig("PROVIDERS"));
},
mounted: function mounted() {},
unmounted: function unmounted() {
var endTime = new Date().getTime();
console.log("\n".concat(" %c framework4js %c ", _package["default"].version, " ").concat(endTime - startTime, "ms ", "\n"), "color: #fadfa3; background: #030307; padding:5px 0;border-radius:3px 0 0 3px;", "color:#000000;background: #ebd29a; padding:5px 0;border-radius:0 3px 3px 0;");
}
};
}
}, {
key: "getProviders",
value: function getProviders() {
return Application.getInstance()._providers;
}
}, {
key: "setProviders",
value: function setProviders() {
var providers = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
Application.getInstance()._providers = Application.getInstance()._getProviders().concat(providers);
}
}, {
key: "registerProvider",
value: function registerProvider(Provider) {
var payload = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
if (!(0, _is.isClass)(Provider)) {
throw new _Exception["default"]("Provider Error", "The first argument must be a class");
}
new Provider(Application.getInstance()).register(payload);
}
}, {
key: "registerProviders",
value: function registerProviders() {
var providers = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
if (Array.isArray(providers)) {
providers.map(function (provider) {
Application.registerProvider(provider);
});
} else {
Application.registerProvider(providers);
}
}
}, {
key: "bindAdapter",
value: function bindAdapter(Adapter) {
var payload = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
if (!(0, _is.isClass)(Adapter)) {
throw new _Exception["default"]("The argument must be a class", "AdapterError");
}
var adapter = payload ? new Adapter(payload) : new Adapter();
Application.setAdapter(Adapter, adapter);
}
}, {
key: "setAdapter",
value: function setAdapter(Adapter, adapter) {
if (Application.getInstance().getAdapter(Adapter)) {
// Error
throw new _Exception["default"]("The argument must be a class", "AdapterError");
}
Application.getInstance()._adapters.set(Adapter, adapter);
}
}, {
key: "getAdapter",
value: function getAdapter(Adapter) {
if (!(0, _is.isClass)(Adapter)) {
throw new _Exception["default"]("The argument must be a class", "AdapterError");
}
return Application.getInstance()._adapters.get(Adapter);
}
}, {
key: "setAppConfig",
value: function setAppConfig(config) {
Application.getInstance().singleton("$config", config);
}
}, {
key: "getAppConfig",
value: function getAppConfig(name) {
var config = Application.getInstance().make("$config") || {};
if (name) {
return config[name];
}
return config || {};
}
/**
* Static method getInstance
* @returns
*/
}, {
key: "getInstance",
value: function getInstance() {
var instance = Application._instance;
if (!instance || !(instance instanceof Application)) {
instance = Application._instance = new Application();
}
return instance.proxyInstance();
}
}, {
key: "run",
value: function run(callback) {
var boot = Application.lifecycle();
boot.created();
boot.mounted();
boot.unmounted();
// callback instance
if ((0, _is.isFunction)(callback)) {
callback(Application.getInstance().get());
}
}
}]);
return Application;
}(_Container2["default"]);
exports["default"] = Application;
// VERSION
(0, _defineProperty2["default"])(Application, "VERSION", _package["default"].version);