我想知道是否可以"加宽";我使用sequenceT时的终极阅读器类型?当使用chainW等按顺序链接操作时,这是可能的,但当使用sequenceT时,似乎每个项目都必须使用相同的Reader类型。我想这样做,这样我就可以在适当的时候并行执行一些任务,但仍然可以通过Reader使用依赖项注入。
示例:
import { sequenceT } from 'fp-ts/lib/Apply'
import { log } from 'fp-ts/lib/Console'
import { pipe } from 'fp-ts/lib/function'
import * as RT from 'fp-ts/ReaderTask'
interface Person {
name: string
}
const getMe = (name: string) => (deps: { api: any }) => async (): Promise<Person> => {
const person = {
name,
}
return person
}
const getMum = (child: Person) => (deps: { api2: any }) => async (): Promise<Person> => {
const person = {
name: child.name + "'s mum",
}
return person
}
const getDad = (child: Person) => (deps: { api2: any }) => async (): Promise<Person> => {
const person = {
name: child.name + "'s dad",
}
return person
}
const getFamily = (name: string) =>
pipe(
getMe(name),
RT.chainW(me =>
sequenceT(RT.readerTask)(
getMum(me),
getDad(me))
))
getFamily('John')({ api: 'x', api2: 'y' })().then(
([mum, dad]) => {
log(mum)()
log(dad)()
})
这编译精细并输出:
$ node dist/src/index.js
{ name: "John's mum" }
{ name: "John's dad" }
现在假设getDad依赖于一个不同的api,比如api3。如果我更新代码,它将不再编译,因为getMum和getDad不使用相同的Reader类型。
示例(不编译(:
import { sequenceT } from 'fp-ts/lib/Apply'
import { log } from 'fp-ts/lib/Console'
import { pipe } from 'fp-ts/lib/function'
import * as RT from 'fp-ts/ReaderTask'
interface Person {
name: string
}
const getMe = (name: string) => (deps: { api: any }) => async (): Promise<Person> => {
const person = {
name,
}
return person
}
const getMum = (child: Person) => (deps: { api2: any }) => async (): Promise<Person> => {
const person = {
name: child.name + "'s mum",
}
return person
}
const getDad = (child: Person) => (deps: { api3: any }) => async (): Promise<Person> => {
const person = {
name: child.name + "'s dad",
}
return person
}
const getFamily = (name: string) =>
pipe(
getMe(name),
RT.chainW(me =>
sequenceT(RT.readerTask)(
getMum(me), // compiler complains here
getDad(me))
))
getFamily('John')({ api: 'x', api2: 'y', api3: 'z' })().then( // compiler complains here, on api3
([mum, dad]) => {
log(mum)()
log(dad)()
})
事实上,我在StateReaderTaskAnyer中尝试过这一点,但在这个例子中,我将其简化为使用ReaderTask——然而,sequenceT也表现出了同样的限制。
有什么解决办法吗?
这正是Reader/ReaderTask/ReaderTaskEither.local
的用途!我经常用这个。例如,如果您正在对API的HTTP调用进行并行处理,其中一些调用需要auth令牌+基本URL,而另一些调用只需要基本URL(因此一些调用使用interface Auth { token:string, baseUrl: string }
,而其他调用则使用interface NoAuth { baseUrl: string }
interface Apis {
java: JavaRepository,
db: DbRepository,
redis: RedisRepository,
}
interface DomainError {}
declare const javaApiCall: RTE<JavaRepository, DomainError, JavaResult>
declare const dbApiCall: RTE<DbRepository, DomainError, DbResult>
declare const redisApiCall: RTE<RedisRepository, DomainError, RedisResult>
declare const apis: Apis
const getJava = (apis:Apis) => apis.java
const getDb = (apis:Apis) => apis.db
const getRedis = (apis:Apis) => apis.redis
sequenceT(readerTaskEither)(
RTE.local(getJava)(javaApiCall),
RTE.local(getDb)(dbApiCall),
RTE.local(getRedix)(redisApiCall),
)(apis) // TaskEither<DomainError, [JavaResult,DbResult,RedisResult]>
我在阅读了更多fp-ts代码后发现了这个问题。我得出的答案是,只需手动执行sequenceT有效执行的操作。
这是我的解决方案:
import { log } from 'fp-ts/lib/Console'
import { pipe } from 'fp-ts/lib/function'
import * as RT from 'fp-ts/ReaderTask'
interface Person {
name: string
}
const getMe = (name: string) => (deps: { api: any }) => async (): Promise<Person> => {
const person = {
name,
}
return person
}
const getMum = (child: Person) => (deps: { api2: any }) => async (): Promise<Person> => {
const person = {
name: child.name + "'s mum",
}
return person
}
const getDad = (child: Person) => (deps: { api3: any }) => async (): Promise<Person> => {
const person = {
name: child.name + "'s dad",
}
return person
}
const getFamily = (name: string) =>
pipe(
getMe(name),
RT.chainW(me =>
pipe(
RT.of((mum: Person) => (dad: Person) => [mum, dad]),
RT.apW(getMum(me)),
RT.apW(getDad(me))
)
))
getFamily('John')({ api: 'x', api2: 'y', api3: 'z' })().then(
([mum, dad]) => {
log(mum)()
log(dad)()
})