Skip to content

Instantly share code, notes, and snippets.

View shrynx's full-sized avatar
🤦‍♂️
github statuses are stupid. waiting for github stories

shrynx shrynx

🤦‍♂️
github statuses are stupid. waiting for github stories
View GitHub Profile
const events = {
events: {},
on: function (eventName, fn) {
this.events[eventName] = this.events[eventName] || [];
this.events[eventName].push(fn);
},
emit: function (eventName, data) {
if (this.events[eventName]) {
this.events[eventName].map(function(fn) {
fn(data);
// module a.js
let a
function setValueOfA(val) {
a = val
// we are now emmiting event 'aChanged'
// with data as value of a
events.emit('aChanged', a)
}
// module b.js
let b
// module a.js
let a
function assignValueToA(val) {
a = val
calculateValueOfB(a)
}
// module b.js
let b
function calculateValueOfB(a) {
b = a + 100
// declaring an independent variable
let a = 50
// declaring a mathematical constraint between the variable a and b
let b = a + 100
// variable b is now holding value 150
console.log(b) // 150
// manipulating value of variable a
a = 100
// variable b is still holding value 150
console.log(b) // 150
// Select the button we need need to listen upon.
const myButton = document.querySelector('#click-me')
// Create a stream of clicks, using fromEvent function,
// by passing the button and the event listener.
const click$ = Rx.Observable.fromEvent(myButton, 'click')
// Select the button we need need to listen upon.
const myButton = document.querySelector('#click-me')
// Create a stream of clicks, using fromEvent function,
// by passing the button and the event listener.
const click$ = Rx.Observable.fromEvent(myButton, 'click')
// From the click stream, we create a new stream,
// where we first collect all the clicks
// that happen in 250ms inverval of the previous click
// and group them together.
const dbClick$ = click$.bufferWhen(() => click$.debounceTime(250))
const time$ = Rx.Observable.interval(1000)
const filtered$ = time$
.filter(x => x%2 === 0)
.map(x => `${x} seconds have passed`)
filtered$.subscribe(x => console.log(x))
@shrynx
shrynx / cloudSettings
Last active June 8, 2020 06:36
Visual Studio Code Settings Sync Gist
{"lastUpload":"2020-06-08T06:36:34.305Z","extensionVersion":"v3.4.3"}
@shrynx
shrynx / macOSOpenURLInChrome.js
Created March 8, 2018 11:23
open url in chrome on macOS and reuse already open tab if possible.
#!/usr/bin/env osascript -l JavaScript
const Chrome = Application('Chrome');
// although there is ES6 support , but "run" function
// can't use fat arrow functions.
// https://github.com/JXA-Cookbook/JXA-Cookbook/wiki/ES6-Features-in-JXA#arrow-functions
function run(args) {
const url = args[0];
const tabData = findTabForUrl(url);
@shrynx
shrynx / react simple context demo.js
Last active May 10, 2018 01:01
simple demo of react's context api
const CounterContext = React.createContext(0);
class CounterProvider extends React.Component {
state = { count: 0 };
incrementCount = () => {
this.setState(({ count }) => ({ count: count + 1 }));
};
componentDidMount() {
setInterval(this.incrementCount, 1000);
}