Skip to content

Instantly share code, notes, and snippets.

View bloodyowl's full-sized avatar
🦉

Matthias Le Brun bloodyowl

🦉
View GitHub Profile
@bloodyowl
bloodyowl / part2.md
Created October 29, 2017 10:08 — forked from wyeo/part2.md
Observable

Bienvenue dans la deuxième partie du tutoriel sur les Observables.

Résumé de partie 1

Dans la première partie, nous avons vu que les Observables nous permettent de traiter des données synchrones et asynchrones. Aussi, nous avons vu la méthode subscribe() à laquelle nous appliquons notre Observer afin de consulter les données. Enfin dans le dernier exemple, une méthode unsubscribe() qui va nous permettre en tant que Observer d'avoir la possibilité de decider de ne plus écouter les données émises.

Vous l’aurez compris, les Observables sont surtout utilisés dans l’objectif de manipuler de la donnée asynchrone. Nous allons donc essayer de comprendre, comment est-ce que les données reçue peuvent être manipulées.

Prenons un cas concret pour illustrer notre problématique :

@bloodyowl
bloodyowl / Component.js
Created January 4, 2017 15:16
Component library in less than 140 bytes
b=0;$=x=>(p,i="_"+b++,u=($[i]=q=>eval(i).innerHTML=x(p,$[q],u),z=>($[++b]=z,`$.${i}(${b})`)))=>`<c id=${i}>${x(p,[]._,u)}</c>`
@bloodyowl
bloodyowl / redux-light-example.js
Created February 29, 2016 17:12
redux in 14 lines of code
const store = createStore((state = { counter: 0 }, action) => {
switch(action.type) {
case "INCREMENT":
return { counter: state.counter + 1 }
case "DECREMENT":
return { counter: state.counter - 1 }
default:
return state
}
})
@bloodyowl
bloodyowl / example.js
Created December 18, 2015 12:35
fetchURL.js
fetchURL("/").then(({ responseText }) => console.log(responseText))
const request = fetchURL("/")
request.cancel()
request.then(() => console.log("NOT HAPPENING"))
@bloodyowl
bloodyowl / .js
Created September 7, 2015 09:23
ES6 vs ES5
const f = ({ foo = "foo", bar = "bar" } = {}, ...args) => ({
foo,
[`${ bar }1`]: args,
})
// vs
var f = function(options) {
var args = [].slice.call(arguments, 1)
options = options !== undefined ? options : {}
@bloodyowl
bloodyowl / .js
Created September 2, 2015 12:11
escapeRE for string templates
const merge = (strings, substitutions) =>
strings.reduceRight((acc, string, index) => [string, substitutions[index], ...acc], [])
const escapeRE = (strings, ...substitutions) =>
merge(strings, [...substitutions, ""]).map((s) => String(s).replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("")
console.log(escapeRE`^foo[foo](${ "(" + 1 + 2 })`) // "\^foo\[foo\]\(\(12\)"
// signature is (props:object, state:object, setState:function)
function App({ label }, { count = 0 }, setState) {
return (
<div>
<button
onClick={() => setState({ count: count + 1 })}>
{label + " " + count}
</button>
</div>
)
@bloodyowl
bloodyowl / gist:de2ac8f7b0c9801112f7
Created June 21, 2015 15:29
using the ES7 bind syntax with react components
import React, {Component, PropTypes} from "react"
class MyComponent extends Component {
static propTypes = {
onClick: PropTypes.func,
}
handleClick(event) {
const {onClick} = this.props
@bloodyowl
bloodyowl / core.js
Last active August 29, 2015 14:23
core
export function $(value) {
if(value == null) {
return []
}
if(typeof value === "string") {
return [...document.querySelectorAll(value)]
}
if(typeof value.nodeType === "number") {
return Array.of(value)
}
@bloodyowl
bloodyowl / dom.js
Created May 19, 2015 23:46
ES7 bindings
function first() {
if(!this.length) {
return this[0]
}
throw new Error("empty list")
}
export default Array.prototype
export function $(selector, root = document) {