|
| 1 | +"use strict"; |
| 2 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 | +exports.Junction = void 0; |
| 4 | +const index_1 = require("../index"); |
| 5 | +const enum_1 = require("../types/enum"); |
| 6 | +/** |
| 7 | + * Pipe Junction. |
| 8 | + * |
| 9 | + * Manages Pipes for a Module. |
| 10 | + * |
| 11 | + * When you register a Pipe with a Junction, it is |
| 12 | + * declared as being an INPUT pipe or an OUTPUT pipe. |
| 13 | + * |
| 14 | + * You can retrieve or remove a registered Pipe by name, |
| 15 | + * check to see if a Pipe with a given name exists,or if |
| 16 | + * it exists AND is an INPUT or an OUTPUT Pipe. |
| 17 | + * |
| 18 | + * You can send an `PipeMessage` on a named INPUT Pipe |
| 19 | + * or add a `PipeListener` to registered INPUT Pipe. |
| 20 | + */ |
| 21 | +class Junction { |
| 22 | + constructor() { |
| 23 | + /** |
| 24 | + * The names of the INPUT pipes |
| 25 | + */ |
| 26 | + this.inputPipes = new Set(); |
| 27 | + /** |
| 28 | + * The names of the OUTPUT pipes |
| 29 | + */ |
| 30 | + this.outputPipes = new Set(); |
| 31 | + /** |
| 32 | + * The map of pipe names to their pipes |
| 33 | + */ |
| 34 | + this.pipesMap = new Map(); |
| 35 | + /** |
| 36 | + * The map of pipe names to their types |
| 37 | + * @type {Map<string, JunctionType.INPUT | JunctionType.OUTPUT>} |
| 38 | + */ |
| 39 | + this.pipeTypesMap = new Map(); |
| 40 | + } |
| 41 | + /** |
| 42 | + * Register a pipe with the junction. |
| 43 | + * |
| 44 | + * Pipes are registered by unique name and type, |
| 45 | + * which must be either `Junction.INPUT` |
| 46 | + * or `Junction.OUTPUT`. |
| 47 | + * |
| 48 | + * NOTE: You cannot have an INPUT pipe and an OUTPUT |
| 49 | + * pipe registered with the same name. All pipe names |
| 50 | + * must be unique regardless of type. |
| 51 | + * |
| 52 | + * @return boolean true if successfully registered. false if another pipe exists by that name. |
| 53 | + */ |
| 54 | + registerPipe({ name, type, pipe, }) { |
| 55 | + if (!name) |
| 56 | + return false; |
| 57 | + let success = true; |
| 58 | + if (!this.pipesMap.get(name)) { |
| 59 | + this.pipesMap.set(name, pipe); |
| 60 | + this.pipeTypesMap.set(name, type); |
| 61 | + switch (type) { |
| 62 | + case enum_1.JunctionType.INPUT: |
| 63 | + this.inputPipes.add(name); |
| 64 | + break; |
| 65 | + case enum_1.JunctionType.OUTPUT: |
| 66 | + this.outputPipes.add(name); |
| 67 | + break; |
| 68 | + default: |
| 69 | + success = false; |
| 70 | + } |
| 71 | + } |
| 72 | + else { |
| 73 | + success = false; |
| 74 | + } |
| 75 | + return success; |
| 76 | + } |
| 77 | + /** |
| 78 | + * Does this junction have a pipe by this name? |
| 79 | + * |
| 80 | + * @param name the pipe to check for |
| 81 | + * @return boolean whether as pipe is registered with that name. |
| 82 | + */ |
| 83 | + hasPipe(name) { |
| 84 | + return this.pipesMap.get(name) !== null; |
| 85 | + } |
| 86 | + /** |
| 87 | + * Does this junction have an INPUT pipe by this name? |
| 88 | + * |
| 89 | + * @param name the pipe to check for |
| 90 | + * @return boolean whether an INPUT pipe is registered with that name. |
| 91 | + */ |
| 92 | + hasInputPipe(name) { |
| 93 | + return (this.hasPipe(name) && this.pipeTypesMap.get(name) === enum_1.JunctionType.INPUT); |
| 94 | + } |
| 95 | + /** |
| 96 | + * Does this junction have an OUTPUT pipe by this name? |
| 97 | + * |
| 98 | + * @param name the pipe to check for |
| 99 | + * @return boolean whether an OUTPUT pipe is registered with that name. |
| 100 | + */ |
| 101 | + hasOutputPipe(name) { |
| 102 | + return (this.hasPipe(name) && this.pipeTypesMap.get(name) === enum_1.JunctionType.OUTPUT); |
| 103 | + } |
| 104 | + /** |
| 105 | + * Remove the pipe with this name if it is registered. |
| 106 | + * |
| 107 | + * NOTE: You cannot have an INPUT pipe and an OUTPUT |
| 108 | + * pipe registered with the same name. All pipe names |
| 109 | + * must be unique regardless of type. |
| 110 | + * |
| 111 | + * @param name the pipe to remove |
| 112 | + */ |
| 113 | + removePipe(name) { |
| 114 | + if (this.hasPipe(name)) { |
| 115 | + let type = this.pipeTypesMap.get(name); |
| 116 | + let pipesList; |
| 117 | + switch (type) { |
| 118 | + case enum_1.JunctionType.INPUT: |
| 119 | + pipesList = this.inputPipes; |
| 120 | + break; |
| 121 | + case enum_1.JunctionType.OUTPUT: |
| 122 | + pipesList = this.outputPipes; |
| 123 | + break; |
| 124 | + default: |
| 125 | + pipesList = new Set(); |
| 126 | + break; |
| 127 | + } |
| 128 | + for (const item of pipesList) { |
| 129 | + if (item === name) { |
| 130 | + pipesList.delete(item); |
| 131 | + break; |
| 132 | + } |
| 133 | + } |
| 134 | + this.pipesMap.delete(name); |
| 135 | + this.pipeTypesMap.delete(name); |
| 136 | + } |
| 137 | + } |
| 138 | + /** |
| 139 | + * Retrieve the named pipe. |
| 140 | + * |
| 141 | + * @param name the pipe to retrieve |
| 142 | + * @return IPipeFitting the pipe registered by the given name if it exists |
| 143 | + */ |
| 144 | + retrievePipe(name) { |
| 145 | + return this.pipesMap.get(name); |
| 146 | + } |
| 147 | + /** |
| 148 | + * Add a PipeListener to an INPUT pipe. |
| 149 | + * |
| 150 | + * NOTE: there can only be one PipeListener per pipe, |
| 151 | + * and the listener function must accept an PipeMessage |
| 152 | + * as its sole argument. |
| 153 | + * |
| 154 | + * @param inputPipeName the INPUT pipe to add a PipeListener to |
| 155 | + * @param listener the callback function to invoke |
| 156 | + */ |
| 157 | + addPipeListener(inputPipeName, listener) { |
| 158 | + let success = false; |
| 159 | + if (this.hasInputPipe(inputPipeName)) { |
| 160 | + let pipe = this.pipesMap.get(inputPipeName); |
| 161 | + success = (pipe === null || pipe === void 0 ? void 0 : pipe.connect(new index_1.PipeListener(listener))) || false; |
| 162 | + } |
| 163 | + return success; |
| 164 | + } |
| 165 | + /** |
| 166 | + * Send a message on an OUTPUT pipe. |
| 167 | + * |
| 168 | + * @param outputPipeName the OUTPUT pipe to send the message on |
| 169 | + * @param message the PipeMessage to send |
| 170 | + */ |
| 171 | + sendMessage(outputPipeName, message) { |
| 172 | + let success = false; |
| 173 | + if (this.hasOutputPipe(outputPipeName)) { |
| 174 | + let pipe = this.pipesMap.get(outputPipeName); |
| 175 | + success = (pipe === null || pipe === void 0 ? void 0 : pipe.write(message)) || false; |
| 176 | + } |
| 177 | + return success; |
| 178 | + } |
| 179 | +} |
| 180 | +exports.Junction = Junction; |
0 commit comments