Permalink
Browse files

init version

  • Loading branch information...
1 parent 475d42b commit c62e72c5e0695151b235825dc76a1c14ffdc83c6 @neutra committed Apr 2, 2013
Showing with 375 additions and 0 deletions.
  1. +23 −0 Cakefile
  2. +100 −0 lib/merge_pool.js
  3. +10 −0 lib/merge_pool.map
  4. +17 −0 package.json
  5. +63 −0 src/merge_pool.coffee
  6. +59 −0 test/test.coffee
  7. +103 −0 test/test.js
View
@@ -0,0 +1,23 @@
+fs = require 'fs'
+path = require 'path'
+{spawn,exec} = require 'child_process'
+
+launch = (cmd, args=[], callback) ->
+ app = spawn cmd, args, {cwd: process.cwd()}
+ app.stdout.pipe process.stdout
+ app.stderr.pipe process.stderr
+ app.on 'exit', (status) ->
+ process.exit(1) if status != 0
+ cb() if typeof cb is 'function'
+
+clean = ->
+ fs.unlinkSync path.join 'lib',file for file in fs.readdirSync 'lib'
+
+task 'build', 'compile source', ->
+ clean()
+ exec "coffee -cm -o lib src", (err) ->
+ return if err
+ exec "coffee -c \"#{path.join 'test','test.coffee'}\"",(err) ->
+
+task 'test', 'run test', ->
+ launch 'node',[path.join __dirname,"test","test.js"],(err) ->
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -0,0 +1,10 @@
+{
+ "version": 3,
+ "file": "merge_pool.js",
+ "sourceRoot": "..",
+ "sources": [
+ "src\\merge_pool.coffee"
+ ],
+ "names": [],
+ "mappings": ";AAAA;CAAA,KAAA,sBAAA;KAAA,aAAA;;CAAA,CAAA,CAAO,CAAP,EAAO,CAAA;;CAAP,CACA,CAAQ,EAAR,EAAQ;;CADR,CAGA,CAAO,CAAP,KAAQ;CACC,EAAR,CAAA,GAAO,IAAP,EAAa;CAJd,EAGO;;CAHP,CAMM;CACL;;;;;CAKa,EAAA,CAAA,eAAC;;GAAI,KAAJ;QACb;CAAA;;;;;CAAA;CAAA,CAAA,CAKY,CAAX,EAAD,EAAA;AACU,CANV,EAMS,CAAR,CAAD,CAAA;CAZD,IAKa;;CALb,EAcA,MAAK;CACJ,SAAA,2CAAA;SAAA,GAAA;;CAAA,CADS,IAAJ;CACL;;;;;;CAAA;CAQA,GAAO,EAAP,KAAA;CACC,GAAU,CAAA,SAAA,2BAAA;QATX;AAUmB,CAAnB,EAAA,CAAO,CAAY,CAAnB,EAAO;CACN,EAAA,KAAA;QAXD;AAYqB,CAArB,GAAO,CAAc,CAArB,IAAO;CACN,GAAU,CAAA,SAAA,0BAAA;QAbX;AAcqB,CAArB,GAAO,CAAc,CAArB,EAAA,EAAO;CACN,GAAU,CAAA,SAAA,8BAAA;QAfX;CAAA,EAkBO,CAAP,EAAA;CAAO,CAAC,MAAA;CAlBR,OAAA;CAAA,EAmBQ,CAAC,CAAT,CAAA,EAAkB;CAClB,GAAG,EAAH,OAAA;CACC,GAAA,CAAK,GAAL;CACA,aAAA;QAtBD;CAAA,EAuBQ,CAAA,CAAR,CAAA;CAvBA,EAwBU,CAAT,CAxBD,CAwBA,EAAU;CAxBV,CA0BY,CAAL,CAAP,EAAA,GAAQ;AACP,CAAA,EAAiB,EAAT,CAAR,EAAA;CACA,EAAqB,CAAA,EAArB,EAAA,IAAoC;CAApC,EAAA,GAAM,IAAN;UADA;CAEA,GAAwB,EAAA,EAAxB,IAA0C;CAA1C,KAAM,IAAN;UAFA;CAGM,EAAQ,CAAA,CAAT,EAAL,EAAe,MAAf;CACM,CAAgB,CAArB,CAAI,CAAJ,CAAA,EAAa,SAAb;CADD,QAAc;CA9Bf,MA0BO;CAQP;CACC,CAAiB,EAAb,CAAJ,GAAA,KAAiB;MADlB,EAAA;CAGC,KAAA,EADK;CACL,EAAiB,IAAV,CAAP,CAAiB;CAAQ,EAAL,CAAA,aAAA;CAApB,QAAiB;QAtCd;CAdL,IAcK;;CAdL;;CAPD;;CAAA,CA8DA,CAAiB,GAAX,CAAN,EA9DA;CAAA"
+}
View
@@ -0,0 +1,17 @@
+{
+ "name": "merge-pool",
+ "description": "merge multi requests, process one and response all the same requests",
+ "keywords": ["merge-pool","merge"],
+ "version": "0.1",
+ "author": {
+ "name": "neutra"
+ },
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/neutra/merge-pool.git"
+ },
+ "engine": [
+ "node >= 0.6"
+ ],
+ "main": "./lib/merge_pool.js"
+}
View
@@ -0,0 +1,63 @@
+util = require 'util'
+async = require 'async'
+
+_log = (text) ->
+ console.log "MergePool: #{text}"
+
+class MergePool
+ ###
+ Merge Pool
+
+ merge multi requests, process one and response all the same requests
+ ###
+ constructor: (opt={}) ->
+ ###
+ constructor
+ > @opt:
+ > debug:
+ ###
+ @pendings = {}
+ @debug = !!opt.debug
+
+ run: (key,exec,args...,callback) ->
+ ###
+ run(key,exec,args...,callback)
+ > @key: string()
+ > @exec: (args...,callback) -> void()
+ > @callback: (err,result) -> void()
+ ###
+
+ # check params
+ unless key?
+ throw new Error 'MergePool.run: key can not must be null'
+ unless 'string' is typeof key
+ key = key.toString()
+ unless 'function' is typeof exec
+ throw new Error 'MergePool.run: exec must be a function'
+ unless 'function' is typeof callback
+ throw new Error 'MergePool.run: callback must be a function'
+
+ # enqueue item
+ item = {callback}
+ queue = @pendings[key]
+ if queue?
+ queue.push item
+ return
+ queue = [item]
+ @pendings[key] = queue
+
+ done = (err,result) =>
+ delete @pendings[key]
+ Object.freeze err if err instanceof Object
+ Object.freeze result if result instanceof Object
+ queue.forEach (item) =>
+ item.callback.call @,err,result
+
+ # execute
+ try
+ exec.apply null,[args...,done]
+ catch err
+ process.nextTick -> done err
+ return
+
+module.exports = MergePool
View
@@ -0,0 +1,59 @@
+async = require 'async'
+assert = require 'assert'
+
+MergePool = require '../lib/merge_pool'
+
+exports.test = (test) ->
+ mp = new MergePool
+ outputs = null
+ echo = (input,cb) ->
+ console.log "accept input: #{input}"
+ op = ->
+ console.log "echo #{input+100}"
+ cb null, input+100
+ setTimeout op, 500
+ wait = (ms) -> (cb) ->
+ console.log "wait #{ms}ms"
+ setTimeout cb,ms
+ push = (key,args...) -> (cb) ->
+ for item in args
+ console.log "push #{item} -> #{key}"
+ mp.run key, echo, item, (err,result) ->
+ assert.equal err,null
+ assert.ok result?
+ console.log "#{item} -> #{key} return #{result}"
+ outputs[key].push result
+ do cb
+ reset = (cb) ->
+ console.log "reset"
+ outputs = A: [], B: []
+ do cb
+ check = (expect) -> (cb) ->
+ console.log "check"
+ assert.deepEqual outputs, expect
+ do cb
+ async.series [
+ reset
+ check A:[], B:[]
+ push 'A', 1, 3, 5
+ push 'B', 2, 4
+ push 'A', 7
+ check A:[], B:[]
+ wait 1000
+ check A:[101,101,101,101], B:[102,102]
+
+ reset
+ check A:[], B:[]
+ push 'A', 9
+ push 'B', 6, 8, 10
+ push 'A', 11, 13
+ check A:[], B:[]
+ wait 1000
+ check A:[109,109,109], B:[106,106,106]
+ ],(err) ->
+ assert.equal err, null
+ test.done()
+
+if require.main is module
+ test = done: -> console.log "test done"
+ exports.test test
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit c62e72c

Please sign in to comment.