Skip to content

Instantly share code, notes, and snippets.

View loilo's full-sized avatar

Florian Reuschel loilo

View GitHub Profile
@loilo
loilo / deepAsync.js
Last active May 29, 2019 12:17
Deeply resolves all promises in a data structure
// Arbitrarily nested object containing Promises goes in
// Plain data structure comes out
async function deepAsync (object) {
// Walk arrays
if (Array.isArray(object)) {
return Promise.all(object.map(async item => await deepAsync(item)))
// Walk objects
} else if (typeof object === 'object' && String(object) === '[object Object]') {
return Object
@loilo
loilo / bubble.md
Last active April 27, 2023 00:21
Make Vue events bubble

Make Vue events bubble

Vue events don't bubble the component tree on their own. However when writing wrapper components this can be the desired behaviour.

This code registers a global bubble directive which allows to re-emit all given events:

Let's say we want to bubble events start, accelerate and brake of our component Car.

Without any help, we'd roughly have to do this:

@loilo
loilo / argstr.md
Last active September 27, 2019 21:11
Build a command line arguments string from a PHP associative array

PHP arg string generator

Generate a command line arg string from an associative array.

This is purely built for functionality, not for beauty: no grouping of shorthand flags, all args will be escaped and wrapped in single quotes, no matter if needed or not.

Usage

Associative key-value pairs will be treated as command line options while array entries with no/integer keys will be used as regular arguments.

@loilo
loilo / magic-methods.js
Last active November 21, 2023 09:17
PHP Magic Methods in JavaScript
function magicMethods (clazz) {
// A toggle switch for the __isset method
// Needed to control "prop in instance" inside of getters
let issetEnabled = true
const classHandler = Object.create(null)
// Trap for class instantiation
classHandler.construct = (target, args, receiver) => {
// Wrapped class instance
@loilo
loilo / split-pull-requests.md
Last active April 3, 2024 07:24
Split a large pull request into two
@loilo
loilo / DomKeepAlive.md
Last active October 2, 2023 13:48
Keep pre-existing DOM nodes alive inside Vue components

Keep pre-existing DOM alive in Vue

For my use cases, Vue has one critical pitfall: I frequently have/want to use Vue components with <slot>s as wrappers for content from a CMS which I don't have control over. That is, the content comes over the wire via HTML, and I have to activate Vue for some of it.

<interactive-element>
  <p>Slot content I don't have control over</p>
</interactive-element>

I need to activate the Vue component <interactive-element>.

@loilo
loilo / HashStateManager.js
Last active May 11, 2018 12:42
Synchronize shareable key-value state to the window.location.hash as human-readable as possible
class HashStateManager {
constructor ({ global = window, prefix = '!' } = {}) {
this.window = global
this.prefix = prefix
this.callbacks = new Set
this.urlListenerEnabled = true
this.boundUrlListener = this.urlListener.bind(this)
this.internalState = this.parseState()
this.publicState = this.createState()
@loilo
loilo / DNode.php
Last active August 31, 2018 06:54
DNode Comfort Interface (Node.js Server, PHP Client)
<?php
require_once __DIR__ . "/vendor/erasys/dnode-php-sync-client/DnodeSyncClient.php";
class DNode
{
protected $connection;
public static function connect($host, $port)
{
@loilo
loilo / pass-slots.md
Last active March 27, 2024 20:58
Vue: Pass Slots through from Parent to Child Components

Vue: Pass Slots through from Parent to Child Components

The Situation

  • We've got some components A, B and C which provide different slots.
    const A = {
      template: `<div><slot name="a">Default A Content</slot></div>`
    }

const B = {

@loilo
loilo / vue.config.js
Created August 3, 2018 20:29
Activate Object Spread in Templates of a Vue CLI v3 Project
// Store this as vue.config.js in your project root
// to enable object spread in templates.
// Note that you'll need to provide an Object.assign()
// polyfill for legacy browsers.
module.exports = {
chainWebpack: config => {
config.module
.rule('vue')
.use('vue-loader')