How to use the @politie/sherlock.atom function in @politie/sherlock

To help you get started, we’ve selected a few @politie/sherlock examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github politie / sherlock / extensions / sherlock-proxy / proxy.spec.ts View on Github external
it('should compare the lensed values of DerivableProxies', () => {
                    const pd = new ProxyDescriptor();
                    // We force the target to be differing numbers, while the lensed value is the correct boolean.
                    let idx = 1;
                    pd.$lens = () => ({
                        get: v => v > 0,
                        set: v => v ? idx++ : -idx++,
                    });
                    const lhs = pd.$create(atom(0));
                    lhs.$value = identityValue;
                    const rhs = pd.$create(atom(0));
                    rhs.$value = false;
                    const result = lhs[method](rhs) as Derivable;
                    expect(result.get()).to.equal(false);
                    rhs.$value = true;
                    expect(result.get()).to.equal(true);
                });
github politie / sherlock / extensions / sherlock-utils / src / derivable-cache.ts View on Github external
get(key) {
            const cachedDerivable = cache.get(key);
            // If the cache has a hit for the current key, we know it is already connected (through another proxy).
            if (cachedDerivable) {
                return cachedDerivable.getState();
            }

            // A cache miss means no other proxy is currently connected.
            const newDerivable = _internal.independentTracking(() => derivableFactory(key));
            // We don't want final-value-optimalization, because that defeats the purpose of the cache. A final value
            // is not registered as an observed value, which means we cannot track the usage of our newly created derivable.
            // Therefore introduce a non-final atom (`atom(0)`) in the derivation:
            const derivable = isSettableDerivable(newDerivable)
                ? lens({ get: () => newDerivable.get(), set: v => newDerivable.set(v) }, atom(0))
                : atom(0).derive(() => newDerivable.get());

            if (delayedEviction) {
                derivable.autoCache();
            }

            // Get the state of our derivable early so it connects when needed.
            const state = derivable.getState();
            if (derivable.connected) {
                derivable[CACHED_PROXY] = this;
                cache.set(key, derivable);
                derivable.connected$.react(() => cache.delete(key), { skipFirst: true, once: true });
            }
            return state;
        },
        set(newValue, key) {
github politie / sherlock / extensions / sherlock-utils / src / state.spec.ts View on Github external
it('should set error state', () => {
        const to$ = atom(123);
        setStateObject(to$, { error: 'womp womp', errored: true, resolved: true });
        expect(to$.error).to.equal('womp womp');
    });
});
github politie / sherlock / extensions / sherlock-proxy / proxy.spec.ts View on Github external
it('should not shadow setter-errors with getter-errors', () => {
                const a$ = atom(0).map(
                    () => { throw Error('from get'); },
                    () => { throw Error('from set'); },
                );
                const pd = new ProxyDescriptor();
                pd.$target = a$;
                expect(() => pd.$targetValue = 1).to.throw('from set');
            });
        });
github politie / sherlock / extensions / sherlock-proxy / proxy.spec.ts View on Github external
it('should support reacting to derivables in settable $lens', () => {
                const magic$ = atom(1);
                const pd = new class extends ProxyDescriptor {
                    $lens() {
                        const addMagic = (v: number) => v + magic$.get();
                        const removeMagic = (v: number) => v - magic$.get();
                        return this.$expression ? { get: addMagic, set: removeMagic } : undefined;
                    }
                };
                const lhs = pd.$create(atom({ prop: 10 })) as any;
                let value = 0;
                const done = lhs.prop.$react((v: number) => value = v);
                expect(value).to.equal(11);
                magic$.set(2);
                expect(value).to.equal(12);
                lhs.prop.$value = 3;
                expect(lhs.$value).to.deep.equal({ prop: 1 });
                done();
github politie / sherlock / extensions / sherlock-utils / src / state.spec.ts View on Github external
it('should sync all possible states between the two Derivables', () => {
        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 target$ = atom.unresolved();

        const done = syncState(d$, target$);

        expect(target$.value).to.equal(42);

        a$.set(1);
github politie / sherlock / extensions / sherlock-rxjs / rxjs.spec.ts View on Github external
it('should work with a fallback when given and not connected', () => {
            const subj = new Subject();
            const f$ = atom('fallback');
            const d$ = fromObservable(subj).fallbackTo(f$);
            expect(d$.get()).to.equal('fallback');
            expect(subj.observers).to.be.empty;

            let value: string | undefined;
            let reactions = 0;
            const done = d$.react(v => (++reactions, value = v));

            expect(subj.observers).to.have.length(1);
            expect(reactions).to.equal(1);
            expect(value).to.equal('fallback');

            subj.next('value');

            expect(reactions).to.equal(2);
            expect(value).to.equal('value');
github politie / sherlock / extensions / sherlock-utils / src / struct.spec.ts View on Github external
it('should turn an array of derivables into an unwrapped derivable', () => {
        const number1$ = atom(1);
        const number2$ = atom(2);
        const number3$ = number1$.derive(n => n + number2$.get());

        const number$s = [number1$, number2$, number3$];
        const numbers$ = struct(number$s);

        expect(numbers$.get()).to.deep.equal([1, 2, 3]);

        number2$.set(3);
        expect(numbers$.get()).to.deep.equal([1, 3, 4]);
    });
github politie / sherlock / extensions / sherlock-utils / src / derivable-cache.spec.ts View on Github external
beforeEach('setup derivables', () => {
                input$ = atom(['key1', 'key2']);
                output$ = input$.derive(keys => keys.map(resultCache).map(unwrap));
                stopConnection = output$.react(() => 0);

                expect(derivableFactory).to.have.been.calledTwice;
                derivableFactory.resetHistory();
                expect(output$.value).to.deep.equal([
                    'result from key1',
                    'result from key2',
                ]);
            });
github politie / sherlock / extensions / sherlock-proxy / proxy.spec.ts View on Github external
function createForObject(obj: T): ProxyType {
        return new ProxyDescriptor().$create(atom(obj)) as ProxyType;
    }