Stéphane
Stéphane
DDeno
Created by Stéphane on 7/24/2024 in #help
Weird behaviour with deno autocomplete
No description
3 replies
DDeno
Created by Stéphane on 7/24/2024 in #help
Fresh - Error while running deno run on the built _fresh directory
Hi, I installed the latest version of Deno yesterday on Manjaro using the install.sh script. I then created a fresh (haha) fresh project. I noticed that the cli task was not happy because it uses the --unstable flag which is not valid anymore. Then I used the build task that successfully created a _fresh folder, but when I tried deno run -A _fresh/main.js I got the following error :
error: Uncaught (in promise) ReferenceError: history is not defined
at file:///home/stephane/deno/fresh-project/_fresh/main.js:1:7893
error: Uncaught (in promise) ReferenceError: history is not defined
at file:///home/stephane/deno/fresh-project/_fresh/main.js:1:7893
Is it an expected behaviour ?
6 replies
DDeno
Created by Stéphane on 12/10/2023 in #help
How to use a class name as a qualifier for an enum ?
Hi there, it's more of a generic typescript question but I'm using it in the Deno context and the linter throws some warnings at my code. I'm trying to do this little helper class for rich text logging in the terminal :
export class RichText {
// deno-lint-ignore no-explicit-any
private _data: any;
private _properties: Array<
| RichText.COLOR
| RichText.BACKGROUND
| RichText.STYLE
>;

static RESET = "\x1b[0m";

constructor(
// deno-lint-ignore no-explicit-any
data: any,
...properties: Array<
| RichText.COLOR
| RichText.BACKGROUND
| RichText.STYLE
>
) {
this._data = data;
this._properties = properties;
}

toString() {
return this._properties.join("") + `${this._data}${RichText.RESET}`;
}
}
// deno-lint-ignore no-namespace
export namespace RichText {
export enum COLOR {
RED = "\x1b[31m",
GREEN = "\x1b[32m",
YELLOW = "\x1b[33m",
BLUE = "\x1b[34m",
MAGENTA = "\x1b[35m",
CYAN = "\x1b[36m",
WHITE = "\x1b[37m",
}

export enum BACKGROUND {
RED = "\x1b[41m",
GREEN = "\x1b[42m",
YELLOW = "\x1b[43m",
BLUE = "\x1b[44m",
MAGENTA = "\x1b[45m",
CYAN = "\x1b[46m",
WHITE = "\x1b[47m",
}

export enum STYLE {
BRIGHT = "\x1b[1m",
DIM = "\x1b[2m",
UNDERSCORE = "\x1b[4m",
BLINK = "\x1b[5m",
REVERSE = "\x1b[7m",
HIDDEN = "\x1b[8m",
}
}
export class RichText {
// deno-lint-ignore no-explicit-any
private _data: any;
private _properties: Array<
| RichText.COLOR
| RichText.BACKGROUND
| RichText.STYLE
>;

static RESET = "\x1b[0m";

constructor(
// deno-lint-ignore no-explicit-any
data: any,
...properties: Array<
| RichText.COLOR
| RichText.BACKGROUND
| RichText.STYLE
>
) {
this._data = data;
this._properties = properties;
}

toString() {
return this._properties.join("") + `${this._data}${RichText.RESET}`;
}
}
// deno-lint-ignore no-namespace
export namespace RichText {
export enum COLOR {
RED = "\x1b[31m",
GREEN = "\x1b[32m",
YELLOW = "\x1b[33m",
BLUE = "\x1b[34m",
MAGENTA = "\x1b[35m",
CYAN = "\x1b[36m",
WHITE = "\x1b[37m",
}

export enum BACKGROUND {
RED = "\x1b[41m",
GREEN = "\x1b[42m",
YELLOW = "\x1b[43m",
BLUE = "\x1b[44m",
MAGENTA = "\x1b[45m",
CYAN = "\x1b[46m",
WHITE = "\x1b[47m",
}

export enum STYLE {
BRIGHT = "\x1b[1m",
DIM = "\x1b[2m",
UNDERSCORE = "\x1b[4m",
BLINK = "\x1b[5m",
REVERSE = "\x1b[7m",
HIDDEN = "\x1b[8m",
}
}
and I want to be able to just import RichText and use it like that : console.log(new RichText("coucou", RichText.COLOR.BLUE, RichText.STYLE.BRIGHT)); which actually works, but I wonder if there's an "official" clean way to do it. I don't want to import generic terms such as COLOR or STYLE so I'd like them to be qualified with the same name as the class for clarity's sake.
4 replies
DDeno
Created by Stéphane on 9/19/2023 in #help
API architecture for generic callback
Hi there, I have a general question about code architecture for an API I'm doing. Basically, it listens to MIDI messages from a native library and I use an FFI binding to forward the messages to the user. A message is represented by an array of bytes, and from that you can describe its data and how to interpret it. I made a layer that converts the raw data to a typed object, but what I want to do is offer a choice to the user so he can retrieve either the raw data or the converted typed object. What I have so far is like that :
interface InputCallbackParams<T extends Message<MessageData> | number[]> {
message: T;
deltaTime?: number;
}

