byte-fe / react-model

The next generation state management library for React
235 stars 22 forks source link

Redesign middleware mechanism #163

Open leecade opened 4 years ago

leecade commented 4 years ago

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 commented 4 years ago

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