Skip to content

Instantly share code, notes, and snippets.

@Leo7654
Last active January 8, 2021 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Leo7654/93c1d5bddc2defdc7ab9bfc08a50d3e3 to your computer and use it in GitHub Desktop.
Save Leo7654/93c1d5bddc2defdc7ab9bfc08a50d3e3 to your computer and use it in GitHub Desktop.
const { rxObserver, palette } = require('api/v0.3');
const { iif,pipe,of,merge, timer, from, once } = require('rxjs');
const { switchMapTo,mapTo,filter,windowWhen,delay,mergeMap,map,switchMap, concatMap, take, zip, ignoreElements, startWith } = require('rxjs/operators');
// stream for coloring
const palette$ = from(palette);
const callStt = pipe(
filter(i=>i%3==2),
map(i=> Marble(i+">", i.color)),
delay(5)
)
const callChatbot = (data) =>
new Promise(resolve =>
setTimeout(() =>
resolve(Marble(data+"@", data.color)),
10));
const callTts = pipe(
mergeMap(v => timer(0, 15).pipe(
take(3),
map(v2=> Marble(v+"#"+v2, v.color)),
))
)
function call(s) {
s.subscribe(rxObserver('voicebot request'))
s
.pipe(
pipeIf(v => v.valueOf() === 'I', pipe, callStt),
mergeMap(callChatbot),
callTts
)
.subscribe(rxObserver('voicebot response (>: STT, @: Chatbot, #: TTS)'))
}
// helpers
// creates a colored Marble
function Marble(value,color) {
return {
valueOf: ()=>value
, color
};
}
function pipeIf(predicate, a, b=pipe) {
return function (source) {
return source.pipe(
mergeMap(value => predicate(value) ?
of(value).pipe(a) :
of(value).pipe(b))
)
}
}
function colorize(color) {
return pipe(
map(y => Marble(y, color))
);
}
const source1$ = merge(
of('I'),
timer(5, 20).pipe(take(10))
).pipe(
zip(from(palette), Marble)
);
call(source1$)
const source2$ = merge(
of('I'),
).pipe(
zip(from(palette), Marble)
);
call(source2$)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment