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

try out regional deep equality #2259

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 9 additions & 1 deletion packages/effect/src/Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @since 2.0.0
*/
import type * as Cause from "./Cause.js"
import type { LazyArg } from "./Function.js"
import * as core from "./internal/core.js"
import * as internal from "./internal/data.js"
import { StructuralPrototype } from "./internal/effectable.js"
Expand Down Expand Up @@ -452,10 +453,17 @@ export const TaggedError = <Tag extends string>(tag: Tag): new<A extends Record<
return Base as any
}

/**
* Use deep equality to compare objects by value
*
* @since 2.0.0
*/
export const withDeepEquality: <A>(f: LazyArg<A>) => A = internal.withDeepEquality

/**
* Uses a proxy to compare objects by value, supports deep equality
*
* @since 2.0.0
* @category constructors
*/
export const proxy: <A>(value: A, options?: { deep: boolean }) => A = internal.proxy
export const proxy: <A>(value: A) => A = internal.proxy
23 changes: 19 additions & 4 deletions packages/effect/src/internal/data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as Equal from "../Equal.js"
import type { LazyArg } from "../Function.js"
import { pipe } from "../Function.js"
import { globalValue } from "../GlobalValue.js"
import * as Hash from "../Hash.js"
import type * as Types from "../Types.js"
import { StructuralPrototype } from "./effectable.js"
Expand Down Expand Up @@ -38,8 +40,21 @@ export const struct = <As extends Readonly<Record<string, any>>>(as: As): As =>

const deepSymbol = Symbol.for("effect/Data/deep")

const regionalConfig = globalValue("effect/Data/regionalConfig", () => ({ deep: false }))

/** @internal */
export const withDeepEquality = <A>(f: LazyArg<A>): A => {
try {
regionalConfig.deep = true
return f()
} finally {
regionalConfig.deep = false
}
}

/** @internal */
export const proxy = <A>(value: A, options?: { deep: boolean }): A => {
export const proxy = <A>(value: A): A => {
const useDeep = regionalConfig.deep
if ((typeof value === "function" || typeof value === "object") && value !== null) {
let hashCache: any = "INIT"
return new Proxy(value, {
Expand All @@ -51,12 +66,12 @@ export const proxy = <A>(value: A, options?: { deep: boolean }): A => {
},
get(target, p) {
if (p === deepSymbol) {
return options?.deep
return useDeep
}
if (p === Hash.symbol) {
return () => {
if (hashCache === "INIT") {
hashCache = options?.deep === true
hashCache = useDeep
? deepHash(value)
: Array.isArray(value)
? Hash.array(value)
Expand All @@ -66,7 +81,7 @@ export const proxy = <A>(value: A, options?: { deep: boolean }): A => {
}
}
if (p === Equal.symbol) {
if (options?.deep === true) {
if (useDeep) {
return deepComp
}
if (Array.isArray(value)) {
Expand Down
28 changes: 16 additions & 12 deletions packages/effect/test/Data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,27 @@ describe("Data", () => {
})

it("proxy deep", () => {
const x = Data.proxy({
a: 0,
b: 1,
c: { a: 0, b: 7 }
}, { deep: true })

const y = Data.proxy({
a: 0,
b: 1,
c: { a: 0, b: 7 }
}, { deep: true })
const x = Data.withDeepEquality(() =>
Data.proxy({
a: 0,
b: 1,
c: { a: 0, b: 7 }
})
)

const y = Data.withDeepEquality(() =>
Data.proxy({
a: 0,
b: 1,
c: { a: 0, b: 7 }
})
)

const z = Data.proxy({
a: 0,
b: 1,
c: { a: 0, b: 7 }
}, { deep: false })
})

expect(Equal.equals(x, y)).toBe(true)
expect(Equal.equals(x, z)).toBe(false)
Expand Down