Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
Stop importing shallowEqual from fbjs/lib/shallowEqual.
Browse files Browse the repository at this point in the history
This shaves a tiny amount of bundle size, but more importantly it
implements shallowEqual using TypeScript, and avoids one of the few
remaining uses of require.
  • Loading branch information
benjamn committed Jan 20, 2019
1 parent f0c395b commit b72dfd6
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 26 deletions.
23 changes: 2 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@
"zen-observable-ts": "0.8.13"
},
"dependencies": {
"fbjs": "^1.0.0",
"hoist-non-react-statics": "^3.0.0",
"invariant": "^2.2.2",
"lodash.isequal": "^4.5.0",
Expand Down
2 changes: 1 addition & 1 deletion src/Mutation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ApolloClient, { PureQueryOptions, ApolloError, FetchPolicy } from 'apollo
import { DataProxy } from 'apollo-cache';
const invariant = require('invariant');
import { DocumentNode, GraphQLError } from 'graphql';
const shallowEqual = require('fbjs/lib/shallowEqual');
import shallowEqual from './utils/shallowEqual';

import { OperationVariables, RefetchQueriesProviderFn } from './types';
import { parser, DocumentType } from './parser';
Expand Down
2 changes: 1 addition & 1 deletion src/Query.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { parser, DocumentType, IDocumentDefinition } from './parser';
import { getClient } from './component-utils';
import { RenderPromises } from './getDataFromTree';

const shallowEqual = require('fbjs/lib/shallowEqual');
import shallowEqual from './utils/shallowEqual';
const invariant = require('invariant');

export type ObservableQueryFields<TData, TVariables> = Pick<
Expand Down
2 changes: 1 addition & 1 deletion src/Subscriptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ZenObservable } from 'zen-observable-ts';
import { OperationVariables } from './types';
import { getClient } from './component-utils';

const shallowEqual = require('fbjs/lib/shallowEqual');
import shallowEqual from './utils/shallowEqual';
const invariant = require('invariant');

export interface SubscriptionResult<TData = any> {
Expand Down
2 changes: 1 addition & 1 deletion src/queryRecycler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ObservableQuery } from 'apollo-client';
import { ZenObservable } from 'zen-observable-ts';
import { QueryOpts } from './types';

const shallowEqual = require('fbjs/lib/shallowEqual');
import shallowEqual from './utils/shallowEqual';

// XXX move this logic to ObservableQuery / QueryManager in apollo-client

Expand Down
32 changes: 32 additions & 0 deletions src/utils/shallowEqual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { hasOwnProperty } = Object.prototype;

function is(x: any, y: any) {
if (x === y) {
return x !== 0 || y !== 0 || 1 / x === 1 / y;
}
return x !== x && y !== y;
}

function isObject(obj: any): obj is { [key: string]: any } {
return obj !== null && typeof obj === "object";
}

export default function shallowEqual(objA: any, objB: any) {
if (is(objA, objB)) {
return true;
}

if (!isObject(objA) || !isObject(objB)) {
return false;
}

const keys = Object.keys(objA);

if (keys.length !== Object.keys(objB).length) {
return false;
}

return keys.every(
key => hasOwnProperty.call(objB, key) && is(objA[key], objB[key]),
);
}

0 comments on commit b72dfd6

Please sign in to comment.