Coder Social home page Coder Social logo

Typescript typings file :) about caf HOT 6 OPEN

getify avatar getify commented on June 15, 2024
Typescript typings file :)

from caf.

Comments (6)

noomorph avatar noomorph commented on June 15, 2024 1

Oh, thanks for reminding me about the existence of this issue 😆

I'll leave it here for now (spent a couple of minutes with ChatGPT):

declare module "caf" {
  export class CancelToken {
    controller: AbortController | null;
    signal: AbortSignal;
    constructor(controller?: AbortController);
    abort(reason?: any): void;
    discard(): void;
    // _trackSignalReason(reason: any): void;
  }

  export function CAF<T, A extends any[]>(
    generatorFn: (signal: AbortSignal, ...args: A) => AsyncGenerator<any, T, any>
  ): (...args: [CancelToken | AbortSignal, ...A]) => Promise<T>;

  export function cancelToken(): CancelToken;

  export function delay(
    tokenOrSignal: CancelToken | AbortSignal | number,
    ms?: number
  ): Promise<string>;

  export function timeout(
    duration: number,
    message?: string
  ): CancelToken;

  export function signalRace(signals: AbortSignal[]): AbortSignal;

  export function signalAll(signals: AbortSignal[]): AbortSignal;

  export function tokenCycle(): () => CancelToken;
}

I (or someone else) might take it from here and finish it. I'm a bit busy currently but I hope to find some time in the nearest 2-3 months.

from caf.

mustafa519 avatar mustafa519 commented on June 15, 2024 1

If it is possible to reproduce the codebase in ES6

The codebase is already ES6 (actually, at least ES2018, in that it uses async generators). For example, here's a let keyword being used where I believe it should be used:

CAF/src/caf.js

Line 73 in a2b12de

let doCancelTimer = function cancelTimer(v){

Specifically, the use of var declarations

I know there's a lot of cult wisdom in the "modern JS" way of thinking, followed by lint rules, that say var should never be used. I completely and totally disagree. There's nothing about var that's "non-ES6".

Put simply, I use var for any declaration that's at the top-most scope of a function (or module). I use let for any declaration that appears inside a block (such as if or for loop). One little exception is when I have a declaration inside a try that I want to access outside that block, I use a var there.

More info on how I advocate using var / let / const: https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/apA.md#the-case-for-var

I was unsure whether I needed to add let or const to free up memory.

That's not the appropriate reason to decide between these different declaration types. See link above.

However, I need the codebase to be written in a way that is easier for me to understand and work with

I'm sorry you found the codebase too confusing. I wish my code were considered readable by folks. IMO it's much more readable than most codebases I see others write. But I appreciate that you may disagree.

In any case, I won't be rewriting the codebase. The code style I use here is how I've always written OSS code and how I've always taught JS code.

Don't misunderstand me, I do appreciate that you have fixed a very big problem as your project which is open-source. And, I really respect your efforts for JS.

I don't think codebase is entirely confusing or has bad readability. I am not good enough at JS to judge someone's project. Just confusing for the folks who have learned after ES6 and don't know the past of JS. I really don't know it well yet, like your other repo's purpose of the name. So, the problem was mostly about me, not about the codebase.

Sorry, if it was disrespectful to your efforts, I didn't mean that.

@noomorph you're lifesaver. Just in time. Thank you for sharing!

from caf.

noomorph avatar noomorph commented on June 15, 2024 1

@mustafa519 I'm not sure it will work well enough without fixing, but it's worth trying. Share with us the improvements and fixes if you manage to integrate typings into your project

from caf.

getify avatar getify commented on June 15, 2024

I would consider/accept a contribution of one, but I don't plan to make one (I'm not competent enough at TS).

I think I've heard that things like generators are really hard for TS to type properly. But that's just anecdotal and I might be totally wrong about that.

from caf.

mustafa519 avatar mustafa519 commented on June 15, 2024

@getify

I attempted to reproduce the codebase on my local environment, but I ultimately gave up. While I am comfortable writing code in ES6+ with many ESLint rules, I am not an expert in JavaScript and I found the codebase confusing. Specifically, the use of var declarations and parameter reassignments made it difficult for me to follow the flow of the code. Like, I was unsure whether I needed to add let or const to free up memory.

If it is possible to reproduce the codebase in ES6, I would be willing to try to add the necessary Typescript types. However, I need the codebase to be written in a way that is easier for me to understand and work with, as a noob JS developer.

from caf.

getify avatar getify commented on June 15, 2024

If it is possible to reproduce the codebase in ES6

The codebase is already ES6 (actually, at least ES2018, in that it uses async generators). For example, here's a let keyword being used where I believe it should be used:

CAF/src/caf.js

Line 73 in a2b12de

let doCancelTimer = function cancelTimer(v){

Specifically, the use of var declarations

I know there's a lot of cult wisdom in the "modern JS" way of thinking, followed by lint rules, that say var should never be used. I completely and totally disagree. There's nothing about var that's "non-ES6".

Put simply, I use var for any declaration that's at the top-most scope of a function (or module). I use let for any declaration that appears inside a block (such as if or for loop). One little exception is when I have a declaration inside a try that I want to access outside that block, I use a var there.

More info on how I advocate using var / let / const: https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/scope-closures/apA.md#the-case-for-var

I was unsure whether I needed to add let or const to free up memory.

That's not the appropriate reason to decide between these different declaration types. See link above.

However, I need the codebase to be written in a way that is easier for me to understand and work with

I'm sorry you found the codebase too confusing. I wish my code were considered readable by folks. IMO it's much more readable than most codebases I see others write. But I appreciate that you may disagree.

In any case, I won't be rewriting the codebase. The code style I use here is how I've always written OSS code and how I've always taught JS code.

from caf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.