Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs-vitepress/api/global-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ mpx.use(test)
mpx.use(test, {prefix: 'mpx'}, 'otherparams')
```

插件成功安装后,Mpx 会将插件对象或函数的 `__installed` 属性设置为 `true`,外部代码可以通过该标记判断插件是否已经安装。Mpx 内部仍使用独立的插件列表避免重复安装。

### watch

watch 可以通过全局实例访问,也可以使用具名导出的方式,二者逻辑相同,我们推荐使用具名导出的方式。[查看详情](reactivity-api/computed-watch-api.html#watch)
Expand Down
9 changes: 7 additions & 2 deletions packages/core/@types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,10 +567,15 @@ interface ImplementOptions {

export function toPureObject<T extends object> (obj: T): T

declare type PluginInstallFunction = (app: Mpx, ...options: any[]) => any
declare type PluginInstallHandler = (app: Mpx, ...options: any[]) => any

declare type PluginInstallFunction = PluginInstallHandler & {
__installed?: boolean
}

export type Plugin = PluginInstallFunction | {
install: PluginInstallFunction
install: PluginInstallHandler
__installed?: boolean
}

export type PluginFunction<T extends Plugin> = T extends PluginInstallFunction ? T : T extends { install: infer U } ? U : never
Expand Down
29 changes: 29 additions & 0 deletions packages/core/__tests__/common/use.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
global.__mpx_mode__ = 'wx'
global.mpxGlobal = {}
global.wx = {}

jest.mock('@mpxjs/perf', () => ({}), { virtual: true })

const Mpx = require('../../src').default

describe('Mpx.use', () => {
it('should mark an object plugin as installed', () => {
const plugin = {
install: jest.fn()
}

Mpx.use(plugin)

expect(plugin.install).toHaveBeenCalledTimes(1)
expect(plugin.__installed).toBe(true)
})

it('should mark a function plugin as installed', () => {
const plugin = jest.fn()

Mpx.use(plugin)

expect(plugin).toHaveBeenCalledTimes(1)
expect(plugin.__installed).toBe(true)
})
})
1 change: 1 addition & 0 deletions packages/core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ function use (plugin, options = {}) {
extendProps(Mpx, proxyMpx, rawProps, options)
extendProps(Mpx.prototype, proxyMpx.prototype, rawPrototypeProps, options)
installedPlugins.push(plugin)
plugin.__installed = true
return this
}

Expand Down
Loading