export interface InputCallbackOptions {
callback?: (params: InputCallbackParams<Message<MessageData>>) => void;
rawCallback?: (params: InputCallbackParams<number[]>) => void;
}
interface InputCallbackParams<T extends Message<MessageData> | number[]> {
message: T;
deltaTime?: number;
}

export interface InputCallbackOptions {
callback?: (params: InputCallbackParams<Message<MessageData>>) => void;
rawCallback?: (params: InputCallbackParams<number[]>) => void;
}
And the method that the user can use to listen to messages :
onMessage(
options: InputCallbackOptions,
): void {
this.callback = Deno.UnsafeCallback.threadSafe(
RtMidiCCallbackCallbackDefinition,
(
deltaTime: number,
message: Deno.PointerValue<unknown>,
messageSize: number | bigint,
) => {
const msg_data = new Uint8Array(
new Deno.UnsafePointerView(message!).getArrayBuffer(
messageSize as number,
),
);

if (options.callback !== undefined) {
options.callback({
message: decodeMessage(msg_data),
deltaTime,
});
}

if (options.rawCallback !== undefined) {
options.rawCallback({
message: Array.from(msg_data),
deltaTime,
});
}
},
);
rtmidi.rtmidi_in_set_callback(this.device, this.callback!.pointer, null);
}
onMessage(
options: InputCallbackOptions,
): void {
this.callback = Deno.UnsafeCallback.threadSafe(
RtMidiCCallbackCallbackDefinition,
(
deltaTime: number,
message: Deno.PointerValue<unknown>,
messageSize: number | bigint,
) => {
const msg_data = new Uint8Array(
new Deno.UnsafePointerView(message!).getArrayBuffer(
messageSize as number,
),
);

if (options.callback !== undefined) {
options.callback({
message: decodeMessage(msg_data),
deltaTime,
});
}

if (options.rawCallback !== undefined) {
options.rawCallback({
message: Array.from(msg_data),
deltaTime,
});
}
},
);
rtmidi.rtmidi_in_set_callback(this.device, this.callback!.pointer, null);
}
And I wonder if there's a better way to do that and if it looks understandable as an API user. Any thoughts welcome 🙂
30 replies
DDeno
Created by Stéphane on 8/29/2023 in #help
Launch tests with unstable flag inside VS Code
Hi there, I'm creating a module that uses FFI and Deno.dlopen and I wrote tests to validate some of the API calls, like this one :
import { assertEquals } from "https://deno.land/std@0.198.0/assert/mod.ts";
import { get_version } from "./mod.ts";
Deno.test({ name:"get_version", permissions: { ffi: true } }, () => {
assertEquals(get_version(), "6.0.0");
});
import { assertEquals } from "https://deno.land/std@0.198.0/assert/mod.ts";
import { get_version } from "./mod.ts";
Deno.test({ name:"get_version", permissions: { ffi: true } }, () => {
assertEquals(get_version(), "6.0.0");
});
I can run them in the command line typing deno test --unstable --allow-ffi but I can't call them directly from the interface in VS Code, it just says "Test failed" with no output and no explanation on why it fails. Is it a known issue and is there a workaround ? Command line is fine of course but I like the VS Code integration, it would be nice if it could work there too 🙂 Thanks !
1 replies
DDeno
Created by Stéphane on 2/8/2023 in #help
[Resolved][Fresh] How to load data asynchronously after rendering a page ?
Hi ! I was wondering if someone could help me understand the paradigm to load data asynchronously after a page is rendered. My use case is that I load data from the backend, reading some folders and retrieving git information. It takes a few seconds so I don't want to wait for the whole information before rendering the rest of the page. My code runs well if I do that synchronously but I don't understand how to do it after the page render. Should I use a component ? An island ? I don't understand well which code is executed on the server and when so it's not easy to troubleshoot, and I couldn't really find any examples that do something similar. I did a post here with more details but maybe it wasn't the right place for it : https://discord.com/channels/684898665143206084/991511118524715139/1072518216728977489 Any help would be appreciated, cheers !
3 replies