Skip to content

Instantly share code, notes, and snippets.

View adrianmcli's full-sized avatar
:shipit:
What's happening?

Adrian Li adrianmcli

:shipit:
What's happening?
View GitHub Profile
{
"parser": "babel-eslint",
"env": {
"browser": true,
"node": true,
"es6": true
},
"ecmaFeatures": {
"modules": true,
"jsx": true
{"keys": ["tab"], "command": "expand_abbreviation_by_tab", "context":
[
{ "operand": "source.js", "operator": "equal", "match_all": true, "key": "selector" },
{ "match_all": true, "key": "selection_empty" },
{ "operator": "equal", "operand": false, "match_all": true, "key": "has_next_field" },
{ "operand": false, "operator": "equal", "match_all": true, "key": "auto_complete_visible" },
{ "match_all": true, "key": "is_abbreviation" }
]
}
/*** Hot Stream ***/
// there is only "one real" invocation of the stream
const count$ = Rx.Observable.interval(1000).share();
count$.subscribe(x => console.log('A' + x));
setTimeout(() => {
count$.subscribe(x => console.log(' B' + x));
}, 2000);
@adrianmcli
adrianmcli / typing-tracker.js
Created January 9, 2017 03:22
A React component that tracks how long you've been typing in the textarea. There is also an indicator to show when you are typing.
import React from 'react';
import Rx from 'rxjs/Rx';
import { observeComponent, fromComponent } from 'observe-component/rxjs';
// Create the component with the listeners we want
const TextArea = observeComponent('onInput')('textarea');
export default class MyComponent extends React.Component {
constructor() {
super();
@adrianmcli
adrianmcli / typing-clock-2.js
Last active January 8, 2017 21:01
Tracks how long you've been typing by starting a counting stream every time a typing event has been detected.
/*** Helper Functions ***/
const showTyping = () => $('.typing').text('User is typing...');
const showIdle = () => $('.typing').text('');
const updateTimer = (x) => $('.timer').text(x);
/*** Program Logic ***/
// declare shared streams
const inputEvents$ = Rx.Observable.fromEvent($('#input'), 'input').share();
@adrianmcli
adrianmcli / typing-clock.js
Last active January 8, 2017 20:59
Tracks how long you've been typing by creating a stream of state change-events and then sampling from that stream every second.
/*** Helper Functions ***/
const showTyping = () => $('.typing').text('User is typing...');
const showIdle = () => $('.typing').text('');
const updateTimer = (x) => $('.timer').text(x);
const handleTypingStateChange = state =>
state === 1 ? showTyping() : showIdle();
/*** Program Logic ***/
@adrianmcli
adrianmcli / simple-event-loop-example.js
Created January 6, 2017 20:26
A simple event loop example. Guess the output (answer in the comments).
var a = "hey";
var cb = function() { console.log(a); }
setTimeout(cb, 1000);
// let's make a delay of 3 seconds
var timestamp = Date.now() + 3000;
while (Date.now() < timestamp);
// now that 3 seconds have passed,
@adrianmcli
adrianmcli / tutorial-refactor.js
Created January 6, 2017 19:24
A refactor of André Staltz's example twitter program in his tutorial on RxJS: https://gist.github.com/staltz/868e7e9bc2a7b8c1f754
// UI Event Streams --------------------------------------------
const refreshButton = document.querySelector('.refresh');
const closeButton1 = document.querySelector('.close1');
const closeButton2 = document.querySelector('.close2');
const closeButton3 = document.querySelector('.close3');
const refreshClickStream = Rx.Observable.fromEvent(refreshButton, 'click');
const close1ClickStream = Rx.Observable.fromEvent(closeButton1, 'click');
const close2ClickStream = Rx.Observable.fromEvent(closeButton2, 'click');
const close3ClickStream = Rx.Observable.fromEvent(closeButton3, 'click');
@adrianmcli
adrianmcli / event-loop.js
Created January 6, 2017 00:42
A quiz on the Javascript event loop from Daniel Parker's JavaScript with Promises
var async = true;
var xhr = new XMLHttpRequest();
xhr.open('get', 'data.json', async);
xhr.send();
// Create a three second delay (don't do this in real life)
var timestamp = Date.now() + 3000;
while (Date.now() < timestamp);
// Now that three seconds have passed,
@adrianmcli
adrianmcli / IsTyping.js
Created December 31, 2016 02:38
A simple React component to show how to add a time-delayed typing indicator for a textarea.
import React, { Component } from 'react';
class IsTyping extends Component {
constructor() {
super();
this.state = {
typing: false,
};
this.timeout = null;
this.onChange = this.onChange.bind(this);