Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View cefn's full-sized avatar

Cefn Hoile cefn

View GitHub Profile
var mosca = require("mosca");
var mqtt = require("mqtt");
function promiseMosca(){
var moscaOpts = {
http:{
port:3000,
static:false,
bundle:false
},
var chai = require("chai"),
chaiAsPromised = require("chai-as-promised"),
selenium = require("selenium-standalone"),
wd = require("wd"),
Q=require("q");
chai.use(chaiAsPromised);
chai.should();
chaiAsPromised.transferPromiseness = wd.transferPromiseness;
var Kefir = require("kefir"),
asap = require("asap");
function cacheStream(source){
var eventCache = null;
var sink = Kefir.stream(function(emitter){
var handler = function(event){
if(event.type==="value"){
eventCache = event;
}
var _ = require("lodash");
/** A draft approach to substituting Kefir streams with a bespoke library, suitable as a means of replacing the chained filters by withHandler calls.
* Benefits: more lightweight, (not cloning a stream for every operation), more debuggable (inspectable through the stack).
* */
function Listenable(){
this.listeners = {};
this.active = false;
}
//Of course, these can't really be called tests, but they indicate the syntax which successfully produces console
//output validating the various referenced functions
it("Can create a stream", function(){
var stream = makeStream();
});
it("Can log a stream", function(){
var stream = makeStream();
stream.log("Original");
@cefn
cefn / promiseNextBefore.js
Last active September 28, 2015 11:28
A way to enable Kefir streams to implement a timeout on the next event. Modified to improve timeout handling.
var Kefir = require("kefir");
var period = 1000;
//var wait = 2 * period; //success condition
var wait = 0.5 * period; //failure condition
function promiseNextBefore(stream, timeout){
return new Promise(function(resolve, reject){
function handler(event){
if(event.type==="value"){
@cefn
cefn / stressMqtt.js
Created November 20, 2015 17:08
A minimal test of MQTT publish-subscribe round-trip, implemented as a Mocha (node) test suite
var child_process = require("child_process"),
spawn = child_process.spawn,
mqtt = require("mqtt"),
mosca = require("mosca");
//uses the callback convention of calling 'done()' with
// callback with no argument for success
// callback with an argument for error
// no callback if nothing to report
@cefn
cefn / pivot_binding.js
Last active January 6, 2016 02:55
Reference syntax for topic tree binding to a fixed template
var albumBinder =
pivot("album", ".album",
pivot("*", ".song",
pivot("verses/*", ".verse",
pivot("*", ".line", function(topic, control){
return screenplay.bindLeaf(tree, topic, control);
})
)
)
);
<html>
<head>
<script src="browserified/track_template.js"></script>
</head>
<body>
<section class="template">
<ul class="album">
<li class="song">
<h1>Song Title</h1>
<ul class="verse">
var albumSource = {
hello:{
chorus:[
"Hello, I love you",
"Won't you tell me your name?",
"Hello, I love you",
"Let me jump in your game",
],
verses:[
[