Skip to content

Commit

Permalink
Cache, ScopedCache swap type parameters [another try] (#2239)
Browse files Browse the repository at this point in the history
Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
  • Loading branch information
2 people authored and mikearnaldi committed Apr 16, 2024
1 parent 5c2b561 commit 10c169e
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 126 deletions.
7 changes: 7 additions & 0 deletions .changeset/tough-jeans-allow.md
@@ -0,0 +1,7 @@
---
"effect": minor
---

`Cache<Key, Error, Value>` has been changed to `Cache<Key, Value, Error = never>`.
`ScopedCache<Key, Error, Value>` has been changed to `ScopedCache<Key, Value, Error = never>`.
`Lookup<Key, Environment, Error, Value>` has been changed to `Lookup<Key, Value, Error = never, Environment = never>`
22 changes: 12 additions & 10 deletions packages/effect/src/Cache.ts
Expand Up @@ -43,7 +43,7 @@ export type CacheTypeId = typeof CacheTypeId
* @since 2.0.0
* @category models
*/
export interface Cache<in out Key, out Error, out Value> extends ConsumerCache<Key, Error, Value> {
export interface Cache<in out Key, out Value, out Error = never> extends ConsumerCache<Key, Value, Error> {
/**
* Retrieves the value associated with the specified key if it exists.
* Otherwise computes the value with the lookup function, puts it in the
Expand Down Expand Up @@ -83,7 +83,7 @@ export interface Cache<in out Key, out Error, out Value> extends ConsumerCache<K
* @since 2.0.0
* @category models
*/
export interface ConsumerCache<in out Key, out Error, out Value> extends Cache.Variance<Key, Error, Value> {
export interface ConsumerCache<in out Key, out Value, out Error = never> extends Cache.Variance<Key, Value, Error> {
/**
* Retrieves the value associated with the specified key if it exists.
* Otherwise returns `Option.none`.
Expand Down Expand Up @@ -156,7 +156,7 @@ export declare namespace Cache {
* @since 2.0.0
* @category models
*/
export interface Variance<in out Key, out Error, out Value> {
export interface Variance<in out Key, out Value, out Error> {
readonly [CacheTypeId]: {
readonly _Key: Types.Invariant<Key>
readonly _Error: Types.Covariant<Error>
Expand All @@ -172,13 +172,13 @@ export declare namespace Cache {
* @since 2.0.0
* @category constructors
*/
export const make: <Key, Environment, Error, Value>(
export const make: <Key, Value, Error = never, Environment = never>(
options: {
readonly capacity: number
readonly timeToLive: Duration.DurationInput
readonly lookup: Lookup<Key, Environment, Error, Value>
readonly lookup: Lookup<Key, Value, Error, Environment>
}
) => Effect.Effect<Cache<Key, Error, Value>, never, Environment> = internal.make
) => Effect.Effect<Cache<Key, Value, Error>, never, Environment> = internal.make

/**
* Constructs a new cache with the specified capacity, time to live, and
Expand All @@ -188,13 +188,13 @@ export const make: <Key, Environment, Error, Value>(
* @since 2.0.0
* @category constructors
*/
export const makeWith: <Key, Environment, Error, Value>(
export const makeWith: <Key, Value, Error = never, Environment = never>(
options: {
readonly capacity: number
readonly lookup: Lookup<Key, Environment, Error, Value>
readonly lookup: Lookup<Key, Value, Error, Environment>
readonly timeToLive: (exit: Exit.Exit<Value, Error>) => Duration.DurationInput
}
) => Effect.Effect<Cache<Key, Error, Value>, never, Environment> = internal.makeWith
) => Effect.Effect<Cache<Key, Value, Error>, never, Environment> = internal.makeWith

/**
* `CacheStats` represents a snapshot of statistics for the cache as of a
Expand Down Expand Up @@ -249,4 +249,6 @@ export const makeEntryStats: (loadedMillis: number) => EntryStats = internal.mak
* @since 2.0.0
* @category models
*/
export type Lookup<Key, Environment, Error, Value> = (key: Key) => Effect.Effect<Value, Error, Environment>
export type Lookup<Key, Value, Error = never, Environment = never> = (
key: Key
) => Effect.Effect<Value, Error, Environment>
2 changes: 1 addition & 1 deletion packages/effect/src/Request.ts
Expand Up @@ -257,7 +257,7 @@ export interface Listeners {
* @since 2.0.0
*/
export interface Cache extends
_Cache.ConsumerCache<Request<any, any>, never, {
_Cache.ConsumerCache<Request<any, any>, {
listeners: Listeners
handle: Deferred<unknown, unknown>
}>
Expand Down
20 changes: 11 additions & 9 deletions packages/effect/src/ScopedCache.ts
Expand Up @@ -27,7 +27,9 @@ export type ScopedCacheTypeId = typeof ScopedCacheTypeId
* @since 2.0.0
* @category models
*/
export interface ScopedCache<in Key, out Error, out Value> extends ScopedCache.Variance<Key, Error, Value>, Pipeable {
export interface ScopedCache<in Key, out Value, out Error = never>
extends ScopedCache.Variance<Key, Value, Error>, Pipeable
{
/**
* Retrieves the value associated with the specified key if it exists.
* Otherwise returns `Option.none`.
Expand Down Expand Up @@ -98,7 +100,7 @@ export declare namespace ScopedCache {
* @since 2.0.0
* @category models
*/
export interface Variance<in Key, out Error, out Value> {
export interface Variance<in Key, out Value, out Error> {
readonly [ScopedCacheTypeId]: {
_Key: Types.Contravariant<Key>
_Error: Types.Covariant<Error>
Expand All @@ -114,13 +116,13 @@ export declare namespace ScopedCache {
* @since 2.0.0
* @category constructors
*/
export const make: <Key, Environment, Error, Value>(
export const make: <Key, Value, Error = never, Environment = never>(
options: {
readonly lookup: Lookup<Key, Environment, Error, Value>
readonly lookup: Lookup<Key, Value, Error, Environment>
readonly capacity: number
readonly timeToLive: Duration.DurationInput
}
) => Effect.Effect<ScopedCache<Key, Error, Value>, never, Scope.Scope | Environment> = internal.make
) => Effect.Effect<ScopedCache<Key, Value, Error>, never, Scope.Scope | Environment> = internal.make

/**
* Constructs a new cache with the specified capacity, time to live, and
Expand All @@ -130,20 +132,20 @@ export const make: <Key, Environment, Error, Value>(
* @since 2.0.0
* @category constructors
*/
export const makeWith: <Key, Environment, Error, Value>(
export const makeWith: <Key, Value, Error = never, Environment = never>(
options: {
readonly capacity: number
readonly lookup: Lookup<Key, Environment, Error, Value>
readonly lookup: Lookup<Key, Value, Error, Environment>
readonly timeToLive: (exit: Exit.Exit<Value, Error>) => Duration.DurationInput
}
) => Effect.Effect<ScopedCache<Key, Error, Value>, never, Scope.Scope | Environment> = internal.makeWith
) => Effect.Effect<ScopedCache<Key, Value, Error>, never, Scope.Scope | Environment> = internal.makeWith

/**
* Similar to `Cache.Lookup`, but executes the lookup function within a `Scope`.
*
* @since 2.0.0
* @category models
*/
export type Lookup<Key, Environment, Error, Value> = (
export type Lookup<Key, Value, Error = never, Environment = never> = (
key: Key
) => Effect.Effect<Value, Error, Environment | Scope.Scope>
74 changes: 37 additions & 37 deletions packages/effect/src/internal/cache.ts
Expand Up @@ -29,13 +29,13 @@ import * as fiberRuntime from "./fiberRuntime.js"
*
* @internal
*/
export type MapValue<Key, Error, Value> =
| Complete<Key, Error, Value>
| Pending<Key, Error, Value>
| Refreshing<Key, Error, Value>
export type MapValue<Key, Value, Error> =
| Complete<Key, Value, Error>
| Pending<Key, Value, Error>
| Refreshing<Key, Value, Error>

/** @internal */
export interface Complete<out Key, out Error, out Value> {
export interface Complete<out Key, out Value, out Error> {
readonly _tag: "Complete"
readonly key: MapKey<Key>
readonly exit: Exit.Exit<Value, Error>
Expand All @@ -44,26 +44,26 @@ export interface Complete<out Key, out Error, out Value> {
}

/** @internal */
export interface Pending<out Key, in out Error, in out Value> {
export interface Pending<out Key, in out Value, in out Error> {
readonly _tag: "Pending"
readonly key: MapKey<Key>
readonly deferred: Deferred.Deferred<Value, Error>
}

/** @internal */
export interface Refreshing<out Key, in out Error, in out Value> {
export interface Refreshing<out Key, in out Value, in out Error> {
readonly _tag: "Refreshing"
readonly deferred: Deferred.Deferred<Value, Error>
readonly complete: Complete<Key, Error, Value>
readonly complete: Complete<Key, Value, Error>
}

/** @internal */
export const complete = <Key, Error, Value>(
export const complete = <Key, Value, Error>(
key: MapKey<Key>,
exit: Exit.Exit<Value, Error>,
entryStats: Cache.EntryStats,
timeToLiveMillis: number
): MapValue<Key, Error, Value> =>
): MapValue<Key, Value, Error> =>
Data.struct({
_tag: "Complete" as const,
key,
Expand All @@ -73,21 +73,21 @@ export const complete = <Key, Error, Value>(
})

/** @internal */
export const pending = <Key, Error, Value>(
export const pending = <Key, Value, Error>(
key: MapKey<Key>,
deferred: Deferred.Deferred<Value, Error>
): MapValue<Key, Error, Value> =>
): MapValue<Key, Value, Error> =>
Data.struct({
_tag: "Pending" as const,
key,
deferred
})

/** @internal */
export const refreshing = <Key, Error, Value>(
export const refreshing = <Key, Value, Error>(
deferred: Deferred.Deferred<Value, Error>,
complete: Complete<Key, Error, Value>
): MapValue<Key, Error, Value> =>
complete: Complete<Key, Value, Error>
): MapValue<Key, Value, Error> =>
Data.struct({
_tag: "Refreshing" as const,
deferred,
Expand Down Expand Up @@ -216,8 +216,8 @@ export const makeKeySet = <K>(): KeySet<K> => new KeySetImpl<K>()
*
* @internal
*/
export interface CacheState<in out Key, in out Error, in out Value> {
map: MutableHashMap.MutableHashMap<Key, MapValue<Key, Error, Value>> // mutable by design
export interface CacheState<in out Key, in out Value, in out Error> {
map: MutableHashMap.MutableHashMap<Key, MapValue<Key, Value, Error>> // mutable by design
keys: KeySet<Key> // mutable by design
accesses: MutableQueue.MutableQueue<MapKey<Key>> // mutable by design
updating: MutableRef.MutableRef<boolean> // mutable by design
Expand All @@ -230,14 +230,14 @@ export interface CacheState<in out Key, in out Error, in out Value> {
*
* @internal
*/
export const makeCacheState = <Key, Error, Value>(
map: MutableHashMap.MutableHashMap<Key, MapValue<Key, Error, Value>>,
export const makeCacheState = <Key, Value, Error>(
map: MutableHashMap.MutableHashMap<Key, MapValue<Key, Value, Error>>,
keys: KeySet<Key>,
accesses: MutableQueue.MutableQueue<MapKey<Key>>,
updating: MutableRef.MutableRef<boolean>,
hits: number,
misses: number
): CacheState<Key, Error, Value> => ({
): CacheState<Key, Value, Error> => ({
map,
keys,
accesses,
Expand All @@ -251,7 +251,7 @@ export const makeCacheState = <Key, Error, Value>(
*
* @internal
*/
export const initialCacheState = <Key, Error, Value>(): CacheState<Key, Error, Value> =>
export const initialCacheState = <Key, Value, Error>(): CacheState<Key, Value, Error> =>
makeCacheState(
MutableHashMap.empty(),
makeKeySet(),
Expand Down Expand Up @@ -292,14 +292,14 @@ export const makeEntryStats = (loadedMillis: number): Cache.EntryStats => ({
loadedMillis
})

class CacheImpl<in out Key, in out Error, in out Value> implements Cache.Cache<Key, Error, Value> {
class CacheImpl<in out Key, in out Value, in out Error> implements Cache.Cache<Key, Value, Error> {
readonly [CacheTypeId] = cacheVariance
readonly cacheState: CacheState<Key, Error, Value>
readonly cacheState: CacheState<Key, Value, Error>
constructor(
readonly capacity: number,
readonly context: Context.Context<any>,
readonly fiberId: FiberId.FiberId,
readonly lookup: Cache.Lookup<Key, any, Error, Value>,
readonly lookup: Cache.Lookup<Key, Value, Error, any>,
readonly timeToLive: (exit: Exit.Exit<Value, Error>) => Duration.DurationInput
) {
this.cacheState = initialCacheState()
Expand Down Expand Up @@ -460,7 +460,7 @@ class CacheImpl<in out Key, in out Error, in out Value> implements Cache.Cache<K
effect.when(() => {
const current = Option.getOrUndefined(MutableHashMap.get(this.cacheState.map, k))
if (Equal.equals(current, value)) {
const mapValue = refreshing(deferred, value as Complete<Key, Error, Value>)
const mapValue = refreshing(deferred, value as Complete<Key, Value, Error>)
MutableHashMap.set(this.cacheState.map, k, mapValue)
return true
}
Expand Down Expand Up @@ -496,7 +496,7 @@ class CacheImpl<in out Key, in out Error, in out Value> implements Cache.Cache<K
MutableHashMap.set(
this.cacheState.map,
k,
mapValue as Complete<Key, Error, Value>
mapValue as Complete<Key, Value, Error>
)
})
)
Expand Down Expand Up @@ -545,7 +545,7 @@ class CacheImpl<in out Key, in out Error, in out Value> implements Cache.Cache<K
}

resolveMapValue(
value: MapValue<Key, Error, Value>,
value: MapValue<Key, Value, Error>,
ignorePending = false
): Effect.Effect<Option.Option<Value>, Error> {
return effect.clockWith((clock) => {
Expand Down Expand Up @@ -665,13 +665,13 @@ class CacheImpl<in out Key, in out Error, in out Value> implements Cache.Cache<K
}

/** @internal */
export const make = <Key, Environment, Error, Value>(
export const make = <Key, Value, Error = never, Environment = never>(
options: {
readonly capacity: number
readonly timeToLive: Duration.DurationInput
readonly lookup: Cache.Lookup<Key, Environment, Error, Value>
readonly lookup: Cache.Lookup<Key, Value, Error, Environment>
}
): Effect.Effect<Cache.Cache<Key, Error, Value>, never, Environment> => {
): Effect.Effect<Cache.Cache<Key, Value, Error>, never, Environment> => {
const timeToLive = Duration.decode(options.timeToLive)
return makeWith({
capacity: options.capacity,
Expand All @@ -681,13 +681,13 @@ export const make = <Key, Environment, Error, Value>(
}

/** @internal */
export const makeWith = <Key, Environment, Error, Value>(
export const makeWith = <Key, Value, Error = never, Environment = never>(
options: {
readonly capacity: number
readonly lookup: Cache.Lookup<Key, Environment, Error, Value>
readonly lookup: Cache.Lookup<Key, Value, Error, Environment>
readonly timeToLive: (exit: Exit.Exit<Value, Error>) => Duration.DurationInput
}
): Effect.Effect<Cache.Cache<Key, Error, Value>, never, Environment> =>
): Effect.Effect<Cache.Cache<Key, Value, Error>, never, Environment> =>
core.map(
fiberRuntime.all([core.context<Environment>(), core.fiberId]),
([context, fiberId]) =>
Expand All @@ -701,12 +701,12 @@ export const makeWith = <Key, Environment, Error, Value>(
)

/** @internal */
export const unsafeMakeWith = <Key, Error, Value>(
export const unsafeMakeWith = <Key, Value, Error = never>(
capacity: number,
lookup: Cache.Lookup<Key, never, Error, Value>,
lookup: Cache.Lookup<Key, Value, Error>,
timeToLive: (exit: Exit.Exit<Value, Error>) => Duration.DurationInput
): Cache.Cache<Key, Error, Value> =>
new CacheImpl<Key, Error, Value>(
): Cache.Cache<Key, Value, Error> =>
new CacheImpl<Key, Value, Error>(
capacity,
Context.empty() as Context.Context<any>,
none,
Expand Down
4 changes: 2 additions & 2 deletions packages/effect/src/internal/query.ts
Expand Up @@ -12,7 +12,7 @@ import * as core from "./core.js"
import { ensuring } from "./fiberRuntime.js"
import { Listeners } from "./request.js"

type RequestCache = Cache.Cache<Request.Request<any, any>, never, {
type RequestCache = Cache.Cache<Request.Request<any, any>, {
listeners: Request.Listeners
handle: Deferred<any, any>
}>
Expand All @@ -21,7 +21,7 @@ type RequestCache = Cache.Cache<Request.Request<any, any>, never, {
export const currentCache = globalValue(
Symbol.for("effect/FiberRef/currentCache"),
() =>
core.fiberRefUnsafeMake<RequestCache>(unsafeMakeWith<Request.Request<any, any>, never, {
core.fiberRefUnsafeMake<RequestCache>(unsafeMakeWith<Request.Request<any, any>, {
listeners: Request.Listeners
handle: Deferred<any, any>
}>(
Expand Down

0 comments on commit 10c169e

Please sign in to comment.