Skip to content

Commit

Permalink
More examples
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastian Lorenz <fubhy@fubhy.com>
  • Loading branch information
schickling and fubhy authored Sep 22, 2023
1 parent 970697c commit e17b2f5
Show file tree
Hide file tree
Showing 14 changed files with 409 additions and 2 deletions.
8 changes: 8 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
if test -f ./.envrc.local; then
source_env ./.envrc.local
fi

if command -v nix-shell &> /dev/null
then
use_flake
fi
25 changes: 25 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,28 @@ jobs:
working-directory: starter-vite-tsc
- run: pnpm build
working-directory: starter-vite-tsc

node-scripts:
strategy:
matrix:
node-version: [16, 18, 20]
os: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2
with:
version: 8
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'pnpm'
- run: pnpm install
working-directory: node-scripts
- run: pnpm tsc
working-directory: node-scripts
- run: pnpm tsx src/fs-write-file.ts
working-directory: node-scripts
- run: pnpm tsx src/async-promises.ts
working-directory: node-scripts
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
node_modules/
.direnv
22 changes: 22 additions & 0 deletions .vscode/operators.code-snippets
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"Effect Gen Function": {
"prefix": "egen",
"body": ["Effect.gen(function* ($) {\n\t$0\n})"],
"description": "Effect generator FUnction with $ input"
},
"Gen Function": {
"prefix": "gen",
"body": ["function* ($) {}"],
"description": "Generator FUnction with $ input"
},
"Gen Yield * tmp": {
"prefix": "yy",
"body": ["yield* $($0)"],
"description": "Yield generator calling $()"
},
"Gen Yield *": {
"prefix": "!",
"body": ["yield* $($0)"],
"description": "Yield generator calling $()"
}
}
4 changes: 4 additions & 0 deletions cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "cli",
"private": true
}
61 changes: 61 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/release-23.05";
flake-utils.url = "github:numtide/flake-utils";
};

outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
corepack = pkgs.runCommand "corepack-enable" {} ''
mkdir -p $out/bin
${pkgs.nodejs_20}/bin/corepack enable --install-directory $out/bin
'';
in
{
devShell = with pkgs; pkgs.mkShell {
buildInputs = [
act
nodejs_20
corepack
];
};

});
}
1 change: 1 addition & 0 deletions node-scripts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
output
7 changes: 7 additions & 0 deletions node-scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# node-scripts

## Example

```bash
pnpm dev src/fs-write-file.ts
```
15 changes: 15 additions & 0 deletions node-scripts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "node-scripts",
"private": true,
"scripts": {
"dev": "tsx --watch"
},
"dependencies": {
"effect": "2.0.0-next.34",
"@effect/platform-node": "^0.17.0",
"@effect/platform": "^0.16.1"
},
"devDependencies": {
"tsx": "^3.12.10"
}
}
51 changes: 51 additions & 0 deletions node-scripts/src/async-promises.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as Node from '@effect/platform-node/Runtime'
import { Cause, Duration, Effect } from 'effect'
import { TaggedClass } from 'effect/Data'

// --- HELPERS ---

const getMagicNumber = async () => {
await sleep(200)

return 42
}

const throwSomeError = async () => {
throw new Error('some error')
}

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))

class MyCustomError extends TaggedClass('MyCustomError')<{ readonly cause: unknown }> {}

const someEffect = Effect.succeed(69).pipe(Effect.delay(Duration.seconds(0.5)))

const someAsyncFnRunningAnEffect = async () => {
const result = await Effect.runPromise(someEffect)

return result
}

// --- MAIN ---

const main = Effect.gen(function* ($) {
// calling a async function/promise from Effect
const result1 = yield* $(Effect.promise(() => getMagicNumber()))

console.log('Got result 1:', result1)

// calling a async function/promise from Effect (with error handling)
const result2 = yield* $(
Effect.tryPromise({ try: () => throwSomeError(), catch: (cause) => new MyCustomError({ cause }) }),
Effect.catchTag('MyCustomError', (error) => Effect.succeed(`Got error: ${error.cause}`)),
)

console.log('Got result 2:', result2)

// calling an Effect from an async function/promise
const result3 = yield* $(Effect.promise(() => someAsyncFnRunningAnEffect()))

console.log('Got result 3:', result3)
})

Node.runMain(main.pipe(Effect.tapErrorCause((_) => Effect.log(Cause.pretty(_)))))
20 changes: 20 additions & 0 deletions node-scripts/src/fs-write-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as FS from '@effect/platform/FileSystem'
import * as Node from '@effect/platform-node/Runtime'
import * as NodeContext from '@effect/platform-node/NodeContext'
import { Cause, Effect } from 'effect'

const main = Effect.gen(function* ($) {
const fs = yield* $(FS.FileSystem)

yield* $(fs.makeDirectory('output'))
yield* $(fs.writeFileString('output/fs-write-file.txt', 'Hello World!'))

console.log('Wrote file (output/fs-write-file.txt)')
})

Node.runMain(
main.pipe(
Effect.provideSomeLayer(NodeContext.layer),
Effect.tapErrorCause((_) => Effect.log(Cause.pretty(_))),
),
)
10 changes: 10 additions & 0 deletions node-scripts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"noEmit": true,
"module": "Node16",
"moduleResolution": "Node16",
"strict": true,
"plugins": [{ "name": "@effect/language-service" }],
},
"include": ["src/**/*.ts"]
}
Loading

0 comments on commit e17b2f5

Please sign in to comment.