Skip to content

Commit

Permalink
Various fixes, bumped version
Browse files Browse the repository at this point in the history
  • Loading branch information
MCJack123 committed Mar 16, 2022
1 parent 3fc36a3 commit 37fbb09
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 72 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,21 @@ A library for handling events in a nicer way (e.g. using named properties rather

Example:
```ts
const timer = os.startTimer(5)
import * as event from "./event";

const timer = os.startTimer(5);
while (true) {
const ev = event.pullEventAs<event.TimerEvent>("timer");
const ev = event.pullEventAs(event.TimerEvent, "timer");
if (ev.id == timer) break;
}
```

All types are included in the compiled output, even if they were never used. To avoid this, comment out the event class declarations you don't need, and remove the init functions from `eventInitializers`. Do not remove `GenericEvent`, as this is the fallback event type when other types aren't available.

## `tsconfig.json` Options
The `tsconfig.json` file contains some options used by TypeScriptToLua to adjust how Lua code is generated. Some of these may be useful for CC development. See [the TSTL webpage](https://typescripttolua.github.io/docs/configuration) for the rest of the options.

* `luaTarget`: Sets the version of Lua the compiler should target. This affects things like bitwise operators and `continue` support. CC has a feature set mixed between multiple versions; it uses 5.1 as a base language but supports many of 5.2 and 5.3's library features, including `bit32`. By default, TSTL will not compile bitwise operators in 5.1 mode even though CC supports it, but using 5.2 will enable `continue`, which is not supported in CC. I have chosen a middle ground of LuaJIT, but if you have issues you may change this to another version, depending on what you need.
* `noImplicitSelf`: Controls whether functions have a `this`/`self`. By default, all functions are given a `self` parameter (even ones not in tables!) to allow JavaScript's `this` value to work. This can be disabled per-function with `/** @noSelf **/` or per-file with `/** @noSelfInFile **/`; but if you don't use `this` or don't want to have `self` added to functions, you can set this option to `true` to disable `this`/`self`.
* `luaLibImport`: Controls how TypeScript polyfills are emitted in the Lua code. The following options are available:
* `inline`: Inserts the only required boilerplate code in each file. This is the default, and is recommended for projects with few files. However, this may generate duplicate code in projects with lots of files.
Expand Down
32 changes: 30 additions & 2 deletions event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,31 @@ export class TurtleInventoryEvent implements IEvent {
return ev;
}
}

class SpeakerAudioEmptyEvent implements IEvent {
public side: string = "";
public get_name() {return "speaker_audio_empty";}
public get_args() {return [this.side];}
public static init(args: any[]): IEvent | null {
if (!(typeof args[0] === "string") || (args[0] as string) != "speaker_audio_empty") return null;
let ev: SpeakerAudioEmptyEvent;
ev.side = args[1] as string;
return ev;
}
}

class ComputerCommandEvent implements IEvent {
public args: string[] = [];
public get_name() {return "computer_command";}
public get_args() {return this.args;}
public static init(args: any[]): IEvent | null {
if (!(typeof args[0] === "string") || (args[0] as string) != "computer_command") return null;
let ev: ComputerCommandEvent;
ev.args = args.slice(1);
return ev;
}
}

/*
class Event implements IEvent {
Expand Down Expand Up @@ -318,9 +343,12 @@ let eventInitializers: ((args: any[]) => IEvent | null)[] = [
MouseEvent.init,
ResizeEvent.init,
TurtleInventoryEvent.init,
SpeakerAudioEmptyEvent.init,
ComputerCommandEvent.init,
GenericEvent.init
];

type Constructor<T extends {} = {}> = new (...args: any[]) => T;
export function pullEventRaw(filter: string | null = null): IEvent | null {
let args: any[] = table.pack(coroutine.yield(filter));
for (let init of eventInitializers) {
Expand All @@ -334,12 +362,12 @@ export function pullEvent(filter: string | null = null): IEvent | null {
if (ev instanceof TerminateEvent) throw "Terminated";
return ev;
}
export function pullEventRawAs<T extends IEvent>(type: () => T, filter: string | null = null): T | null {
export function pullEventRawAs<T extends IEvent>(type: Constructor<T>, filter: string | null = null): T | null {
let ev = pullEventRaw(filter);
if ((ev instanceof type)) return ev as T;
else return null;
}
export function pullEventAs<T extends IEvent>(type: () => T, filter: string | null = null): T | null {
export function pullEventAs<T extends IEvent>(type: Constructor<T>, filter: string | null = null): T | null {
let ev = pullEvent(filter);
if ((ev instanceof type)) return ev as T;
else return null;
Expand Down
121 changes: 75 additions & 46 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "cc-tstl-template",
"version": "1.100.1",
"version": "1.100.3",
"description": "Template project for ComputerCraft programs written in TypeScript.",
"main": "index.js",
"main": "main.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tstl"
},
"dependencies": {
"devDependencies": {
"typescript": "^4.5.0",
"lua-types": "^2.8.0",
"typescript-to-lua": "^1.3.0"
},
Expand Down
Loading

0 comments on commit 37fbb09

Please sign in to comment.