@ektorpu/signal 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 +21 -0
- package/package.json +34 -0
- package/src/signal/index.ts +68 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ektorpu
|
|
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/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ektorpu/signal",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Signal library for roblox-ts",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"roblox",
|
|
8
|
+
"roblox-ts",
|
|
9
|
+
"signal"
|
|
10
|
+
],
|
|
11
|
+
"type": "commonjs",
|
|
12
|
+
"main": "src/signal/index.ts",
|
|
13
|
+
"files": [
|
|
14
|
+
"LICENSE",
|
|
15
|
+
"src/signal"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "rbxtsc",
|
|
19
|
+
"watch": "rbxtsc -w"
|
|
20
|
+
},
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"roblox-ts": "^3.0.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@rbxts/compiler-types": "^3.0.0-types.0",
|
|
29
|
+
"@rbxts/types": "^1.0.940",
|
|
30
|
+
"roblox-ts": "^3.0.0",
|
|
31
|
+
"typescript": "^5.9.3",
|
|
32
|
+
"prettier": "^3.7.4"
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
export class Signal<Params extends unknown[] = []> {
|
|
2
|
+
private readonly connections: Set<Connection<Params>> = new Set();
|
|
3
|
+
private readonly sleepingThreads: thread[] = [];
|
|
4
|
+
|
|
5
|
+
public Fire(...params: Params) {
|
|
6
|
+
for (const connection of this.connections) {
|
|
7
|
+
connection.Run(...params);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
for (const thread of this.sleepingThreads) {
|
|
11
|
+
task.spawn(thread, ...params);
|
|
12
|
+
}
|
|
13
|
+
this.sleepingThreads.clear();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
private makeConnection(
|
|
17
|
+
callback: (...params: Params) => unknown,
|
|
18
|
+
persistent: boolean
|
|
19
|
+
) {
|
|
20
|
+
const connection = new Connection(
|
|
21
|
+
callback,
|
|
22
|
+
this.connections,
|
|
23
|
+
persistent
|
|
24
|
+
);
|
|
25
|
+
this.connections.add(connection);
|
|
26
|
+
return connection;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public Connect(callback: (...params: Params) => unknown) {
|
|
30
|
+
return this.makeConnection(callback, true);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public Once(callback: (...params: Params) => unknown) {
|
|
34
|
+
return this.makeConnection(callback, false);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public Await(): Params {
|
|
38
|
+
this.sleepingThreads.push(coroutine.running());
|
|
39
|
+
return coroutine.yield() as unknown as Params;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
class Connection<Params extends unknown[]> {
|
|
44
|
+
private active = true;
|
|
45
|
+
|
|
46
|
+
constructor(
|
|
47
|
+
private readonly callback: (...params: Params) => unknown,
|
|
48
|
+
private readonly connectionList: Set<Connection<Params>>,
|
|
49
|
+
private readonly persistent: boolean
|
|
50
|
+
) {}
|
|
51
|
+
|
|
52
|
+
public Run(...params: Params) {
|
|
53
|
+
if (!this.active) return;
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
this.callback(...params);
|
|
57
|
+
} catch (err) {
|
|
58
|
+
warn(err);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (!this.persistent) this.Disconnect();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public Disconnect() {
|
|
65
|
+
this.connectionList.delete(this);
|
|
66
|
+
this.active = false;
|
|
67
|
+
}
|
|
68
|
+
}
|