Last active
August 29, 2015 14:07
_.throttle()の用途
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
// クライアントサイドの、まじてきとうなサンプルだょ | |
var util = require('util'); | |
var EventEmitter = util.EventEmitter; | |
var _ = require('lodash'); | |
var io = require('socket.io-client'); | |
function Chart(socket, max_length){ | |
this.ticks = []; | |
this.max_length = max_length || 1000; | |
socket.on('tick', this.add.bind(this)); | |
this.on('add', this.render.bind(this)); | |
EventEmitter.call(this); | |
} | |
util.inherits(Chart, EventEmitter); | |
Chart.prototype.add = function(tick){ | |
if(this.ticks.length >= this.max_length){ | |
this.ticks.pop(); | |
} | |
this.ticks.unshift(tick); | |
this.emit('add'); | |
} | |
Chart.prototype.render = _.throttle(function(){ | |
// d3でゃたらとSVGにグラフを描画する、ひじょうに重ぃ処理! | |
}, 50); | |
var socket = io('http://localhost'); | |
var chart = new Chart(socket, 2000); | |
// socketからすごぃ勢ぃでデータが届くときに、描画が追ぃっかなぃ! | |
// throttleで、N msごとに1回しかrenderが呼ばれなぃょーにするょ | |
// addゎ必ずしてるから、renderをちょっとサボっても問題なぃょ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment