UI for Zipcoin Blue

hooks.js 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const tslib_1 = require("tslib");
  4. class Hook {
  5. constructor(source, name, callable) {
  6. this.source = source;
  7. this.name = name;
  8. this.callable = callable;
  9. }
  10. fire(args) {
  11. return this.callable(args);
  12. }
  13. }
  14. exports.Hook = Hook;
  15. class HookEngine {
  16. constructor() {
  17. this.hooks = new Map();
  18. }
  19. register(source, hook, listener) {
  20. const h = new Hook(source, hook, listener);
  21. this.getRegistered(hook).push(h);
  22. }
  23. fire(hook, args) {
  24. return tslib_1.__awaiter(this, void 0, void 0, function* () {
  25. const registeredHooks = this.hooks.get(hook) || [];
  26. return Promise.all(registeredHooks.map((h) => h.fire(args)));
  27. });
  28. }
  29. getSources(hook) {
  30. return [...new Set(this.getRegistered(hook).map(h => h.source))];
  31. }
  32. hasSources(hook, sources) {
  33. return sources.filter(s => this.getSources(hook).includes(s)).length > 0;
  34. }
  35. deleteSource(source) {
  36. for (let [hookName, hooks] of this.hooks.entries()) {
  37. this.hooks.set(hookName, hooks.filter((h) => h.source !== source));
  38. }
  39. }
  40. getRegistered(hook) {
  41. let registeredHooks = this.hooks.get(hook);
  42. if (!registeredHooks) {
  43. registeredHooks = [];
  44. this.hooks.set(hook, registeredHooks);
  45. }
  46. return registeredHooks;
  47. }
  48. }
  49. exports.HookEngine = HookEngine;