Skip to content

Instantly share code, notes, and snippets.

@pygy
pygy / cancelPromise.md
Last active May 9, 2024 13:27
You can already cancel ES6 Promises

The gist: by having a Promise adopt the state of a forever pending one, you can suspend its then handlers chain.

Promise.pending = Promise.race.bind(Promise, [])

let cancel

new Promise(function(fulfill, reject) {
  cancel = function() {fulfill(Promise.pending())}
  setTimeout(fulfill, 1000, 5)
// Never forget a stray console.log()
// ----------------------------------
//
// This one is for you printf debuggers
//
// This spies on console.*() calls and reports the call sites
// ordered by file name then by position, all at once at the
// end of the report (while making the suite fail, this counts
// as a failed assertion).
// USAGE
// -----
//
// TODO("Finish this")
// Add this anywhere in your project or test suite,
// it will cause your tests to fail with the message.
//
// This is not meant to ever be commited, but you're sure
// to never lose track of a TODO before pushing your
// changes
@pygy
pygy / lutn.lua
Last active October 12, 2021 08:01
lutn: Load a JSON-like Lua expression, securely. (see also: ltin)
-- Loads a single Lua expression as a JSON-like document, securely.
-- function expressions are prohibited, and the __index of the
-- strings metatable is temporarily stripped or replaced.
-- see also: ltin
-- usage:
-- lutn = require "lutn"
-- result = lutn(source_string [, environment = nil] [, string_metatable_index = nil])
@pygy
pygy / utf-8.lua
Last active March 11, 2021 01:28
UTF-8 encoder/decoder in Lua
local
function utf8_to_codepoint (stream, i)
-- returns the codepoint and the index of the next character in the stream.
local msB = string.byte(stream, i)
local b2, b1, b0
if msB < 128 then return msB, i + 1
elseif msB < 192 then
error("Byte values between 0x80 to 0xBF cannot start a multibyte sequence")
<!doctype html>
<title>ChargingGate </title>
<input type=checkbox id="hasGetBattery"><input type=checkbox id="batteryStatus">
<div id=ua></div>
<hr>
<h1>
#ChargingGate?
</h1>
<p>
Another day, another dystopian browser API...

Keybase proof

I hereby claim:

  • I am pygy on github.
  • I am pygy (https://keybase.io/pygy) on keybase.
  • I have a public key ASAuqI8Te5JdZeup5yQBEEPyoy-24Wl6UwYGMBg8PQ9WvAo

To claim this, I am signing this object:

[vella][1]

Hopefully quick and light view library (benchmarks TBD :-).

S.js tutorial

vella is currently built on top of S.js by Adam Haile. It is more or less Surplus, but with Hyperscript (and composability cranked to eleven). We may end up forking or replacing S, but the core idea will probably persist. Understanding S is required to get what comes next.

Lexicon: in the S docs, streams are called "signals", and dependent streams are called "computations". I'll use the same terminology for now here to make it easier for you if you're also scouring the S docs. They are mostly push streams AFAIU, but the author descrbes them as hybrid push/pull somewhere in the issues, not sure what he meant.

@pygy
pygy / mithril.login_redirect.md
Last active March 23, 2019 16:51
Mithril Login redirect by Barney Carol

Taken from https://gitter.im/lhorie/mithril.js?at=553bdd1763c55be30636a16b in case it disappeared.

@barneycarol said:

[...] FWIW you can overcome a lot of apparent limitations in m.route by reinitialising m.route when things change

@tobscure said:

interesting, examples?

@barneycarol said:

Well I use it when authentication status changes