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

Use mutable hashmap for rx family #168

Merged
merged 4 commits into from
Jan 14, 2025
Merged
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
5 changes: 5 additions & 0 deletions .changeset/gold-ants-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect-rx/rx": patch
---

fix: family returning same atom if same hash
33 changes: 17 additions & 16 deletions packages/rx/src/Rx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import * as Either from "effect/Either"
import * as Exit from "effect/Exit"
import { constVoid, dual, pipe } from "effect/Function"
import { globalValue } from "effect/GlobalValue"
import * as Hash from "effect/Hash"
import * as Inspectable from "effect/Inspectable"
import * as Layer from "effect/Layer"
import * as MutableHashMap from "effect/MutableHashMap"
import * as Option from "effect/Option"
import { type Pipeable, pipeArguments } from "effect/Pipeable"
import * as Runtime from "effect/Runtime"
Expand Down Expand Up @@ -1025,34 +1025,35 @@ export const family = typeof WeakRef === "undefined" || typeof FinalizationRegis
<Arg, T extends object>(
f: (arg: Arg) => T
): (arg: Arg) => T => {
const atoms = new Map<number, T>()
const atoms = MutableHashMap.empty<Arg, T>()
return function(arg) {
const hash = Hash.hash(arg)
const atom = atoms.get(hash)
if (atom !== undefined) {
return atom
const atomEntry = MutableHashMap.get(atoms, arg)
if (atomEntry._tag === "Some") {
return atomEntry.value
}
const newAtom = f(arg)
atoms.set(hash, newAtom)
MutableHashMap.set(atoms, arg, newAtom)
return newAtom
}
} :
<Arg, T extends object>(
f: (arg: Arg) => T
): (arg: Arg) => T => {
const atoms = new Map<number, WeakRef<T>>()
const registry = new FinalizationRegistry<number>((hash) => {
atoms.delete(hash)
const atoms = MutableHashMap.empty<Arg, WeakRef<T>>()
const registry = new FinalizationRegistry<Arg>((arg) => {
MutableHashMap.remove(atoms, arg)
})
return function(arg) {
const hash = Hash.hash(arg)
const atom = atoms.get(hash)?.deref()
if (atom !== undefined) {
return atom
const atomEntry = MutableHashMap.get(atoms, arg).pipe(
Option.flatMapNullable((ref) => ref.deref())
)

if (atomEntry._tag === "Some") {
return atomEntry.value
}
const newAtom = f(arg)
atoms.set(hash, new WeakRef(newAtom))
registry.register(newAtom, hash)
MutableHashMap.set(atoms, arg, new WeakRef(newAtom))
registry.register(newAtom, arg)
return newAtom
}
}
Expand Down
Loading