Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@seanlinsley
seanlinsley / iterable_weak_set.js
Last active August 2, 2023 03:58
Iterable WeakSet in JavaScript
// spec: https://github.com/tc39/proposal-weakrefs
// the spec contains an [iterable WeakMap implementation](https://github.com/tc39/proposal-weakrefs#iterable-weakmaps)
// NOTE: this WeakSet implementation is incomplete, only does what I needed
// In Firefox Nightly, visit about:config and enable javascript.options.experimental.weakrefs
class IterableWeakSet extends Set {
add(el) {
super.add(new WeakRef(el))
}
forEach(fn) {
@seanlinsley
seanlinsley / malloc_trim.md
Created June 25, 2023 18:18
Prevent memory bloat on Linux with malloc_trim(0)

The default allocator on Linux has a tendency to free unused memory less aggressively than Mac and Windows. This creates the appearance of memory bloat. Calling malloc_trim(0) may not prevent out of memory events of that single process, but it helps you see the real memory usage, and frees up memory for use by other processes.

Relevant links:

// sourced from https://github.com/golang/go/issues/13271, with additions at the bottom of the file
package main
import (
"testing"
//
"reflect"
"unsafe"
@seanlinsley
seanlinsley / example_usage.js.erb
Last active July 10, 2021 03:13
Handlebars + ERB
Backbone.View.extend({
template: Handlebars.compile(
<%= embed('template.hbs').inspect %>
// some .hbs.erb files are also loaded
),
// ...
})
@seanlinsley
seanlinsley / seeyouspacecowboy.rb
Last active August 30, 2020 23:46
A Ruby verison, because the ANSI colors weren't showing up on logout in the Shell version this is forked from
#!/usr/bin/env ruby
# SEE YOU SPACE COWBOY by DANIEL REHN (danielrehn.com)
# Displays a timeless message in your terminal with cosmic color effects
# Usage: add `ruby ~/seeyouspacecowboy.rb; sleep 2` to .bash_logout (or similar) in your home directory
# (adjust the sleep variable to display the message for more seconds)
class String
def colorize(color_code)
@seanlinsley
seanlinsley / chat_controller.rb
Last active August 26, 2020 01:13
Websockets with Postgres listen/notify and the Tubesock gem
require 'pg_notify'
class ChatController < ApplicationController
include Tubesock::Hijack
@@notifier = PGNotify.new 'chat'
def chat
hijack do |websocket|
websocket.onopen do
@@notifier.subscribe websocket do |payload|
# The Active Admin equivalent of putting this in your application layout:
# <head>
# <%= cloudinary_js_config %>
# </head>
module ActiveAdmin
module Views
module Pages
class Base < Arbre::HTML::Document
@seanlinsley
seanlinsley / gist:2038003
Last active June 7, 2019 08:00
A better form to edit multiple child records. Created for https://github.com/gregbell/active_admin/issues/1097
<%= semantic_form_for @parent do |a| %>
<%= a.inputs "Family Details" do %>
<%= a.input :name %>
<%= a.input :user %>
<%= a.input :region %>
<% end %>
<%= a.inputs "Children" do %>
<table>
@seanlinsley
seanlinsley / chromedriver.rb
Created March 14, 2019 00:55
Ensures that chromedriver always matches the version of Chrome that's currently installed
# https://github.com/flavorjones/chromedriver-helper/issues/79
# http://chromedriver.storage.googleapis.com/index.html
require 'open-uri'
require 'shellwords'
executable = if RUBY_PLATFORM =~ /darwin/
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
else
'google-chrome'
@seanlinsley
seanlinsley / active_admin.scss
Last active April 18, 2018 19:33
Make Active Admin's primary color change between dev/staging/production
// SASS variable overrides must be declared before loading up Active Admin's styles.
//
// To view the variables that Active Admin provides, take a look at
// `app/assets/stylesheets/active_admin/mixins/_variables.css.scss` in the
// Active Admin source.
//
// For example, to change the sidebar width:
// $sidebar-width: 242px;
$primary-color: dynamic_active_admin_color();