wonka
Version:
A fast push & pull stream library for Reason, loosely following the [callbag spec](https://github.com/callbag/callbag)
103 lines (96 loc) • 4.1 kB
JavaScript
import * as Block from "bs-platform/lib/es6/block.js";
import * as Curry from "bs-platform/lib/es6/curry.js";
import * as Wonka_helpers from "../wonka_helpers.js";
function switchMap(f) {
return (function (source) {
return (function (sink) {
var state = /* record */[
/* outerTalkback */Wonka_helpers.talkbackPlaceholder,
/* innerTalkback */Wonka_helpers.talkbackPlaceholder,
/* innerActive */false,
/* closed */false,
/* ended */false
];
var applyInnerSource = function (innerSource) {
return Curry._1(innerSource, (function (signal) {
if (typeof signal === "number") {
state[/* innerActive */2] = false;
state[/* innerTalkback */1] = Wonka_helpers.talkbackPlaceholder;
if (state[/* ended */4]) {
return sink(/* End */0);
} else {
return 0;
}
} else if (signal.tag) {
if (state[/* closed */3]) {
return /* () */0;
} else {
sink(/* Push */Block.__(1, [signal[0]]));
return state[/* innerTalkback */1](/* Pull */0);
}
} else {
var tb = signal[0];
state[/* innerActive */2] = true;
state[/* innerTalkback */1] = tb;
return tb(/* Pull */0);
}
}));
};
Curry._1(source, (function (signal) {
if (typeof signal === "number") {
if (state[/* ended */4]) {
return /* () */0;
} else {
state[/* ended */4] = true;
if (state[/* innerActive */2]) {
return 0;
} else {
return sink(/* End */0);
}
}
} else if (signal.tag) {
if (state[/* ended */4]) {
return /* () */0;
} else {
if (state[/* innerActive */2]) {
state[/* innerTalkback */1](/* Close */1);
state[/* innerTalkback */1] = Wonka_helpers.talkbackPlaceholder;
}
applyInnerSource(f(signal[0]));
return state[/* outerTalkback */0](/* Pull */0);
}
} else {
var tb = signal[0];
state[/* outerTalkback */0] = tb;
return tb(/* Pull */0);
}
}));
return sink(/* Start */Block.__(0, [(function (signal) {
if (signal) {
state[/* innerTalkback */1](/* Close */1);
if (state[/* ended */4]) {
return 0;
} else {
state[/* ended */4] = true;
state[/* closed */3] = true;
state[/* outerTalkback */0](/* Close */1);
state[/* innerTalkback */1] = Wonka_helpers.talkbackPlaceholder;
return /* () */0;
}
} else {
return state[/* innerTalkback */1](/* Pull */0);
}
})]));
});
});
}
function switchAll(source) {
return switchMap((function (x) {
return x;
}))(source);
}
export {
switchMap ,
switchAll ,
}
/* No side effect */