Fixed data loading

- fixed multiple loading requests on pending data load
pull/1/head
Trivernis 5 years ago
parent 46733d1046
commit 770c8a35fa

@ -1,5 +1,5 @@
import {EventEmitter} from "events";
import * as crypto from "crypto"; import * as crypto from "crypto";
import {EventEmitter} from "events";
export class MemoryCache extends EventEmitter { export class MemoryCache extends EventEmitter {
private cacheItems: any = {}; private cacheItems: any = {};

@ -1,10 +1,14 @@
/** /**
* abstact DataObject class * abstact DataObject class
*/ */
export abstract class DataObject { import {EventEmitter} from "events";
export abstract class DataObject extends EventEmitter {
protected dataLoaded: boolean = false; protected dataLoaded: boolean = false;
private loadingData: boolean = false;
constructor(public id: number, protected row?: any) { constructor(public id: number, protected row?: any) {
super();
this.id = Number(id); this.id = Number(id);
} }
@ -22,8 +26,15 @@ export abstract class DataObject {
* Loads data from the database if data has not been loaded * Loads data from the database if data has not been loaded
*/ */
protected async loadDataIfNotExists() { protected async loadDataIfNotExists() {
if (!this.dataLoaded) { if (!this.dataLoaded && !this.loadingData) {
this.loadingData = true;
await this.loadData(); await this.loadData();
this.loadingData = false;
this.emit("loaded");
} else if (this.loadingData) {
return new Promise((res) => {
this.on("loaded", () => res());
});
} }
} }
} }

@ -16,6 +16,7 @@ export class Post extends DataObject {
*/ */
public async upvotes(): Promise<number> { public async upvotes(): Promise<number> {
const result = await queryHelper.first({ const result = await queryHelper.first({
cache: true,
text: "SELECT COUNT(*) count FROM votes WHERE item_id = $1 AND vote_type = 'UPVOTE'", text: "SELECT COUNT(*) count FROM votes WHERE item_id = $1 AND vote_type = 'UPVOTE'",
values: [this.id], values: [this.id],
}); });
@ -27,6 +28,7 @@ export class Post extends DataObject {
*/ */
public async downvotes(): Promise<number> { public async downvotes(): Promise<number> {
const result = await queryHelper.first({ const result = await queryHelper.first({
cache: true,
text: "SELECT COUNT(*) count FROM votes WHERE item_id = $1 AND vote_type = 'DOWNVOTE'", text: "SELECT COUNT(*) count FROM votes WHERE item_id = $1 AND vote_type = 'DOWNVOTE'",
values: [this.id], values: [this.id],
}); });
@ -80,6 +82,7 @@ export class Post extends DataObject {
*/ */
public async userVote(userId: number): Promise<dataaccess.VoteType> { public async userVote(userId: number): Promise<dataaccess.VoteType> {
const result = await queryHelper.first({ const result = await queryHelper.first({
cache: true,
text: "SELECT vote_type FROM votes WHERE user_id = $1 AND item_id = $2", text: "SELECT vote_type FROM votes WHERE user_id = $1 AND item_id = $2",
values: [userId, this.id], values: [userId, this.id],
}); });
@ -127,6 +130,7 @@ export class Post extends DataObject {
result = this.row; result = this.row;
} else { } else {
result = await queryHelper.first({ result = await queryHelper.first({
cache: true,
text: "SELECT * FROM posts WHERE posts.id = $1", text: "SELECT * FROM posts WHERE posts.id = $1",
values: [this.id], values: [this.id],
}); });

@ -115,6 +115,7 @@ namespace dataaccess {
* @param type * @param type
*/ */
export async function createPost(content: string, authorId: number, type?: string): Promise<Post> { export async function createPost(content: string, authorId: number, type?: string): Promise<Post> {
type = type || "MISC";
const result = await queryHelper.first({ const result = await queryHelper.first({
text: "INSERT INTO posts (content, author, type) VALUES ($1, $2, $3) RETURNING *", text: "INSERT INTO posts (content, author, type) VALUES ($1, $2, $3) RETURNING *",
values: [content, authorId, type], values: [content, authorId, type],

@ -53,7 +53,7 @@ type Mutation {
sendMessage(chatId: ID!, content: String!): ChatMessage sendMessage(chatId: ID!, content: String!): ChatMessage
"create the post" "create the post"
createPost(content: String!): Boolean createPost(content: String!): Post
"delete the post for a given post id" "delete the post for a given post id"
deletePost(postId: ID!): Boolean deletePost(postId: ID!): Boolean
@ -150,6 +150,9 @@ type Profile implements UserData {
"represents a single user post" "represents a single user post"
type Post { type Post {
"The id of the post."
id: ID!
"the text of the post" "the text of the post"
content: String content: String
@ -166,7 +169,7 @@ type Post {
author: User! author: User!
"date the post was created" "date the post was created"
creationDate: String! createdAt: String!
"the type of vote the user performed on the post" "the type of vote the user performed on the post"
userVote: VoteType userVote: VoteType

Loading…
Cancel
Save