event_emitter

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2023 License: MIT Imports: 4 Imported by: 0

README

EventEmitter

Build Status codecov

Install
go get -v github.com/lxzan/event_emitter@latest
Usage
package main

import (
	"context"
	"fmt"
	"github.com/lxzan/event_emitter"
	"time"
)

func main() {
	var em = event_emitter.New(&event_emitter.Config{
		BucketNum:   16,
		BucketCap:   128,
		Concurrency: 8,
	})
	em.Subscribe(em.NewSubscriber(), "greet", func(msg any) {
		fmt.Printf("recv: %v\n", msg)
	})
	em.Subscribe(em.NewSubscriber(), "greet", func(msg any) {
		fmt.Printf("recv: %v\n", msg)
	})
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	_ = em.Publish(ctx, "greet", "hello!")
	time.Sleep(time.Second)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	BucketNum   int64 // 分片数 (number of slices)
	BucketCap   int64 // 每个分片的初始化容量 (initialized capacity per slice)
	Concurrency int64 // 每个主题的并发度 (Concurrency of each topic)
}

type EventEmitter

type EventEmitter struct {
	// contains filtered or unexported fields
}

func New

func New(conf *Config) *EventEmitter

New 创建事件发射器实例 Creating an EventEmitter Instance

func (*EventEmitter) CountSubscriberByTopic

func (c *EventEmitter) CountSubscriberByTopic(topic string) int

CountSubscriberByTopic 获取主题订阅人数 Get the number of subscribers to a topic

func (*EventEmitter) GetTopicsBySubId

func (c *EventEmitter) GetTopicsBySubId(subId int64) []string

GetTopicsBySubId 通过订阅号获取主题列表 Get a list of topics by subscription

func (*EventEmitter) NewSubscriber

func (c *EventEmitter) NewSubscriber() (subId int64)

NewSubscriber 获取订阅号 Get subscription number

func (*EventEmitter) Publish

func (c *EventEmitter) Publish(ctx context.Context, topic string, msg any) error

Publish 向主题发布消息 Publish a message to the topic

func (*EventEmitter) Subscribe

func (c *EventEmitter) Subscribe(subId int64, topic string, f func(msg any))

Subscribe 订阅主题消息 Subscribe messages from the topic

func (*EventEmitter) UnSubscribe

func (c *EventEmitter) UnSubscribe(subId int64, topic string)

UnSubscribe 取消一个订阅主题 Cancel a subscribed topic

func (*EventEmitter) UnSubscribeAll

func (c *EventEmitter) UnSubscribeAll(subId int64)

UnSubscribeAll 取消所有订阅主题 Cancel all subscribed topics

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL