Skip to content

Instantly share code, notes, and snippets.

💭
Building cool things at @testimio

Benjamin Gruenbaum benjamingr

💭
Building cool things at @testimio
Block or report user

Report or block benjamingr

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@benjamingr
benjamingr / chrome_click.cc
Created Apr 16, 2019
What happens on a click
View chrome_click.cc
// actual differences (in chrome, IE, Firefox, Safari an Edge are all different) are:
// - behavior in disabled 'form' controls
// - phoning to home base (not here, it's defined elsewhere, can dig it up)
// - dealing with clicks dispatching clicks 'recursively'
// this is HTMLElement::click
void HTMLElement::click() {
DispatchSimulatedClick(nullptr, kSendNoEvents,
SimulatedClickCreationScope::kFromScript);
}
View xhr-in-fetch.js
class Dispatch extends EventTarget {
dispatch(eventName) {
const ev = new Event(eventName);
if (("on" + eventName) in this) {
this["on" + eventName](ev);
}
this.dispatchEvent(ev);
}
}
class XMLHttpRequest extends Dispatch {
View intercept-network-requests.js
function interceptNetworkRequests(ee) {
const open = XMLHttpRequest.prototype.open;
const send = XMLHttpRequest.prototype.send;
const isRegularXHR = open.toString().indexOf('native code') !== -1;
// don't hijack if already hijacked - this will mess up with frameworks like Angular with zones
// we work if we load first there which we can.
if (isRegularXHR) {
View shadow-dom-utils.js
function querySelectorAllDeep(root, selector) {
const [start, ...rest] = Array.isArray(selector) ? selector : selector.split(" /deep/ ");
if (rest.length === 0) return [...root.querySelectorAll(start)];
return [...root.querySelectorAll(start + " *")].map(x => x.shadowRoot).filter(Boolean).map(x => querySelectorAllDeep(x, rest)).flat();
}
function unshadowDOM(dom, tagnNameOverride) {
const root = document.createElement(tagnNameOverride || dom.tagName || "fragment");
for(const {name, value} of (dom.attributes || [])) {
try {
View index.js
const React = require('react');
const { expect } = require('chai');
const { shallow, configure } = require('enzyme');
const { createSandbox } = require('sinon');
require('react-dom'); // required by the enzyme adapter internally
const Adapter = require('enzyme-adapter-react-16');
configure({ adapter: new Adapter() });
var called = 0;
View index.js
const React = require('react');
const { expect } = require('chai');
const { shallow, configure } = require('enzyme');
const { createSandbox } = require('sinon');
require('react-dom'); // required by the enzyme adapter internally
const Adapter = require('enzyme-adapter-react-16');
configure({ adapter: new Adapter() });
var called = 0;
View AggregateAbortController.js
class AggregateAbortController extends AbortController {
constructor(...controllers = []) {
super();
this.controllers = controllers;
}
abort() {
for(const controllers of this.controllers) {
controllers.abort();
}
}
View Mine.js
// generators are lazy so maxItems isn't needed anymore - you just take as many items as you need
// it's also lazy, so if you need less items you will pay less for it. Naming a Set `set` is like naming a Number `number`
const uniqueMergeWithoutMax = function *(arrays, withoutValue) {
const seen = new Set();
for(const array of arrays) {
for(const item of array) {
if (item === withoutValue || seen.has(item)) continue;
seen.add(item);
yield item;
View answer.md

A Promise handles a single event when an async operation completes or fails.

So, a promise doesn't handle anything - a promise is just a value over time. The charactaristics of promises are that:

  • Because a promise is just a value - promises are multicast which means multiple .thens are 'transparent'.
  • Native promises are what async functions return and what they await.
  • Promises have three states (fulfilled, pending and rejected) and transition between them just once.

Promises aren't really eager - but if you have a promise an operation has already started - so terms lik running promises don't make sense. They have a built in (stage 4 and part of JavaScript) multi-value counterpart (async iterators) which do multiple values and

View sample.js
function cleanUp (arr, max) {
const cnts = {} // keep track of what we find
return arr.reduce((a, i) => { // loop over the array index by index
cnts[i] = (cnts[i] || 0) + 1; // mark that I seen the number
if (cnts[i] <= max) { // check to see if we are under the max
return a.concat(i) //if we are, add it to an arry
}
return a // return the array for reduce
}, [])
}
You can’t perform that action at this time.