Skip to content

Instantly share code, notes, and snippets.

View snewcomer's full-sized avatar

Scott Newcomer snewcomer

View GitHub Profile
@snewcomer
snewcomer / exponential.js
Created April 16, 2020 17:25
exponential-backoff
function createScheduler({
callback,
time,
callbackTimeout,
backoffMultiplier = 1.5,
backoffMaxTime = 20000
}) {
let run;
let timeoutId;
let ticker;
const createDataTree = dataset => {
let hashTable = Object.create(null);
dataset.forEach(aData => {
return (hashTable[aData.id] = { ...aData, childNodes: [] });
});
let dataTree = [];
dataset.forEach(aData => {
if (aData.parentId) {
hashTable[aData.parentId].childNodes.push(hashTable[aData.id]);
} else {
@snewcomer
snewcomer / promise-timer.js
Created April 19, 2020 20:44
Promise timer
let counter = 0;
class MyPromise extends Promise {
constructor(cb) {
const stack = (() => { try { throw new Error(); } catch (e) { return e; } })().stack;
const id = `promise ${counter++}`;
console.time(id);
super(function(resolve, reject) {
@snewcomer
snewcomer / ember-fastboot-architecture
Created September 25, 2020 20:24
Proposal: Ember FastBoot App Server architecture
## General Design
- Cluster management.
- Being aware of distribution changes and downloading of new assets.
- Delegation of responsibilities between three separate primary objects:
- Cluster Master
- Cluster Workers
- Express HTTP Server
## Constituent Components
@snewcomer
snewcomer / cli-fastboot-proposal
Created October 2, 2020 03:59
ember-cli-fastboot proposal
## Architecture
A possible future design of ember-cli-fastboot which achieves the goal of "production ready."
### General Design
ember-cli-fastboot combines a couple of critical components for a user's experience in development and production.
1. **Production** - fastboot service, utils to add to and clear the shoebox, and managing the HTML on handoff to the client.
2. **Development** - an express server that instantiates a FastBoot instance, plugs the FastBoot middleware into the pipeline, reload capabilities and build time utilities.
@snewcomer
snewcomer / gist:589bcf6361e0ddf9d0f564c7042c6f87
Last active October 30, 2020 03:38
ember-unit-testing-components.js
// test helper
import { getContext } from '@ember/test-helpers';
export default function createComponent(lookupPath, named = {}) {
let { owner } = getContext();
let componentManager = owner.lookup('component-manager:glimmer');
let { class: componentClass } = owner.factoryFor(lookupPath);
return componentManager.createComponent(componentClass, { named });
}
@snewcomer
snewcomer / fastboot-runtime-test.js
Created December 23, 2020 15:56
Ember FastBoot runtime test
/* eslint-env node */
/**
* This can be run as an npm script a part of your CI testing
*
* This is a "smoke test" to make sure that the application, when run
* within FastBoot, isn't calling APIs that aren't supported by the
* Node.js environment. (e.g. window.alert)
*/
// Code from: http://patshaughnessy.net/2020/1/20/downloading-100000-files-using-async-rust
//
// Cargo.toml:
// [dependencies]
// tokio = { version = "0.2", features = ["full"] }
// reqwest = { version = "0.10", features = ["json"] }
// futures = "0.3"
use std::io::prelude::*;
use std::fs::File;
@snewcomer
snewcomer / rust-matrix.rs
Created June 3, 2021 18:58
Interview Question in Rust!!
fn find_positive_sum(vec: &Vec<[i32; 5]>, coords: Vec<(i32, i32)>) -> i32 {
let mut sum = 0;
let mut coords_sorted = coords.clone();
coords_sorted.sort(); // sort to get duplicates as neighbors
coords_sorted.dedup();
for (x, y) in coords_sorted.iter() {
if let Some(inner) = vec.get(*y as usize) { // Some(inner) or None
if let Some(inner_x) = inner.get(*x as usize) { // Some(inner) or None
sum += inner_x;
}
@snewcomer
snewcomer / sort-back.rs
Created June 4, 2021 18:21
Rust Sort 0's to back
// Your last Rust code is saved below:
fn move_to_back(v: &Vec<u32>) -> Vec<u32> {
// extra memory
let mut res = vec![];
let mut num_of_zeros = 0;
for i in v.iter() {
if *i != 0 as u32 {
res.push(*i);
} else {
num_of_zeros += 1;