2019-08-16 12:00:50 +03:00

69 lines
1.6 KiB
TypeScript

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");
encoder.setString("msg", "cannot username request");
encoder.popObject();
return encoder.toString();
}
}
export class PostResponse extends Response {
msgCount: i32;
constructor(msgCount: i32) {
super();
this.msgCount = msgCount;
}
serialize(): string {
let encoder = new JSONEncoder();
encoder.pushObject(null);
encoder.setString("action", "Post");
encoder.setInteger("count", this.msgCount);
encoder.popObject();
return encoder.toString();
}
}
export class FetchResponse extends Response {
posts: Array<string>;
constructor(posts: Array<string>) {
super();
this.posts = posts;
}
serialize(): string {
let encoder = new JSONEncoder();
encoder.pushObject(null);
encoder.setString("action", "Fetch");
encoder.pushArray("posts");
for (let i = 0; i < this.posts.length; i++) {
let message = this.posts[i];
encoder.pushObject(null);
encoder.setString("msg", message);
encoder.popObject();
}
encoder.popArray();
encoder.popObject();
return encoder.toString();
}
}