@siredvin/cc-events 0.1.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.
Files changed (3) hide show
  1. package/index.ts +55 -0
  2. package/key.ts +26 -0
  3. package/package.json +46 -0
package/index.ts ADDED
@@ -0,0 +1,55 @@
1
+ export class BaseEvent<T extends any[]> {
2
+ constructor(readonly name: string, readonly args: T) {}
3
+ }
4
+
5
+ export class TerminateEvent extends BaseEvent<[]> {}
6
+
7
+ export const eventInitializers = new LuaTable<
8
+ string,
9
+ (name: string, args: any[]) => BaseEvent<any[]>
10
+ >();
11
+ eventInitializers.set(
12
+ "terminate",
13
+ (name: string, args: []) => new TerminateEvent(name, args)
14
+ );
15
+
16
+ type Constructor<T extends {} = {}> = new (name: string, ...args: any[]) => T;
17
+
18
+ export function parseEvent(args: any[]): BaseEvent<any[]> {
19
+ return parseNamedEvent(args[0], args.slice(1));
20
+ }
21
+
22
+ export function parseNamedEvent(name: string, args: any[]): BaseEvent<any[]> {
23
+ if (eventInitializers.has(name)) {
24
+ return eventInitializers.get(name)(name, args);
25
+ }
26
+ return new BaseEvent(name, args);
27
+ }
28
+
29
+ export function pullEventRaw(
30
+ filter: string | null = null
31
+ ): BaseEvent<any> | null {
32
+ const args: any[] = os.pullEventRaw(filter);
33
+ return parseEvent(args);
34
+ }
35
+ export function pullEvent(filter: string | null = null): BaseEvent<any> | null {
36
+ const ev = pullEventRaw(filter);
37
+ if (ev instanceof TerminateEvent) throw "Terminated";
38
+ return ev;
39
+ }
40
+ export function pullEventRawAs<T extends BaseEvent<V>, V extends any[]>(
41
+ type: Constructor<T>,
42
+ filter: string | null = null
43
+ ): T | null {
44
+ const ev = pullEventRaw(filter);
45
+ if (ev instanceof type) return ev as T;
46
+ else return null;
47
+ }
48
+ export function pullEventAs<T extends BaseEvent<V>, V extends any[]>(
49
+ type: Constructor<T>,
50
+ filter: string | null = null
51
+ ): T | null {
52
+ const ev = pullEvent(filter);
53
+ if (ev instanceof type) return ev as T;
54
+ else return null;
55
+ }
package/key.ts ADDED
@@ -0,0 +1,26 @@
1
+ import { eventInitializers, BaseEvent } from ".";
2
+
3
+ export class KeyEvent extends BaseEvent<[number, boolean]> {
4
+ getKey(): number {
5
+ return this.args[0];
6
+ }
7
+
8
+ isHeld(): boolean {
9
+ return this.args[1];
10
+ }
11
+ }
12
+
13
+ export class KeyUpEvent extends BaseEvent<[number]> {
14
+ getKey(): number {
15
+ return this.args[0];
16
+ }
17
+ }
18
+
19
+ eventInitializers.set(
20
+ "key",
21
+ (name: string, args: [number, boolean]) => new KeyEvent(name, args)
22
+ );
23
+ eventInitializers.set(
24
+ "key_up",
25
+ (name: string, args: [number]) => new KeyUpEvent(name, args)
26
+ );
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@siredvin/cc-events",
3
+ "version": "0.1.0",
4
+ "description": "Events library for cc:tweaked",
5
+ "files": [
6
+ "./*.ts"
7
+ ],
8
+ "author": "SirEdvin",
9
+ "license": "MIT",
10
+ "scripts": {
11
+ "test": "echo \"Error: no test specified\" && exit 1",
12
+ "build": "tstl",
13
+ "clean": "rm -f *.lua",
14
+ "lint": "eslint . --ext .ts,.js",
15
+ "depcheck": "depcheck"
16
+ },
17
+ "nx": {
18
+ "targets": {
19
+ "lint": {
20
+ "executor": "@nx/linter:eslint",
21
+ "outputs": [
22
+ "{options.outputFile}"
23
+ ],
24
+ "options": {
25
+ "lintFilePatterns": [
26
+ "packages/corelib/**/*.{ts,tsx,js,jsx}"
27
+ ]
28
+ }
29
+ },
30
+ "depcheck": {
31
+ "executor": "nx:run-commands",
32
+ "options": {
33
+ "command": "depcheck",
34
+ "cwd": "packages/corelib"
35
+ }
36
+ }
37
+ }
38
+ },
39
+ "devDependencies": {
40
+ "@siredvin/cc-types": "*",
41
+ "@siredvin/craftos-types": "*",
42
+ "@jackmacwindows/lua-types": "^2.13.1",
43
+ "typescript-to-lua": "*",
44
+ "typescript": "*"
45
+ }
46
+ }