Skip to content

Commit

Permalink
feat(ebus): add ebus
Browse files Browse the repository at this point in the history
  • Loading branch information
novlan1 committed Jan 6, 2024
1 parent da9716c commit b7389c7
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 33 deletions.
42 changes: 13 additions & 29 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,35 @@
# 更新日志

### [1.3.48](https://github.com/novlan1/t-comm/compare/v1.3.47...v1.3.48) (2024-01-04)


### Features 🎉

* **e-bus:** add ebus ([487b969](https://github.com/novlan1/t-comm/commit/487b96901973f11b58fc38d7ae0cf65bda82b5bf))

### [1.3.47](https://github.com/novlan1/t-comm/compare/v1.3.46...v1.3.47) (2023-12-25)


### Documentation 📖

* update docs ([77b214e](https://github.com/novlan1/t-comm/commit/77b214e2e1dfcd8f46587f49f0db29767807824b))


### Features 🎉

* **change-log:** update insert change log ([c69b4b3](https://github.com/novlan1/t-comm/commit/c69b4b36ca659e30049b0839ddf1ffd42c72428f))

### [1.3.46](https://github.com/novlan1/t-comm/compare/v1.3.45...v1.3.46) (2023-12-22)


### Documentation 📖

* update docs ([cd7be3e](https://github.com/novlan1/t-comm/commit/cd7be3e04ad9d664616bd32186f9644547e87713))


### Features 🎉






























* **msdk:** add msdk full screen mixin ([f2c74e3](https://github.com/novlan1/t-comm/commit/f2c74e385bd87c96b57428faa1d21d239d0e465d))

### [1.3.45](https://github.com/novlan1/t-comm/compare/v1.3.44...v1.3.45) (2023-12-21)

Expand Down
43 changes: 43 additions & 0 deletions docs/zh/e-bus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[[toc]]

## 引入

```ts
import { initGlobalVue3EBus, initDiffVue3EBus } from 't-comm';

// or

import { initGlobalVue3EBus, initDiffVue3EBus} from 't-comm/lib/e-bus/index';
```


## `initGlobalVue3EBus`


**描述**:<p>挂载统一的eBus,所有实例共用一个</p>

**参数**


| 参数名 | 描述 |
| --- | --- |
| app | <p>Vue3 应用实例</p> |



<a name="initDiffVue3EBus"></a>

## `initDiffVue3EBus`


**描述**:<p>挂载唯一的eBus,不同实例用不同的</p>

**参数**


| 参数名 | 描述 |
| --- | --- |
| app | <p>Vue3 应用实例</p> |



20 changes: 18 additions & 2 deletions docs/zh/mixin.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
## 引入

```ts
import { getMorsePwdMixin } from 't-comm';
import { getMorsePwdMixin, getMsdkFullScreen } from 't-comm';

// or

import { getMorsePwdMixin} from 't-comm/lib/mixin/index';
import { getMorsePwdMixin, getMsdkFullScreen} from 't-comm/lib/mixin/index';
```


Expand All @@ -34,3 +34,19 @@ getMorsePwdMixin([1, 1, 1, 1, 1], function () {
this.onShowLaunchApp();
}),
```
<a name="getMsdkFullScreen"></a>

## `getMsdkFullScreen()`


**描述**:<p>msdk 浏览器全屏方法,点击外链时可全屏,返回时退出全屏</p>

**参数**



**示例**

```ts
mixins: [getMsdkFullScreen()],
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "t-comm",
"version": "1.3.45",
"version": "1.3.48",
"description": "丰富易用的工具库",
"main": "lib/index.js",
"module": "lib/index.esm.js",
Expand Down
2 changes: 1 addition & 1 deletion src/change-log/change-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function insertDocChangeLog({
console.log('[insertDocChangeLog] version: ', version);
if (!version) return;

const reg = new RegExp(`(\\n[#]+\\s*\\[${version}\\].*?\\n)(?=[#]+\\s*\\[\\d+\\.\\d+\\.\\d+)`, 's');
const reg = new RegExp(`\\n([#]+\\s*\\[${version}\\].*?\\n)(?=[#]+\\s*\\[\\d+\\.\\d+\\.\\d+)`, 's');

const match = changeLog.match(reg);

Expand Down
34 changes: 34 additions & 0 deletions src/e-bus/e-bus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export class EventBus {
private events: Record<string, Array<Function>>;

constructor() {
this.events = {};
}

emit(eventName: string, data: any) {
if (this.events[eventName]) {
this.events[eventName].forEach((fn) => {
fn(data);
});
}
}
on(eventName: string, fn: any) {
this.events[eventName] = this.events[eventName] || [];
this.events[eventName].push(fn);
}

off(eventName: string, fn: any) {
if (!fn) {
delete this.events[eventName];
return;
}
if (this.events[eventName]) {
for (let i = 0; i < this.events[eventName].length; i++) {
if (this.events[eventName][i] === fn) {
this.events[eventName].splice(i, 1);
break;
}
}
}
}
}
2 changes: 2 additions & 0 deletions src/e-bus/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { EventBus } from './e-bus';
export { initDiffVue3EBus, initGlobalVue3EBus } from './init';
34 changes: 34 additions & 0 deletions src/e-bus/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { EventBus } from './e-bus';


const globalEBus = new EventBus();


/**
* 挂载统一的eBus,所有实例共用一个
* @param app Vue3 应用实例
*/
export const initGlobalVue3EBus = function (app: any) {
if (!app?.config?.globalProperties) {
return app;
}

app.config.globalProperties.$ebus = globalEBus;
return app;
};


/**
* 挂载唯一的eBus,不同实例用不同的
* @param app Vue3 应用实例
*/
export const initDiffVue3EBus = function (app: any) {
if (!app?.config?.globalProperties) {
return app;
}

const eBus = new EventBus();
app.config.globalProperties.$ebus = eBus;
return app;
};

1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export * from './devops';
export * from './dialog';
export * from './dom';
export * from './dom-to-image';
export * from './e-bus';
export * from './e2e-test';
export * from './env';
export * from './env-variable';
Expand Down
1 change: 1 addition & 0 deletions src/mixin/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { morsePwdMixin, getMorsePwdMixin } from './morse-password-mixin';
export { getMsdkFullScreen } from './msdk-full-screen';
export { getVisibilityChangeMixin } from './visibllity-change-mixin';
22 changes: 22 additions & 0 deletions src/mixin/msdk-full-screen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { callJsSetFullScreen, callJsReSetFullScreen } from '../msdk/msdk';


/**
* msdk 浏览器全屏方法,点击外链时可全屏,返回时退出全屏
* @example
* ```ts
* mixins: [getMsdkFullScreen()],
* ```
*/
export function getMsdkFullScreen() {
return {
onShow() {
callJsSetFullScreen();
},
methods: {
callJsReSetFullScreen() {
callJsReSetFullScreen();
},
},
};
}

0 comments on commit b7389c7

Please sign in to comment.