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

Expose typed EventHandlersDataMap #265

Open
artdevgame opened this issue Sep 10, 2021 · 3 comments
Open

Expose typed EventHandlersDataMap #265

artdevgame opened this issue Sep 10, 2021 · 3 comments

Comments

@artdevgame
Copy link
Contributor

Description:

This could just be that I'm a novice with TypeScript, but I'm using this library in my project and I would like to reference the EventHandlersDataMap type. I don't think I can currently do that as the file concerned isn't available in node_modules.

I've tried getting hold of the map with this, but it feels hacky/wrong and I'm still unsure how to type an event handler to a specific use-case:

import ObsWebSocket from 'obs-websocket-js';

const obs = new ObsWebSocket();

type OnParams = Parameters<typeof obs.on>;

export type EventHandlerKey = OnParams[0];
export type EventHandlerListener = OnParams[1];

export type ObsSocketListener = Partial<Record<EventHandlerKey, EventHandlerListener>>;

This is how I'm trying to get a specific data map for an event handler:

type ProfileChangedHandler = Extract<EventHandlerListener, { profile: string; }> // <- Always returns 'never'

// expecting `ProfileChangeHandler` to be `{ profile: string; }`

It looks like obsWebsocket.ts would be a lot easier to do this kind of thing, is that possible?

Versions Used (if applicable):

  • obs-websocket-js version: 4.0.3
@artdevgame
Copy link
Contributor Author

I've managed to figure out how to get to it in a really long-winded way, but would still prefer the file exposed in the package if possible, this is my solution:

type ProfileChangedHandler = Extract<Parameters<EventHandlerListener>[0], { profile: string; }>;

Evaluates as:

type ProfileChangedHandler = {
    profile: string;
}

@polerin
Copy link

polerin commented May 21, 2022

Agree, I was really confused when this wasn't exposed.

@polerin
Copy link

polerin commented Jun 2, 2022

I've exported all of the EventHandlersData params in a way that is similar to @artdevgame, but I don't think there's a way for me to do dynamic registration of listeners in a manner similar to below

    protected registerEventTransformers(transformers : V4EventTransformerSet) : void 
    {
        for (const transformer of transformers) {
            this.transformerRegistery[transformer.obsEventType] = transformer;

            this.websocket.on(transformer.obsEventType, this.notifyListener.bind(this, transformer.obsEventType, transformer));
        }
    }

    protected notifyListener<EventType extends ObsV4EventNames>(obsEventType : EventType, transformer : V4EventTransformer, eventData : ObsV4EventHandlersData[EventType]) : void 
    {
        if (this.callback) {
            this.callback(transformer.systemMessageType, transformer.buildSystemMessage(eventData));
        }
    }

I know y'all are probably full steam ahead on v5, but it is sort of a major roadblock for me. Any help?

polerin pushed a commit to polerin/obs-tau-blend that referenced this issue Jun 2, 2022
Translators are currently not registering as their event signatures
are not being recognized by the obs websocket `.on()` method.

See: obs-websocket-community-projects/obs-websocket-js#265
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants