Skip to content

Instantly share code, notes, and snippets.

A Common Interface Among Streaming APIs

After reading the slide from Isaac's talk in November. I began to think about using little reactive controllers wired together to control the overall resposes of an application.

To demonstreate this concept. Say let's write a chat server that accept HTTP POST as input, and respond to GET with current char content.

Note that connection: 'keep-alive' may be one of the prerequisite for this to work.

# Server

{Bacon} = require 'Bacon'

@hden
hden / index.html
Created March 23, 2013 02:38
Unicode characters should be decoded properly and shown in the web inspector.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sample</title>
<script>
var source = "var Sample;\n\nSample = (function() {\n\n function Sample() {\n var a;\n a = 0;\n throw new Error(\"Hello\");\n }\n\n return Sample;\n\n})();\n\nnew Sample();\n\n//@ sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYXBwLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiYXBwLmNvZmZlZSJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBR0EsSUFBQSxFQUFBO0NBQUE7QUFBTSxDQUFOO0NBQ0c7Q0FBYyxDQUFBLENBQUEsYUFBQTtDQUNULE9BQUE7Q0FBQSxFQUFJLENBQUo7Q0FDQSxHQUFVLENBQUEsRUFBQSxHQUFBO0NBRmYsRUFBYzs7Q0FBZDs7Q0FESDs7QUFLSSxDQUxKLEdBS0ksRUFBQSIsInNvdXJjZXNDb250ZW50IjpbIiMgVW5pY29kZSBjaGFyYWN0ZXJzIGJlbG93IHNob3VsZCBiZSBkZWNvZGVkIHByb3Blcmx5LlxuIyDjgYLjgYTjgYbjgYjjgYpcblxuY2xhc3MgU2FtcGxlXG4gICAgY29uc3RydWN0b3I6IC0+XG4gICAgICAgIGEgPSAwXG4gICAgICAgIHRocm93IG5ldyBFcnJvcihcIkhlbGxvXCIpXG5cbm5ldyBTYW1wbGUoKVxuIl19\n//@ sourceURL=app.js";
Function(source)()
</script>
</head>
@hden
hden / fibo,ciffee
Created November 11, 2013 15:25
Fibonacci via fast doubling
# Fibonacci via fast doubling
fibo = (n) ->
if n is 0
[0, 1]
else
[a, b] = fibo ~~(n / 2)
c = a * (2 * b - a)
d = b * b + a * a
if n % 2 is 0 then [c, d] else [d, c + d]
var styles = [
'background: linear-gradient(steelblue, darkgray)'
, 'border: 1px solid #3E0E02'
, 'color: white'
, 'display: block'
, 'text-shadow: 0 1px 0 rgba(0, 0, 0, 0.3)'
, 'box-shadow: 0 1px 0 rgba(255, 255, 255, 0.4) inset, 0 5px 3px -5px rgba(0, 0, 0, 0.5), 0 -13px 5px -10px rgba(255, 255, 255, 0.4) inset'
, 'text-align: center'
, 'font-weight: bold'
, 'font: 18px sans-serif'
@hden
hden / errorMsg.txt
Created December 1, 2013 03:41
Polymer.js error
event.returnValue is deprecated. Please use the standard event.preventDefault() instead. wrappers.js:141
Uncaught TypeError: Object #<Object> has no method 'created' base.js:21
Uncaught TypeError: Object #<Object> has no method 'prepareElement' base.js:50
@hden
hden / br.csv
Created December 10, 2013 02:04
Treemap
did name q12 q34
768 漢來海港餐廳(巨蛋店-巨蛋會館5樓) 11.151650829973422 10.874212934830874
42 十二廚自助餐廳 - 台北喜來登大飯店 10.047123912114026 10.32755264408124
1317 上閤屋 JOGOYA - 台北南京店 10.926295994781112 10.149747119504683
1316 上閤屋 JOGOYA - 台中復興店 10.045759661382684 10.056637715113201
604 雲軒西餐廳 La Rotisserie - 君品酒店 9.46352437327118 10.053925881531105
654 Lawry's 勞瑞斯牛肋排餐廳 9.823367240046236 10.036173612553485
1415 潮港城-太陽百匯 9.142107057302551 9.791162888555018
652 Diamond Tony's 101 隨意鳥地方高空觀景餐廳 9.398743691938193 9.428360172704291
1718 這一鍋皇室祕藏鍋物(中山北店) 1 9.422064766172813
@hden
hden / bookmarklet.js
Created December 18, 2013 13:15
Github Bookmarklet
javascript:(function() {var e = document.getElementById('pull_request_body');if (e) {e.value += '# What? Why?\n\n\n# How was it tested?\n\n\ncc CD team @';}})();
@hden
hden / Cakefile
Created January 29, 2014 04:44
HuffPost-style Title Generator
'use strict'
_ = require 'underscore'
class TitleGenerator
n: =>
'' + _.random(1, 10) + '間'
getTitle: =>
n = do @n

Fair Dispatch Pattern for Node.js Stream

One-to-many stream propogation inspired by nsqio/nsq#307 (comment)

'use strict'

{Transform} = require 'stream'

class Mux extends Transform
@hden
hden / index.coffee
Created September 25, 2014 04:22
minimun event-driven email
'use strict'
debug = require('debug')('quest:project-name')
mandrill = require 'mandrill-api/mandrill'
r = require 'rethinkdb'
unit = require 'unt'
mandrill_client = new mandrill.Mandrill('mandrill-key')
app = unit.application()