Skip to content

Instantly share code, notes, and snippets.

View cdoremus's full-sized avatar

Craig Doremus cdoremus

View GitHub Profile
@marvinhagemeister
marvinhagemeister / web-components-render-to-string.tsx
Last active May 28, 2023 01:53
Preact SSR collect web components
import { options, VNode } from 'preact';
import { renderToString } from 'preact-render-to-string';
// List of web components that were rendered
let webComponents = new Set<string>();
// Called when a vnode is rendered
let oldDiffedHook = options.diffed;
options.diffed = (vnode: VNode) => {
if (typeof vnode.type === 'string' && vnode.type.includes('-')) {
webComponents.add(vnode.type);
const minute = 60;
const hour = minute * 60;
const day = hour * 24;
const week = day * 7;
const month = day * 30;
const year = day * 365;
/**
* Convert a date to a relative time string, such as
* "a minute ago", "in 2 hours", "yesterday", "3 months ago", etc.
ngOnInit() {
this.filteredRoutes$ = from(this.routes).pipe(
concatMap((route) => {
// handle if nothing is in canActivate
if (!route.canActivate || !route.canActivate.length) {
// create an object that has the route and result for filtering
return of({ route, result: true });
}
return from(route.canActivate).pipe(
@remarkablemark
remarkablemark / webdriverjs_localstorage.js
Created June 28, 2016 19:18
Get and set localStorage with WebDriverJS `executeScript` and `executeAsyncScript`.
'use strict';
/**
* Module dependencies.
*/
const webdriver = require('selenium-webdriver');
/**
* Build driver.
*/
@markerikson
markerikson / dispatching-action-creators.js
Last active June 28, 2024 05:29
Dispatching action creators comparison
// approach 1: define action object in the component
this.props.dispatch({
type : "EDIT_ITEM_ATTRIBUTES",
payload : {
item : {itemID, itemType},
newAttributes : newValue,
}
});
// approach 2: use an action creator function
import {action1, action2} from "myActions";
import {bindActionCreators} from "redux";
const mapDispatchToProps(dispatch) => {
return {
manuallyBoundAction : (...args) => dispatch(action1(...args)),
autoBoundAction : bindActionCreators(action2, dispatch),
multipleActionsTogether : bindActionCreators({action1, action2}, dispatch)
}
};
@ficusk
ficusk / GsonRequest.java
Last active January 6, 2025 22:43
A Volley adapter for JSON requests that will be parsed into Java objects by Gson.
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;