Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Support passing self to DenoWorkerRunner once more #25

Merged
merged 1 commit into from
Feb 18, 2025
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
4 changes: 2 additions & 2 deletions packages/platform-deno/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Bumped dependency lower bounds ([#10], [#16] \[thanks to [@pixeleet]\], [#20], [#21]).
- Only support passing in `MessagePort`s to `layerMessagePort` ([#21]).
This means that you’ll need to add `webworker.lib.d.ts` to your TypeScript `compilerOptions`
This means that you’ll need to add `deno.worker` to your TypeScript `compilerOptions`
if you use `DenoWorkerRunner`.
```json
{
"compilerOptions": {
"lib": ["webworker"]
"lib": ["deno.worker"]
}
}
```
Expand Down
32 changes: 18 additions & 14 deletions packages/platform-deno/src/DenoWorkerRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @since 0.1.1
*/

/// <reference lib="deno.worker" />

import { WorkerRunner as Runner, WorkerError } from "@effect/platform";
import {
Cause,
Expand Down Expand Up @@ -32,15 +34,20 @@ if (typeof self !== "undefined" && "onconnect" in self) {
self.onconnect = globalHandleConnect;
}

/**
* Just a simple alias for {@linkcode globalThis} and {@linkcode MessagePort}.
*
* @internal
*/
export type Self = typeof globalThis | MessagePort;

/**
* Constructs a {@linkplain Runner.PlatformRunner | runner} from a {@linkcode MessagePort}.
*
* @since 0.1.1
* @category constructors
*/
export const make: (self: MessagePort) => Runner.PlatformRunner = (
self: MessagePort,
) =>
export const make: (self: Self) => Runner.PlatformRunner = (self: Self) =>
Runner.PlatformRunner.of({
[Runner.PlatformRunnerTypeId]: Runner.PlatformRunnerTypeId,
start<I, O>(
Expand All @@ -49,10 +56,7 @@ export const make: (self: MessagePort) => Runner.PlatformRunner = (
return Effect.sync(() => {
let currentPortId = 0;

const ports = new Map<
number,
readonly [MessagePort, Scope.CloseableScope]
>();
const ports = new Map<number, readonly [Self, Scope.CloseableScope]>();
const send = (
portId: number,
message: O,
Expand Down Expand Up @@ -87,9 +91,9 @@ export const make: (self: MessagePort) => Runner.PlatformRunner = (
}
}

function onMessage(portId: number): (event: MessageEvent) => void {
function onMessage(portId: number): (event: Event) => void {
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: It's fine. I guess.
return (event: MessageEvent): void => {
return (event: Event): void => {
const message = (event as MessageEvent)
.data as Runner.BackingRunner.Message<I>;
if (message[0] === 0) {
Expand All @@ -114,25 +118,25 @@ export const make: (self: MessagePort) => Runner.PlatformRunner = (
}
};
}
function onMessageError(error: MessageEvent): void {
function onMessageError(error: Event): void {
Deferred.unsafeDone(
closeLatch,
new WorkerError.WorkerError({
reason: "decode",
cause: error.data,
cause: (error as MessageEvent).data,
}),
);
}
function onError(error: MessageEvent): void {
function onError(error: Event): void {
Deferred.unsafeDone(
closeLatch,
new WorkerError.WorkerError({
reason: "unknown",
cause: error.data,
cause: (error as MessageEvent).data,
}),
);
}
function handlePort(port: MessagePort): void {
function handlePort(port: Self): void {
const fiber = Scope.fork(scope, ExecutionStrategy.sequential).pipe(
Effect.flatMap((scope) => {
const portId = currentPortId++;
Expand Down