Skip to content

Commit fe43a1b

Browse files
committed
update readme and docs
1 parent e88de47 commit fe43a1b

File tree

7 files changed

+140
-59
lines changed

7 files changed

+140
-59
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ node_modules
2828

2929
dist/
3030
.nyc_output/
31+
.vscode

.vscode/settings.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

CHANGELOG.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ _(none)_
66
---
77

88
## 2.0.0 (2023-03-29)
9-
_(none)_
10-
11-
## 2.0.0-2 (2023-03-29)
129

1310
- `.add` is now type safe. The type of the listener is checked against the type variable in the constructor as well as an optional "flavor".
1411
- `.add` now returns a node reference instead of a object. The returned node cannot be removed directly; it must be from the signal using `MiniSignal#detach`.

README.md

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ MiniSignals v2.0.0 has been rewritten in TypeScript and had it's API changed to
1818

1919
New features:
2020

21-
- `.add` now returns a weak node reference which can be used to remove the listener directly from the signal. Reduces memory leaks.
21+
- `.add` now returns a node reference which can be used to remove the listener directly from the signal. Reduces memory leaks.
2222
- `.add` is now type safe. The type of the listener is checked against the type variable in the constructor as well as an optional "flavor".
2323

2424
Breaking changes:
2525

26-
- `.add` now returns a node reference instead of a object. The returned node cannot be removed directly; it must be from the signal using `MiniSignal#detach`.
26+
- `.add` now returns a node reference instead of an object. The returned node cannot be removed directly; it must be from the signal using `MiniSignal#detach`.
2727
- `.once` has been removed. Use `.add` instead with a call to `.detach` in the callback.
2828
- The `thisArg` parameter has been removed from `.add`. Use `.add` with a call to `.bind` or (preferred) use an arrow function with a closure.
2929
- `.dispatch` now throws an error if the signal is already dispatching.
30+
`.detach` now throws an error if the node reference was not generated from the signal.
3031

3132
## Install
3233

@@ -36,12 +37,12 @@ Breaking changes:
3637
npm install mini-signals
3738
```
3839

39-
## Example Usage
40+
## Examples
4041

4142
```ts
4243
import { MiniSignal } from 'mini-signals';
4344

44-
const mySignal = new MiniSignal<[string, string]>(); // the type variable is optional and defines the parameters to be dispatched
45+
const mySignal = new MiniSignal<[string, string]>(); // the type variable optionally and defines the parameters to be dispatched
4546

