Skip to content

Instantly share code, notes, and snippets.

View samoshkin's full-sized avatar

Alexey Samoshkin samoshkin

View GitHub Profile
@samoshkin
samoshkin / fetch_vs_xhr.md
Created November 1, 2018 20:04
Fetch API features and limitations

XHR limitations and drawbacks:

  • more complicated API, request and response concepts are mixed together.
  • lacks streaming, whole response is going to buffer in memory, not available for binary data

Fetch API features and pros:

  1. more pleasant simpler API. Has Request and Response abstractions, can be used separately (for example in ServiceWorkers). Based on Promises.
  2. supports streaming, response.body is ReadableStream, you can read data chunk by chunk without buffering, available for binary data. Can access partial content while response is being received.
  3. has cache control support (default, no-store, reload, no-cache, force-cache, only-if-cached)
@samoshkin
samoshkin / systemjs-builder-code-splitting.js
Created January 19, 2018 21:58
Create common,vendor and several entry point bundles using systemjs-builder
let Builder = require('systemjs-builder');
let fs = require('fs');
let mapValues = require('lodash/mapValues');
let config = {
baseURL: '.',
map: {
'vendor': 'lib/vendor'
}
};
@samoshkin
samoshkin / es6-symbol-toprimitive.js
Created January 17, 2018 18:15
ES6 replace default object-to-primitive conversion logic using Symbol.toPrimitive method
class Disk {
constructor(capacity){
this.capacity = capacity;
}
[Symbol.toPrimitive](hint){
switch (hint) {
case 'string':
return 'Capacity: ' + this.capacity + ' bytes';
@samoshkin
samoshkin / es5-toprimitive-hooks.js
Created January 17, 2018 18:02
Custom toString() and valueOf() methods
var obj = {
prop: 101,
toString(){
return 'Prop: ' + this.prop;
},
valueOf() {
return this.prop;
}
};
@samoshkin
samoshkin / date-object-to-primitive-conversion.js
Created January 17, 2018 17:57
Date assumes string conversion as a default
let d = new Date();
// get string representation
let str = d.toString(); // 'Wed Jan 17 2018 16:15:42'
// get numeric representation, num of milliseconds since Unix epoch
let num = d.valueOf(); // 1516198542525
// compare with a string representation
// true because d is converted to same string
@samoshkin
samoshkin / js-to-primitive-internal.js
Created January 17, 2018 13:42
EcmaScript ToPrimitive internal operation pseudo-code
function ToPrimitive(input, preferredType){
switch (preferredType){
case Number:
return toNumber(input);
break;
case String:
return toString(input);
break
default:
@samoshkin
samoshkin / es-module-syntax.js
Created January 16, 2018 20:09
ES module syntax examples
// import a module with side-effect without any import bindings
import 'jquery';
// import the default export of a module
import $ from 'jquery';
// import a named export of a module
import { reduce } from 'underscore';
// import a named export to a different name
@samoshkin
samoshkin / generator_runner.js
Created January 15, 2018 20:02
Simple "generator + promise" runner routine. Write sync - execute async.
function r(genFn, context){
return function(...args){
const iter = genFn.apply(context || this, args);
return traverse(iter);
}
function traverse(iter){
return new Promise((res, rej) => {
function step(err, arg) {
@samoshkin
samoshkin / remote.tmux.conf
Created November 25, 2017 19:04
remote specific tmux configuration
# show status bar at bottom for remote session,
# so it do not stack together with local session's one
set -g status-position bottom
# In remote mode we don't show "clock" and "battery status" widgets
set -g status-left "$wg_session"
set -g status-right "#{prefix_highlight} $wg_is_keys_off $wg_is_zoomed #{sysstat_cpu} | #{sysstat_mem} | #{sysstat_loadavg} | $wg_user_host | #{online_status}"
@samoshkin
samoshkin / toggle_keybindings.tmux.conf
Last active February 5, 2024 02:26
tmux.conf excerpt to toggle on/off session keybindings and prefix handling
bind -T root F12 \
set prefix None \;\
set key-table off \;\
set status-style "fg=$color_status_text,bg=$color_window_off_status_bg" \;\
set window-status-current-format "#[fg=$color_window_off_status_bg,bg=$color_window_off_status_current_bg]$separator_powerline_right#[default] #I:#W# #[fg=$color_window_off_status_current_bg,bg=$color_window_off_status_bg]$separator_powerline_right#[default]" \;\
set window-status-current-style "fg=$color_dark,bold,bg=$color_window_off_status_current_bg" \;\
if -F '#{pane_in_mode}' 'send-keys -X cancel' \;\
refresh-client -S \;\
bind -T off F12 \