@apollo/client#ReactiveVar TypeScript Examples

The following examples show how to use @apollo/client#ReactiveVar. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReactiveVarsCache.ts    From apollo-cache-policies with Apache License 2.0 6 votes vote down vote up
private watchReactiveVar<T>(id: string, rv: ReactiveVar<T>) {
    rv.onNextChange((value) => {
      this.writeCachedVar(id, value)
      // Reactive variables support an `onNextChange` API that allows listeners
      // to subscribe to the next value change. This only applies to a single change,
      // so to subscribe to every change, a new listener must be added after processing
      // the current change.
      this.watchReactiveVar<T>(id, rv);
    });
  }
Example #2
Source File: ReactiveVarsCache.ts    From apollo-cache-policies with Apache License 2.0 6 votes vote down vote up
registerCachedVar<T>(id: string, defaultValue: T): ReactiveVar<T> {
    const cachedValue = this.readCachedVar<T>(id);
    const rv = makeVar<T>(cachedValue ?? defaultValue);

    if (this.registeredVars[id]) {
      console.warn(`Duplicate cached reactive variable with ID ${id} detected. Multiple cached reactive variables should not share the same ID.`);
    }

    this.registeredVars[id] = {
      rv,
      defaultValue,
    }

    // If the cache did not already had a value for the CachedReactiveVar with this ID,
    // then it should be seeded with one using the provided default value.
    if (!cachedValue) {
      this.writeCachedVar(id, defaultValue);
    }

    this.watchReactiveVar(id, rv);
    return rv;
  }
Example #3
Source File: ReactiveVarsCache.ts    From apollo-cache-policies with Apache License 2.0 5 votes vote down vote up
registeredVars: Record<string, {
    rv: ReactiveVar<any>;
    defaultValue: any;
  }> = {};
Example #4
Source File: ReactiveVarsCache.ts    From apollo-cache-policies with Apache License 2.0 5 votes vote down vote up
export function makeCachedVar<T>(id: string, value: T): ReactiveVar<T> {
  return rvCache.registerCachedVar<T>(id, value);
}
Example #5
Source File: ReactiveVarsCache.test.ts    From apollo-cache-policies with Apache License 2.0 4 votes vote down vote up
describe('ReactiveVarsCache', () => {
  let cache: InvalidationPolicyCache;
  let rv: ReactiveVar<any>;

  beforeEach(() => {
    cache = new InvalidationPolicyCache();
    rv = makeCachedVar<boolean>('test', false);
  });

  test('should cache value changes', () => {
    expect(cache.extract(true, false)).toEqual({
      "CachedReactiveVar:test": {
        __typename: cachedReactiveVarTypename,
        id: 'test',
        value: false,
      },
      __META: {
        extraRootIds: ['CachedReactiveVar:test']
      }
    });
    rv(true);
    expect(cache.extract(true, false)).toEqual({
      "CachedReactiveVar:test": {
        __typename: cachedReactiveVarTypename,
        id: 'test',
        value: true,
      },
      __META: {
        extraRootIds: ['CachedReactiveVar:test']
      }
    });
    rv(false);
    expect(cache.extract(true, false)).toEqual({
      "CachedReactiveVar:test": {
        __typename: cachedReactiveVarTypename,
        id: 'test',
        value: false,
      },
      __META: {
        extraRootIds: ['CachedReactiveVar:test']
      }
    });
  });

  test('should be initialized with the existing cached value', () => {
    rv(true);
    const rv2 = makeCachedVar('test', false);
    expect(rv2()).toEqual(true);
  });

  test('should persist null values to the cache', () => {
    makeCachedVar('test-null', null);
    expect(cache.extract(true, false)).toEqual({
      "CachedReactiveVar:test": {
        __typename: cachedReactiveVarTypename,
        id: 'test',
        value: false,
      },
      "CachedReactiveVar:test-null": {
        __typename: cachedReactiveVarTypename,
        id: 'test-null',
        value: null,
      },
      __META: {
        extraRootIds: ['CachedReactiveVar:test', 'CachedReactiveVar:test-null']
      }
    });
  });

  test('should not persist undefined values to the cache', () => {
    makeCachedVar('test-undefined', undefined);
    expect(cache.extract(true, false)).toEqual({
      "CachedReactiveVar:test": {
        __typename: cachedReactiveVarTypename,
        id: 'test',
        value: false,
      },
      __META: {
        extraRootIds: ['CachedReactiveVar:test']
      }
    });
  });

  describe('on restore', () => {
    test('should update reactive reactive vars to their updated cache values', () => {
      expect(rv()).toEqual(false);
      cache.restore({
        "CachedReactiveVar:test": {
          __typename: cachedReactiveVarTypename,
          id: 'test',
          value: true,
        },
        __META: {
          extraRootIds: ['CachedReactiveVar:test']
        }
      });
      expect(rv()).toEqual(true);
    });
  });

  describe('on reset', () => {
    test('should reset reactive reactive vars to their defaults values', () => {
      expect(rv()).toEqual(false);
      rv(true);
      expect(rv()).toEqual(true);
      cache.reset();
      expect(rv()).toEqual(false);
    });
  });
});