@siggn/core 0.0.1 → 0.0.3
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 +21 -0
- package/README.md +123 -6
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/siggn.d.ts +25 -0
- package/dist/siggn.d.ts.map +1 -0
- package/dist/siggn.js +42 -0
- package/dist/siggn.js.map +1 -0
- package/dist/siggn.test.d.ts +2 -0
- package/dist/siggn.test.d.ts.map +1 -0
- package/dist/siggn.test.js +84 -0
- package/dist/siggn.test.js.map +1 -0
- package/dist/types.d.ts +14 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +35 -21
- package/.github/workflows/release-package.yml +0 -33
- package/bun.lock +0 -29
- package/index.ts +0 -1
- package/rules/use-bun-instead-of-node-vite-npm-pnpm.mdc +0 -111
- package/siggn.ts +0 -32
- package/test.ts +0 -35
- package/tsconfig.json +0 -29
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Guilherme Guerreiro
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,15 +1,132 @@
|
|
|
1
|
-
# siggn
|
|
1
|
+
# @siggn/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A lightweight and type-safe event-driven pub/sub system for TypeScript projects.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Type-Safe**: Leverages TypeScript to ensure that message payloads are correct at compile and
|
|
8
|
+
runtime.
|
|
9
|
+
- **Lightweight**: Zero dependencies and a minimal API surface.
|
|
10
|
+
- **Simple API**: Easy to learn and use, with a clear and concise API.
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
You can install the package using your favorite package manager:
|
|
4
15
|
|
|
5
16
|
```bash
|
|
6
|
-
|
|
17
|
+
npm install @siggn/core
|
|
7
18
|
```
|
|
8
19
|
|
|
9
|
-
|
|
20
|
+
```bash
|
|
21
|
+
yarn add @siggn/core
|
|
22
|
+
```
|
|
10
23
|
|
|
11
24
|
```bash
|
|
12
|
-
|
|
25
|
+
pnpm add @siggn/core
|
|
13
26
|
```
|
|
14
27
|
|
|
15
|
-
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
Here's a basic example of how to use Siggn:
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import { Siggn } from '@siggn/core';
|
|
34
|
+
|
|
35
|
+
// 1. Define your message types
|
|
36
|
+
type Message =
|
|
37
|
+
| { type: 'user_created'; userId: string; name: string }
|
|
38
|
+
| { type: 'user_deleted'; userId: string };
|
|
39
|
+
|
|
40
|
+
// 2. Create a new Siggn instance
|
|
41
|
+
const siggn = new Siggn<Message>();
|
|
42
|
+
|
|
43
|
+
// 3. Subscribe to events
|
|
44
|
+
// Use a unique ID for each subscriber to manage subscriptions
|
|
45
|
+
const subscriberId = 'analytics-service';
|
|
46
|
+
|
|
47
|
+
siggn.subscribe(subscriberId, 'user_created', (msg) => {
|
|
48
|
+
console.log(`[Analytics] New user created: ${msg.name} (ID: ${msg.userId})`);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// 4. Publish events
|
|
52
|
+
siggn.publish({ type: 'user_created', userId: '123', name: 'John Doe' });
|
|
53
|
+
// Output: [Analytics] New user created: John Doe (ID: 123)
|
|
54
|
+
|
|
55
|
+
// 5. Unsubscribe from all events for a given ID
|
|
56
|
+
siggn.unsubscribe(subscriberId);
|
|
57
|
+
|
|
58
|
+
siggn.publish({ type: 'user_created', userId: '456', name: 'Jane Doe' });
|
|
59
|
+
// No output, because the subscriber was removed.
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Using the `make` helper
|
|
63
|
+
|
|
64
|
+
The `make` method simplifies managing subscriptions for a specific component or service.
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
const userComponent = siggn.make('user-component');
|
|
68
|
+
|
|
69
|
+
userComponent.subscribe('user_deleted', (msg) => {
|
|
70
|
+
console.log(`[UI] User ${msg.userId} was deleted. Updating view...`);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
siggn.publish({ type: 'user_deleted', userId: '123' });
|
|
74
|
+
// Output: [UI] User 123 was deleted. Updating view...
|
|
75
|
+
|
|
76
|
+
// Unsubscribe from all subscriptions made by 'user-component'
|
|
77
|
+
userComponent.unsubscribe();
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Subscribing to multiple events
|
|
81
|
+
|
|
82
|
+
You can use `subscribeMany` to group subscriptions for a single subscriber.
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
const auditService = siggn.make('audit-service');
|
|
86
|
+
|
|
87
|
+
auditService.subscribeMany((subscribe) => {
|
|
88
|
+
subscribe('user_created', (msg) => {
|
|
89
|
+
console.log(`[Audit] User created: ${msg.name}`);
|
|
90
|
+
});
|
|
91
|
+
subscribe('user_deleted', (msg) => {
|
|
92
|
+
console.log(`[Audit] User deleted: ${msg.userId}`);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
siggn.publish({ type: 'user_created', userId: '789', name: 'Peter Pan' });
|
|
97
|
+
siggn.publish({ type: 'user_deleted', userId: '123' });
|
|
98
|
+
|
|
99
|
+
// Unsubscribe from all audit-service events
|
|
100
|
+
auditService.unsubscribe();
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## API
|
|
104
|
+
|
|
105
|
+
### `new Siggn<T>()`
|
|
106
|
+
|
|
107
|
+
Creates a new message bus instance. `T` is a union type of all possible messages.
|
|
108
|
+
|
|
109
|
+
### `publish(msg)`
|
|
110
|
+
|
|
111
|
+
Publishes a message to all relevant subscribers.
|
|
112
|
+
|
|
113
|
+
### `subscribe(id, type, callback)`
|
|
114
|
+
|
|
115
|
+
Subscribes a callback to a specific message type with a unique subscriber ID.
|
|
116
|
+
|
|
117
|
+
### `unsubscribe(id)`
|
|
118
|
+
|
|
119
|
+
Removes all subscriptions associated with a specific subscriber ID.
|
|
120
|
+
|
|
121
|
+
### `make(id)`
|
|
122
|
+
|
|
123
|
+
Returns a helper object with `subscribe`, `subscribeMany`, and `unsubscribe` methods pre-bound to
|
|
124
|
+
the provided ID. This is useful for encapsulating subscription logic within a component or service.
|
|
125
|
+
|
|
126
|
+
### `subscribeMany(id, setup)`
|
|
127
|
+
|
|
128
|
+
A convenience method to subscribe to multiple message types for a single ID.
|
|
129
|
+
|
|
130
|
+
## License
|
|
131
|
+
|
|
132
|
+
This project is licensed under the [MIT License](LICENSE).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC"}
|
package/dist/siggn.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Msg } from './types.js';
|
|
2
|
+
export declare class Siggn<T extends Msg> {
|
|
3
|
+
private subscriptions;
|
|
4
|
+
constructor();
|
|
5
|
+
make(id: string): {
|
|
6
|
+
subscribe: <K extends T['type']>(type: K, callback: (msg: Extract<T, {
|
|
7
|
+
type: K;
|
|
8
|
+
}>) => void) => void;
|
|
9
|
+
unsubscribe: () => void;
|
|
10
|
+
subscribeMany: (setup: (subscribe: <K extends T['type']>(type: K, callback: (msg: Extract<T, {
|
|
11
|
+
type: K;
|
|
12
|
+
}>) => void) => void) => void) => void;
|
|
13
|
+
};
|
|
14
|
+
subscribeMany(id: string, setup: (subscribe: <K extends T['type']>(type: K, callback: (msg: Extract<T, {
|
|
15
|
+
type: K;
|
|
16
|
+
}>) => void) => void) => void): void;
|
|
17
|
+
subscribe<K extends T['type']>(id: string, type: K, callback: (msg: Extract<T, {
|
|
18
|
+
type: K;
|
|
19
|
+
}>) => void): void;
|
|
20
|
+
publish<K extends T['type']>(msg: Extract<T, {
|
|
21
|
+
type: K;
|
|
22
|
+
}>): void;
|
|
23
|
+
unsubscribe(id: string): void;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=siggn.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"siggn.d.ts","sourceRoot":"","sources":["../src/siggn.ts"],"names":[],"mapp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S,CAAC,CAAC,MAAM,CAAC,EAAE,GAAG,EAAE,OAAO,CAAC,CAAC,EAAE;QAAE,IAAI,EAAE,CAAC,CAAA;KAAE,CAAC;IAUzD,WAAW,CAAC,EAAE,EAAE,MAAM;CAQvB"}
|
package/dist/siggn.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export class Siggn {
|
|
2
|
+
subscriptions;
|
|
3
|
+
constructor() {
|
|
4
|
+
this.subscriptions = new Map();
|
|
5
|
+
}
|
|
6
|
+
make(id) {
|
|
7
|
+
return {
|
|
8
|
+
subscribe: (type, callback) => {
|
|
9
|
+
this.subscribe(id, type, callback);
|
|
10
|
+
},
|
|
11
|
+
unsubscribe: () => {
|
|
12
|
+
this.unsubscribe(id);
|
|
13
|
+
},
|
|
14
|
+
subscribeMany: (setup) => {
|
|
15
|
+
this.subscribeMany(id, setup);
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
subscribeMany(id, setup) {
|
|
20
|
+
setup((type, callback) => this.subscribe(id, type, callback));
|
|
21
|
+
}
|
|
22
|
+
subscribe(id, type, callback) {
|
|
23
|
+
if (!this.subscriptions.has(type)) {
|
|
24
|
+
this.subscriptions.set(type, []);
|
|
25
|
+
}
|
|
26
|
+
this.subscriptions.get(type)?.push({ id, callback });
|
|
27
|
+
}
|
|
28
|
+
publish(msg) {
|
|
29
|
+
if (!this.subscriptions.has(msg.type)) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
this.subscriptions.get(msg.type)?.forEach((sub) => {
|
|
33
|
+
sub.callback(msg);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
unsubscribe(id) {
|
|
37
|
+
for (const [type, subscriptions] of this.subscriptions) {
|
|
38
|
+
this.subscriptions.set(type, subscriptions.filter((sub) => sub.id !== id));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=siggn.js.map
|
|
@@ -0,0 +1 @@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AO;QACT,CAAC;QAED,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;YAChD,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QACpB,CAAC,CAAC,CAAC;IACL,CAAC;IAED,WAAW,CAAC,EAAU;QACpB,KAAK,MAAM,CAAC,IAAI,EAAE,aAAa,CAAC,IAAI,IAAI,CAAC,aAAa,EAAE,CAAC;YACvD,IAAI,CAAC,aAAa,CAAC,GAAG,CACpB,IAAI,EACJ,aAAa,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,CAAC,CAC7C,CAAC;QACJ,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"siggn.test.d.ts","sourceRoot":"","sources":["../src/siggn.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { Siggn } from './siggn.js';
|
|
2
|
+
import { test, expect } from 'vitest';
|
|
3
|
+
test('user should be able to use subscribe and publish', () => {
|
|
4
|
+
const siggn = new Siggn();
|
|
5
|
+
let count = 0;
|
|
6
|
+
siggn.subscribe('1', 'increment_count', (msg) => {
|
|
7
|
+
count += msg.value;
|
|
8
|
+
});
|
|
9
|
+
siggn.subscribe('1', 'decrement_count', (msg) => {
|
|
10
|
+
count -= msg.value;
|
|
11
|
+
});
|
|
12
|
+
siggn.publish({ type: 'increment_count', value: 4 });
|
|
13
|
+
expect(count).toBe(4);
|
|
14
|
+
siggn.publish({ type: 'decrement_count', value: 2 });
|
|
15
|
+
expect(count).toBe(2);
|
|
16
|
+
});
|
|
17
|
+
test('user should be able to use unsubscribe', () => {
|
|
18
|
+
const siggn = new Siggn();
|
|
19
|
+
let count = 0;
|
|
20
|
+
siggn.subscribe('1', 'increment_count', (msg) => {
|
|
21
|
+
count += msg.value;
|
|
22
|
+
});
|
|
23
|
+
siggn.subscribe('1', 'decrement_count', (msg) => {
|
|
24
|
+
count -= msg.value;
|
|
25
|
+
});
|
|
26
|
+
siggn.publish({ type: 'increment_count', value: 4 });
|
|
27
|
+
expect(count).toBe(4);
|
|
28
|
+
siggn.publish({ type: 'decrement_count', value: 2 });
|
|
29
|
+
expect(count).toBe(2);
|
|
30
|
+
siggn.unsubscribe('1');
|
|
31
|
+
siggn.publish({ type: 'increment_count', value: 4 });
|
|
32
|
+
expect(count).toBe(2);
|
|
33
|
+
});
|
|
34
|
+
test('user should be able to use makeSubscriptions to subscribe and publish', () => {
|
|
35
|
+
const siggn = new Siggn();
|
|
36
|
+
let count = 0;
|
|
37
|
+
const subscriptions = siggn.make('1');
|
|
38
|
+
subscriptions.subscribe('increment_count', (msg) => {
|
|
39
|
+
count += msg.value;
|
|
40
|
+
});
|
|
41
|
+
subscriptions.subscribe('decrement_count', (msg) => {
|
|
42
|
+
count -= msg.value;
|
|
43
|
+
});
|
|
44
|
+
siggn.publish({ type: 'increment_count', value: 4 });
|
|
45
|
+
expect(count).toBe(4);
|
|
46
|
+
siggn.publish({ type: 'decrement_count', value: 2 });
|
|
47
|
+
expect(count).toBe(2);
|
|
48
|
+
});
|
|
49
|
+
test('user should be able to use makeSubscriptions to unsubscribe', () => {
|
|
50
|
+
const siggn = new Siggn();
|
|
51
|
+
let count = 0;
|
|
52
|
+
const subscriptions = siggn.make('1');
|
|
53
|
+
subscriptions.subscribe('increment_count', (msg) => {
|
|
54
|
+
count += msg.value;
|
|
55
|
+
});
|
|
56
|
+
subscriptions.subscribe('decrement_count', (msg) => {
|
|
57
|
+
count -= msg.value;
|
|
58
|
+
});
|
|
59
|
+
siggn.publish({ type: 'increment_count', value: 4 });
|
|
60
|
+
expect(count).toBe(4);
|
|
61
|
+
siggn.publish({ type: 'decrement_count', value: 2 });
|
|
62
|
+
expect(count).toBe(2);
|
|
63
|
+
subscriptions.unsubscribe();
|
|
64
|
+
siggn.publish({ type: 'increment_count', value: 4 });
|
|
65
|
+
expect(count).toBe(2);
|
|
66
|
+
});
|
|
67
|
+
test('user should be able to use makeSubscriptions to subscribeMany and publish', () => {
|
|
68
|
+
const siggn = new Siggn();
|
|
69
|
+
let count = 0;
|
|
70
|
+
const subscriptions = siggn.make('1');
|
|
71
|
+
subscriptions.subscribeMany((subscribe) => {
|
|
72
|
+
subscribe('increment_count', (msg) => {
|
|
73
|
+
count += msg.value;
|
|
74
|
+
});
|
|
75
|
+
subscribe('decrement_count', (msg) => {
|
|
76
|
+
count -= msg.value;
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
siggn.publish({ type: 'increment_count', value: 4 });
|
|
80
|
+
expect(count).toBe(4);
|
|
81
|
+
siggn.publish({ type: 'decrement_count', value: 2 });
|
|
82
|
+
expect(count).toBe(2);
|
|
83
|
+
});
|
|
84
|
+
//# sourceMappingURL=siggn.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"siggn.test.js","sourceRoot":"","sources":["../src/siggn.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAStC,IAAI,CAAC,kDAAkD,EAAE,GAAG,EAAE;IAC5D,MAAM,KAAK,GAAG,IAAI,KAAK,EAAO,CAAC;IAC/B,IAAI,KAAK,GAAG,CAAC,CAAC;IAEd,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE,iBAAiB,EAAE,CAAC,GAAG,EAAE,EAAE;QAC9C,KAAK,IAAI,GAAG,CAAC,KAAK,CAAC;IACrB,CAAC,CAAC,CAAC;IAEH,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE,iBAAiB,EAAE,CAAC,GAAG,EAAE,EAAE;QAC9C,KAAK,IAAI,GAAG,CAAC,KAAK,CAAC;IACrB,CAAC,CAAC,CAAC;IAEH,KAAK,CAAC,OA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;IAEH,KAAK,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,iBAAiB,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;IAErD,MAAM,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAEtB,KAAK,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,iBAAiB,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;IAErD,MAAM,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACxB,CAAC,CAAC,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type Msg = {
|
|
2
|
+
type: string;
|
|
3
|
+
[key: string]: unknown;
|
|
4
|
+
};
|
|
5
|
+
export type Subscription<T extends Msg, K extends T['type']> = {
|
|
6
|
+
id: string;
|
|
7
|
+
callback: (msg: Extract<T, {
|
|
8
|
+
type: K;
|
|
9
|
+
}>) => void;
|
|
10
|
+
};
|
|
11
|
+
export type SubscriptionMap<T extends Msg> = {
|
|
12
|
+
[K in T['type']]: Array<Subscription<T, K>>;
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,GAAG,GAAG;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,IAAI;IAC7D,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,EAAE,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,EAAE;QAAE,IAAI,EAAE,CAAC,CAAA;KAAE,CAAC,KAAK,IAAI,CAAC;CAClD,CAAC;AAEF,MAAM,MAAM,eAAe,CAAC,CAAC,SAAS,GAAG,IAAI;KAC1C,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC,YAAY,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;CAC5C,CAAC"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@siggn/core",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "A lightweight
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"description": "A lightweight message bus system for Typescript",
|
|
5
5
|
"keywords": [
|
|
6
|
-
"pub",
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"
|
|
6
|
+
"pub/sub",
|
|
7
|
+
"message",
|
|
8
|
+
"event",
|
|
9
|
+
"bus"
|
|
10
10
|
],
|
|
11
11
|
"publishConfig": {
|
|
12
12
|
"@siggn:registry": "https://registry.npmjs.org/",
|
|
@@ -18,26 +18,40 @@
|
|
|
18
18
|
},
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|
|
21
|
-
"url": "git+https://github.com/Guiguerreiro39/siggn.git"
|
|
21
|
+
"url": "git+https://github.com/Guiguerreiro39/siggn.git/",
|
|
22
|
+
"directory": "packages/core"
|
|
22
23
|
},
|
|
23
|
-
"
|
|
24
|
-
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"author": "Guilherme Guerreiro (https://guilhermegr.com)",
|
|
25
29
|
"type": "module",
|
|
26
|
-
"main": "index.
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
"main": "dist/index.js",
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"module": "./dist/index.mjs",
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"require": "./dist/index.js",
|
|
36
|
+
"import": "./dist/index.mjs",
|
|
37
|
+
"types": "./dist/index.d.ts"
|
|
38
|
+
}
|
|
35
39
|
},
|
|
36
40
|
"devDependencies": {
|
|
37
|
-
"
|
|
41
|
+
"typescript": "^5.9.3",
|
|
42
|
+
"vitest": "^3.2.4"
|
|
38
43
|
},
|
|
39
44
|
"peerDependencies": {
|
|
40
45
|
"typescript": "^5"
|
|
41
46
|
},
|
|
42
|
-
"
|
|
43
|
-
|
|
47
|
+
"scripts": {
|
|
48
|
+
"dev": "vitest",
|
|
49
|
+
"test": "vitest run",
|
|
50
|
+
"build": "tsc",
|
|
51
|
+
"ci": "pnpm run build && pnpm run check-format && pnpm run test",
|
|
52
|
+
"format": "prettier --write .",
|
|
53
|
+
"check-format": "prettier --check .",
|
|
54
|
+
"version": "changeset version",
|
|
55
|
+
"release": "changeset publish"
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
name: Node.js Package
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
release:
|
|
5
|
-
types: [created]
|
|
6
|
-
|
|
7
|
-
jobs:
|
|
8
|
-
build:
|
|
9
|
-
runs-on: ubuntu-latest
|
|
10
|
-
steps:
|
|
11
|
-
- uses: actions/checkout@v5
|
|
12
|
-
- uses: actions/setup-node@v4
|
|
13
|
-
with:
|
|
14
|
-
node-version: 20
|
|
15
|
-
- run: npm ci
|
|
16
|
-
- run: npm test
|
|
17
|
-
|
|
18
|
-
publish-gpr:
|
|
19
|
-
needs: build
|
|
20
|
-
runs-on: ubuntu-latest
|
|
21
|
-
permissions:
|
|
22
|
-
packages: write
|
|
23
|
-
contents: read
|
|
24
|
-
steps:
|
|
25
|
-
- uses: actions/checkout@v5
|
|
26
|
-
- uses: actions/setup-node@v4
|
|
27
|
-
with:
|
|
28
|
-
node-version: 20
|
|
29
|
-
registry-url: https://npm.pkg.github.com/
|
|
30
|
-
- run: npm ci
|
|
31
|
-
- run: npm publish
|
|
32
|
-
env:
|
|
33
|
-
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
package/bun.lock
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"lockfileVersion": 1,
|
|
3
|
-
"workspaces": {
|
|
4
|
-
"": {
|
|
5
|
-
"name": "siggn",
|
|
6
|
-
"devDependencies": {
|
|
7
|
-
"@types/bun": "latest",
|
|
8
|
-
},
|
|
9
|
-
"peerDependencies": {
|
|
10
|
-
"typescript": "^5",
|
|
11
|
-
},
|
|
12
|
-
},
|
|
13
|
-
},
|
|
14
|
-
"packages": {
|
|
15
|
-
"@types/bun": ["@types/bun@1.3.0", "", { "dependencies": { "bun-types": "1.3.0" } }, "sha512-+lAGCYjXjip2qY375xX/scJeVRmZ5cY0wyHYyCYxNcdEXrQ4AOe3gACgd4iQ8ksOslJtW4VNxBJ8llUwc3a6AA=="],
|
|
16
|
-
|
|
17
|
-
"@types/node": ["@types/node@24.8.1", "", { "dependencies": { "undici-types": "~7.14.0" } }, "sha512-alv65KGRadQVfVcG69MuB4IzdYVpRwMG/mq8KWOaoOdyY617P5ivaDiMCGOFDWD2sAn5Q0mR3mRtUOgm99hL9Q=="],
|
|
18
|
-
|
|
19
|
-
"@types/react": ["@types/react@19.2.2", "", { "dependencies": { "csstype": "^3.0.2" } }, "sha512-6mDvHUFSjyT2B2yeNx2nUgMxh9LtOWvkhIU3uePn2I2oyNymUAX1NIsdgviM4CH+JSrp2D2hsMvJOkxY+0wNRA=="],
|
|
20
|
-
|
|
21
|
-
"bun-types": ["bun-types@1.3.0", "", { "dependencies": { "@types/node": "*" }, "peerDependencies": { "@types/react": "^19" } }, "sha512-u8X0thhx+yJ0KmkxuEo9HAtdfgCBaM/aI9K90VQcQioAmkVp3SG3FkwWGibUFz3WdXAdcsqOcbU40lK7tbHdkQ=="],
|
|
22
|
-
|
|
23
|
-
"csstype": ["csstype@3.1.3", "", {}, "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw=="],
|
|
24
|
-
|
|
25
|
-
"typescript": ["typescript@5.9.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw=="],
|
|
26
|
-
|
|
27
|
-
"undici-types": ["undici-types@7.14.0", "", {}, "sha512-QQiYxHuyZ9gQUIrmPo3IA+hUl4KYk8uSA7cHrcKd/l3p1OTpZcM0Tbp9x7FAtXdAYhlasd60ncPpgu6ihG6TOA=="],
|
|
28
|
-
}
|
|
29
|
-
}
|
package/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./siggn";
|
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
description: Use Bun instead of Node.js, npm, pnpm, or vite.
|
|
3
|
-
globs: "*.ts, *.tsx, *.html, *.css, *.js, *.jsx, package.json"
|
|
4
|
-
alwaysApply: false
|
|
5
|
-
---
|
|
6
|
-
|
|
7
|
-
Default to using Bun instead of Node.js.
|
|
8
|
-
|
|
9
|
-
- Use `bun <file>` instead of `node <file>` or `ts-node <file>`
|
|
10
|
-
- Use `bun test` instead of `jest` or `vitest`
|
|
11
|
-
- Use `bun build <file.html|file.ts|file.css>` instead of `webpack` or `esbuild`
|
|
12
|
-
- Use `bun install` instead of `npm install` or `yarn install` or `pnpm install`
|
|
13
|
-
- Use `bun run <script>` instead of `npm run <script>` or `yarn run <script>` or `pnpm run <script>`
|
|
14
|
-
- Bun automatically loads .env, so don't use dotenv.
|
|
15
|
-
|
|
16
|
-
## APIs
|
|
17
|
-
|
|
18
|
-
- `Bun.serve()` supports WebSockets, HTTPS, and routes. Don't use `express`.
|
|
19
|
-
- `bun:sqlite` for SQLite. Don't use `better-sqlite3`.
|
|
20
|
-
- `Bun.redis` for Redis. Don't use `ioredis`.
|
|
21
|
-
- `Bun.sql` for Postgres. Don't use `pg` or `postgres.js`.
|
|
22
|
-
- `WebSocket` is built-in. Don't use `ws`.
|
|
23
|
-
- Prefer `Bun.file` over `node:fs`'s readFile/writeFile
|
|
24
|
-
- Bun.$`ls` instead of execa.
|
|
25
|
-
|
|
26
|
-
## Testing
|
|
27
|
-
|
|
28
|
-
Use `bun test` to run tests.
|
|
29
|
-
|
|
30
|
-
```ts#index.test.ts
|
|
31
|
-
import { test, expect } from "bun:test";
|
|
32
|
-
|
|
33
|
-
test("hello world", () => {
|
|
34
|
-
expect(1).toBe(1);
|
|
35
|
-
});
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
## Frontend
|
|
39
|
-
|
|
40
|
-
Use HTML imports with `Bun.serve()`. Don't use `vite`. HTML imports fully support React, CSS, Tailwind.
|
|
41
|
-
|
|
42
|
-
Server:
|
|
43
|
-
|
|
44
|
-
```ts#index.ts
|
|
45
|
-
import index from "./index.html"
|
|
46
|
-
|
|
47
|
-
Bun.serve({
|
|
48
|
-
routes: {
|
|
49
|
-
"/": index,
|
|
50
|
-
"/api/users/:id": {
|
|
51
|
-
GET: (req) => {
|
|
52
|
-
return new Response(JSON.stringify({ id: req.params.id }));
|
|
53
|
-
},
|
|
54
|
-
},
|
|
55
|
-
},
|
|
56
|
-
// optional websocket support
|
|
57
|
-
websocket: {
|
|
58
|
-
open: (ws) => {
|
|
59
|
-
ws.send("Hello, world!");
|
|
60
|
-
},
|
|
61
|
-
message: (ws, message) => {
|
|
62
|
-
ws.send(message);
|
|
63
|
-
},
|
|
64
|
-
close: (ws) => {
|
|
65
|
-
// handle close
|
|
66
|
-
}
|
|
67
|
-
},
|
|
68
|
-
development: {
|
|
69
|
-
hmr: true,
|
|
70
|
-
console: true,
|
|
71
|
-
}
|
|
72
|
-
})
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
HTML files can import .tsx, .jsx or .js files directly and Bun's bundler will transpile & bundle automatically. `<link>` tags can point to stylesheets and Bun's CSS bundler will bundle.
|
|
76
|
-
|
|
77
|
-
```html#index.html
|
|
78
|
-
<html>
|
|
79
|
-
<body>
|
|
80
|
-
<h1>Hello, world!</h1>
|
|
81
|
-
<script type="module" src="./frontend.tsx"></script>
|
|
82
|
-
</body>
|
|
83
|
-
</html>
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
With the following `frontend.tsx`:
|
|
87
|
-
|
|
88
|
-
```tsx#frontend.tsx
|
|
89
|
-
import React from "react";
|
|
90
|
-
|
|
91
|
-
// import .css files directly and it works
|
|
92
|
-
import './index.css';
|
|
93
|
-
|
|
94
|
-
import { createRoot } from "react-dom/client";
|
|
95
|
-
|
|
96
|
-
const root = createRoot(document.body);
|
|
97
|
-
|
|
98
|
-
export default function Frontend() {
|
|
99
|
-
return <h1>Hello, world!</h1>;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
root.render(<Frontend />);
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
Then, run index.ts
|
|
106
|
-
|
|
107
|
-
```sh
|
|
108
|
-
bun --hot ./index.ts
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
For more information, read the Bun API docs in `node_modules/bun-types/docs/**.md`.
|
package/siggn.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
type Msg = {
|
|
2
|
-
type: string
|
|
3
|
-
[key: string]: unknown
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
export class Siggn <T extends Msg> {
|
|
7
|
-
private subscriptions: Map<T['type'], Map<string, Function>>
|
|
8
|
-
|
|
9
|
-
constructor() {
|
|
10
|
-
this.subscriptions = new Map();
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
subscribe(id: string, type: T['type'], callback: (msg: T) => void) {
|
|
14
|
-
if (!this.subscriptions.has(type)) {
|
|
15
|
-
this.subscriptions.set(type, new Map());
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
this.subscriptions.get(type)?.set(id, callback);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
emit(msg: T) {
|
|
22
|
-
this.subscriptions.get(msg.type)?.forEach((callback) => {
|
|
23
|
-
callback(msg);
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
unsubscribe(id: string) {
|
|
28
|
-
this.subscriptions.forEach((sub) => {
|
|
29
|
-
sub.delete(id);
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
}
|
package/test.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { Siggn } from "./siggn";
|
|
2
|
-
|
|
3
|
-
type Msg = {
|
|
4
|
-
type: 'increment_count',
|
|
5
|
-
value: number
|
|
6
|
-
} | {
|
|
7
|
-
type: 'decrement_count',
|
|
8
|
-
value: number
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const siggn = new Siggn<Msg>();
|
|
12
|
-
|
|
13
|
-
let count = 0
|
|
14
|
-
|
|
15
|
-
siggn.subscribe('1', 'increment_count', (msg) => {
|
|
16
|
-
count += msg.value
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
siggn.subscribe('2', 'decrement_count', (msg) => {
|
|
20
|
-
count -= msg.value
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
function incrementCount(r: typeof siggn) {
|
|
24
|
-
r.emit({ type: 'increment_count', value: 4 })
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function decrementCount(r: typeof siggn) {
|
|
28
|
-
r.emit({ type: 'decrement_count', value: 2 })
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
incrementCount(siggn)
|
|
32
|
-
console.log(count)
|
|
33
|
-
|
|
34
|
-
decrementCount(siggn)
|
|
35
|
-
console.log(count)
|
package/tsconfig.json
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
// Environment setup & latest features
|
|
4
|
-
"lib": ["ESNext"],
|
|
5
|
-
"target": "ESNext",
|
|
6
|
-
"module": "Preserve",
|
|
7
|
-
"moduleDetection": "force",
|
|
8
|
-
"jsx": "react-jsx",
|
|
9
|
-
"allowJs": true,
|
|
10
|
-
|
|
11
|
-
// Bundler mode
|
|
12
|
-
"moduleResolution": "bundler",
|
|
13
|
-
"allowImportingTsExtensions": true,
|
|
14
|
-
"verbatimModuleSyntax": true,
|
|
15
|
-
"noEmit": true,
|
|
16
|
-
|
|
17
|
-
// Best practices
|
|
18
|
-
"strict": true,
|
|
19
|
-
"skipLibCheck": true,
|
|
20
|
-
"noFallthroughCasesInSwitch": true,
|
|
21
|
-
"noUncheckedIndexedAccess": true,
|
|
22
|
-
"noImplicitOverride": true,
|
|
23
|
-
|
|
24
|
-
// Some stricter flags (disabled by default)
|
|
25
|
-
"noUnusedLocals": false,
|
|
26
|
-
"noUnusedParameters": false,
|
|
27
|
-
"noPropertyAccessFromIndexSignature": false
|
|
28
|
-
}
|
|
29
|
-
}
|