Skip to content

Instantly share code, notes, and snippets.

@lhorie
lhorie / post.md
Last active March 24, 2022 02:02
Friday Fun: Svelte-like variable reactivity in 7LOC with with Proxy

Friday Fun: Svelte-like variable reactivity in 7LOC with with Proxy

Every once in a blue moon, I like to hack up some crazy toy proof of concept to get away from everyday's stiff serious production-ready coding. This morning, I decided to mess around with an idea of implementing something similar to Svelte's reactive variables, but using pure Javascript.

So here's that godawful eye-bleeding fun hack: a 7-line "svelte" (needless to say, it doesn't do nearly enough to be useful in the real world and breaks just about every best practice rule in the book because why the hell not)

https://codepen.io/lhorie/pen/BaRzgRe

Can you figure out why this works? Any ideas to make it more devilish/clever/insane are welcome :)

@idudinov
idudinov / prerenderPlugin.js
Last active June 16, 2021 14:56
PrerenderPlugin for HtmlWebpackPlugin – Pre-renders html during Webpack build phase
const HtmlWebpackPlugin = require('html-webpack-plugin');
const jsdom = require('jsdom');
/** @typedef {import("webpack/lib/Compiler.js")} WebpackCompiler */
/** @typedef {import("webpack/lib/Compilation.js")} WebpackCompilation */
/** @typedef {(import 'jsdom').ResourceLoaderConstructorOptions} ResourceLoaderConstructorOptions */
class PrerenderHtmlPlugin {
constructor(options) {
this._options = options || { };
@sebmarkbage
sebmarkbage / Infrastructure.js
Last active March 14, 2024 17:40
SynchronousAsync.js
let cache = new Map();
let pending = new Map();
function fetchTextSync(url) {
if (cache.has(url)) {
return cache.get(url);
}
if (pending.has(url)) {
throw pending.get(url);
}
@tcnksm
tcnksm / NOTE.md
Last active April 13, 2024 21:28
Small note of gRPC Best Practice @ CoreOSFest 2017
PDO::FETCH_FUNC without validation
10: 0.001568078994751
0.00071001052856445
0.00085306167602539
0.00087618827819824
0.00083112716674805
100: 0.0012631416320801
0.0014598369598389
@CMCDragonkai
CMCDragonkai / flock_timeout.php
Last active March 3, 2022 21:24
PHP: Simulating Lock Timeout with PHP's Flock
<?php
/**
* Acquires a lock using flock, provide it a file stream, the
* lock type, a timeout in microseconds, and a sleep_by in microseconds.
* PHP's flock does not currently have a timeout or queuing mechanism.
* So we have to hack a optimistic method of continuously sleeping
* and retrying to acquire the lock until we reach a timeout.
* Doing this in microseconds is a good idea, as seconds are too
* granular and can allow a new thread to cheat the queue.
<?php
class ConfigController {
const MODEL = 'App\Models\Config';
// see RestControllerTrait. sends less data on indexing the list of the model
// protected $indexFields = [ 'name' ];
protected $triggers = [
// 'show' => 'extendObject',
// 'update' => 'dataFormatFix',
^([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))$
@JamesMessinger
JamesMessinger / circular-refs.js
Created July 16, 2015 03:31
JSON Schema Circular $Refs
{
"definitions": {
"thing": {
"$ref": "#/definitions/thing" // circular reference to self
},
"person": {
"properties": {
"name": {
"type": "string"
},