Gate
gate() chooses which values to pass and which to drop based on signals
it receives on its
.control:
/*!*/import { wrap, pipe, gate, map, pin, sink } from '@connectv/core';
import { fromEvent} from 'rxjs';
import { debounceTime } from 'rxjs/operators';
let a = document.getElementById('a') as HTMLInputElement;
let btn = document.getElementById('btn');
let p1 = document.getElementById('p1');
let p2 = document.getElementById('p2');
/*!*/let g = gate();
let p = pin();
/*!*/wrap(fromEvent(btn, 'click')).to(g.control); //--> open gate when button is clicked
wrap(fromEvent(a, 'input')) //--> when input comes in
.to(map(() => a.value)) //--> map it to input's value
.to(pipe(debounceTime(1000))) //--> wait for typing to finish
/*!*/ .to(g, p) //--> pass it to the gate and some pin
.serialTo(
sink(v => p2.innerHTML += v + '<br>'), //--> display what the gate lets through
sink(v => p1.innerHTML += v + '<br>') //--> display what the pin receives
)
.subscribe();
gate() will either pass on each received value or drop it based on the boolean value
it receives on
.control:
/*!*/import { wrap, pipe, gate, map, sink } from '@connectv/core';
import { fromEvent } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
let a = document.getElementById('a') as HTMLInputElement;
let p = document.getElementById('p');
/*!*/let validator = map(v => v.startsWith('q'));
/*!*/let g = gate();
/*!*/validator.to(g.control); //--> if the validator sends `true`, the gate will
/*!*/ //... let the value pass. otherwise, it will
/*!*/ //... drop the value.
wrap(fromEvent(a, 'input'))
.to(map(() => a.value))
.to(pipe(debounceTime(1000)))
/*!*/ .to(g, validator) //--> each value goes to the gate and the validator
.serialTo(sink(v => p.innerHTML += v + '<br>'))
.subscribe();
When multiple sources are connected to a
gate's control, it will only pass values for which
all sources emit
true, and drop the rest:
import { wrap, pipe, gate, map, sink } from '@connectv/core';
import { fromEvent } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
let a = document.getElementById('a') as HTMLInputElement;
let p = document.getElementById('p');
let validator1 = map(v => v.startsWith('q'));
let validator2 = map(v => v.length > 5);
let g = gate();
/*!*/validator1.to(g.control); //--> only values will be passed
/*!*/validator2.to(g.control); //... that both validators send `true` for,
/*!*/ //... the rest will be dropped.
wrap(fromEvent(a, 'input'))
.to(map(() => a.value))
.to(pipe(debounceTime(1000)))
/*!*/ .to(g, validator1, validator2)
.serialTo(sink(v => p.innerHTML += v + '<br>'))
.subscribe();
Signature
Each
gate() has one
"value" input, and one
"value" output,
which are also accessible via
.input and
.output shortcut properties respectively.
let g = gate();
g.in("value") == g.input;
g.out("value") == g.output;
Further reading