Skip to content

Instantly share code, notes, and snippets.

@JSila
JSila / main.rs
Last active Jul 21, 2020
Rust: example of using channels to communicate between threads (mutiple producers, single consumer)
View main.rs
use std::sync::mpsc::channel;
use chrono::{DateTime, Local};
use threadpool::ThreadPool;
#[derive(Debug)]
struct Site {
id: String,
url: String,
changed: Option<bool>,
@JSila
JSila / main.rs
Last active Jul 21, 2020
Rust: updating an array field of mutable struct in a seperate function
View main.rs
use std::env;
use std::fs::File;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
struct Site {
id: String,
#[serde(skip_serializing_if = "Option::is_none")]
status_changed: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
@JSila
JSila / router.js
Created Apr 23, 2018
custom router solution for react (experiment)
View router.js
import React, {Component, createElement} from 'react'
import createHistory from 'history/createBrowserHistory'
import classnames from "classnames"
export const history = createHistory()
/**
* Determines if route path matches history location's pathname. If it does, extracts its params. If not, returns null.
*
* @param routePath
@JSila
JSila / withForm-1.js
Last active Apr 30, 2018
withForm higher-order component
View withForm-1.js
import React from "react"
const noop = () => false
const withForm = (fields, validateFn = noop, validSubmitFn) => WrappedComponent => {
class WithForm extends React.Component {
constructor(props) {
super(props)
this.state = {
@JSila
JSila / index.js
Created Mar 4, 2018
Helpers for providing and consuming multiple react contexts
View index.js
import React, {Component} from 'react'
import ReactDOM from 'react-dom'
import App from './App'
import createContext from 'create-react-context'
const Theme = createContext("light")
const Language = createContext("en")
const Whatever = createContext("whatever")
@JSila
JSila / client.go
Last active Oct 4, 2017
Context cancelation example in Go
View client.go
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"net/http"
"time"
)
@JSila
JSila / middleware.go
Last active Jul 4, 2017
JSON schema middleware and valide helpers for Go
View middleware.go
import (
"context"
"net/http"
"github.com/Sirupsen/logrus"
"github.com/matryer/respond"
)
func CheckJSON(log *logrus.Logger, h http.Handler, data interface{}, contextKey string) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@JSila
JSila / WP_Settings.php
Last active Sep 2, 2019
WP Settings API in OOP way
View WP_Settings.php
<?php
class WP_Settings_Section {
protected $id = '';
protected $title = '';
protected $output_callback = '';
protected $fields = array();
public function __construct($id, $title, $output_callback) {
$this->id = $id;
@JSila
JSila / unsbscribe.js
Last active Jan 28, 2017
higher order function for react that auto-unsubscribes from RxJS Observables on componentWillUnmount
View unsbscribe.js
import React, {Component} from 'react'
import forEach from 'lodash/forEach'
export default () => ComposedComponent => class extends Component {
subscriptions = {}
componentWillUnmount() {
forEach(this.subscriptions, subscription => {
subscription.unsubscribe()
})
}
@JSila
JSila / App.js
Created Jan 12, 2017
Example of showing/hiding Twitter Bootstrap (4) modal - with react, not jquery
View App.js
import React, { Component } from 'react'
import Modal from './Modal'
export default class extends Component {
constructor(props) {
super(props)
this.state = {
isOpen: false