npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@et-modules/common

v1.0.75

Published

@et-modules/common

Downloads

842

Readme

EventEmitter

事件总线,主要用于主框架和组件通信,也可用于其他需要跨页面通信的场景。

使用

import { eventEmitter } from '@et-modules/common'

API

| 参数 | 说明 | 类型 | | - | - | - | | on | 添加事件监听 | function(type: EventType, callback) | | off | 移除事件监听 | function(type: EventType, callback) | | once | 只监听一次 | function(type: EventType, callback) | | emit | 触发一个事件并传递数据 | function(type: EventType, ..args) |

EventType 说明

在模块内设置了用于主框架和组件通信的事件类型: EventType , 后续开发中可继续增加类型

当使用 emit 函数触发事件时,传递参数说明如下:

| 类型 | 参数 | 说明 | | - | - | - | | FRAME_REFRESH | {module: Module} | 通知主框架刷新 | | VIEW_REFRESH | {module: Module} | 通知组件刷新 | | OPEN_TAB | {id: string, name: string, module: Module} | 在主框架打开一个tab页签 | | LOGOUT | 无 | 通知主框架退出 |

示例

import { eventEmitter, EventType,  Module} from '@et-modules/common'

const refreshEvent = (data) => {
  if(data?.module === Module.PRODUCT) {
    console.log('主框架刷新产品数据')
  }
}
eventEmitter.emit(EventType.FRAME_REFRESH, {module: Module.PRODUCT})
eventEmitter.on(EventType.FRAME_REFRESH, refreshEvent)
eventEmitter.off(EventType.FRAME_REFRESH, refreshEvent)

和主框架通信

  • 主框架
import React from 'react';
import { ProductView } from '@et-modules/product';
import { eventEmitter, EventType } from '@et-modules/common';

const Product = ({ resource }) => {

  useEffect(() => {
    const handleRefresh = (data) => {
      if(data?.module === Module.PRODUCT) {
      // 接收到需要主框架刷新的事件,刷新主框架数据
      }
    }
    eventEmitter.on(EventType.FRAME_REFRESH, handleRefresh);

    return () => {
      eventEmitter.off(EventType.FRAME_REFRESH, handleRefresh);
    };
  }, []);

  return <ProductView  />;
};

export default Product;
  • 组件内
import React, { FC } from 'react';
import { eventEmitter, EventType } from '@et-modules/common';


const ProductView: FC<ProductViewProps> = ({ eventEmitter }) => {
  const handleDelete = () => {
    // 删除成功后
    eventEmitter.emit(EventType.FRAME_REFRESH,  { module: Module.PRODUCT })
  }

  useEffect(() => {
    const handleRefresh = () => {
      if(data?.module === Module.PRODUCT) {
      // 接收到主框架传递的数据,刷新组件内的数据
      }
    };
    eventEmitter.on(EventType.VIEW_REFRESH, handleRefresh);
    return () => {
      eventEmitter.off(EventType.FRAME_REFRESH, handleRefresh);
    };
  }, []);

  return <div>xxxx</div>
}

style 公共样式

为了统一样式,且每个模块都能共用,将样式变量定义文件移入到 @et-modules/common 模块中。

尽量使用 variable.less 中的变量去处理样式,如果有需要统一的样式均添加到 variable.less 中。

使用

@import '@et-modules/common/lib/style/variable.less';

.app {
  background-color: @hover-bg-color;
}

Icon 图标

使用

import { Icon } from '@et-modules/common';

const App = () => {
  return (
    <Icon type='icon-xxx' />
  )
}