Skip to content

Instantly share code, notes, and snippets.

@JSila
JSila / main.rs
Created July 17, 2023 17:43
deno_core example in rust
use deno_core::v8::{self};
use deno_core::{JsRuntime, RuntimeOptions};
fn main() {
let script1 = std::fs::read_to_string("script1.js").unwrap();
let script2 = std::fs::read_to_string("script2.js").unwrap();
let mut runtime = JsRuntime::new(RuntimeOptions::default());
let _ = runtime.execute_script("script1", script1.into()).unwrap();
@JSila
JSila / main.rs
Last active July 21, 2020 07:51
Rust: example of using channels to communicate between threads (mutiple producers, single consumer)
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 July 21, 2020 07:49
Rust: updating an array field of mutable struct in a seperate function
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 April 23, 2018 14:10
custom router solution for react (experiment)
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 April 30, 2018 09:59
withForm higher-order component
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 March 4, 2018 13:28
Helpers for providing and consuming multiple react contexts
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 October 4, 2017 16:58
Context cancelation example in Go
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"net/http"
"time"
)
@JSila
JSila / middleware.go
Last active July 4, 2017 09:27
JSON schema middleware and valide helpers for 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 September 2, 2019 21:47
WP Settings API in OOP way
<?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 January 28, 2017 13:20
higher order function for react that auto-unsubscribes from RxJS Observables on componentWillUnmount
import React, {Component} from 'react'
import forEach from 'lodash/forEach'
export default () => ComposedComponent => class extends Component {
subscriptions = {}
componentWillUnmount() {
forEach(this.subscriptions, subscription => {
subscription.unsubscribe()
})
}