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

Issue with the Subscribe/Unsubscribe function #7

Open
FlyingFork opened this issue Sep 3, 2022 · 2 comments
Open

Issue with the Subscribe/Unsubscribe function #7

FlyingFork opened this issue Sep 3, 2022 · 2 comments
Labels
bug Something isn't working cant fix

Comments

@FlyingFork
Copy link

subOverloads += `\n---@overload fun(${cls.staticClass ? "" : `self: ${cls.name}, `}event_name: "${event.name}", callback: ${callbackSig}): ${callbackSig} ${generateInlineDocstring(event)}`;
unsubOverloads += `\n---@overload fun(${cls.staticClass ? "" : `self: ${cls.name}, `}event_name: "${event.name}", callback: ${callbackSig}) ${generateInlineDocstring(event)}`;
});
events = `
---Subscribe to an event
---@param event_name string @Name of the event to subscribe to
---@param callback function @Function to call when the event is triggered
---@return function @The callback function passed${subOverloads}
function ${cls.name}${cls.staticClass ? "." : ":"}Subscribe(event_name, callback) end
---Unsubscribe from an event
---@param event_name string @Name of the event to unsubscribe from
---@param callback? function @Optional callback to unsubscribe (if no callback is passed then all callbacks in this Package will be unsubscribed from this event)${unsubOverloads}
function ${cls.name}${cls.staticClass ? "." : ":"}Unsubscribe(event_name, callback) end`;

When adding the Subscribe/Unsubscribe functions to the annotations it should always be static. If you leave it with : it will give an error in the game but not in the IDE. So to fix it I think that lines 231-246 should be changed to this:

			subOverloads += `\n---@overload fun(event_name: "${event.name}", callback: ${callbackSig}): ${callbackSig} ${generateInlineDocstring(event)}`;
			unsubOverloads += `\n---@overload fun(event_name: "${event.name}", callback: ${callbackSig}) ${generateInlineDocstring(event)}`;
		});

		events = `

---Subscribe to an event
---@param event_name string @Name of the event to subscribe to
---@param callback function @Function to call when the event is triggered
---@return function @The callback function passed${subOverloads}
function ${cls.name}.Subscribe(event_name, callback) end

---Unsubscribe from an event
---@param event_name string @Name of the event to unsubscribe from
---@param callback? function @Optional callback to unsubscribe (if no callback is passed then all callbacks in this Package will be unsubscribed from this event)${unsubOverloads}
function ${cls.name}.Unsubscribe(event_name, callback) end`;
@Derpius
Copy link
Owner

Derpius commented Sep 3, 2022

This is due to narrowing issues in the language server LuaLS/lua-language-server#1456

If both static and non-static overloads are provided, then it wont correctly select between overloads taking self depending on the first param.

The way I get around this is by only defining static or non-static subscribes depending on the class, despite most classes implementing both.
The only alternative is breaking autocomplete until the above is fixed.

Also the annotations shouldn’t always be static, most classes implement both a static and non-static version

@Derpius Derpius added cant fix bug Something isn't working labels Sep 3, 2022
@Vurv78
Copy link

Vurv78 commented Mar 7, 2023

This works fine LuaLS/lua-language-server#1456 (comment)

It breaks the doc popup though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working cant fix
Projects
None yet
Development

No branches or pull requests

3 participants