-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4032 from apollographql/fix-issue-3992
Various improvements around previousResult/newData change detection.
- Loading branch information
Showing
8 changed files
with
95 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,54 @@ | ||
import fclone from 'fclone'; | ||
|
||
/** | ||
* Deeply clones a value to create a new instance. | ||
*/ | ||
export function cloneDeep<T>(value: T): T { | ||
return fclone(value); | ||
} | ||
const { toString } = Object.prototype; | ||
|
||
/** | ||
* Deeply clones a value to create a new instance. | ||
*/ | ||
export function cloneDeep<T>(value: T): T { | ||
return cloneDeepHelper(value, new Map()); | ||
} | ||
|
||
function cloneDeepHelper<T>(val: T, seen: Map<any, any>): T { | ||
switch (toString.call(val)) { | ||
case "[object Array]": { | ||
if (seen.has(val)) return seen.get(val); | ||
const copy: T & any[] = (val as any).slice(0); | ||
seen.set(val, copy); | ||
copy.forEach(function (child, i) { | ||
copy[i] = cloneDeepHelper(child, seen); | ||
}); | ||
return copy; | ||
} | ||
|
||
case "[object Date]": | ||
return new Date(+val) as T & Date; | ||
|
||
case "[object Object]": { | ||
if (seen.has(val)) return seen.get(val); | ||
// High fidelity polyfills of Object.create and Object.getPrototypeOf are | ||
// possible in all JS environments, so we will assume they exist/work. | ||
const copy = Object.create(Object.getPrototypeOf(val)); | ||
seen.set(val, copy); | ||
|
||
if (typeof Object.getOwnPropertyDescriptor === "function") { | ||
const handleKey = function (key: string | symbol) { | ||
const desc = Object.getOwnPropertyDescriptor(val, key); | ||
desc.value = cloneDeepHelper((val as any)[key], seen); | ||
Object.defineProperty(copy, key, desc); | ||
}; | ||
Object.getOwnPropertyNames(val).forEach(handleKey); | ||
if (typeof Object.getOwnPropertySymbols === "function") { | ||
Object.getOwnPropertySymbols(val).forEach(handleKey); | ||
} | ||
} else { | ||
Object.keys(val).forEach(key => { | ||
copy[key] = cloneDeepHelper((val as any)[key], seen); | ||
}); | ||
} | ||
|
||
return copy; | ||
} | ||
|
||
default: | ||
return val; | ||
} | ||
} |