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

fix(filters): support early return in modify_post and modify_feed #72

Merged
merged 2 commits into from
Mar 8, 2024

Conversation

shouya
Copy link
Owner

@shouya shouya commented Mar 8, 2024

Previously, the user code is embedded in a function that looks like this:

async function modify_post(feed, post) {
  <USER CODE>;
  return post;
}

It means in the filter config, if the user wants to early return, they must call return post; explicitly. This is a little clumsy given we want to give the illusion that the user no longer has to explicitly return post or anything.

I hope to enable the user to write code like this in modify_post and modify_feed filter:

if (condition 1) {
  // do no modify post by early return
  return;
}

if (condition 2) {
  // delete post by assigning it null;
  post = null;
  return;
}

if (condition 3) {
  // alternatively, delete post by returning null
  return null;
}

// normally edit the post
post.field = 'new value';

Because now the return statement for user code has different semantics from the return statement of modify_post function, we must wrap the user code in a different function.

This pull request changes the wrapper we embedded user code to:

async function modify_post(feed, post) {
  const retval = await (async function(){ <USER CODE> })();
  if (retval === null) {
    return null;
  }
  return post;
}

@shouya shouya merged commit ef340d8 into master Mar 8, 2024
2 checks passed
@shouya shouya deleted the support-early-return-in-modify-post branch March 8, 2024 01:21
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.

1 participant