Skip to content

Instantly share code, notes, and snippets.

TSH code sharing tsh-code

Block or report user

Report or block tsh-code

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View handler.js
class JoinGameHandler {
constructor(games, messagesToSocketStream$) {
this.games = games;
this.messagesToSocketStream$ = messagesToSocketStream$
}
async handle(command) {
// BUSINESS LOGIC HERE
this.messagesToSocketStream$.next({
View command-bus.js
class CommandBus {
constructor(handlers) {
this.handlers = handlers;
}
handle(command) {
if (!this.handlers[command.type]) {
throw new Error("No handler for command");
}
View websocket-handler-with-command.js
io.on("connection", socket => {
socket.on("message", message => {
commandBus.handle({
...message,
socketId: socket.id
})
});
});
@tsh-code
tsh-code / websocket-handler.js
Created Nov 18, 2019
Typical WebSocket handler
View websocket-handler.js
io.on("connection", socket => {
socket.on("message", message => {
if (message.method === "JOIN_GAME") {
// DO SOMETHING
}
if (message.method === "LEAVE_GAME") {
// DO SOMETHING ELSE
}
})
})
@tsh-code
tsh-code / rest-endpoint.js
Created Nov 18, 2019
Typical REST endpoint
View rest-endpoint.js
router.post('/users', (req, res) => {
const users = [];
res
.status(200)
.json(users)
})
View plain.ts
const observer = new IntersectionObserverr(onCallbackIntersectionObserver, {
threshold: 0,
// Begin intersecting 25% of the viewport height before it appears to reduce chance of displaying
// unloaded content
rootMargin: '0px 0px 25% 0px',
});
function onCallbackIntersectionObserver(entries: IntersectionObserverEntry[]) {
// Ignore additional entries since there's only one.
// Add the logic if you plan to handle multiple entries per intersection observer instance
View LazyImage.tsx
import React, { Component, ReactNode, ImgHTMLAttributes } from 'react';
import { findDOMNode } from 'react-dom';
interface LazyImageComponentPropsInterface extends ImgHTMLAttributes<HTMLImageElement> {
onChangeIntersection?: (isIntersecting: boolean) => void;
placeholder?: string;
}
interface LazyImageComponentStateInterface {
isLoadable: boolean;
@tsh-code
tsh-code / symfony-mailer-complex.php
Created Oct 28, 2019
Symfony Mailer complex example
View symfony-mailer-complex.php
<?php
declare(strict_types=1);
require_once __DIR__ . '/../vendor/autoload.php';
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\NamedAddress;
@tsh-code
tsh-code / symfony-mailer-basic.php
Created Oct 28, 2019
Symfony Mailer basic example
View symfony-mailer-basic.php
<?php
declare(strict_types=1);
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\NamedAddress;
$transport = new EsmtpTransport('192.168.99.100', 5025);
$mailer = new Mailer($transport);
@tsh-code
tsh-code / zend-mail-complex.php
Created Oct 28, 2019
Zend Mail complex example
View zend-mail-complex.php
<?php
declare(strict_types=1);
use Zend\Mail;
use Zend\Mime\Message as MimeMessage;
use Zend\Mime\Mime;
use Zend\Mime\Part as MimePart;
$transport = new Mail\Transport\Smtp();
$options = new Mail\Transport\SmtpOptions([
You can’t perform that action at this time.