Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Use std 211 #228

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion import_map.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"preact-render-to-string": "https://esm.sh/*[email protected]",
"@preact/signals": "https://esm.sh/*@preact/[email protected]",
"@preact/signals-core": "https://esm.sh/@preact/[email protected]",
"std/": "https://deno.land/std@0.182.0/"
"std/": "https://deno.land/std@0.211.0/"
}
}
11 changes: 7 additions & 4 deletions utils/pool.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { Deferred, deferred } from "std/async/deferred.ts";

export const createPool = <T>(resources: T[]) => {
const taken = new Set<number>();
const free = new Set<number>(resources.map((_, i) => i));

const waiting: Deferred<T>[] = [];
const waiting: Array<{
promise: Promise<T>;
resolve: (value: T | PromiseLike<T>) => void;
// deno-lint-ignore no-explicit-any
reject: (reason?: any) => void;
}> = [];

return {
acquire: () => {
if (free.size === 0) {
const p = deferred<T>();
const p = Promise.withResolvers<T>();
waiting.push(p);

return p;
Expand Down
14 changes: 8 additions & 6 deletions utils/worker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// deno-lint-ignore-file no-explicit-any
import { Deferred, deferred } from "std/async/deferred.ts";

/**
* Deco labs: 🐁🐁🐁
*
Expand Down Expand Up @@ -52,9 +50,13 @@ export const createWorker = (
url: URL,
options?: WorkerOptions | undefined,
): Promise<any> => {
const setup = deferred();
const setup = Promise.withResolvers();
const worker = new Worker(new URL(import.meta.url), options);
const invokes = new Map<string, Deferred<unknown>>([]);
const invokes = new Map<string, {
promise: Promise<any>;
resolve: (value: any | PromiseLike<any>) => void;
reject: (reason?: any) => void;
}>([]);

worker.postMessage({ type: "setup", payload: url.href });

Expand All @@ -65,7 +67,7 @@ export const createWorker = (
case "setup:fulfill": {
const mod = payload.reduce((acc, curr) => {
acc[curr] = (...args: any[]) => {
const run = deferred<void>();
const run = Promise.withResolvers<any>();
const id = crypto.randomUUID();

invokes.set(id, run);
Expand Down Expand Up @@ -116,7 +118,7 @@ export const createWorker = (
}
});

return setup;
return setup.promise;
};

if (IS_WORKER) {
Expand Down