Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
struct Memoized<A: Equatable, R> {
typealias Value = (_ params: A) -> R
var callback: Value
@XState private var prevParam: A? = nil
@XState private var prevValue: R? = nil
init(_ callback: @escaping Value) {
self.callback = callback
}
func call(_ param: A) -> R {
defer {
self.prevParam = param
}
if self.prevParam != param {
self.prevValue = callback(param)
}
return self.prevValue!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment