Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onChange callback in cache configuration #217

Merged
merged 11 commits into from
Nov 8, 2017
Merged

Conversation

yuit
Copy link
Contributor

@yuit yuit commented Nov 1, 2017

Add onChange to cache configuration API to allow users to perform tasks such as dispatch redux action when there is a change in the cache or log the update etc.

@yuit yuit requested a review from nevir November 1, 2017 21:59
@@ -4,13 +4,11 @@ import { OptimisticUpdateQueue } from './OptimisticUpdateQueue';
/**
* Maintains an immutable, point-in-time view of the cache.
*/
export class CacheSnapshot {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👎 keep the class: It helps enforce that we always construct objects with the same shape (e.g. the same set of properties, constructed in the same order). Makes bookkeeping in the VM lighter. It also aids in debugging by tagging these objects with their type

See https://jsperf.com/class-vs-object-literal-creation also

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Talk offline.... keep the class add comment to make sure we keep it and remove spread operator

src/Cache.ts Outdated
@@ -120,6 +124,10 @@ export class Cache implements Queryable {
const { snapshot, editedNodeIds } = transaction.commit();
this._setSnapshot(snapshot, editedNodeIds);

// Call the on-change callback here to notify any change
if (this._context.onChange && typeof this._context.onChange === 'function') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can drop the initial check and just if (typeof this._context.onChange === 'function') {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or, actually - just go with if (this._context.onChange) { - we already enforce that onChange is a function via types (and could do a runtime check when constructing the cache if we want to be paranoid)

Copy link
Contributor Author

@yuit yuit Nov 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Errr fright the typeof should guard for that... yea I was worry about user in JS lib who doesn't have types to pass garbage in

Copy link
Contributor Author

@yuit yuit Nov 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually if we gonna do runtime check we should do for others callback as well so it is much better to do when constructing...I agree now that there shouldn't be this runtime typecheck floating all over and just aggregate at one location

Open a separate issue -> #220

@@ -54,4 +55,8 @@ export class Hermes extends ApolloQueryable implements ApolloCache<GraphSnapshot
const query = toQuery(options.query, options.variables, options.rootId);
return this._queryable.watch(query, options.callback);
}

getCacheSnapshot(): CacheSnapshot {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currentCacheSnapshot IMO - will help communicate that it frequently changes

@@ -18,6 +19,8 @@ export namespace CacheContext {
export type EntityIdMapper = (node: JsonObject) => string | number | undefined;
export type EntityTransformer = (node: JsonObject) => void;
export type LogEmitter = (message: string, ...metadata: any[]) => void;
export type OnChangeCallBack = (newCacheShapshot: CacheSnapshot, editedNodeIds: Set<String>) => void;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: ChangeCallback (callback is one word)

src/Cache.ts Outdated
@@ -120,6 +124,10 @@ export class Cache implements Queryable {
const { snapshot, editedNodeIds } = transaction.commit();
this._setSnapshot(snapshot, editedNodeIds);

// Call the on-change callback here to notify any change
if (this._context.onChange && typeof this._context.onChange === 'function') {
this._context.onChange(this._snapshot, editedNodeIds);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We update the snapshot in other cases (reset()). It would probably be clearer to move this logic into _setSnapshot

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree... forgot about the reset operation

@yuit yuit merged commit a2da1d7 into master Nov 8, 2017
@yuit yuit deleted the yuit/onChangeUpdateCB branch November 8, 2017 18:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants