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

Make swr zero dependency #1024

Merged
merged 2 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@
"ts-jest": "25.5.1",
"typescript": "3.6.4"
},
"dependencies": {
"dequal": "2.0.2"
},
"peerDependencies": {
"react": "^16.11.0 || ^17.0.0"
}
Expand Down
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dequal } from 'dequal/lite'
import deeEqual from './libs/deep-equal'
huozhi marked this conversation as resolved.
Show resolved Hide resolved
import { Configuration, RevalidatorOptions, Revalidator } from './types'
import Cache from './cache'
import webPreset from './libs/web-preset'
Expand Down Expand Up @@ -64,7 +64,7 @@ const defaultConfig = {
refreshWhenOffline: false,
shouldRetryOnError: true,
suspense: false,
compare: dequal,
compare: deeEqual,
huozhi marked this conversation as resolved.
Show resolved Hide resolved

isPaused: () => false,
...webPreset
Expand Down
31 changes: 31 additions & 0 deletions src/libs/deep-equal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const has = Object.prototype.hasOwnProperty
huozhi marked this conversation as resolved.
Show resolved Hide resolved

export default function deeEqual(foo: any, bar: any) {
let ctor, len
if (foo === bar) return true

if (foo && bar && (ctor = foo.constructor) === bar.constructor) {
if (ctor === Date) return foo.getTime() === bar.getTime()
if (ctor === RegExp) return foo.toString() === bar.toString()

if (ctor === Array) {
if ((len = foo.length) === bar.length) {
while (len && deeEqual(foo[len], bar[len])) {
len--
}
}
return len === -1
}

if (!ctor || typeof foo === 'object') {
len = 0
for (ctor in foo) {
if (has.call(foo, ctor) && ++len && !has.call(bar, ctor)) return false
if (!(ctor in bar) || !deeEqual(foo[ctor], bar[ctor])) return false
}
return Object.keys(bar).length === len
}
}

return foo !== foo && bar !== bar
}
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1737,11 +1737,6 @@ delayed-stream@~1.0.0:
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
integrity sha1-3zrhmayt+31ECqrgsp4icrJOxhk=

[email protected]:
version "2.0.2"
resolved "https://registry.yarnpkg.com/dequal/-/dequal-2.0.2.tgz#85ca22025e3a87e65ef75a7a437b35284a7e319d"
integrity sha512-q9K8BlJVxK7hQYqa6XISGmBZbtQQWVXSrRrWreHC94rMt1QL/Impruc+7p2CYSYuVIUr+YCt6hjrs1kkdJRTug==

detect-newline@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/detect-newline/-/detect-newline-3.1.0.tgz#576f5dfc63ae1a192ff192d8ad3af6308991b651"
Expand Down