Skip to content

Commit

Permalink
feat(shared): add middleware function (#1858)
Browse files Browse the repository at this point in the history
* feat(shared): add applyMiddleware function

* test(shared): add middleware tests
  • Loading branch information
janryWang authored Jul 22, 2021
1 parent 8632bc3 commit e54525d
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 15 deletions.
12 changes: 6 additions & 6 deletions docs/guide/advanced/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ export default {
extraBabelPlugins: [
[
'babel-plugin-import',
{ libraryName: 'antd', libraryDirectory: 'lib', style: true },
{ libraryName: 'antd', libraryDirectory: 'es', style: true },
],
[
'babel-plugin-import',
{ libraryName: '@formily/antd', libraryDirectory: 'lib', style: true },
{ libraryName: '@formily/antd', libraryDirectory: 'esm', style: true },
],
],
}
Expand Down Expand Up @@ -94,12 +94,12 @@ modify `config-overrides.js`
+ module.exports = override(
+ fixBabelImports('import', {
+ libraryName: 'antd',
+ libraryDirectory: 'lib',
+ libraryDirectory: 'es',
+ style: true
+ }),
+ fixBabelImports('import', {
+ libraryName: '@formily/antd',
+ libraryDirectory: 'lib',
+ libraryDirectory: 'esm',
+ style: true
+ }),
+ );
Expand Down Expand Up @@ -128,15 +128,15 @@ Modify `.babelrc` or babel-loader
"import",
{
"libraryName": "antd",
"libraryDirectory": "lib",
"libraryDirectory": "es",
"style": true
}
],
[
"import",
{
"libraryName": "@formily/antd",
"libraryDirectory": "lib",
"libraryDirectory": "esm",
"style": true
}
]
Expand Down
12 changes: 6 additions & 6 deletions docs/guide/advanced/build.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ export default {
extraBabelPlugins: [
[
'babel-plugin-import',
{ libraryName: 'antd', libraryDirectory: 'lib', style: true },
{ libraryName: 'antd', libraryDirectory: 'es', style: true },
],
[
'babel-plugin-import',
{ libraryName: '@formily/antd', libraryDirectory: 'lib', style: true },
{ libraryName: '@formily/antd', libraryDirectory: 'esm', style: true },
],
],
}
Expand Down Expand Up @@ -94,12 +94,12 @@ yarn add babel-plugin-import --dev
+ module.exports = override(
+ fixBabelImports('import', {
+ libraryName: 'antd',
+ libraryDirectory: 'lib',
+ libraryDirectory: 'es',
+ style: true
+ }),
+ fixBabelImports('import', {
+ libraryName: '@formily/antd',
+ libraryDirectory: 'lib',
+ libraryDirectory: 'esm',
+ style: true
+ }),
+ );
Expand Down Expand Up @@ -128,15 +128,15 @@ yarn add babel-plugin-import --dev
"import",
{
"libraryName": "antd",
"libraryDirectory": "lib",
"libraryDirectory": "es",
"style": true
}
],
[
"import",
{
"libraryName": "@formily/antd",
"libraryDirectory": "lib",
"libraryDirectory": "esn",
"style": true
}
]
Expand Down
2 changes: 1 addition & 1 deletion packages/react/docs/api/shared/Schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ It can only be consumed by expressions in x-reactions, corresponding to the depe
### $form
Can only be consumed by expressions in x-reactions, representing the current field instance
Can only be consumed by expressions in x-reactions, representing the current Form instance
### $target
Expand Down
2 changes: 1 addition & 1 deletion packages/react/docs/api/shared/Schema.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ type SchemaReactions<Field = any> =
### $form
只能在 x-reactions 中的表达式消费,代表当前字段实例
只能在 x-reactions 中的表达式消费,代表当前 Form 实例
### $target
Expand Down
22 changes: 22 additions & 0 deletions packages/shared/src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { merge } from '../merge'
import { instOf } from '../instanceof'
import { isFn, isHTMLElement, isNumberLike, isReactElement } from '../checkers'
import { defaults } from '../defaults'
import { applyMiddleware } from '../middleware'

describe('array', () => {
test('toArr', () => {
Expand Down Expand Up @@ -499,6 +500,7 @@ describe('shared Subscribable', () => {
objWithCustomNotify.subscribe(cb)
objWithCustomNotify.notify({ key3: 'val3' })
expect(customNotify).toBeCalledTimes(1)
objWithCustomNotify.unsubscribe()
})
})

Expand Down Expand Up @@ -694,3 +696,23 @@ test('defaults', () => {
mm: { value: 123 },
})
})

test('applyMiddleware', async () => {
expect(
await applyMiddleware(0, [
(num: number, next) => next(num + 1),
(num: number, next) => next(num + 1),
(num: number, next) => next(num + 1),
])
).toEqual(3)

expect(
await applyMiddleware(0, [
(num: number, next) => next(num + 1),
() => '123',
(num: number, next) => next(num + 1),
])
).toEqual('123')

expect(await applyMiddleware(0)).toEqual(0)
})
1 change: 1 addition & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from './global'
export * from './path'
export * from './deprecate'
export * from './subscribable'
export * from './middleware'
export * from './merge'
export * from './instanceof'
export * from './defaults'
Expand Down
13 changes: 13 additions & 0 deletions packages/shared/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export interface IMiddleware {
(payload: any, next: (payload: any) => any): any
}

export const applyMiddleware = (payload: any, fns: IMiddleware[] = []) => {
const compose = (payload: any, fns: IMiddleware[]) => {
if (fns.length === 0) return Promise.resolve(payload)
return Promise.resolve(
fns[0](payload, (payload) => compose(payload, fns.slice(1)))
)
}
return compose(payload, fns)
}
2 changes: 1 addition & 1 deletion packages/vue/docs/api/shared/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ type SchemaReactions<Field = any> =
### $form
只能在 x-reactions 中的表达式消费,代表当前字段实例
只能在 x-reactions 中的表达式消费,代表当前 Form 实例
### $target
Expand Down

0 comments on commit e54525d

Please sign in to comment.