Skip to content

Commit

Permalink
Merge pull request #8 from Undertone0809/docs-update-1
Browse files Browse the repository at this point in the history
docs: optimize docs
  • Loading branch information
Undertone0809 authored Jan 11, 2023
2 parents e2df444 + 520d84d commit 795e68f
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 97 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,16 @@ There is a easy demo to show how to use broadcast-service.
```python
from broadcast_service import broadcast_service


# callback of common method
def handle_msg(params):
print(params)
print(f"handle_msg receive params: {params}")


# callback of decorator
@broadcast_service.on_listen(['my_topic'])
def handle_decorator_msg(params):
print(params)
print(f"handle_decorator_msg receive params: {params}")

if __name__ == '__main__':
info = 'This is very important msg'
Expand All @@ -59,7 +60,6 @@ if __name__ == '__main__':

# publish broadcast
broadcast_service.publish('my_topic', info)

```

- You can use `publish, emit, broadcast` to send your topic msg and use `listen, on, subscribe` to listen your topic msg.
Expand Down
6 changes: 3 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ There is a easy demo to show how to use broadcast-service.
```python
from broadcast_service import broadcast_service


# callback of common method
def handle_msg(params):
print(params)
print(f"handle_msg receive params: {params}")


# callback of decorator
@broadcast_service.on_listen(['my_topic'])
def handle_decorator_msg(params):
print(params)
print(f"handle_decorator_msg receive params: {params}")

if __name__ == '__main__':
info = 'This is very important msg'
Expand All @@ -56,7 +57,6 @@ if __name__ == '__main__':

# publish broadcast
broadcast_service.publish('my_topic', info)

```

**About more example, please see [Quick Start](/quickstart.md)**
Expand Down
2 changes: 1 addition & 1 deletion docs/_coverpage.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
- Provide different syntax writing modes for lambda, callback functions, decorators, etc

