This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const util = require('util'); | |
const R = require('ramda'); | |
const chalk = require('chalk'); | |
const jsondiffpatch = require('jsondiffpatch'); | |
const toJson = require('./to-json'); | |
let prefix; | |
let coloredOutput = false; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable no-return-assign */ | |
const chalk = require('chalk'); | |
const ora = require('ora'); | |
const prettyMs = require('pretty-ms'); | |
const throttle = require('lodash/throttle'); | |
const getHeapUsed = throttle( | |
() => { | |
const heapUsed = process.memoryUsage().heapUsed / 1024 / 1024; | |
return Math.round(heapUsed, 2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Problem | |
We have a function that updates objects and generates audit records of the changes. | |
Think about financial systems or just any app where it's useful to know who made changes to what. | |
Audit records needs to be saved to the DB as well as updates to objects, but ideally | |
we would want to execute business logic, accumulate all updates and audit records in memory | |
and then save it to DB all at once. Let's look at the specifics. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* tl;dr of the problem | |
- We have a funtion that updates objects and produces two values [updates, auditLogRecord]. | |
Let's call it "applyChangesWithAuditLog" | |
- We need to use this function in the context of another function (let's call it "update") and that another function should not be | |
aware of the fact that that auditLogRecord's are being produced and collected. | |
- When function "update" is executed, we want to collect all auditLogRecords that originated from calls | |
to "applyChangesWithAuditLog" inside "update" function. | |
- Then we want to save all collected auditLog items to DB. | |
Code below achieves this by building a functional wrapper. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// step module | |
const applyUpdates = () => {}; | |
const updateStepBody = useDb => (stepBody, updates) => { | |
const updatedBody = applyUpdates(stepBody, updates); | |
useDb(transaction => { | |
createHistoryRecord(updatedBody, updates); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// original version with binding to module instance | |
const { createAuditLogRecords } = require('../commands'); | |
const getStepFactory = require('./get-factory-by-type'); | |
const { createStep, updateStep } = require('./commands'); | |
const applyStepUpdates = require('./apply-step-updates'); | |
const createContainer = (items = []) => ({ | |
getAll: () => items, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// prototype of a pattern that allows to collect certain objects/operations | |
// during function exectution | |
const createContainer = (items = []) => ({ | |
getAll: () => items, | |
add: x => items.push(x), | |
}); | |
const applyUpdates = ({ onNewAuditLogRecord }) => x => { | |
onNewAuditLogRecord(x); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// effect functor | |
const Effect = f => ({ | |
map: g => Effect(x => g(f(x))), | |
run: x => f(x), | |
join: x => f(x), | |
chain: f => | |
Effect(f) | |
.map(g) | |
.join(), | |
ap: eff => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://runkit.com/embed/ogydgfyotoqc | |
var base64 = require("base-64") | |
const R = require('ramda') | |
var utf8 = require('utf8') | |
var text = 'Yeah, right?!' | |
const encode = R.pipe(utf8.encode, base64.encode) | |
const encodeN = R.pipe(...R.times(() => encode, 16)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.clear() | |
async function main() { | |
const delay = time=>new Promise(resolve => setTimeout(()=>resolve(), time)) | |
const functionInvocationsObservable = new Rx.Subject() | |
.bufferTime(/* buffer time*/ 500, /* restart buffer, -1 means never */ -1, /* maxBufferSize*/ 3) | |
.filter(x => x.length > 0) | |
// .subscribe(console.log) | |
const batchify = func => { |