Skip to content

cloneDeep: use structuredClone in platforms where that's possible #12491

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

Open
wants to merge 5 commits into
base: release-4.0
Choose a base branch
from

Conversation

phryneas
Copy link
Member

No description provided.

@phryneas phryneas requested review from jerelmiller and Copilot and removed request for jerelmiller March 27, 2025 17:10
Copy link

changeset-bot bot commented Mar 27, 2025

🦋 Changeset detected

Latest commit: 8662e58

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@apollo/client Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@phryneas phryneas requested a review from jerelmiller March 27, 2025 17:10
@phryneas phryneas changed the title cloneDeep: use structuredClone in platform where that's possible cloneDeep: use structuredClone in platforms where that's possible Mar 27, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR updates the cloneDeep implementation to use the native structuredClone API where available while preserving compatibility with React Native by maintaining a dedicated implementation. The key changes include:

  • Refactoring cloneDeep in common/cloneDeep.ts to use structuredClone with a fallback.
  • Introducing a new cloneDeep implementation for React Native in common/cloneDeep.react-native.ts.
  • Export updates in index.react-native.ts and a corresponding changeset update.

Reviewed Changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 1 comment.

File Description
src/utilities/index.react-native.ts Updates module exports for React Native by referencing the new file.
src/utilities/common/cloneDeep.ts Refactors cloneDeep to use structuredClone with a fallback.
src/utilities/common/cloneDeep.react-native.ts Provides a React Native version of cloneDeep (duplicate helper definition present).
.changeset/nice-icons-approve.md Documents the changeset for the cloneDeep refactor.
Files not reviewed (1)
  • package.json: Language not supported
Comments suppressed due to low confidence (1)

src/utilities/common/cloneDeep.react-native.ts:41

  • Duplicate definition of cloneDeepHelper detected; remove the redundant implementation to prevent confusion and potential maintenance issues.
function cloneDeepHelper<T>(val: T, seen?: Map<any, any>): T {

Comment on lines +7 to 9
} catch {
return value;
}
Copy link
Preview

Copilot AI Mar 27, 2025

Choose a reason for hiding this comment

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

The fallback in cloneDeep returns the original value instead of performing a deep clone, which may result in unintended shared references; consider implementing a recursive deep cloning fallback for environments without structuredClone support.

Suggested change
} catch {
return value;
}
} catch {
return deepClone(value);
}
}
function deepClone<T>(value: T): T {
if (value === null || typeof value !== 'object') {
return value;
}
if (Array.isArray(value)) {
const arrCopy = [] as any[];
for (const item of value) {
arrCopy.push(deepClone(item));
}
return arrCopy as any;
}
const objCopy = {} as { [key: string]: any };
for (const key in value) {
if (Object.prototype.hasOwnProperty.call(value, key)) {
objCopy[key] = deepClone((value as { [key: string]: any })[key]);
}
}
return objCopy as T;

Copilot uses AI. Check for mistakes.

@svc-apollo-docs
Copy link

svc-apollo-docs commented Mar 27, 2025

⚠️ Docs preview not attached to branch

The preview was not built because the PR's base branch release-4.0 is not in the list of sources.

An Apollo team member can comment one of the following commands to dictate which branch to attach the preview to:

  • !docs set-base-branch version-2.6
  • !docs set-base-branch main

Build ID: 7e4b66d1ad35243c92a6ccf3

Copy link

pkg-pr-new bot commented Mar 27, 2025

npm i https://pkg.pr.new/@apollo/client@12491

commit: 8662e58

Copy link

netlify bot commented Mar 27, 2025

Deploy Preview for apollo-client-docs ready!

Name Link
🔨 Latest commit 8662e58
🔍 Latest deploy log https://app.netlify.com/sites/apollo-client-docs/deploys/67e589152e4f8b0008843a19
😎 Deploy Preview https://deploy-preview-12491--apollo-client-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

github-actions bot commented Mar 27, 2025

size-limit report 📦

Path Size
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" (CJS) 41.13 KB (-0.22% 🔽)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" (production) (CJS) 36.66 KB (-0.1% 🔽)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" 31.75 KB (-0.26% 🔽)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" (production) 26.71 KB (-0.54% 🔽)
import { ApolloProvider } from "@apollo/client/react" 5.17 KB (0%)
import { ApolloProvider } from "@apollo/client/react" (production) 960 B (0%)
import { useQuery } from "@apollo/client/react" 7.14 KB (0%)
import { useQuery } from "@apollo/client/react" (production) 2.89 KB (0%)
import { useLazyQuery } from "@apollo/client/react" 6.39 KB (0%)
import { useLazyQuery } from "@apollo/client/react" (production) 2.17 KB (0%)
import { useMutation } from "@apollo/client/react" 6.46 KB (0%)
import { useMutation } from "@apollo/client/react" (production) 2.23 KB (0%)
import { useSubscription } from "@apollo/client/react" 6.83 KB (0%)
import { useSubscription } from "@apollo/client/react" (production) 2.62 KB (0%)
import { useSuspenseQuery } from "@apollo/client/react" 8.21 KB (0%)
import { useSuspenseQuery } from "@apollo/client/react" (production) 4 KB (0%)
import { useBackgroundQuery } from "@apollo/client/react" 8.06 KB (0%)
import { useBackgroundQuery } from "@apollo/client/react" (production) 3.83 KB (0%)
import { useLoadableQuery } from "@apollo/client/react" 8.06 KB (0%)
import { useLoadableQuery } from "@apollo/client/react" (production) 3.87 KB (0%)
import { useReadQuery } from "@apollo/client/react" 5.83 KB (0%)
import { useReadQuery } from "@apollo/client/react" (production) 1.61 KB (0%)
import { useFragment } from "@apollo/client/react" 5.89 KB (0%)
import { useFragment } from "@apollo/client/react" (production) 1.66 KB (0%)

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