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(middleware): api desgin draft #2293

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

feat(middleware): api desgin draft #2293

wants to merge 2 commits into from

Conversation

Gcaufy
Copy link
Contributor

@Gcaufy Gcaufy commented Nov 5, 2021

Issue

#2282

Description

It's gonna be a big change, check the design darft, and see if I can move on.

Demo Code

  1. How to define a middleware
// 抽象通过能力为中间件,由中间件控制事件响应
const filterMiddleWare = (options) {
  return async (message, next) => {

    const room = message.room()

    if (!room) return 
    if (message.type() !== option.type) return 
    if (!matchers.roomMatcher(option.room)) return

    await next();
  }
}
  1. Add middleware to specified event
// Only room a print `banana`
bot.on('message', 
  filterMiddlerWare({ type: type.Message.Text, room: 'room a'}),
  (message) => console.log('banana')
);

// All rooms print `apple` 
bot.on('message', 
  (message) => console.log('apple')
);
  1. Add global middleware
WechatyImpl.middleware({
  message: filterMiddleWare({ type.Message.Text, room: [ 'room-d', 'room-e' ]})
})

// Only room-d, room-e print apple.
bot.on('message', 
  (message) => console.log('apple')
);

// only room-d, room-e print orange(50% probability)
bot.on('message', async (message, next) => {
  if (Math.random() < 0.5) {
    await next();
  }
}, (message) => console.log('orange'))
  1. Work with plugins
WechatyImpl.middleware({
  message: filterMiddleWare({ type.Message.Text, room: [ 'room-d', 'room-e' ]})
})

// Only room-d, room-e has kickoff feature
WechatyImpl.use(KickOffPlugin(options));


// TODO: use support rest params, so we need to think about how design it.
// WechatyImpl.use({ message: someMiddleWare() }, KickOffPlugin(options));
  1. Work in plugins
const onMessage = (message) => {
  // plugin logic
}
export MyPlugin = (bot: WechatyInterface) => {
  bot.on('message', [
    MiddleWareA(),
    MiddleWareB(),
    MiddleWareC(),
  ], onMessage)
}

@CLAassistant
Copy link

CLAassistant commented Nov 5, 2021

CLA assistant check
All committers have signed the CLA.

TFunction extends (...args: any) => any,
TParameters extends [...args: any]
> = (
...args: [...Parameters<TFunction>, ...TParameters]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that we can hard code the next: () => void | Promise<void> at here to replace TParameters?

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

3 participants