Skip to content

Instantly share code, notes, and snippets.

@jack-guy
jack-guy / app-injector.ts
Created March 7, 2016 22:39
How to set up a Socket.io-based Feathers app with Angular 2
import {Injector} from 'angular2/core';
let appInjectorRef: Injector;
export const appInjector = (injector?: Injector):Injector => {
if (injector) {
appInjectorRef = injector;
}
return appInjectorRef;
};
import { Directive, ViewChild, Inject, ContentChildren, QueryList, ContentChild,
ElementRef, Input, forwardRef, AfterViewInit, EventEmitter } from 'angular2/core';
import { Subject } from 'rxjs';
const imagesLoaded = require('imagesloaded');
@Directive({
selector: '[masonry-item]',
host: {
'[style.position]': 'position',
@jack-guy
jack-guy / feathers.d.ts
Last active September 7, 2017 10:34
These are basic typings for Feathers.js based off of those in DefinitelyTyped for Express. This doesn't type feathers plugins, but does have some interfaces that can be used in them. WIP.
// Type definitions for Feathers
// Project: http://feathersjs.com/
// Definitions by: Jack Guy <http://thatguyjackguy.com>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import { Application, Handler, ErrorRequestHandler } from 'express';
type HandlerArgument = Handler | Handler[];
export = Feathers;
const iceCreamFlavors = ['vanilla', 'chocolate', 'strawberry'];
const iceCreamPrices = ['$5.50', '$5.75', '$6.00'];
let iceCreamFlavorToPrice;
for (let i = 0; i < iceCreamFlavors.length; i++) {
iceCreamFlavorToPrice[iceCreamFlavors[i]] = iceCreamPrices[i];
}
import { from, zip } from 'rxjs';
import { scan } from 'rxjs/operators';
const iceCreamFlavors = ['vanilla', 'chocolate', 'strawberry'];
const iceCreamPrices = ['$5.50', '$5.75', '$6.00'];
const iceCreamFlavorToPrice$ = zip(
from(iceCreamFlavors),
from(iceCreamPrices),
).pipe(scan((prev, [flavor, price]) => {
function getIceCreamFlavorToPrice(flavors, prices) {
let iceCreamFlavorToPrice;
for (let i = 0; i < flavors.length; i++) {
iceCreamFlavorToPrice[flavors[i]] = prices[i];
}
return iceCreamFlavorToPrice;
}
let iceCreamFlavors = ['vanilla', 'chocolate', 'strawberry'];
let iceCreamPrices = ['$5.50', '$5.75', '$6.00'];
import { from, fromEvent, zip } from 'rxjs';
import { scan } from 'rxjs/operators';
const initialIceCreamFlavors = ['vanilla', 'chocolate', 'strawberry'];
const initialIceCreamPrices = ['$5.50', '$5.75', '$6.00'];
const iceCreamFormSubmit$ = fromEvent(formEl, 'submit');
const iceCreamFormFlavors$ = iceCreamFormSubmit$.pipe((map(({flavor}) => flavor)));
const iceCreamFormPrices$ = iceCreamFormSubmit$.pipe((map(({price}) => price)));
function getIceCreamById (iceCreamId) {
return fetch(`/icecream/${id}`);
}
const iceCreamIds = [4, 5, 10, 12];
// Slow, effectively synchronous
for (let iceCreamId of iceCreamIds) {
console.log(await getIceCreamById(iceCreamId));
}
import { from, fromPromise } from 'rxjs';
const iceCreamIds = [4, 5, 10, 12];
from(iceCreamIds).pipe(
flatMap((iceCreamId) => fromPromise(getIceCreamById(iceCreamId)))
).subscribe((iceCream) => {
console.log(iceCream);
});
from(iceCreamIds).pipe(
withLatestFrom(
iceCreamAuth$,
iceCreamPreferences$,
iceCreamVendors$,
iceCreamInventory$,
iceCreamTemperature$,
),
map(([