|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | +"use strict"; |
| 5 | + |
| 6 | +module.metadata = { |
| 7 | + "stability": "unstable" |
| 8 | +}; |
| 9 | + |
| 10 | +let { emit, on, off } = require("./core"); |
| 11 | + |
| 12 | +// This module provides set of high order function for working with event |
| 13 | +// streams (streams in a NodeJS style that dispatch data, end and error |
| 14 | +// events). |
| 15 | + |
| 16 | +// Function takes a `target` object and returns set of implicit references |
| 17 | +// (non property references) it keeps. This basically allows defining |
| 18 | +// references between objects without storing the explicitly. See transform for |
| 19 | +// more details. |
| 20 | +let refs = (function() { |
| 21 | + let refSets = new WeakMap(); |
| 22 | + return function refs(target) { |
| 23 | + if (!refSets.has(target)) refSets.set(target, new Set()); |
| 24 | + return refSets.get(target); |
| 25 | + } |
| 26 | +})(); |
| 27 | + |
| 28 | +function transform(f, input) { |
| 29 | + let output = {}; |
| 30 | + |
| 31 | + // Since event listeners don't prevent `input` to be GC-ed we wanna presrve |
| 32 | + // it until `output` can be GC-ed. There for we add implicit reference which |
| 33 | + // is removed once `input` ends. |
| 34 | + refs(output).add(input); |
| 35 | + |
| 36 | + function next(data) emit(output, "data", data); |
| 37 | + on(input, "error", function(error) emit(output, "error", error)); |
| 38 | + on(input, "end", function() { |
| 39 | + refs(output).delete(input); |
| 40 | + emit(output, "end"); |
| 41 | + }); |
| 42 | + on(input, "data", function(data) f(data, next)); |
| 43 | + return output; |
| 44 | +} |
| 45 | + |
| 46 | +// High order event transformation function that takes `input` event channel |
| 47 | +// and returns transformation containing only events on which `p` predicate |
| 48 | +// returns `true`. |
| 49 | +function filter(predicate, input) { |
| 50 | + return transform(function(data, next) { |
| 51 | + if (predicate(data)) next(data) |
| 52 | + }, input); |
| 53 | +} |
| 54 | +exports.filter = filter; |
| 55 | + |
| 56 | +// High order function that takes `input` and returns input of it's values |
| 57 | +// mapped via given `f` function. |
| 58 | +function map(f, input) transform(function(data, next) next(f(data)), input) |
| 59 | +exports.map = map; |
| 60 | + |
| 61 | +// High order function that takes `input` stream of streams and merges them |
| 62 | +// into single event stream. Like flatten but time based rather than order |
| 63 | +// based. |
| 64 | +function merge(inputs) { |
| 65 | + let output = {}; |
| 66 | + let open = 1; |
| 67 | + let state = []; |
| 68 | + output.state = state; |
| 69 | + refs(output).add(inputs); |
| 70 | + |
| 71 | + function end(input) { |
| 72 | + open = open - 1; |
| 73 | + refs(output).delete(input); |
| 74 | + if (open === 0) emit(output, "end"); |
| 75 | + } |
| 76 | + function error(e) emit(output, "error", e); |
| 77 | + function forward(input) { |
| 78 | + state.push(input); |
| 79 | + open = open + 1; |
| 80 | + on(input, "end", function() end(input)); |
| 81 | + on(input, "error", error); |
| 82 | + on(input, "data", function(data) emit(output, "data", data)); |
| 83 | + } |
| 84 | + |
| 85 | + // If `inputs` is an array treat it as a stream. |
| 86 | + if (Array.isArray(inputs)) { |
| 87 | + inputs.forEach(forward) |
| 88 | + end(inputs) |
| 89 | + } |
| 90 | + else { |
| 91 | + on(inputs, "end", function() end(inputs)); |
| 92 | + on(inputs, "error", error); |
| 93 | + on(inputs, "data", forward); |
| 94 | + } |
| 95 | + |
| 96 | + return output; |
| 97 | +} |
| 98 | +exports.merge = merge; |
| 99 | + |
| 100 | +function expand(f, inputs) merge(map(f, inputs)) |
| 101 | +exports.expand = expand; |
0 commit comments