Skip to content

Instantly share code, notes, and snippets.

@DavidJCobb
DavidJCobb / README.md
Created March 13, 2023 11:38
JavaScript event bubbling: simple example

JavaScript event bubbling example

When JavaScript fires an event, like click, it doesn't just fire it on the event target (i.e. the thing that was clicked). It also fires the event on every ancestor element. This means that an event listener on an ancestor can handle clicks on any of its children.

The code below demonstrates this with a list, where we want to react to the user clicking any list item, and we want our reaction to take into account which list item they clicked. We use a listener on the entire list, and just check e.target to see what list item was clicked.

@DavidJCobb
DavidJCobb / README.md
Last active January 11, 2024 11:02
JavaScript: using a Proxy to create const references to an object (i.e. `const Object&`)

The as_const function returns a Proxy wrapping the passed-in argument. This proxy attempts to prevent all modifications to the underlying object. The difference between this and using Object.freeze is that the latter makes the object itself immutable, while as_const simply prevents direct changes to the object via a specific reference to the object. Additionally, as_const prevents changes to any object that is referenced through the proxy (i.e. as_const(foo).bar.data = 5 is equivalent to as_const(foo.bar).data = 5 and will throw an exception).

In C++ terms, Object.freeze is akin to instantiating a const Object, whereas the result of as_const is a const Object& and the original object can still be modified elsewhere.

This idea is leaky enough that I would recommend against using it. If you need functionality like this, prefer a language that offers it reliably as a built-in feature.

@DavidJCobb
DavidJCobb / regex.js
Created January 23, 2022 21:02
Helper for defining regexes
//
// This file contains a JavaScript tag function to let you
// define regexes without needing to backslash-escape every
// forward slash in the regex text; good for URLs.
//
function regex(tag) {
return new RegExp(tag.raw, "i");
}
@DavidJCobb
DavidJCobb / QPlainTextEdit maxlength implementation.cpp
Last active September 8, 2021 18:49
QPlainTextEdit maxlength implementation
// given:
QPlainTextEdit* widget;
int maxlength;
auto* doc = widget->document();
assert(doc);
QObject::connect(doc, &QTextDocument::contentsChange, this, [maxlength, widget](int pos, int removed, int added) {
if (added - removed <= 0)
return;
if (maxlength < 0)
@DavidJCobb
DavidJCobb / CobbMapHelper.lua
Created July 19, 2019 22:22
ESO Experiment/Rough Draft - Building and Rendering a Heightmap
--
-- This two-file add-on will track your position every tenth of a second
-- (or thirtieth when mounted, if I got the update re-registration right)
-- and maintain a grid of where you've been. Every fifth of a second, it
-- redraws the grid using Texture controls rendered in 3D as gridlines.
--
-- ISSUES:
--
-- - Performance (see below comments)
--
@DavidJCobb
DavidJCobb / TES5Edit-count-persistent-refs.psc
Created April 20, 2019 05:27
Skyrim - xEdit - Count the number of persistent references in all worldspaces in a file
Unit CobbCountPersistents;
Var
giCount: Integer;
Function Initialize: Integer;
Var
eFile: IInterface;
eWorlds: IInterface;
eWorld: IInterface;
@DavidJCobb
DavidJCobb / AHK - Make Windows notifications click-through-able.ahk
Last active August 29, 2020 07:11
Tired of Windows 10 notifications blocking access to toolbars, buttons, and Twitch chat textboxes in the lower-right corner of the screen? Sick of how the darn things block an invisible strip of space above them, too? This should fix that.
;
; Make Windows notifications semitransparent, and allow the user to click through
; all but their rightmost portion (so as to keep the close/dismiss button usable).
;
#Persistent
#SingleInstance
#NoEnv
#Warn
SendMode Input
SetWorkingDir %A_ScriptDir%