Skip to content

Instantly share code, notes, and snippets.

View JaapRood's full-sized avatar

Jaap van Hardeveld JaapRood

View GitHub Profile
@JaapRood
JaapRood / encode.js
Last active July 9, 2019 15:00
Encoding an avsc message with a Confluent spec header
const magicByte = 0
// Optimized to try and not have to re-allocate memory through Buffer.concat.
// We allocate 1k to begin with, hoping that fits the message
// and we don't have to relocate again. Otherwise, tough luck.
function toBuffer(value, type, schemaId, length = 1024) {
const buf = Buffer.allocUnsafe(length) // unsafe, might contain old / sensitive data
buf.writeInt8(magicByte)
buf.writeInt32BE(schemaId, 1)
@JaapRood
JaapRood / index.js
Created October 7, 2014 03:17
requirebin sketch
var Immutable = require('immutable'),
_ = require('lodash'),
IMap = Immutable.Map,
IVector = Immutable.Vector;
var messagesData = [
{
id: 'm_1',
threadID: 't_1',
threadName: 'Jing and Bill',
@JaapRood
JaapRood / gist:cc8fe4533d45508f3f51
Last active August 29, 2015 14:05
A way to use AmpersandState and React together a bit more easily by mixing in Backbone's Event
// Not tested, but the main gist of glueing ampersand state and react components together well. The actual mixin I use in production is slightly more eleborate (handling the stopping of listening and rebinding through the component's lifecycle), but the main idea is there.
var Events = require('backbone-events-standalone'),
React = require('react'),
State = require('ampersand-state');
var PostModel = State.extend({
props: {
title: 'string',
body: 'string'
@JaapRood
JaapRood / index.js
Created August 6, 2014 07:40
requirebin sketch
var AmpersandState = require('ampersand-state');
var AmpersandCollection = require('ampersand-collection');
var PostA = AmpersandState.extend({});
var PostB = AmpersandState.extend({});
var Posts = AmpersandCollection.extend({
model: function(attrs, options) {
if (1 === 1) {
@JaapRood
JaapRood / gist:ea55b11841d6b004fda9
Created July 16, 2014 02:10
ampersand-state mixin to use 'state' prop instead of children to get correct behaviour
var State = require('ampersand-state'),
_ = require('lodash');
module.exports = function(childModels) {
if (!childModels) childModels = {};
var props = {};
_.each(childModels, function(def, name) {
props[name] = 'state';
@JaapRood
JaapRood / index.js
Created July 16, 2014 00:28
requirebin sketch
var AmpersandState = require('ampersand-state');
var Smile = AmpersandState.extend({
props: {
width: 'number'
}
});
var Person = AmpersandState.extend({
props: {