[GitHub](https://github.com/Undertone0809/broadcast-service)
[Getting Started](/broadcast-service.md)
[Getting Started](/README.md)
3 changes: 2 additions & 1 deletion docs/_sidebar.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- Introduction
- [broadcast-service](broadcast-service.md)
- [broadcast-service](README.md)

- Getting started

Expand All @@ -8,5 +8,6 @@

- Other

- [Update](update.md)
- [Plan](plan.md)
- [Contribution](contribution.md)
73 changes: 0 additions & 73 deletions docs/broadcast-service.md

This file was deleted.

104 changes: 88 additions & 16 deletions docs/quickstart.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
# Quick start

## Install
It is recommended to install latest version, which provides more features and less bugs. Old version is shit.
It is recommended to install latest version, which provides more features and less bugs. The old version is shit.

```bash
pip install --upgrade broadcast-service
```

## Initialize
## Example
`broadcast-service` use single pattern to build the broadcast pattern/pubsub pattern, which means you can use it like a util class and you didn't initialize any other redundant class.

Now we create a easy pubsub pattern.
<p align='center'>
<img src="https://zeeland-bucket.oss-cn-beijing.aliyuncs.com/typora_img/20230111230418.png"/>
</p>

**Now you can see a easy pubsub pattern.**
```python
from broadcast_service import broadcast_service

Expand All @@ -31,45 +35,113 @@ if __name__ == '__main__':

```

## Decorator
Moreover, it is recommend to use decorator to listen your topic.
> :warning: Attention: `listen` and `subscribe` mean the same thing in the following passage.
## How to subscribe?
Actually, `broadcast-service` support mutiple subscirbe and publish methods, such as `common callback function` and `decorator`. You can see more example as follows.

- **subscribe single topic**

```python
from broadcast_service import broadcast_service


# callback of decorator
# common callback function method
def handle_msg(params):
print(params)


# decorator method
@broadcast_service.on_listen(['my_topic'])
def handle_decorator_msg(params):
def handle_decorator_msg1(params):
print(params)


# if you just listen one topic, you don't have to pass a topic list
@broadcast_service.on_listen("my_topic")
def handle_decorator_msg2(params):
print(params)


if __name__ == '__main__':
info = 'This is very important msg'

# subscribe topic
broadcast_service.subscribe('my_topic', handle_msg)
# you can also pass a topics list to subscribe multiple topics
broadcast_service.subscribe(['my_topic'], handle_msg)

# publish broadcast
broadcast_service.publish('my_topic', info)
```

You can listen multiple topics by decorator. The function is called back whenever a topic is triggered.
- **subscribe multiple topics**

<p align='center'>
<img src="https://zeeland-bucket.oss-cn-beijing.aliyuncs.com/typora_img/20230111230943.png"/>
</p>

```python
from broadcast_service import broadcast_service


# callback of decorator
@broadcast_service.on_listen(['my_topic'])
def handle_decorator_msg():
pass
def handle_msg(params):
print(params)


@broadcast_service.on_listen(['my_topic1', 'my_topic2'])
def handle_decorator_msg(params):
print(params)


if __name__ == '__main__':
info = 'This is very important msg'
# This callback function is triggered when a message is sent from one of two topics subscribed to
broadcast_service.subscribe(['my_topic1', 'my_topic2'], handle_msg)

# publish broadcast
broadcast_service.publish('my_topic')
broadcast_service.publish('my_topic1', "msg1")
broadcast_service.publish('my_topic2', "msg2")
```

## How to publish?
You can pass multiple arguments to a topic or multiple topics by `broadcast-service`. You can see more example as follows.

- **publish a topic**

```python
from broadcast_service import broadcast_service


@broadcast_service.on_listen("no_param")
def handle_no_param():
print("no param")

broadcast_service.publish("no_param")


@broadcast_service.on_listen("one_param")
def handle_one_param(params):
print(params)

broadcast_service.publish("one_param", 123)


@broadcast_service.on_listen("three_params")
def handle_three_params(a, b, c):
print(a)
print(b)
print(c)

broadcast_service.publish("one_param", 11, 22, 33)


@broadcast_service.on_listen("multi_params")
def handle_multi_params(*args, **kwargs):
print(args) # [11, 22]
print(kwargs['msg']) # hello

broadcast_service.publish("one_param", 11, 22, msg="hello")
```

## Argument
About argument, you'd better know how many arguments you should take. If your callback function arguments is different from the message arguments of your publish, you may not recevice the callback. Here are some example:
Expand Down Expand Up @@ -141,14 +213,14 @@ if __name__ == '__main__':
broadcast_service.publish('my_topic1', params=[11,22])
broadcast_service.publish('my_topic2', params={"msg": "success"})
```

**output:**

```text
{'params': [11, 22]}
{'params': {'msg': 'success'}}
```



## Attention
You can use `publish, emit, broadcast` to send your topic msg. Using `listen, on, subscribe` to listen your topic msg. Using `stop_listen, off, unsubscribe` to cancel subscribe.

Expand Down
82 changes: 82 additions & 0 deletions docs/update.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Update

## Upgrade Version
Please update the latest version. The old version is shit.

```bash
pip install --upgrade broadcast-service
```

## v1.2.0 2023-01-12

#### feat
1. Add support multiple publications and multiple subscriptions.

```python
broadcast_service.publish(["topic1", "topic2"], "message")
```

2. Optimize the function of `broadcast_service.subscribe()` and `broadcast_service.publish()`

- The following two cases are equivalent.

```python
@broadcast_service.on_listen(['topic1'])
def handle_all_msg():
# your code

@broadcast_service.on_listen('topic1')
def handle_all_msg():
# your code

```

```python
broadcast_service.subscribe('topic1')
broadcast_service.subscribe(['topic1'])
```


#### test
1. Add more test cases


## v1.1.7 2023-01-10

#### feat

1. Add decorator, optimize syntactic expression [#5](https://github.com/Undertone0809/broadcast-service/pull/5)

- You can use the following sentences to subscribe topic

```python
@broadcast_service.on_listen(['topic1'])
def handle_all_msg():
# your code

@broadcast_service.on_listen(['topic1','topic2'])
def handle_all_msg():
# your code

@broadcast_service.on_listen()
def handle_all_msg(*args, **kwargs):
# your code
```

2. Add some equals function name

```python
# send topic msg
broadcast_service.subscribe('my_topic', handle_msg)
broadcast_service.listen('my_topic', handle_msg)
broadcast_service.on('my_topic', handle_msg)

# listen topic msg
broadcast_service.broadcast('my_topic', info)
broadcast_service.publish('my_topic', info)
broadcast_service.emit('my_topic', info)
```

#### test

1. Optimize test cases and add some demo [#5](https://github.com/Undertone0809/broadcast-service/pull/5)

0 comments on commit 795e68f

Please sign in to comment.