@mary-ext/simple-event-emitter 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2025, Mary
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # simple-event-emitter
2
+
3
+ a really simple event emitter
4
+
5
+ ```ts
6
+ type UserStatus = 'online' | 'away' | 'offline';
7
+
8
+ const userStatusChanged = new SimpleEventEmitter<[username: string, status: UserStatus]>();
9
+
10
+ const cleanup = userStatusChanged.subscribe((username, status) => {
11
+ console.log(`@${username} is now ${status}`);
12
+ });
13
+
14
+ userStatusChanged.emit('mary', 'offline');
15
+ ```
@@ -0,0 +1,8 @@
1
+ export type EventListener<TArgs extends unknown[]> = (...args: TArgs) => void;
2
+ export type CleanupFunction = () => void;
3
+ export declare class SimpleEventEmitter<TArgs extends unknown[]> {
4
+ #private;
5
+ emit(...args: TArgs): boolean;
6
+ subscribe(listener: EventListener<TArgs>): CleanupFunction;
7
+ unsubscribe(listener: EventListener<TArgs>): void;
8
+ }
package/dist/index.js ADDED
@@ -0,0 +1,53 @@
1
+ export class SimpleEventEmitter {
2
+ #listener;
3
+ emit(...args) {
4
+ const listener = this.#listener;
5
+ if (listener === undefined) {
6
+ return false;
7
+ }
8
+ if (typeof listener === 'function') {
9
+ listener.apply(this, args);
10
+ }
11
+ else {
12
+ for (let idx = 0, len = listener.length; idx < len; idx++) {
13
+ listener[idx].apply(this, args);
14
+ }
15
+ }
16
+ return true;
17
+ }
18
+ subscribe(listener) {
19
+ const existing = this.#listener;
20
+ if (existing === undefined) {
21
+ this.#listener = listener;
22
+ }
23
+ else if (typeof existing === 'function') {
24
+ this.#listener = [existing, listener];
25
+ }
26
+ else {
27
+ this.#listener = existing.concat(listener);
28
+ }
29
+ return this.unsubscribe.bind(listener);
30
+ }
31
+ unsubscribe(listener) {
32
+ const existing = this.#listener;
33
+ if (existing === undefined) {
34
+ return;
35
+ }
36
+ if (existing === listener) {
37
+ this.#listener = undefined;
38
+ }
39
+ else if (typeof existing !== 'function') {
40
+ const index = existing.indexOf(listener);
41
+ if (index !== -1) {
42
+ if (existing.length === 2) {
43
+ // ^ flips the bit, it's either 0 or 1 here.
44
+ this.#listener = existing[index ^ 1];
45
+ }
46
+ else {
47
+ this.#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oB,CAAC;IAC3D,CAAC;IAED,WAAW,CAAC,QAA8B;QACzC,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC;QAEhC,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;YAC5B,OAAO;QACR,CAAC;QAED,IAAI,QAAQ,KAAK,QAAQ,EAAE,CAAC;YAC3B,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC5B,CAAC;aAAM,IAAI,OAAO,QAAQ,KAAK,UAAU,EAAE,CAAC;YAC3C,MAAM,KAAK,GAAG,QAAQ,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;YAEzC,IAAI,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC;gBAClB,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;oBAC3B,4CAA4C;oBAC5C,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;gBACtC,CAAC;qBAAM,CAAC;oBACP,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;gBAC/C,CAAC;YACF,CAAC;QACF,CAAC;IACF,CAAC;CACD"}
package/lib/index.ts ADDED
@@ -0,0 +1,63 @@
1
+ type MaybeArray<T> = T | T[];
2
+
3
+ export type EventListener<TArgs extends unknown[]> = (...args: TArgs) => void;
4
+ export type CleanupFunction = () => void;
5
+
6
+ export class SimpleEventEmitter<TArgs extends unknown[]> {
7
+ #listener: MaybeArray<EventListener<TArgs>> | undefined;
8
+
9
+ emit(...args: TArgs): boolean {
10
+ const listener = this.#listener;
11
+
12
+ if (listener === undefined) {
13
+ return false;
14
+ }
15
+
16
+ if (typeof listener === 'function') {
17
+ listener.apply(this, args);
18
+ } else {
19
+ for (let idx = 0, len = listener.length; idx < len; idx++) {
20
+ listener[idx].apply(this, args);
21
+ }
22
+ }
23
+
24
+ return true;
25
+ }
26
+
27
+ subscribe(listener: EventListener<TArgs>): CleanupFunction {
28
+ const existing = this.#listener;
29
+
30
+ if (existing === undefined) {
31
+ this.#listener = listener;
32
+ } else if (typeof existing === 'function') {
33
+ this.#listener = [existing, listener];
34
+ } else {
35
+ this.#listener = existing.concat(listener);
36
+ }
37
+
38
+ return this.unsubscribe.bind(listener) as CleanupFunction;
39
+ }
40
+
41
+ unsubscribe(listener: EventListener<TArgs>): void {
42
+ const existing = this.#listener;
43
+
44
+ if (existing === undefined) {
45
+ return;
46
+ }
47
+
48
+ if (existing === listener) {
49
+ this.#listener = undefined;
50
+ } else if (typeof existing !== 'function') {
51
+ const index = existing.indexOf(listener);
52
+
53
+ if (index !== -1) {
54
+ if (existing.length === 2) {
55
+ // ^ flips the bit, it's either 0 or 1 here.
56
+ this.#listener = existing[index ^ 1];
57
+ } else {
58
+ this.#listener = existing.toSpliced(index, 1);
59
+ }
60
+ }
61
+ }
62
+ }
63
+ }
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "type": "module",
3
+ "name": "@mary-ext/simple-event-emitter",
4
+ "version": "1.0.0",
5
+ "description": "A really simple event emitter",
6
+ "license": "BSD-3-Clause",
7
+ "repository": {
8
+ "url": "https://codeberg.org/mary-ext/npm-simple-event-emitter"
9
+ },
10
+ "files": [
11
+ "dist/",
12
+ "lib/",
13
+ "!lib/**/*.bench.ts",
14
+ "!lib/**/*.test.ts"
15
+ ],
16
+ "exports": {
17
+ ".": "./dist/index.js"
18
+ },
19
+ "scripts": {
20
+ "build": "tsc --project tsconfig.build.json",
21
+ "fmt": "prettier --cache --write .",
22
+ "prepack": "rm -rf dist; bun run build"
23
+ },
24
+ "devDependencies": {
25
+ "prettier": "^3.5.3",
26
+ "typescript": "~5.8.3"
27
+ }
28
+ }