Skip to content

Instantly share code, notes, and snippets.

View medikoo's full-sized avatar

Mariusz Nowak medikoo

View GitHub Profile
@medikoo
medikoo / db-setprototypeof.md
Last active December 17, 2015 11:29
DB setPrototypeOf case

setPrototypeOf use case for Database engine written in JavaScript

Ability to rebuild DB state in unspecified order

e.g. first set property of an object and then set object's instance type, or set object as a value of given property and in later turn deciding of what instance that object is.

In short we need the ability to create the object before knowing it's instance type. It makes it very safe for any race conditions. Thanks to that behavior, in engine I work with, rebuild of data state from localStorage is done in whatever order iteration of keys is done (where each key describes a single property and not whole object definition). There's no need to watch the order, or mind circular references, it just works.

Proto change possibility in that case solves a lot of problems and opens many doors. I worked before with simpler version of such engine, which didn't rely on proto switching, it was much more error prone. Missing links crashed (or blocked) database, also order in which state is re-create

@medikoo
medikoo / descriptor.js
Created May 18, 2013 11:11
Descriptors helper
var d = require('es5-ext/lib/Object/descriptor');
// with helper
Object.defineProperties(obj, {
simple: d(value),
getter: d.gs(function () { return this.a + this.b; }),
});
// without helper
Object.defineProperties(obj, {
var d = require('es5-ext/object/descriptor'); // soon just require('d');
var Foo = function () {};
Object.defineProperties(Foo.prototype, d.binder({
someFn: d(function () {
return this.whatever();
})
}));
lstat = function (path, cb) {
var count = 11;
setImmediate(function self() {
if (--count) setImmediate(self);
else cb(null, {});
});
};
@medikoo
medikoo / strongloop-find-largest-file.js
Created February 5, 2014 10:28
Example from StrongLoop article -> http://strongloop.com/strongblog/node-js-callback-hell-promises-generators/ implemented with deferred promise library
var fs = require('fs')
, join = require('path').join
, promisify = require('deferred').promisify
, readdir = promisify(fs.readdir), lstat = promisify(fs.lstat);
module.exports = function (dir) {
var largest = null, largestSize = -1;
return readdir(dir).map(function (file) {
return lstat(join(dir, file))(function (stats) {
// 1. Deferred
var asyncFn1 = function () {
var deferred = new Deferred();
// Initialize async operation and resolve it with either:
// deferred.resolve or deferred.reject
return deferred.promise;
};
@medikoo
medikoo / safari-issue.js
Last active August 29, 2015 13:56
Terrible getter/value resolution bug that happens in Safari
// This simplified test case unfortunately doesn't reproduce the issue
// nevertheless such error occurs in complex app I'm working with
'use strict';
var Foo = function () {}
, resolve = function () { return {}; };
Object.defineProperty(Foo.prototype, 'bar', {
configurable: true,
'use strict';
// Define reusable utils, outside of exported function body
var someUtilA = function () {
/* ... */
};
var someUtilB = function () {
/* ... */
};
@medikoo
medikoo / es6-shims.md
Last active March 24, 2021 22:29
List of ECMAScript 6 shims

List of ECMAScript 6 shims

Implemented on top of ECMAScript 5

Provided as distinct CJS modules, installable via npm


ECMAScript 5 Built-in Objects extensions

Individual modules of es5-ext package. See ES6 features for usage information.

Array

TIW <-> MaxCom data exchange API

Shared Secret string used to validate requests, is hardcoded within codebase, and not exposed in this document on purpose.

API of MaxCom service

Initialization of payment transaction