Skip to content

Commit

Permalink
Rename InMemoryCache#makeLocalVar to makeVar.
Browse files Browse the repository at this point in the history
Shorter, and not as suggestive of Apollo Client 2.x local state. Returns a
function of type ReactiveVar<T> (f.k.a. LocalVar<T>).

Follow-up to #5799.
  • Loading branch information
benjamn committed Feb 21, 2020
1 parent 42878cb commit 286c10a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
14 changes: 7 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@
This API gracefully handles cases where multiple field values are associated with a single field name, and also removes the need for updating the cache by reading a query or fragment, modifying the result, and writing the modified result back into the cache. Behind the scenes, the `cache.evict` method is now implemented in terms of `cache.modify`. <br/>
[@benjamn](https://github.com/benjamn) in [#5909](https://github.com/apollographql/apollo-client/pull/5909)

- `InMemoryCache` provides a new API for storing local state that can be easily updated by external code:
- `InMemoryCache` provides a new API for storing client state that can be updated from anywhere:
```ts
const lv = cache.makeLocalVar(123)
console.log(lv()) // 123
console.log(lv(lv() + 1)) // 124
console.log(lv()) // 124
lv("asdf") // TS type error
const v = cache.makeVar(123)
console.log(v()) // 123
console.log(v(v() + 1)) // 124
console.log(v()) // 124
v("asdf") // TS type error
```
These local variables are _reactive_ in the sense that updating their values invalidates any previously cached query results that depended on the old values. <br/>
These variables are _reactive_ in the sense that updating their values invalidates any previously cached query results that depended on the old values. <br/>
[@benjamn](https://github.com/benjamn) in [#5799](https://github.com/apollographql/apollo-client/pull/5799)

- Various cache read and write performance optimizations, cutting read and write times by more than 50% in larger benchmarks. <br/>
Expand Down
2 changes: 1 addition & 1 deletion src/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export {
export {
InMemoryCache,
InMemoryCacheConfig,
LocalVar,
ReactiveVar,
} from './inmemory/inMemoryCache';

export {
Expand Down
4 changes: 2 additions & 2 deletions src/cache/inmemory/__tests__/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2293,7 +2293,7 @@ describe("InMemoryCache#modify", () => {
});
});

describe("cache.makeLocalVar", () => {
describe("cache.makeVar", () => {
function makeCacheAndVar(resultCaching: boolean) {
const cache: InMemoryCache = new InMemoryCache({
resultCaching,
Expand All @@ -2308,7 +2308,7 @@ describe("cache.makeLocalVar", () => {
},
});

const nameVar = cache.makeLocalVar("Ben");
const nameVar = cache.makeVar("Ben");

const query = gql`
query {
Expand Down
20 changes: 10 additions & 10 deletions src/cache/inmemory/__tests__/policies.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import gql from "graphql-tag";
import { InMemoryCache, LocalVar } from "../inMemoryCache";
import { StoreValue } from "../../../utilities";
import { FieldPolicy, Policies } from "../policies";

import { InMemoryCache } from "../inMemoryCache";
import { Policies } from "../policies";
import { Reference, StoreObject } from "../../../core";

function reverse(s: string) {
Expand Down Expand Up @@ -925,15 +925,15 @@ describe("type policies", function () {
result: {
read(_, { storage }) {
if (!storage.jobName) {
storage.jobName = cache.makeLocalVar<string>();
storage.jobName = cache.makeVar<string>();
}
return storage.jobName();
},
merge(_, incoming: string, { storage }) {
if (storage.jobName) {
storage.jobName(incoming);
} else {
storage.jobName = cache.makeLocalVar(incoming);
storage.jobName = cache.makeVar(incoming);
}
},
},
Expand Down Expand Up @@ -1439,11 +1439,11 @@ describe("type policies", function () {
// Rather than writing ownTime data into the cache, we maintain it
// externally in this object:
const ownTimes = {
"parent task": cache.makeLocalVar(2),
"child task 1": cache.makeLocalVar(3),
"child task 2": cache.makeLocalVar(4),
"grandchild task": cache.makeLocalVar(5),
"independent task": cache.makeLocalVar(11),
"parent task": cache.makeVar(2),
"child task 1": cache.makeVar(3),
"child task 2": cache.makeVar(4),
"grandchild task": cache.makeVar(5),
"independent task": cache.makeVar(11),
};

cache.writeQuery({
Expand Down
13 changes: 7 additions & 6 deletions src/cache/inmemory/inMemoryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,20 +319,21 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
);
}

public makeLocalVar<T>(value?: T): LocalVar<T> {
return function LocalVar(newValue) {
public makeVar<T>(value?: T): ReactiveVar<T> {
return function rv(newValue) {
if (arguments.length > 0) {
if (value !== newValue) {
value = newValue;
localVarDep.dirty(LocalVar);
varDep.dirty(rv);
}
} else {
localVarDep(LocalVar);
varDep(rv);
}
return value;
};
}
}

const localVarDep = dep<LocalVar<any>>();
export type LocalVar<T> = (newValue?: T) => T;
const varDep = dep<ReactiveVar<any>>();

export type ReactiveVar<T> = (newValue?: T) => T;

0 comments on commit 286c10a

Please sign in to comment.