react-model icon indicating copy to clipboard operation
react-model copied to clipboard

Redesign middleware mechanism

Open leecade opened this issue 4 years ago • 1 comments

Think about the current version vs koa middleware, we've implemented it as an array and it make confusing and chaos here, I think koa middleware is a classic model looks simple and easily to use.

how does it look like

const model = Model(models)

model.use(async (ctx, next) => {
  console.log('1')
  await next()
  console.log('2')
})

model.use(async (ctx, next) => {
  console.log('3')
  await next()
  console.log('4')
})

// 1->3->change state->4->2

It support enhance before and after state change, we should define a middleware interface and provider the most impotant middlewares, no need subscribe api here.

import Model, { middlewares } from 'react-model'
const model = Model(models)

model.use(middlewares.logger())
model.use(middlewares.devTool())

middlewares should works with different model:

const modelA = Model({})
const modelB = Model({})

modelA.use(middlewares.logger())
modelB.use(middlewares.devTool())

const { useStoreA } from modelA
const { useStoreB } from modelB

leecade avatar May 23 '20 21:05 leecade

Don't enable any middleware by default, no one needs to control the order of middleware.

leecade avatar May 23 '20 21:05 leecade