4647
const binding = mySignal.add((foo: string, bar: string) => { // add listener, note the parameter types match the type variable in the constructor
4748
console.log('signal dispatched');
@@ -50,10 +51,10 @@ const binding = mySignal.add((foo: string, bar: string) => { // add listener, no
5051
});
5152

5253
mySignal.dispatch('foo', 'bar'); // dispatch signal passing custom parameters
53-
binding.detach(); // remove a single listener
54+
mySignal.detach(binding); // remove a single listener
5455
```
5556

56-
## Another Example
57+
### Another Example
5758

5859
```ts
5960
const myObject = {
@@ -70,9 +71,24 @@ myObject.foo = 'baz';
7071
myObject.updated.dispatch(); // dispatch signal
7172
```
7273

74+
### Flavoring the signal
75+
76+
```ts
77+
import { MiniSignal } from 'mini-signals';
78+
79+
const mySignal = new MiniSignal<[string, string], 'mySignal'>();
80+
const myOtherSignal = new MiniSignal<[string, string], 'myOtherSignal'>();
81+
82+
const binding = mySignal.add((foo: string, bar: string) => {
83+
// ...
84+
});
85+
86+
myOtherSignal.detach(binding); // TypeScript error: Argument of type 'MiniSignalBinding<[string, string], "mySignal">' is not assignable to parameter of type 'MiniSignalBinding<[string, string], "myOtherSignal">'.
87+
```
88+
7389
## API
7490

75-
See [API.md](https://github.com/Hypercubed/mini-signals/blob/master/API.md)
91+
See [API.md](https://github.com/Hypercubed/mini-signals/blob/master/docs/classes/MiniSignal.md)
7692

7793
## License
7894

docs/README.md

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,24 @@ Custom event/messaging system for TypeScript/JavaScript inspired by [js-signals]
1212

1313
There are several advantages to signals over event-emitters (see [Comparison between different Observer Pattern implementations](https://github.com/millermedeiros/js-signals/wiki/Comparison-between-different-Observer-Pattern-implementations)). However, the current implementation of [js-signals](https://github.com/millermedeiros/js-signals) is (arguably) slow compared to other implementations (see [EventsSpeedTests](https://github.com/Hypercubed/EventsSpeedTests)). `mini-signals` is a fast, minimal emitter, with an API similar to [js-signals](https://github.com/millermedeiros/js-signals).
1414

15-
> Note: Signals here are the type defined by [js-signals](https://github.com/millermedeiros/js-signals) inspired by AS3-Signals. They should not to be confused with [SolidJS](https://www.solidjs.com/tutorial/introduction_signals) or Angular signals.
15+
> Note: Signals here are the type defined by Miller Medeiros in [js-signals](https://github.com/millermedeiros/js-signals) inspired by AS3-Signals. They should not to be confused with [SolidJS](https://www.solidjs.com/tutorial/introduction_signals) or [Angular signals](https://github.com/angular/angular/discussions/49090).
1616
1717
## mini-signals 2.0.0
1818

1919
MiniSignals v2.0.0 has been rewritten in TypeScript and had it's API changed to improve performance and add type safety.
2020

2121
New features:
2222

23-
- `.add` now returns a weak node reference which can be used to remove the listener directly from the signal. Reduces memory leaks.
24-
- `.add` is now type safe. The type of the listener is checked against the type variable in the constructor.
23+
- `.add` now returns a node reference which can be used to remove the listener directly from the signal. Reduces memory leaks.
24+
- `.add` is now type safe. The type of the listener is checked against the type variable in the constructor as well as an optional "flavor".
2525

2626
Breaking changes:
2727

28-
- `.add` now returns a node reference instead of a object, which had a `detach` method. The node reference can be used to remove the listener directly from the signal.
29-
- `.once` has been removed. Use `.add` instead with a call to `.detach` in the listener.
30-
- The `thisArg` parameter has been removed from `.add`. Use `.add` with a call to `.bind` or use an arrow function with a closure.
28+
- `.add` now returns a node reference instead of an object. The returned node cannot be removed directly; it must be from the signal using `MiniSignal#detach`.
29+
- `.once` has been removed. Use `.add` instead with a call to `.detach` in the callback.
30+
- The `thisArg` parameter has been removed from `.add`. Use `.add` with a call to `.bind` or (preferred) use an arrow function with a closure.
3131
- `.dispatch` now throws an error if the signal is already dispatching.
32+
`.detach` now throws an error if the node reference was not generated from the signal.
3233

3334
## Install
3435

@@ -38,12 +39,12 @@ Breaking changes:
3839
npm install mini-signals
3940
```
4041

41-
## Example Usage
42+
## Examples
4243

4344
```ts
4445
import { MiniSignal } from 'mini-signals';
4546

46-
const mySignal = new MiniSignal<[string, string]>(); // the type variable is optional and defines the parameters to be dispatched
47+
const mySignal = new MiniSignal<[string, string]>(); // the type variable optionally and defines the parameters to be dispatched
4748

4849
const binding = mySignal.add((foo: string, bar: string) => { // add listener, note the parameter types match the type variable in the constructor
4950
console.log('signal dispatched');
@@ -52,30 +53,44 @@ const binding = mySignal.add((foo: string, bar: string) => { // add listener, no
5253
});
5354

5455
mySignal.dispatch('foo', 'bar'); // dispatch signal passing custom parameters
55-
binding.detach(); // remove a single listener
56+
mySignal.detach(binding); // remove a single listener
5657
```
5758

58-
## Another Example
59+
### Another Example
5960

6061
```ts
6162
const myObject = {
6263
foo: "bar",
63-
updated: new MiniSignal<never, typeof myObject>() // in this case the type variable is never, since we are not passing any parameters
64+
updated: new MiniSignal<never>() // in this case the type variable is never, since we are not passing any parameters
6465
};
6566

6667
myObject.updated.add(() => {
6768
console.log('signal dispatched');
68-
assert(this === myObject);
69-
assert(this.foo === 'baz');
70-
}, myObject); // add listener with context
69+
assert(myObject.foo === 'baz');
70+
});
7171

7272
myObject.foo = 'baz';
7373
myObject.updated.dispatch(); // dispatch signal
7474
```
7575

76+
### Flavoring the signal
77+
78+
```ts
79+
import { MiniSignal } from 'mini-signals';
80+
81+
const mySignal = new MiniSignal<[string, string], 'mySignal'>();
82+
const myOtherSignal = new MiniSignal<[string, string], 'myOtherSignal'>();
83+
84+
const binding = mySignal.add((foo: string, bar: string) => {
85+
// ...
86+
});
87+
88+
myOtherSignal.detach(binding); // TypeScript error: Argument of type 'MiniSignalBinding<[string, string], "mySignal">' is not assignable to parameter of type 'MiniSignalBinding<[string, string], "myOtherSignal">'.
89+
```
90+
7691
## API
7792

78-
See [API.md](https://github.com/Hypercubed/mini-signals/blob/master/API.md)
93+
See [API.md](https://github.com/Hypercubed/mini-signals/blob/master/docs/classes/MiniSignal.md)
7994

8095
## License
8196

0 commit comments

Comments
 (0)