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

feat: abstract postMessage #5

Merged
merged 2 commits into from Aug 10, 2022
Merged

feat: abstract postMessage #5

merged 2 commits into from Aug 10, 2022

Conversation

JavierCane
Copy link
Member

Purpose: Do not have to take into account from where to where you want to post messages

Current errors:

It seems to properly work because the use cases are executed properly, but it show errors like we would be performing memory accesses out of bounds :S

image

Just as a note, I have tried to refactor the abstracted method as follows, and the error changed to a table index out of bounds, so it could be something related to hoisting or whatever JS behaviour that changes depending on how we declare the function:

export const postMessage: <CommandType extends Command>(
  command: CommandType
) => void = () => {
  const isFromSceneSandboxToUiIframe = typeof window === "undefined";

  if (isFromSceneSandboxToUiIframe) {
    return function <CommandType extends Command>(command: CommandType): void {
      figma.ui.postMessage(command);
    };
  }

  return function <CommandType extends Command>(command: CommandType): void {
    window.parent.postMessage({ pluginMessage: command }, "*");
  };
};

image

@JavierCane JavierCane self-assigned this Aug 9, 2022
@JavierCane
Copy link
Member Author

It does seems that something is already buggy on the main branch. Same behaviour (the plugin executes successfully) but ends also with an error. In this case an error related with function signature mismatch :S

image

@JavierCane
Copy link
Member Author

declaring it as a function has other behaviour:

export function postMessage(): <CommandType extends Command>(
  command: CommandType
) => void {
  const isFromSceneSandboxToUiIframe = typeof window === "undefined";

  if (isFromSceneSandboxToUiIframe) {
    return function <CommandType extends Command>(command: CommandType): void {
      figma.ui.postMessage(command);
    };
  }

  return function <CommandType extends Command>(command: CommandType): void {
    window.parent.postMessage({ pluginMessage: command }, "*");
  };
}

image

@JavierCane
Copy link
Member Author

Tested out again in the main branch. It does not throw any error.

VIVA LA CACHE

@JavierCane JavierCane changed the title [WIP] feat: abstract postMessage feat: abstract postMessage Aug 10, 2022
@JavierCane JavierCane merged commit d6ac579 into main Aug 10, 2022
@JavierCane JavierCane deleted the abstract-post-message branch August 10, 2022 06:59
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

Successfully merging this pull request may close these issues.

None yet

1 participant