Skip to content

Instantly share code, notes, and snippets.

View luin's full-sized avatar
😄
Focusing

Zihua Li luin

😄
Focusing
View GitHub Profile
@luin
luin / demo.js
Created December 5, 2012 07:57
async demo
function some_fun(req,res,next) {
asyc.waterfall([
function getNewObjectID(callback) {
redis.incr('next_id', callback);
},
function storeObject(r, callback) {
var object = {id:r, now:Date.now()};
redis.set('key', object, callback);
},
function sendResponse(_, callback) {
@luin
luin / redis_scheme.sth
Created December 23, 2012 09:11
Redis Scheme Specification
redis_scheme = require 'redis_scheme'
# HASH: users:<id>(name, password, books, __blog.id)
# HASH: __blog:<id>(url)
# LIST: __blog:<id>:subscribers
# ZSET: users:<id>:books
redis_scheme.define_model('user', {
name: redis_scheme.type.string,
password: redis_scheme.type.string,
books: redis_scheme.foreign_key(redis_scheme.model('book'), 'readers'),
@luin
luin / nodis.js
Created January 6, 2013 07:01
A simple fake redis server written in node.
var net = require('net');
var _dict = {};
// Start a TCP Server
net.createServer(function (socket) {
socket.write("Welcome to Nodis!\n");
// Handle incoming messages from clients.
socket.on('data', function (data) {
data = data.toString().replace(/[\r\n]/g, '').split(' ');
@luin
luin / iterators.js
Created January 24, 2013 15:24
Implementing iterators in JavaScript
function iterators (list) {
var index = 0;
return function () {
return list[index++];
}
}
var v;
var str = "hello world!";
angular.module('projectServices', [])
.factory 'User', ->
@authenticated = false
@name = null
isAuthenticated: => @authenticated
getName: => @name
login: (username, password, callback) =>
$.post '/login',
{username: username, password: password},
((data) =>
@luin
luin / gist:5124550
Created March 9, 2013 15:39
a simple pool library for nodejs.
var Pool = function (poolSize, taskHandler) {
this.poolSize = poolSize;
this.taskHandler = taskHandler;
this.free = poolSize;
this.offlineTasks = [];
};
Pool.prototype.check = function () {
if (this.offlineTasks.length > 0 && this.free > 0) {
@luin
luin / unCurrying.js
Created May 28, 2013 12:50
JavaScript反柯里化
Function.prototype.unCurrying = function () {
var self = this;
return function () {
var args = Array.prototype.slice.call(arguments);
return self.apply(args[0], args.slice(1));
};
};
var foo = [];
var push = Array.prototype.push.unCurrying();
@luin
luin / promise.js
Created July 21, 2013 10:08
Make express res.json support promise
// Let express support "Promise"
app.use(function(req, res, next) {
var oldResJson = res.json;
res.json = function(promise) {
if (promise &&
typeof promise === 'object' &&
typeof promise.success === 'function') {
promise.success(function(fetchedObj) {
if (fetchedObj) {
oldResJson.call(res, fetchedObj);
s1, s2 and s3 are operations in the server, and c1, c2 are operations performed on the client.
s1 s2 s3
c1 c2
1. transform(c1, s1) = (c1', s1') => c1 s1' = s1 c1'
2. transform(c1', s2) = (c1'', s2') => c1' s2' = s2 c1''
3. transform(c1'', s3) = (c1''', s3') => c1'' s3' = s3 c1'''
@luin
luin / redis-test.js
Last active December 1, 2021 02:01 — forked from elyas-bhy/redis-test.js
Redis potential bug replication
var redis = require('redis');
var options = {
host: '127.0.0.1',
port: '6379'
};
var publisher = redis.createClient(options);
var subscriber = redis.createClient(options);
function sub(message) {