JavaScript-重构我们通过ARG运行的方式

发布于 2025-01-25 12:03:02 字数 1035 浏览 2 评论 0原文

我已经实现了以下方法:

async function sendPushNotifications(
  title,
  body,
  data,
  badge,
  sound = "default",
  pushNotificationsTokens
) {
  // Generate the messages to be sent
  const messages = buildMessages(
    title,
    body,
    data,
    badge,
    sound,
    pushNotificationsTokens
  );

  // Send the generated messages to the FCM server
  const tickets = await sendMessagesToFCMServer(messages);

  // Finally, handle all possible errors to avoid app removal in stores
  await handleErrorsSendingMessages(tickets, pushNotificationsTokens);
}

我想公开一种更简单的方法,以将推送通知发送到唯一的令牌。

function sendPushNotification(
  title,
  body,
  data,
  badge,
  sound = "default",
  pushNotificationsToken
) {
  return sendPushNotifications(
    title,
    body,
    data,
    badge,
    sound,
    [pushNotificationsToken]
  );
}

如您所见,这里有某种模式。我们得到参数,然后将它们传递到另一种方法,但将pushnotificationstoken推入数组。

我的问题是:是否可以重构第二个功能sendpushnotification?我的意思是,有没有专业的外观功能代码可以一行执行?

I have implemented the following method:

async function sendPushNotifications(
  title,
  body,
  data,
  badge,
  sound = "default",
  pushNotificationsTokens
) {
  // Generate the messages to be sent
  const messages = buildMessages(
    title,
    body,
    data,
    badge,
    sound,
    pushNotificationsTokens
  );

  // Send the generated messages to the FCM server
  const tickets = await sendMessagesToFCMServer(messages);

  // Finally, handle all possible errors to avoid app removal in stores
  await handleErrorsSendingMessages(tickets, pushNotificationsTokens);
}

And I want to expose a simpler method, for sending a push notification to a unique token.

function sendPushNotification(
  title,
  body,
  data,
  badge,
  sound = "default",
  pushNotificationsToken
) {
  return sendPushNotifications(
    title,
    body,
    data,
    badge,
    sound,
    [pushNotificationsToken]
  );
}

As you can see, there is some kind of pattern here. We get the params and just pass them to the other method, but pushing the pushNotificationsToken into an array.

My question is: is it possible to refactor the second function sendPushNotification? I mean, any professional look-alike functional code to do it in one line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸印温柔 2025-02-01 12:03:02

使用REST参数,然后弹出最后一个并将其放入数组中。

const sendPushNotification = (...args) => {
  const token = args.pop();
  return sendPushNotifications(...args, [token]);
};

从技术上讲,可以将其全部推入一行(如果需要的话,可以将整个应用程序都放在一行上),但这是不那么可读的。

Using rest parameters and popping off the last one and putting it into an array instead would work.

const sendPushNotification = (...args) => {
  const token = args.pop();
  return sendPushNotifications(...args, [token]);
};

It'd technically be possible to shove it all into one line (you can put entire applications on a single line if you wanted), but that'd be less readable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文