Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

add Command.provideSync/provideEffect #426

Merged
merged 1 commit into from
Dec 13, 2023
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/three-pianos-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/cli": patch
---

add Command.provideSync
5 changes: 5 additions & 0 deletions .changeset/violet-balloons-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/cli": patch
---

add Command.provideEffect
12 changes: 4 additions & 8 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@
"[typescriptreact]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
},
"eslint.validate": [
"markdown",
"javascript",
"typescript"
],
"eslint.validate": ["markdown", "javascript", "typescript"],
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"editor.quickSuggestions": {
"other": true,
Expand All @@ -42,7 +38,7 @@
"editor.tabCompletion": "off",
"editor.suggest.localityBonus": true,
"editor.suggestSelection": "recentlyUsed",
"editor.wordBasedSuggestions": true,
"editor.wordBasedSuggestions": "matchingDocuments",
"editor.parameterHints.enabled": true,
"files.insertFinalNewline": true,
"files.insertFinalNewline": true
}
34 changes: 34 additions & 0 deletions src/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,24 @@ export const provide: {
): Command<Name, LR | Exclude<R, LA>, E | LE, A>
} = Internal.provide

/**
* @since 1.0.0
* @category combinators
*/
export const provideEffect: {
<I, S, A, R2, E2>(
tag: Tag<I, S>,
effect: Effect<R2, E2, S> | ((_: A) => Effect<R2, E2, S>)
): <Name extends string, R, E>(
self: Command<Name, R, E, A>
) => Command<Name, R2 | Exclude<R, I>, E2 | E, A>
<Name extends string, R, E, A, I, S, R2, E2>(
self: Command<Name, R, E, A>,
tag: Tag<I, S>,
effect: Effect<R2, E2, S> | ((_: A) => Effect<R2, E2, S>)
): Command<Name, R2 | Exclude<R, I>, E | E2, A>
} = Internal.provideEffect

/**
* @since 1.0.0
* @category combinators
Expand All @@ -281,6 +299,22 @@ export const provideEffectDiscard: {
): Command<Name, R | R2, E | E2, A>
} = Internal.provideEffectDiscard

/**
* @since 1.0.0
* @category combinators
*/
export const provideSync: {
<I, S, A>(
tag: Tag<I, S>,
service: S | ((_: A) => S)
): <Name extends string, R, E>(self: Command<Name, R, E, A>) => Command<Name, Exclude<R, I>, E, A>
<Name extends string, R, E, A, I, S>(
self: Command<Name, R, E, A>,
tag: Tag<I, S>,
service: S | ((_: A) => S)
): Command<Name, Exclude<R, I>, E, A>
} = Internal.provideSync

/**
* @since 1.0.0
* @category combinators
Expand Down
42 changes: 42 additions & 0 deletions src/internal/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,27 @@ export const provide = dual<
Effect.provide(effect, typeof layer === "function" ? layer(config) : layer)
}))

/** @internal */
export const provideEffect = dual<
<I, S, A, R2, E2>(
tag: Context.Tag<I, S>,
effect: Effect.Effect<R2, E2, S> | ((_: A) => Effect.Effect<R2, E2, S>)
) => <Name extends string, R, E>(
self: Command.Command<Name, R, E, A>
) => Command.Command<Name, Exclude<R, I> | R2, E | E2, A>,
<Name extends string, R, E, A, I, S, R2, E2>(
self: Command.Command<Name, R, E, A>,
tag: Context.Tag<I, S>,
effect: Effect.Effect<R2, E2, S> | ((_: A) => Effect.Effect<R2, E2, S>)
) => Command.Command<Name, Exclude<R, I> | R2, E | E2, A>
>(3, (self, tag, effect_) =>
makeDerive(self, {
transform: (self, config) => {
const effect = typeof effect_ === "function" ? effect_(config) : effect_
return Effect.provideServiceEffect(self, tag, effect)
}
}))

/** @internal */
export const provideEffectDiscard = dual<
<A, R2, E2, _>(
Expand All @@ -369,6 +390,27 @@ export const provideEffectDiscard = dual<
}
}))

/** @internal */
export const provideSync = dual<
<I, S, A>(
tag: Context.Tag<I, S>,
service: S | ((_: A) => S)
) => <Name extends string, R, E>(
self: Command.Command<Name, R, E, A>
) => Command.Command<Name, Exclude<R, I>, E, A>,
<Name extends string, R, E, A, I, S>(
self: Command.Command<Name, R, E, A>,
tag: Context.Tag<I, S>,
service: S | ((_: A) => S)
) => Command.Command<Name, Exclude<R, I>, E, A>
>(3, (self, tag, f) =>
makeDerive(self, {
transform: (self, config) => {
const service = typeof f === "function" ? (f as any)(config) : f
return Effect.provideService(self, tag, service)
}
}))

/** @internal */
export const withDescription = dual<
(
Expand Down
6 changes: 5 additions & 1 deletion test/Command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ const clone = Command.make("clone", {
}
})).pipe(Command.withDescription("Clone a repository into a new directory"))

const AddService = Context.Tag<"AddService">()

const add = Command.make("add", {
pathspec: Args.text({ name: "pathspec" })
}).pipe(
Command.withHandler(({ pathspec }) =>
Effect.gen(function*(_) {
yield* _(AddService)
const { log } = yield* _(Messages)
const { verbose } = yield* _(git)
if (verbose) {
Expand All @@ -47,7 +50,8 @@ const add = Command.make("add", {
}
})
),
Command.withDescription("Add file contents to the index")
Command.withDescription("Add file contents to the index"),
Command.provideEffect(AddService, (_) => Effect.succeed("AddService" as const))
)

const run = git.pipe(
Expand Down