Skip to content
Merged
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
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# EditorConfig is awesome: https://EditorConfig.org

root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,yaml}]
indent_size = 2
52 changes: 52 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: CI

on:
push:
branches: [master, main]
pull_request:
branches: [master, main]

jobs:
build-and-test:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x, 20.x, 22.x]

steps:
- uses: actions/checkout@v4

- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 10

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'pnpm'

- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Lint
run: pnpm run lint

- name: Build
run: pnpm run build

- name: Test
run: pnpm run test

- name: Coverage
run: pnpm run coverage
if: matrix.node-version == '22.x'

- name: Upload coverage
uses: codecov/codecov-action@v4
if: matrix.node-version == '22.x'
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: false
35 changes: 35 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.1.6] - 2025-02-24

### Changed
- Migrated build tool from rollup to rslib
- Updated publish script

## [1.1.5] - 2025-02-09

### Dependencies
- Updated bittydash to v0.7.1

## [1.1.4] - 2025-02-22

### Dependencies
- Updated prettier to v3.5.2

## [1.1.0] - 2022-11-28

### Added
- Initial release with core event subscriber and dispatcher functionality
- Scope isolation support
- Wildcard event listener (`*`)
- `once` option for one-time listeners

[1.1.6]: https://github.com/Yukiniro/miis/compare/v1.1.5...v1.1.6
[1.1.5]: https://github.com/Yukiniro/miis/compare/v1.1.4...v1.1.5
[1.1.4]: https://github.com/Yukiniro/miis/compare/v1.1.3...v1.1.4
[1.1.0]: https://github.com/Yukiniro/miis/releases/tag/v1.1.0
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022-present Yukiniro

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
258 changes: 131 additions & 127 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,127 +1,131 @@
# miis

![NPM](https://img.shields.io/npm/l/miis?color=blue&style=flat-square) ![npm](https://img.shields.io/npm/v/miis?color=blue&style=flat-square)

> The `miis` is a tiny functional event subscriber and dispatcher.

## Features

- **Tiny**: weighs less than 1kb gzipped
- **Plentiful**: a special "\*" event type listens to all events
- **Scope**: isolate different listening environments by setting scope

## Install

This project need node and npm.

```shell
npm install miis --save
```

or

```shell
pnpm add miis --save
```

## Useage

```javascript
import miis from "miis";

miis.subscribe("a", (...args) => {
console.log("a event call"); // a event call
console.log(...args); /// 1, 2, 3
});
miis.dispatch("a", 1, 2, 3);
```

And it's so easy to operate with react. Here is a [demo](https://stackblitz.com/edit/react-ts-ucliuq?file=App.tsx).

```jsx
import * as React from "react";
import "./style.css";
import miis from "miis";

export default function App() {
const [count, setCount] = React.useState(0);
React.useEffect(() => {
return miis.subscribe("a", () => {
setCount(count + 1);
});
}, [count]);

const handleClick = () => {
miis.dispatch("a");
};

return (
<div>
<button onClick={handleClick}>Dispatch A</button>
<p>Count: {count}</p>
</div>
);
}
```

You could unsubscribe the event lisenter with the result of subscribe.

```javascript
import miis from "miis";

const unsubscribe = miis.subscribe("a", () => {
console.log("a event call");
});
unsubscribe();

miis.dispatch("a"); // not work
```

## API

### subscirbe

Register an event listenter for the given name.

#### Params

- `eventName` **string | symbol** Name of event to listen for.(_`*`_ for all events)
- `listenter` **Function** Function to call in response to given event
- `options` **undefined | Object** Some options. _optional_
- `once` **boolean** Only call once if it is `true`.

#### Returns

- `unsubscribe` **Function** Function to remove the listenter.

### dispatch

Invoke all handlers for the given name.

#### Params

- `eventName` **string | symbol** Name of event to invoke for.

### clear

Clears the specified listeners. It will clear all listeners if the parameter is undefined.

#### Params

- `eventName` **string | symbol | undefiend** Name of event to listen for.(_undefined_ for all events)

### setScope

If you call `dispatch`, only the handlers for the given scope will be invoked.

#### Params

- `scope` **string** Name of scope.

### getScope

Return current `scope`.

### resetScope

Reset the scope to be `default`.
# miis

![NPM](https://img.shields.io/npm/l/miis?color=blue&style=flat-square) ![npm](https://img.shields.io/npm/v/miis?color=blue&style=flat-square) [![CI](https://github.com/Yukiniro/miis/actions/workflows/ci.yml/badge.svg)](https://github.com/Yukiniro/miis/actions/workflows/ci.yml)

> The `miis` is a tiny functional event subscriber and dispatcher.

## Features

- **Tiny**: weighs less than 1kb gzipped
- **Plentiful**: a special "*" event type listens to all events
- **Scope**: isolate different listening environments by setting scope

## Install

This project needs node and npm.

```shell
npm install miis --save
```

or

```shell
pnpm add miis --save
```

## Usage

```javascript
import miis from "miis";

miis.subscribe("a", (...args) => {
console.log("a event call"); // a event call
console.log(...args); // 1, 2, 3
});
miis.dispatch("a", 1, 2, 3);
```

And it's so easy to operate with React. Here is a [demo](https://stackblitz.com/edit/react-ts-ucliuq?file=App.tsx).

```jsx
import * as React from "react";
import "./style.css";
import miis from "miis";

export default function App() {
const [count, setCount] = React.useState(0);
React.useEffect(() => {
return miis.subscribe("a", () => {
setCount(count + 1);
});
}, [count]);

const handleClick = () => {
miis.dispatch("a");
};

return (
<div>
<button onClick={handleClick}>Dispatch A</button>
<p>Count: {count}</p>
</div>
);
}
```

You could unsubscribe the event listener with the result of subscribe.

```javascript
import miis from "miis";

const unsubscribe = miis.subscribe("a", () => {
console.log("a event call");
});
unsubscribe();

miis.dispatch("a"); // not work
```

## API

### subscribe

Register an event listener for the given name.

#### Params

- `eventName` **string | symbol** Name of event to listen for.(_`*`_ for all events)
- `listener` **Function** Function to call in response to given event
- `options` **undefined | Object** Some options. _optional_
- `once` **boolean** Only call once if it is `true`.

#### Returns

- `unsubscribe` **Function** Function to remove the listener.

### dispatch

Invoke all handlers for the given name.

#### Params

- `eventName` **string | symbol** Name of event to invoke for.

### clear

Clears the specified listeners. It will clear all listeners if the parameter is undefined.

#### Params

- `eventName` **string | symbol | undefined** Name of event to clear.(_undefined_ for all events)

### setScope

If you call `dispatch`, only the handlers for the given scope will be invoked.

#### Params

- `scope` **string** Name of scope.

### getScope

Return current `scope`.

### resetScope

Reset the scope to be `default`.

## License

[MIT](./LICENSE)
Loading
Loading