Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function fromObservable(observable: Subscribable): Derivable {
const atom$ = atom.unresolved();
let subscription: Unsubscribable | undefined;
atom$.connected$.react(() => {
if (atom$.connected && !subscription) {
subscription = observable.subscribe(
value => atom$.set(value),
err => atom$.setFinal(new ErrorWrapper(err)),
() => atom$.setFinal(atom$.getState()),
);
}
// This is not chained with the previous as an `else` branch, because this can be true immediately after
// the subscription occurs. Observables can complete synchronously on subscription.
if (!atom$.connected && subscription) {
subscription.unsubscribe();
subscription = undefined;
}
derivableFactory = spy((key: Request) => {
const result = atom.unresolved();
// Do some hard work (an HTTP call for example).
fetchItLater();
return result;
async function fetchItLater() {
await Promise.resolve();
result.set({ code: 200, body: `Result from ${key.method} to ${key.url}.` });
}
});
performCall = derivableCache({ derivableFactory, mapFactory: ImmutableMap.factory });
it('should set value state', () => {
const to$ = atom.unresolved();
setStateObject(to$, { value: 123, errored: false, resolved: true });
expect(to$.get()).to.equal(123);
});
it('should transfer value state', () => {
const from$ = constant(123);
const to$ = atom.unresolved();
copyState(from$, to$);
expect(to$.get()).to.equal(123);
});
it('should support lifecycle options', () => {
const a$ = atom(0);
const unresolved$ = constant.unresolved();
const error = new Error('this is not good!');
const d$ = a$.derive(v => {
switch (v) {
case 0: return 42;
case 1: throw error;
default: return unresolved$.get() + 42;
}
});
const until = atom(false);
const target$ = atom.unresolved();
syncState(d$, target$, { until });
expect(target$.value).to.equal(42);
a$.set(1);
expect(target$.error).to.equal(error);
a$.set(2);
expect(target$.resolved).to.be.false;
a$.set(0);
expect(target$.value).to.equal(42);
export function fromPromise(prom: Promise): Derivable {
const atom$ = atom.unresolved();
prom.then(v => atom$.setFinal(v), e => atom$.setFinal(new ErrorWrapper(e)));
return atom$;
}