Skip to content

Commit 1c0791d

Browse files
committed
Changed example
1 parent b26ac7e commit 1c0791d

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

src/index.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,33 @@ document.addEventListener('DOMContentLoaded', () => {
88
const container = document.getElementById('example-ts');
99
const exampleLua = document.getElementById('example-lua');
1010

11-
let example = `class Greeter {
12-
greeting: string;
13-
constructor(message: string) {
14-
this.greeting = message;
15-
}
16-
greet() {
17-
return "Hello, " + this.greeting;
18-
}
19-
}
11+
let example = `// Declare exposed API
12+
type Vector = [number, number, number];
2013
21-
let greeter = new Greeter("world");
14+
declare interface OnSpellStartEvent {
15+
caster: Unit;
16+
targetLocation: Vector;
17+
}
2218
23-
let button = document.createElement('button');
24-
button.textContent = "Say Hello";
25-
button.onclick = function() {
26-
alert(greeter.greet());
19+
declare class Unit {
20+
getLevel(): number;
21+
isEnemy(other: Unit): boolean;
22+
kill(): void;
2723
}
2824
29-
document.body.appendChild(button);
25+
declare function print(...messages: any[]): void;
26+
declare function FindUnitsInRadius(location: Vector, radius: number): Unit[];
27+
28+
// Use declared API in code
29+
function onSpellStart(event: OnSpellStartEvent): void {
30+
const units = FindUnitsInRadius(event.targetLocation, 500);
31+
const enemies = units.filter(unit => event.caster.isEnemy(unit));
32+
33+
for (const unit of enemies) {
34+
print(unit, unit.getLevel());
35+
unit.kill();
36+
}
37+
}
3038
`;
3139

3240
var queryStringSrcStart = window.location.hash.indexOf("#src=");
@@ -67,7 +75,7 @@ document.addEventListener('DOMContentLoaded', () => {
6775
// wait one second before submitting work
6876
timerVar = setTimeout(() => {
6977
tstlWorker.postMessage({tsStr: tsEditor.getValue()});
70-
window.location.hash = "#src=" + encodeURIComponent(tsEditor.getValue());
78+
window.location.replace("#src=" + encodeURIComponent(tsEditor.getValue()));
7179
ignoreHashChange = true;
7280
}, 500);
7381
}))

0 commit comments

Comments
 (0)