Comparing version 0.2.0 to 0.3.0
@@ -6,5 +6,4 @@ "use strict"; | ||
}); | ||
exports.initConfig = undefined; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
@@ -16,25 +15,20 @@ | ||
var _plugins = require("./plugins"); | ||
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } | ||
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var initConfig = { | ||
lifeCycle: ["_awake", "start"] | ||
var initConfig = exports.initConfig = { | ||
lifeCycle: ["_awake", "start"], | ||
components: [_plugins.INIT] | ||
}; | ||
var Menhera = function () { | ||
function Menhera(_ref) { | ||
var components = _ref.components, | ||
lifeCycle = _ref.lifeCycle, | ||
other = _objectWithoutProperties(_ref, ["components", "lifeCycle"]); | ||
function Menhera() { | ||
_classCallCheck(this, Menhera); | ||
this.components = {}; | ||
this.config = _extends({ | ||
components: components, | ||
lifeCycle: lifeCycle ? lifeCycle : initConfig.lifeCycle | ||
}, other); | ||
this.hooks = {}; | ||
this.config = {}; | ||
} | ||
@@ -44,15 +38,16 @@ | ||
key: "init", | ||
value: function init() { | ||
value: function init(config) { | ||
var _this = this; | ||
var _ = this; | ||
_.config = (0, _utils.ConfigMerger)(initConfig, config); | ||
var _$config = _.config, | ||
_$config$components = _$config.components, | ||
components = _$config$components === undefined ? [] : _$config$components, | ||
components = _$config$components === undefined ? {} : _$config$components, | ||
_$config$lifeCycle = _$config.lifeCycle, | ||
lifeCycle = _$config$lifeCycle === undefined ? [] : _$config$lifeCycle; | ||
console.log(components); | ||
components.forEach(function () { | ||
var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(component) { | ||
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(component) { | ||
var cp, name; | ||
@@ -63,3 +58,3 @@ return regeneratorRuntime.wrap(function _callee$(_context) { | ||
case 0: | ||
cp = typeof component === "function" ? component(_) : component; | ||
cp = typeof component === "function" ? component({ _: _ }) : component; | ||
name = cp.name; | ||
@@ -76,10 +71,12 @@ | ||
_context.next = 6; | ||
return Object.keys(cp).forEach(function (prop) { | ||
var hook = _[prop]; | ||
if (Array.isArray(hook)) { | ||
hook.forEach(function (h) { | ||
(0, _utils.bindHook)({ hook: h, prop: prop, cp: cp }); | ||
}); | ||
} else { | ||
(0, _utils.bindHook)({ hook: hook, prop: prop, cp: cp }); | ||
return Object.keys(cp).forEach(function (_key) { | ||
if (_key.startsWith("_")) { | ||
var hook = _.hooks[_key]; | ||
if (Array.isArray(hook)) { | ||
hook.forEach(function (h) { | ||
(0, _utils.bindHook)({ _: _, hook: h, _key: _key, cp: cp }); | ||
}); | ||
} else { | ||
(0, _utils.bindHook)({ _: _, hook: hook, _key: _key, cp: cp }); | ||
} | ||
} | ||
@@ -89,3 +86,18 @@ }); | ||
case 6: | ||
_context.next = 8; | ||
return Object.keys(cp).forEach(function (_key) { | ||
if (!_key.startsWith("_")) { | ||
var hook = _["hooks"][_key]; | ||
if (Array.isArray(hook)) { | ||
hook.forEach(function (h) { | ||
(0, _utils.bindHook)({ hook: h, _key: _key, cp: cp }); | ||
}); | ||
} else { | ||
(0, _utils.bindHook)({ hook: hook, _key: _key, cp: cp }); | ||
} | ||
} | ||
}); | ||
case 8: | ||
lifeCycle.forEach(function (key) { | ||
@@ -97,3 +109,3 @@ if (!key.startsWith("_")) { | ||
case 7: | ||
case 9: | ||
case "end": | ||
@@ -107,3 +119,3 @@ return _context.stop(); | ||
return function (_x) { | ||
return _ref2.apply(this, arguments); | ||
return _ref.apply(this, arguments); | ||
}; | ||
@@ -110,0 +122,0 @@ }()); |
@@ -6,72 +6,74 @@ "use strict"; | ||
}); | ||
exports.Event = exports.Observer = undefined; | ||
var _events = require("events"); | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
var Observer = exports.Observer = function Observer(_) { | ||
var INIT = exports.INIT = function INIT(_ref) { | ||
var _ = _ref._; | ||
return { | ||
name: "menhera-observer", | ||
awake: function awake() { | ||
var _$config$observable = _.config.observable, | ||
observable = _$config$observable === undefined ? {} : _$config$observable; | ||
name: "INIT", | ||
_awake: function _awake() { | ||
_.hooks._hooks = function (_ref2) { | ||
var key = _ref2.key, | ||
val = _ref2.val, | ||
cp = _ref2.cp; | ||
_.Observer = { Event: new _events.EventEmitter() }; | ||
_.config.observable = new Proxy(observable, { | ||
get: function get(target, key) { | ||
if (key in target) { | ||
return target[key]; | ||
} else { | ||
target[key] = null; | ||
return null; | ||
if (val) { | ||
if (!_.hooks[key]) { | ||
_.hooks[key] = []; | ||
} | ||
}, | ||
set: function set(target, key, val) { | ||
target[key] = val; | ||
_.Observer.Event.emit(key, { val: val }); | ||
return true; | ||
_.hooks[key].push(val); | ||
} | ||
}); | ||
_.onObserver = function (_ref) { | ||
var name = _ref.name, | ||
event = _ref.event; | ||
}; | ||
_.hooks._methods = function (_ref3) { | ||
var key = _ref3.key, | ||
val = _ref3.val, | ||
cp = _ref3.cp; | ||
_.Observer.Event.on(name, event); | ||
if (!cp[key]) { | ||
cp[key] = val; | ||
} else { | ||
console.warn(cp.name + " : \"" + key + "\" in methods is valid, Please use another one"); | ||
} | ||
}; | ||
}, | ||
_.hooks._data = function (_ref4) { | ||
var _val = _ref4._val, | ||
cp = _ref4.cp; | ||
onObserver: { | ||
foo: function foo() { | ||
console.log("bar"); | ||
} | ||
} | ||
}; | ||
}; | ||
var props = _val(); | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
var Event = exports.Event = function Event(_) { | ||
return { | ||
name: "menhera-event", | ||
awake: function awake() { | ||
_.Event = new _events.EventEmitter(); | ||
_.onEvent = function (_ref2) { | ||
var name = _ref2.name, | ||
event = _ref2.event; | ||
try { | ||
for (var _iterator = Object.entries(props)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var _ref5 = _step.value; | ||
_.Event.on(name, event); | ||
}; | ||
var _ref6 = _slicedToArray(_ref5, 2); | ||
_.emit = function (name) { | ||
for (var _len = arguments.length, val = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
val[_key - 1] = arguments[_key]; | ||
var key = _ref6[0]; | ||
var val = _ref6[1]; | ||
if (!cp[key]) { | ||
cp[key] = val; | ||
} else { | ||
console.warn(cp.name + " : \"" + key + " \" is data is valid, Please use another one"); | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
_.Event.emit(name, { val: val }); | ||
}; | ||
}, | ||
onEvent: { | ||
foo: function foo() { | ||
console.log("bar"); | ||
} | ||
} | ||
}; | ||
}; |
@@ -7,2 +7,4 @@ "use strict"; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
@@ -12,10 +14,16 @@ | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } | ||
var bindHook = exports.bindHook = function bindHook(_ref) { | ||
var hook = _ref.hook, | ||
cp = _ref.cp, | ||
prop = _ref.prop; | ||
var name = cp.name; | ||
_ = _ref._, | ||
_key = _ref._key, | ||
cp = _ref.cp; | ||
if (typeof hook === "function") { | ||
if (_typeof(cp[prop]) === "object") { | ||
var _val = cp[_key]; | ||
if (_typeof(cp[_key]) === "object") { | ||
var _iteratorNormalCompletion = true; | ||
@@ -26,3 +34,3 @@ var _didIteratorError = false; | ||
try { | ||
for (var _iterator = Object.entries(cp[prop])[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
for (var _iterator = Object.entries(cp[_key])[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var _ref2 = _step.value; | ||
@@ -32,9 +40,9 @@ | ||
var _name = _ref3[0]; | ||
var value = _ref3[1]; | ||
var key = _ref3[0]; | ||
var val = _ref3[1]; | ||
if (typeof value === "function") { | ||
hook({ name: _name, event: value, cp: cp }); | ||
if (typeof val === "function") { | ||
hook({ key: key, val: val.bind(cp), cp: cp, _key: _key, _val: _val }); | ||
} else { | ||
hook({ name: _name, props: value, cp: cp }); | ||
hook({ key: key, val: val, cp: cp, _key: _key, _val: _val }); | ||
} | ||
@@ -56,8 +64,20 @@ } | ||
} | ||
} else if (typeof cp[prop] === "function") { | ||
hook({ name: name, event: cp[prop], cp: cp }); | ||
} else if (typeof cp[_key] === "function") { | ||
hook({ _key: _key, _val: cp[_key].bind(cp), cp: cp }); | ||
} else { | ||
hook({ name: name, props: cp[prop], cp: cp }); | ||
hook({ _key: _key, _val: _val, cp: cp }); | ||
} | ||
} | ||
}; | ||
}; | ||
var ConfigMerger = function ConfigMerger(initConfig, _ref4) { | ||
var components = _ref4.components, | ||
lifeCycle = _ref4.lifeCycle, | ||
other = _objectWithoutProperties(_ref4, ["components", "lifeCycle"]); | ||
return _extends({ | ||
components: Array.from(new Set([].concat(_toConsumableArray(initConfig.components), _toConsumableArray(components)))), | ||
lifeCycle: lifeCycle ? lifeCycle : initConfig.lifeCycle | ||
}, other); | ||
}; | ||
exports.ConfigMerger = ConfigMerger; |
{ | ||
"name": "menhera", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"main": "dist", | ||
@@ -15,5 +15,7 @@ "files": [ | ||
"devDependencies": { | ||
"@types/minimist": "^1.2.0", | ||
"babel-cli": "^6.26.0", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-preset-stage-3": "^6.24.1", | ||
"minimist": "^1.2.0", | ||
"nodemon": "^1.11.0" | ||
@@ -20,0 +22,0 @@ }, |
118
readme.md
@@ -12,92 +12,54 @@ # Menhera | ||
import Menhera from "menhera"; | ||
import minimist from "minimist"; | ||
export const Observer = _ => ({ | ||
name: "menhera-observer", | ||
_awake() { | ||
const { config: { observable = {} } } = _; | ||
_.Observer = { Event: new EventEmitter() }; | ||
_.config.observable = new Proxy(observable, { | ||
get(target, key) { | ||
if (key in target) { | ||
return target[key]; | ||
} else { | ||
target[key] = null; | ||
return null; | ||
} | ||
}, | ||
set(target, key, val) { | ||
target[key] = val; | ||
_.Observer.Event.emit(key, { val }); | ||
return true; | ||
} | ||
}); | ||
_.onObserver = ({ name, event }) => { | ||
_.Observer.Event.on(name, event); | ||
export const CLI = { | ||
name: "CLI", | ||
_data() { | ||
return { | ||
structs: {}, | ||
Event: new EventEmitter() | ||
}; | ||
}, | ||
onObserver: { | ||
foo() { | ||
console.log("bar"); | ||
start() { | ||
let { _, ...flags } = minimist(process.argv.slice(2)); | ||
let [command = "*", ...inputs] = _; | ||
const { h, help } = flags; | ||
if (h && help) { | ||
let command = this.structs[command]; | ||
command && command.help && command.help(); | ||
} else { | ||
this.Event.emit(command, { inputs, flags }); | ||
} | ||
} | ||
}); | ||
export const Event = _ => ({ | ||
name: "menhera-event", | ||
_awake() { | ||
_.Event = new EventEmitter(); | ||
_.onEvent = ({ name, event }) => { | ||
_.Event.on(name, event); | ||
}; | ||
_.emit = (name, ...val) => { | ||
_.Event.emit(name, { val }); | ||
}; | ||
}, | ||
onEvent: { | ||
foo() { | ||
console.log("bar"); | ||
_hooks: { | ||
onCli({ key, val, cp }) { | ||
const { exec } = val; | ||
this.structs[key] = val; | ||
if (exec) { | ||
this.Event.on(key, exec.bind(cp)); | ||
} | ||
} | ||
} | ||
}); | ||
}; | ||
let Test = _ => ({ | ||
name: "test", | ||
_awake() { | ||
console.log("test0"); | ||
}, | ||
start() { | ||
const { config: { observable: ob } } = _; | ||
ob.test1 = "test1"; | ||
ob.test2 = "test2"; | ||
ob.test3 = ob.test3; | ||
_.emit("test4", "test", "4"); | ||
_.emit("test5", "test", "5"); | ||
}, | ||
onObserver: { | ||
test1({ val }) { | ||
console.log(val); | ||
const cliTest = { | ||
name: "clitest", | ||
onCli: { | ||
"*": { | ||
help() { | ||
console.log("* help"); | ||
} | ||
}, | ||
test2({ val }) { | ||
console.log(val); | ||
}, | ||
test3({ val }) { | ||
console.log(val); | ||
test: { | ||
exec({ inputs, flags }) { | ||
console.log(inputs, flags); | ||
} | ||
} | ||
}, | ||
onEvent: { | ||
test4({ val }) { | ||
console.log(val.join("")); | ||
}, | ||
test5({ val }) { | ||
console.log(val.join("")); | ||
} | ||
} | ||
}; | ||
const _ = new Menhera().init({ | ||
// lifeCycle: ["_awake", "start"], | ||
components: [CLI, cliTest] | ||
}); | ||
const _ = new Menhera({ | ||
lifeCycle: ["_awake", "start"], | ||
components: [Observer, Event, Test], | ||
observable: { test3: "test3" } | ||
}).init(); | ||
``` |
12751
232
6
65