Skip to content

Instantly share code, notes, and snippets.

View wyeo's full-sized avatar
👋

Williams YEO wyeo

👋
View GitHub Profile
class Observable {}
Observable.of = (...args) => {
Observable.prototype.subscribe = (nextFn, errorFn, completeFn) => {
args.map(x => nextFn(x))
completeFn()
}
return new Observable()
Observable.interval = (period) => {
Observable.prototype.subscribe = (nextFn) => {
let i = 0
const timeout = setInterval((function(){ nextFn(i++) }), period)
return clearInterval.bind(null, timeout)
}
return new Observable()
}
const btn = document.getElementById('btn')
const values = [1, 2, 3, 4, 5]
function logger(event) {
console.log(`Logger : ${event}`)
btn.removeEventListener('click', logger)
}
// btn.addEventListener('click', logger)
// const btn$ = Rx.Observable.fromEvent(btn, 'click')
@wyeo
wyeo / internationalStore.js
Created June 25, 2017 19:09
Polygot | HOC | React
import React, { Children } from 'react'
import ReactDOM from 'react-dom'
import PropTypes from 'prop-types'
import Polyglot from 'node-polyglot'
import messages from './message'
const locale = window.localStorage.getItem('locale') || 'fr'
const polyglot = new Polyglot({
locale,
@wyeo
wyeo / part2.md
Last active April 9, 2018 12:16
Observable

La programmation reactive avec RxJS

La programmation reactive est un paradigme de programmation dont le concept repose sur l'émission de données depuis une ou plusieurs sources (producteurs) à destinations d'autres éléments appelés consommateurs. Elle repose sur le design pattern Observable - Observer implémenté par les observables, aisément manipulable via les operateurs.

Dans l'introduction aux observables, nous avons découvert cet objet permettant de souscrire à une source de donnée avec un Observer. Cette mécanique permet de réagir aux données émisent par l'Observable par le biais de Observer, et cela pendant toute la durée du flux.

Maintenant, voyons comment nous pouvons manipuler ce flux grâce aux opérateurs de transformation.

Les opérateurs

import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import classnames from 'classnames'
import { FormattedMessage, injectIntl, intlShape } from 'react-intl'
import { rxForm } from 'rx-react-form'
import { Button, FontH1 } from 'volt-ui'
import ValidatedInput from 'components/ValidatedInput'
import messages from './messages'
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import classnames from 'classnames'
import { connect } from 'react-redux'
import { rxForm } from 'rx-react-form'
import { createSelector } from 'reselect'
import autobind from 'autobind-decorator'
import { FormattedMessage, injectIntl, intlShape } from 'react-intl'
import { Button, FontH1 } from 'volt-ui'
@wyeo
wyeo / git-tag-delete-local-and-remote.sh
Created March 18, 2019 14:15 — forked from mobilemind/git-tag-delete-local-and-remote.sh
how to delete a git tag locally and remote
# delete local tag '12345'
git tag -d 12345
# delete remote tag '12345' (eg, GitHub version too)
git push origin :refs/tags/12345
# alternative approach
git push --delete origin tagName
git tag -d tagName
@wyeo
wyeo / useTraceableState
Last active January 17, 2020 12:41
useTraceableState : get previous value of a state
import { useRef, useEffect, useState } from "react"
const usePreviousValue = (value: any) => {
const ref = useRef()
useEffect(() => {
ref.current = value
})
return ref.current
}
@wyeo
wyeo / breakpoint.js
Created January 17, 2020 12:39 — forked from ryanve/breakpoint.js
JavaScript: Get the current media query breakpoint for a CSS feature.
(function(root, name, make) {
if (typeof module != 'undefined' && module['exports']) module['exports'] = make();
else root[name] = make();
}(this, 'breakpoint', function() {
/**
* @link http://gist.github.com/ryanve/7924792
* @param {string} feature range feature name e.g. "width"
* @param {string=} unit CSS unit for feature e.g. "em"
* @param {number=} init initial guess