Skip to content

Instantly share code, notes, and snippets.

@Offirmo
Last active December 2, 2018 07:51
Show Gist options
  • Save Offirmo/c9bcc9b0ea2b8e201e2c5c8723bdb2c9 to your computer and use it in GitHub Desktop.
Save Offirmo/c9bcc9b0ea2b8e201e2c5c8723bdb2c9 to your computer and use it in GitHub Desktop.
[browser micro-lib] #JavaScript #browser #growth #frontend
https://github.com/terkelg/facon
// in Offirmo monorepo!
function poll(predicate, options) {
// early check to save an initial poll period
let result = predicate();
if (result)
return Promise.resolve(result);
const {periodMs, timeoutMs, debugId} = Object.assign({
periodMs: 100, // check every 100ms
timeoutMs: 10 * 1000, // after 10 seconds, timeout
debugId: 'an unnamed predicate',
}, options || {});
return new Promise((resolve, reject) => {
const waitForElement = setInterval(() => {
result = predicate();
if (!result) return;
clearTimeout(waitForTimeout);
clearInterval(waitForElement);
resolve(result);
}, periodMs);
const waitForTimeout = setTimeout(() => {
clearInterval(waitForElement);
reject(new Error(`${EXPERIMENT_ID} Timed out while waiting for "${debugId}"`));
}, timeoutMs);
});
}
// in Offirmo monorepo!
function pollWindowVariable(varname, options) {
options = Object.assign({}, {debugId: `window.${varname}`}, options || {});
return poll(() => window[varname], options);
}
function pollAngularInjector(angular, options) {
options = Object.assign({}, {debugId: `AngularJS current app $injector`}, options || {});
// https://stackoverflow.com/a/13403660/587407
// https://stackoverflow.com/a/15536532/587407
return poll(() => angular.element(document.body).injector(), options);
}
function pollAngularModule(injector, name, options) {
options = Object.assign({}, {debugId: `AngularJS module ${name}`}, options || {});
return poll(() => injector.has(name), options)
.then(() => injector.get(name));
}
function pollDOMSelectorPresence(selector, options) {
options = Object.assign({}, {
debugId: `DOM selector ${selector}`,
container: document,
}, options || {});
return poll(() => options.container.querySelector(selector), options);
}
// in Offirmo monorepo!
const styleOnce = (css, id, document = window.document) => {
const styleId = `${EXPERIMENT_ID}-${id.replace(/[./]/g, '-')}`;
const existingElement = document.getElementById(id);
if (existingElement)
return existingElement;
const style = document.createElement('style');
style.setAttribute('id', styleId);
style.innerHTML = css;
document.head.appendChild(style);
return style;
}
// XXX deprecated
const searchParams = new URLSearchParams(window.location.search);
const userEmail = searchParams.get('userEmail');
// https://github.com/Offirmo/simple-querystring-parser/blob/master/index.js
function lightQuerystringValueDecoder(val) {
// A bare parameter, is likely to mean true, like ?showUsers
// WARNING this is a voluntary departure from the standard http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
if (typeof val !== 'string')
return true
if (val.match(/^[0-9]+$/))
return Number(val)
if (val === 'true')
return true
if (val === 'false')
return false
return val // no inference
}
function parseQuerystring(querystring, options) {
options = options || {}
options.valueDecoder = options.valueDecoder || lightQuerystringValueDecoder
// Create an object with no prototype https://github.com/sindresorhus/query-string/issues/47
var result = Object.create(null)
if (typeof querystring !== 'string')
return result
// remove possible leading char, whatever it may be
querystring = querystring.trim().replace(/^(\?|#|&)/, '');
if (!querystring)
return result
var raw_options = querystring.split('&')
return raw_options.reduce(function (acc, raw_option) {
if (!raw_option) return acc
var split = raw_option.split('=')
var key = decodeURIComponent(split[0])
var value = decodeURIComponent(split.slice(1).join('=')) || undefined
acc[key] = options.valueDecoder(value)
return acc
}, result)
}
export default parseQuerystring;
export { lightQuerystringValueDecoder, parseQuerystring };
parseQuerystring(window.location.search)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment