Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function fnDoNothing() {}
- function isClass(func) {
- return (
- typeof func === 'function' &&
- /^class\s/.test(Function.prototype.toString.call(func))
- );
- }
- const connect = (stack, fn) => (ctx, next = fnDoNothing) =>
- stack(ctx, (nextCtx) => fn(nextCtx, next));
- class ExecutableNode {
- constructor(name, parent = null) {
- this._name = name;
- this._parent = parent;
- this._children = {};
- this.getName = this.getName.bind(this);
- this.getFullName = this.getFullName.bind(this);
- this.isRoot = this.isRoot.bind(this);
- this._addChild = this._addChild.bind(this);
- this.addChild = this.addChild.bind(this);
- this.use = this.use.bind(this);
- this.execute = this.execute.bind(this);
- this._initGo();
- }
- _initGo() {
- if (this._parent) {
- this.go = this._parent.go;
- } else {
- this.go = ((ctx, next = fnDoNothing) => next(ctx)).bind(this);
- }
- }
- getName() {
- return this._name;
- }
- getFullName() {
- if (this.isRoot()) {
- return this._name;
- }
- const postfix = '.' + this.getFullName();
- return this._parent.getFullName() + postfix;
- }
- isRoot() {
- return this._parent === null;
- }
- _addChild(child, cb) {
- child._parent = this;
- this._children[child.getName()] = child;
- if (typeof cb === 'function') {
- cb(child);
- return this;
- } else {
- return child;
- }
- }
- addChild(nameOrChild, cb) {
- if (typeof nameOrChild === 'string') {
- // name
- const child = new ExecutableNode(nameOrChild, this);
- return this._addChild(child, cb);
- } else {
- return this._addChild(nameOrChild, cb);
- }
- }
- use(fn) {
- this.go = connect(
- this.go,
- fn.bind(this)
- );
- return this;
- }
- execute(ctx, fnHandler = fnDoNothing) {
- this.go(ctx, (lastCtx) => fnHandler(lastCtx));
- }
- }
- class ExceptionGroup extends ExecutableNode {
- constructor(name, parent) {
- super(name, parent);
- this.throw = {};
- }
- addException(name, ctxOrFactoryOrClass, fnHandler = fnDoNothing) {
- if (isClass(ctxOrFactoryOrClass)) {
- // class
- this.throw[name] = (...args) =>
- this.execute(new ctxOrFactoryOrClass(...args), fnHandler);
- }
- if (typeof ctxOrFactoryOrClass === 'function') {
- // factory
- this.throw[name] = (...args) =>
- this.execute(ctxOrFactoryOrClass(...args), fnHandler);
- } else {
- this.throw[name] = () => this.execute(ctxOrFactoryOrClass, fnHandler);
- }
- return this;
- }
- addToastException(name, sMessage) {
- this.addException(name, null, () => {
- // pop toast sMessage
- });
- }
- /**
- * @callback addGroupCallback
- * @param {ExceptionGroup} group - An integer.
- */
- /**
- *
- * @param {string} name
- * @param {addGroupCallback} cb
- * @returns {ExceptionGroup}
- */
- addGroup(name, cb) {
- const g = new ExceptionGroup(name, this);
- this[name] = g;
- return this.addChild(g, cb);
- }
- }
- class ExceptionTree extends ExceptionGroup {
- constructor(name) {
- super(name, null); // tree has no parent
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement