Skip to content

Instantly share code, notes, and snippets.

@AimWhy
AimWhy / BLoc.jsx
Last active December 28, 2022 06:09
const noop = _ => _;
class Subscription {
constructor(_unsubscribe = noop) {
this.closed = false;
this._unsubscribe = _unsubscribe;
}
setInnerUnsubscribe(value) {
this._unsubscribe = value;
}
@AimWhy
AimWhy / async.ts
Last active December 28, 2022 06:09
type AsyncCheckArrLimit = <T>(
array: T[],
testFn: (value: T, index: number, a: T[]) => Promise<boolean> | boolean,
limit?: number
) => Promise<boolean>;
const everySeries: AsyncCheckArrLimit = async (array, testFn) => {
for (let i = 0; i < array.length; i++) {
try {
const result = await testFn(array[i], i, array);
const _Retain_ = Symbol.for("retain");
const _Remove_ = Symbol.for("remove");
const Value = (v) => () => v;
const concatPath = (a, b) => a + ((a === "" || b === "") ? "" : ".") + b;
const recordData = (data, recordMap = {}, path = "") => {
recordMap[path] = data;
import { isFunction, isString, isUndefined } from "lodash";
import React from "react";
import PropTypes from "prop-types";
const componentsRegistry = new Map();
const activeInstances = new Set();
export function registerComponent(name, component) {
if (isString(name) && name !== "") {
componentsRegistry.set(name, isFunction(component) ? component : null);
@AimWhy
AimWhy / cache.js
Created October 23, 2022 07:08 — forked from fibo/cache.js
Service Worker implementing stale-while-revalidate caching strategy.
/* global caches, fetch, self */
// Fill here with your cache name-version.
const CACHE_NAME = 'my-cache-v1'
// This is the list of URLs to be cached by your Progressive Web App.
const CACHED_URLS = [
'/',
'/bundle.js',
'/manifest.json',
'/register.js',
@AimWhy
AimWhy / Promiser.js
Created March 21, 2022 06:46 — forked from jasuperior/Promiser.js
An ES6 async/promise proof of concept using Proxies.
/*
Using Es6 Proxies, I created an object that can resolve promises from a chained object accessor pattern.
simply await the values, or supply a callback to .then() and watch the magic.
*/
Symbol.queue = Symbol("queue"); //using Symbols to hide properties from being used or altered
Symbol.data = Symbol("data");
function Promiser( obj ) {
return new Proxy(obj, {
get(target, prop){
// Core assets
let coreAssets = [];
// On install, cache core assets
self.addEventListener('install', function (event) {
// Cache core assets
event.waitUntil(caches.open('app').then(function (cache) {
for (let asset of coreAssets) {
cache.add(new Request(asset));
@AimWhy
AimWhy / detect-unused-css-selectors.js
Created December 17, 2021 02:13 — forked from victor-homyakov/detect-unused-css-selectors.js
Detect unused CSS selectors. Show possible CSS duplicates. Monitor realtime CSS usage.
/* eslint-disable no-var,no-console */
// detect unused CSS selectors
(function() {
var parsedRules = parseCssRules();
console.log('Parsed CSS rules:', parsedRules);
detectDuplicateSelectors(parsedRules);
var selectorsToTrack = getSelectorsToTrack(parsedRules);
window.selectorStats = { unused: [], added: [], removed: [] };
console.log('Tracking style usage (inspect window.selectorStats for details)...');
@AimWhy
AimWhy / script-template.sh
Created December 11, 2021 16:15 — forked from m-radzikowski/script-template.sh
Minimal safe Bash script template - see the article with full description: https://betterdev.blog/minimal-safe-bash-script-template/
#!/usr/bin/env bash
set -Eeuo pipefail
trap cleanup SIGINT SIGTERM ERR EXIT
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
usage() {
cat <<EOF
Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-v] [-f] -p param_value arg1 [arg2...]
const {useCallback, useEffect, useReducer, useRef} = require('react');
let effectCapture = null;
exports.useReducerWithEmitEffect = function(reducer, initialArg, init) {
let updateCounter = useRef(0);
let wrappedReducer = useCallback(function(oldWrappedState, action) {
effectCapture = [];
try {
let newState = reducer(oldWrappedState.state, action.action);