mirror of
https://github.com/fluencelabs/fluid
synced 2025-06-17 15:21:20 +00:00
refactoring
This commit is contained in:
@ -1,9 +1,8 @@
|
||||
import {Action, decode, PostRequest} from "./request";
|
||||
import {PostResponse, FetchResponse, UnknownResponse} from "./response";
|
||||
import {Action, decode, FetchRequest, PostRequest} from "./request";
|
||||
import {PostResponse, UnknownResponse} from "./response";
|
||||
import {addMessage, createScheme, getMessages, getPostsCount} from "./model";
|
||||
|
||||
let messages = new Array<string>();
|
||||
messages.push("hello");
|
||||
messages.push("hi!");
|
||||
createScheme();
|
||||
|
||||
// main handler for an application
|
||||
export function handler(input: string): string {
|
||||
@ -12,11 +11,16 @@ export function handler(input: string): string {
|
||||
|
||||
if (request.action == Action.Post) {
|
||||
let post = request as PostRequest;
|
||||
let response = new PostResponse(0);
|
||||
addMessage(post.msg, post.username);
|
||||
let count = getPostsCount();
|
||||
|
||||
let response = new PostResponse(count);
|
||||
return response.serialize()
|
||||
} else if (request.action == Action.Fetch) {
|
||||
let response = new FetchResponse(messages);
|
||||
return response.serialize()
|
||||
let fetch = request as FetchRequest;
|
||||
|
||||
let result = getMessages(fetch.username);
|
||||
return result;
|
||||
}
|
||||
|
||||
let response = new UnknownResponse();
|
||||
|
16
backend-assemblyscript/step1-json-api/assembly/model.ts
Normal file
16
backend-assemblyscript/step1-json-api/assembly/model.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import {log} from "../node_modules/assemblyscript-sdk/assembly/logger";
|
||||
|
||||
export function createScheme(): void {
|
||||
log("create scheme");
|
||||
}
|
||||
export function addMessage(msg: string, username: string): void {
|
||||
log("add message");
|
||||
}
|
||||
export function getMessages(username: string | null): string {
|
||||
log("get messages");
|
||||
return "[]";
|
||||
}
|
||||
export function getPostsCount(): u32 {
|
||||
log("get posts count");
|
||||
return 0;
|
||||
}
|
@ -4,6 +4,7 @@ export enum Action {
|
||||
Post,
|
||||
Fetch,
|
||||
Unknown
|
||||
// Error
|
||||
}
|
||||
|
||||
export abstract class Request {
|
||||
@ -24,9 +25,12 @@ export class PostRequest extends Request {
|
||||
}
|
||||
|
||||
export class FetchRequest extends Request {
|
||||
constructor() {
|
||||
public readonly username: string | null;
|
||||
|
||||
constructor(username: string | null) {
|
||||
super();
|
||||
this.action = Action.Fetch;
|
||||
this.username = username;
|
||||
}
|
||||
}
|
||||
|
||||
@ -37,7 +41,7 @@ export class UnknownRequest extends Request {
|
||||
}
|
||||
}
|
||||
|
||||
function string2Bytes(str: string): Uint8Array {
|
||||
export function string2Bytes(str: string): Uint8Array {
|
||||
return Uint8Array.wrap(String.UTF8.encode(str));
|
||||
}
|
||||
|
||||
@ -54,7 +58,7 @@ export function decode(input: string): Request {
|
||||
let request: Request;
|
||||
|
||||
if (action == "Fetch") {
|
||||
request = new FetchRequest();
|
||||
request = new FetchRequest(jsonHandler.filter_handle);
|
||||
} else if (action == "Post") {
|
||||
request = new PostRequest(jsonHandler.msg, jsonHandler.username)
|
||||
} else {
|
||||
@ -69,6 +73,7 @@ class RequestJSONEventsHandler extends JSONHandler {
|
||||
public action: string;
|
||||
public msg: string;
|
||||
public username: string;
|
||||
public filter_handle: string | null;
|
||||
|
||||
setString(name: string, value: string): void {
|
||||
|
||||
@ -78,6 +83,7 @@ class RequestJSONEventsHandler extends JSONHandler {
|
||||
this.msg = value;
|
||||
} else if (name == "username") {
|
||||
this.username = value;
|
||||
this.filter_handle = value;
|
||||
}
|
||||
// json scheme is not strict, so we won't throw an error on excess fields
|
||||
}
|
||||
|
@ -7,6 +7,16 @@ export abstract class Response {
|
||||
};
|
||||
}
|
||||
|
||||
export class Message {
|
||||
msg: string;
|
||||
username: string;
|
||||
|
||||
constructor(msg: string, username: string) {
|
||||
this.msg = msg;
|
||||
this.username = username;
|
||||
}
|
||||
}
|
||||
|
||||
export class UnknownResponse extends Response {
|
||||
constructor() {
|
||||
super();
|
||||
@ -24,8 +34,8 @@ export class UnknownResponse extends Response {
|
||||
}
|
||||
|
||||
export class PostResponse extends Response {
|
||||
msgCount: i32;
|
||||
constructor(msgCount: i32) {
|
||||
msgCount: u32;
|
||||
constructor(msgCount: u32) {
|
||||
super();
|
||||
this.msgCount = msgCount;
|
||||
}
|
||||
@ -42,9 +52,9 @@ export class PostResponse extends Response {
|
||||
}
|
||||
|
||||
export class FetchResponse extends Response {
|
||||
posts: Array<string>;
|
||||
posts: Array<Message>;
|
||||
|
||||
constructor(posts: Array<string>) {
|
||||
constructor(posts: Array<Message>) {
|
||||
super();
|
||||
this.posts = posts;
|
||||
}
|
||||
@ -55,9 +65,10 @@ export class FetchResponse extends Response {
|
||||
encoder.setString("action", "Fetch");
|
||||
encoder.pushArray("posts");
|
||||
for (let i = 0; i < this.posts.length; i++) {
|
||||
let message = this.posts[i];
|
||||
let twit = this.posts[i];
|
||||
encoder.pushObject(null);
|
||||
encoder.setString("msg", message);
|
||||
encoder.setString("msg", twit.msg);
|
||||
encoder.setString("username", twit.username);
|
||||
encoder.popObject();
|
||||
}
|
||||
encoder.popArray();
|
||||
|
Reference in New Issue
Block a user