69 lines
1.6 KiB
TypeScript
Raw Normal View History

2019-08-14 16:35:24 +03:00
import {JSONEncoder} from "../node_modules/assemblyscript-json/assembly/encoder";
export abstract class Response {
serialize(): string {
unreachable();
return "";
};
}
export class UnknownResponse extends Response {
constructor() {
super();
}
serialize(): string {
let encoder = new JSONEncoder();
encoder.pushObject(null);
encoder.setString("action", "Unknown");
2019-08-15 19:10:13 +03:00
encoder.setString("msg", "cannot username request");
2019-08-14 16:35:24 +03:00
encoder.popObject();
return encoder.toString();
}
}
export class PostResponse extends Response {
2019-08-15 12:50:41 +03:00
msgCount: i32;
constructor(msgCount: i32) {
2019-08-14 16:35:24 +03:00
super();
2019-08-15 12:50:41 +03:00
this.msgCount = msgCount;
2019-08-14 16:35:24 +03:00
}
serialize(): string {
let encoder = new JSONEncoder();
encoder.pushObject(null);
encoder.setString("action", "Post");
2019-08-15 12:50:41 +03:00
encoder.setInteger("count", this.msgCount);
2019-08-14 16:35:24 +03:00
encoder.popObject();
return encoder.toString();
}
}
export class FetchResponse extends Response {
2019-08-15 12:50:41 +03:00
posts: Array<string>;
2019-08-14 16:35:24 +03:00
2019-08-15 12:50:41 +03:00
constructor(posts: Array<string>) {
2019-08-14 16:35:24 +03:00
super();
2019-08-15 12:50:41 +03:00
this.posts = posts;
2019-08-14 16:35:24 +03:00
}
serialize(): string {
let encoder = new JSONEncoder();
encoder.pushObject(null);
encoder.setString("action", "Fetch");
2019-08-15 12:50:41 +03:00
encoder.pushArray("posts");
for (let i = 0; i < this.posts.length; i++) {
let message = this.posts[i];
2019-08-14 16:35:24 +03:00
encoder.pushObject(null);
encoder.setString("msg", message);
encoder.popObject();
}
encoder.popArray();
encoder.popObject();
return encoder.toString();
}
}