Skip to content
This repository has been archived by the owner on Jul 6, 2020. It is now read-only.

Commit

Permalink
Refactor store code and move global state to store/data instead (#134)
Browse files Browse the repository at this point in the history
* Move store code to src/store/

* Refactor to move all global state to store/data

* Add some more comments to store/data.ts

* Simplify updateDependencies
  • Loading branch information
kitten authored Dec 13, 2019
1 parent f25882a commit e0ff69a
Show file tree
Hide file tree
Showing 22 changed files with 421 additions and 417 deletions.
1 change: 1 addition & 0 deletions src/ast/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { getFieldArguments, normalizeVariables } from './variables';
export { SchemaPredicates } from './schemaPredicates';
export * from './traversal';
export * from './node';
2 changes: 1 addition & 1 deletion src/ast/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
valueFromASTUntyped,
} from 'graphql';

import { makeDict } from '../helpers/dict';
import { getName } from './node';
import { makeDict } from '../store';
import { Variables } from '../types';

/** Evaluates a fields arguments taking vars into account */
Expand Down
7 changes: 3 additions & 4 deletions src/cacheExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import {
import { IntrospectionQuery } from 'graphql';
import { filter, map, merge, pipe, share, tap } from 'wonka';
import { query, write, writeOptimistic } from './operations';
import { SchemaPredicates } from './ast/schemaPredicates';
import { makeDict } from './helpers/dict';
import { Store } from './store';
import { SchemaPredicates } from './ast';
import { makeDict, Store, clearOptimistic } from './store';

import {
UpdatesConfig,
Expand Down Expand Up @@ -208,7 +207,7 @@ export const cacheExchange = (opts?: CacheExchangeOpts): Exchange => ({
const { key } = operation;
if (optimisticKeys.has(key)) {
optimisticKeys.delete(key);
store.clearOptimistic(key);
clearOptimistic(store.data, key);
}

let writeDependencies: Set<string> | void;
Expand Down
169 changes: 0 additions & 169 deletions src/helpers/data.test.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/helpers/dict.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/helpers/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './operations';
export * from './types';
export { Store } from './store';
export { Store, initDataState, clearDataState } from './store';
export { cacheExchange } from './cacheExchange';
export { populateExchange } from './populateExchange';
9 changes: 3 additions & 6 deletions src/operations/invalidate.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Store, initStoreState, clearStoreState } from '../store';
import gql from 'graphql-tag';
import { write } from './write';
import { invalidate } from './invalidate';
import * as InMemoryData from '../store/data';
import { Store } from '../store';
import { SchemaPredicates } from '../ast/schemaPredicates';

const TODO_QUERY = gql`
Expand Down Expand Up @@ -48,11 +49,7 @@ describe('Query', () => {
);
spy.console = jest.spyOn(console, 'warn');

initStoreState(store, 0);
});

afterEach(() => {
clearStoreState();
InMemoryData.initDataState(store.data, 0);
});

it('should warn once for invalid fields on an entity', () => {
Expand Down
24 changes: 10 additions & 14 deletions src/operations/invalidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import {
SelectionSet,
} from '../types';

import { Store, addDependency } from '../store';
import * as InMemoryData from '../store/data';
import { Store, keyOfField } from '../store';
import { SchemaPredicates } from '../ast';
import { SelectionIterator } from './shared';
import { joinKeys, keyOfField } from '../helpers';
import { SchemaPredicates } from '../ast/schemaPredicates';

interface Context {
store: Store;
Expand Down Expand Up @@ -51,17 +51,15 @@ export const invalidateSelection = (
entityKey: string,
select: SelectionSet
) => {
const { store } = ctx;
const isQuery = entityKey === 'Query';

let typename: EntityField;
if (!isQuery) {
addDependency(entityKey);
typename = store.getField(entityKey, '__typename');
typename = InMemoryData.readRecord(entityKey, '__typename');
if (typeof typename !== 'string') {
return;
} else {
store.writeRecord(undefined, entityKey, keyOfField('__typename'));
InMemoryData.writeRecord(entityKey, '__typename', undefined);
}
} else {
typename = entityKey;
Expand All @@ -76,7 +74,6 @@ export const invalidateSelection = (
fieldName,
getFieldArguments(node, ctx.variables)
);
const key = joinKeys(entityKey, fieldKey);

if (
process.env.NODE_ENV !== 'production' &&
Expand All @@ -86,15 +83,14 @@ export const invalidateSelection = (
ctx.schemaPredicates.isFieldAvailableOnType(typename, fieldName);
}

if (isQuery) addDependency(key);

if (node.selectionSet === undefined) {
store.writeRecord(undefined, entityKey, fieldKey);
InMemoryData.writeRecord(entityKey, fieldKey, undefined);
} else {
const fieldSelect = getSelectionSet(node);
const link = store.getLink(entityKey, fieldKey);
store.writeLink(undefined, entityKey, fieldKey);
store.writeRecord(undefined, entityKey, fieldKey);
const link = InMemoryData.readLink(entityKey, fieldKey);

InMemoryData.writeLink(entityKey, fieldKey, undefined);
InMemoryData.writeRecord(entityKey, fieldKey, undefined);

if (Array.isArray(link)) {
for (let i = 0, l = link.length; i < l; i++) {
Expand Down
Loading

0 comments on commit e0ff69a

Please sign in to comment.