@elium/mighty
Version:
Context agnostic TS & JS ORM
63 lines • 2.28 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var _ = require("lodash");
var Observable_1 = require("rxjs/Observable");
require("rxjs/add/observable/of");
require("rxjs/add/operator/mergeMap");
var Hook = (function () {
function Hook(name, action) {
this.name = name;
this.map = action;
}
return Hook;
}());
exports.Hook = Hook;
function hookable(constructor) {
constructor.prototype.hooks = [];
constructor.prototype.addHook = addHook;
constructor.prototype.removeHook = removeHook;
constructor.prototype.applyHook = applyHook;
function addHook(hook) {
var existingHook = _.find(this.hooks, {
name: hook.name,
map: hook.map
});
if (!_.isEmpty(existingHook)) {
existingHook.map = hook.map;
}
else {
this.hooks.push(hook);
}
}
function removeHook(hook) {
_.remove(this.hooks, { name: hook.name, map: hook.map });
}
function applyHook(name, input) {
var hooks = _.filter(this.hooks, { name: name });
var source = Observable_1.Observable.of(input);
_.forEach(hooks, function (hook) {
if (_.isFunction(hook.map)) {
source = source.flatMap(function (input) { return hook.map(input); });
}
});
return source;
}
}
exports.hookable = hookable;
var Hookable = (function () {
function Hookable() {
this.hooks = [];
}
return Hookable;
}());
Hookable = __decorate([
hookable
], Hookable);
exports.Hookable = Hookable;
//# sourceMappingURL=hook.js.map
;