Skip to content

Instantly share code, notes, and snippets.

View fabriciofmsilva's full-sized avatar

Fabrício Silva fabriciofmsilva

View GitHub Profile
@fabriciofmsilva
fabriciofmsilva / pipe.js
Created April 3, 2018 17:08
RXJS Pipe and Let
// Old way
import { from } from 'rxjs/observable/from';
const source$ = from([1, 2, 3, 4, 5, 6, 7, 8, 9]);
source$
.filter(x => x % 2)
.map(x => x * 2)
.scan((acc, next) => acc + next, 0)
.startWith(0)
@fabriciofmsilva
fabriciofmsilva / fetch.js
Created April 12, 2018 21:26
Cancelling Requests with Abortable Fetch
const url = "https://api.example.com/autocomplete"
let controller;
let signal;
autocompleteInput.addEventListener('keyup', () => {
if (controller !== undefined) {
// Cancel the previous request
controller.abort();
}
config.entry = {
app: [
path.resolve(__dirname, `../app/client/client.jsx`),
],
polyfills: [
`babel-polyfill`,
`whatwg-fetch`,
],
};
swPrecache.write(path.resolve(__dirname, `../public/service-worker.js`), {
cacheId: `know-it-all`,
filename: `service-worker.js`,
stripPrefix: `public/`,
staticFileGlobs: [
`public/app.*.js`, // don't include the polyfills version
`public/*.{html,ico,json,png}`,
],
dontCacheBustUrlsMatching: [
/\.(js|json)$/, // I'm cache busting js and json files myself
@fabriciofmsilva
fabriciofmsilva / implementation.js
Created April 18, 2018 15:50
Module Design Pattern
var HTMLChanger = (function() {
var contents = 'contents'
var changeHTML = function() {
var element = document.getElementById('attribute-to-change');
element.innerHTML = contents;
}
return {
callChangeHTML: function() {
@fabriciofmsilva
fabriciofmsilva / revealing-module-pattern.js
Created April 18, 2018 15:51
Revealing Module Pattern
var Exposer = (function() {
var privateVariable = 10;
var privateMethod = function() {
console.log('Inside a private method!');
privateVariable++;
}
var methodToExpose = function() {
console.log('This is a method I want to expose!');
@fabriciofmsilva
fabriciofmsilva / prototype-design-pattern.js
Created April 18, 2018 15:53
Prototype Design Pattern
var TeslaModelS = function() {
this.numWheels = 4;
this.manufacturer = 'Tesla';
this.make = 'Model S';
}
TeslaModelS.prototype.go = function() {
// Rotate wheels
}
@fabriciofmsilva
fabriciofmsilva / revealing-prototype-pattern.js
Created April 18, 2018 15:54
Revealing Prototype Pattern
var TeslaModelS = function() {
this.numWheels = 4;
this.manufacturer = 'Tesla';
this.make = 'Model S';
}
TeslaModelS.prototype = function() {
var go = function() {
// Rotate wheels
};
@fabriciofmsilva
fabriciofmsilva / observer-design-pattern.js
Created April 18, 2018 15:57
Observer Design Pattern
var Subject = function() {
this.observers = [];
return {
subscribeObserver: function(observer) {
this.observers.push(observer);
},
unsubscribeObserver: function(observer) {
var index = this.observers.indexOf(observer);
if(index > -1) {
@fabriciofmsilva
fabriciofmsilva / publish-subscribe.js
Created April 18, 2018 15:59
Publish/Subscribe Design Pattern
// Implementation