Skip to content

Instantly share code, notes, and snippets.

View tlcheah2's full-sized avatar
💭
Keep thinking and writing code

Tek Loon tlcheah2

💭
Keep thinking and writing code
View GitHub Profile
@tlcheah2
tlcheah2 / virtualList-bug.js
Last active August 13, 2017 06:59 — forked from gastonmorixe/virtualList-bug.js
RN 0.47.1 VirtualizedList infinite rerender Bug
import React, { PureComponent } from 'react'
import {
View,
Text,
VirtualizedList
} from 'react-native'
class DataSource {
getElementAtIndex (index) {
return { key: index }
@tlcheah2
tlcheah2 / login-component.spec.ts
Last active August 19, 2019 00:44
Login Component Isolated Test
const loginServiceSpy = jasmine.createSpyObj('LoginService', ['login']);
describe('Login Component Isolated Test', () => {
let component: LoginComponent;
beforeEach(async(() => {
component = new LoginComponent(routerSpy, new FormBuilder(), loginServiceSpy);
}));
function updateForm(userEmail, userPassword) {
@tlcheah2
tlcheah2 / login-component.spec.ts
Last active August 19, 2019 00:44
Login Component Shallow Test
const loginServiceSpy = jasmine.createSpyObj('LoginService', ['login']);
describe('Login Component Shallow Test', () => {
let fixture: ComponentFixture<LoginComponent>;
function updateForm(userEmail, userPassword) {
fixture.componentInstance.loginForm.controls['username'].setValue(userEmail);
fixture.componentInstance.loginForm.controls['password'].setValue(userPassword);
}
@tlcheah2
tlcheah2 / login-component.spec.ts
Last active August 19, 2019 00:42
Login Component Integrated Test
const loginServiceSpy = jasmine.createSpyObj('LoginService', ['login']);
const testUserData = { id: 1, name: 'TekLoon'};
describe('Login Component Integrated Test', () => {
let fixture: ComponentFixture<LoginComponent>;
let loginSpy;
function updateForm(userEmail, userPassword) {
fixture.componentInstance.loginForm.controls['username'].setValue(userEmail);
fixture.componentInstance.loginForm.controls['password'].setValue(userPassword);
@tlcheah2
tlcheah2 / index.js
Created July 24, 2019 13:37
TimerService
// This is timer producer
const INTERVAL_DURATION = 60000;
const url = process.env.CLOUDAMQP_URL || "amqp://localhost";
const amqp = require('amqplib');
exports.startTimer = () => {
amqp.connect(url).then((connection) => {
connection.createChannel().then((channel) => {
@tlcheah2
tlcheah2 / scrapQuoteConsumer.js
Last active July 24, 2019 14:44
Scrap Quote Consumer
const url = process.env.CLOUDAMQP_URL || "amqp://localhost";
const amqp = require('amqplib');
const { scrapQuoteOfTheDay } = require('../controllers/quoteController');
exports.start = () => {
amqp.connect(url).then((connection) => {
connection.createChannel().then((channel) => {
const exchange = 'quote';
channel.assertExchange(exchange, 'direct', {durable: false});
channel.assertQueue('', { exclusive: true })
@tlcheah2
tlcheah2 / quoteController.js
Created July 24, 2019 14:59
Quote Controller
const rp = require('request-promise');
const $ = require('cheerio');
const fs = require('fs');
const path = require('path');
const quotesFilePath = path.join(process.cwd(), 'public', 'quotes.json');
const url = 'http://wisdomquotes.com/stoic-quotes/';
exports.scrapQuoteOfTheDay = () => {
let qotdUrl = 'http://wisdomquotes.com/?time=' + (Date.now());
rp(qotdUrl).then((html) => {
@tlcheah2
tlcheah2 / index.pug
Created July 24, 2019 15:13
Quote HTML Pug Template
h1 Quote of The Day
h3 List of Quotes that we crawled
p Refresh every minute and you will saw a new quote
ul
- for (var x = 0; x < quotes.length; x++)
li #{quotes[x]}
@tlcheah2
tlcheah2 / server.js
Created July 24, 2019 15:25
Quote Service Express settings
// Import env file
require('dotenv').config();
const express = require('express');
const path = require('path');
const fs = require('fs');
const app = express();
const { keepAwake } = require('./src/util/heroku_util');
const { scrapQuoteOfTheDay } = require('./src/controllers/quoteController');
const { start } = require('./src/consumers/scrapQuoteConsumer');
@tlcheah2
tlcheah2 / index.pug
Last active August 11, 2019 11:26
This pug file show how to write script within pug template and how to connect event stream connection with backend
h1 Quote of The Day
h3 List of Quotes that we crawled
p Refresh every minute and you will saw a new quote
ul#quoteList
script.
let quotes = [];
var eventSource = new EventSource('quoteEvent');
// Listen to New Quote Event
eventSource.addEventListener('newQuote', function (e) {