Permalink
Browse files

refactor, and expose cache

  • Loading branch information...
1 parent 01f4de8 commit 90cf8b284311e00ecb898d4c25ae421a1e214127 @dominictarr committed Jan 6, 2013
Showing with 98 additions and 80 deletions.
  1. +98 −2 index.js
  2. +0 −25 schema.js
  3. +0 −53 sync.js
View
100 index.js
@@ -1,5 +1,101 @@
+var parserx = require('parse-regexp')
+var exports = module.exports = function (schema) {
+ if('function' == typeof schema)
+ return schema
-var exports = module.exports = require('./schema')
+ var rules = []
+ for (var p in schema) {
+ rules.push({rx: parserx(p) || p, fn: schema[p]})
+ }
+
+ function match (key) {
+ for (var i in rules) {
+ var r = rules[i]
+ var m = key.match(r.rx)
+ if(m && m.index === 0)
+ return r.fn(key)
+ }
+ }
+
+ match.schema = schema
+ match.rules = rules
+
+ return match
+}
+
+exports.schema = exports
+
+exports.cache =
+function cached (open) {
+ var local = {}
+ return function (name, tail, cb) {
+ if('function' == typeof tail)
+ cb = tail, tail = true
+
+ var cached = local[name]
+ if(cached && 'function' === typeof cached.clone) {
+ var n = cached.clone()
+ cb(null, n)
+ return n
+ }
+ var clone
+ var scuttlebutt = open(name, tail, function (err) {
+ cb(err, clone || scuttlebutt)
+ })
+
+ //only scuttlebutts with clone can be cleanly cached.
+ if('function' === typeof scuttlebutt.clone) {
+ local[name] = scuttlebutt
+ clone = scuttlebutt.clone()
+ //okay... have something to dispose the scuttlebutt when there are 0 streams.
+ //hmm, count streams... and emit an event 'unstream' or something?
+ //okay, if all the steams have closed but this one, then it means no one is using this,
+ //so close...
+ //TODO add this to level-scuttlebutt.
+ scuttlebutt.on('unstream', function (n) {
+ if(n === 1) scuttlebutt.dispose()
+ })
+ }
+
+ return scuttlebutt
+ }
+}
+
+exports.sync =
+exports.open = function (schema, connect) {
+ return function (name, tail, cb) {
+ if('function' == typeof tail)
+ cb = tail, tail = true
+
+ var scuttlebutt = schema(name)
+ var es = scuttlebutt.createStream()
+ var stream = connect(name)
+
+ if(!stream)
+ return cb(new Error('unable to connect'))
+
+ stream.pipe(es).pipe(stream)
+
+ var ready = false
+ es.once('sync', function () {
+ if(ready) return
+ ready = true
+
+ //cb the stream we are loading the scuttlebutt from,
+ //incase it errors after we cb?
+ //I'm not sure about this usecase.
+ //Actually, just leave that feature out!
+ //that way I don't have to break API when I realize it was a bad idea.
+ if(cb) cb(null, scuttlebutt)
+ if(!tail) es.end()
+ })
+ //hmm, this has no way to detect that the stream has errored
+ stream.once('error', function (err) {
+ if(!ready) return cb(err)
+ })
+
+ return scuttlebutt
+ }
+}
-exports.open = require('./sync')
View
@@ -1,25 +0,0 @@
-var parserx = require('parse-regexp')
-
-module.exports = function (schema) {
- if('function' == typeof schema)
- return schema
-
- var rules = []
- for (var p in schema) {
- rules.push({rx: parserx(p) || p, fn: schema[p]})
- }
-
- function match (key) {
- for (var i in rules) {
- var r = rules[i]
- var m = key.match(r.rx)
- if(m && m.index === 0)
- return r.fn(key)
- }
- }
-
- match.schema = schema
- match.rules = rules
-
- return match
-}
View
53 sync.js
@@ -1,53 +0,0 @@
-
-var sync = module.exports = function (schema, connect) {
- return function (name, tail, cb) {
- if('function' == typeof tail)
- cb = tail, tail = true
-
- var scuttlebutt = schema(name)
- var es = scuttlebutt.createStream()
- var stream = connect(name)
-
- if(!stream)
- return cb(new Error('unable to connect'))
-
- stream.pipe(es).pipe(stream)
-
- var ready = false
- es.once('sync', function () {
- if(ready) return
- ready = true
-
- //cb the stream we are loading the scuttlebutt from,
- //incase it errors after we cb?
- //I'm not sure about this usecase.
- //Actually, just leave that feature out!
- //that way I don't have to break API when I realize it was a bad idea.
- if(cb) cb(null, scuttlebutt)
- if(!tail) es.end()
- })
- //hmm, this has no way to detect that the stream has errored
- stream.once('error', function (err) {
- if(!ready) return cb(err)
- })
-
- return scuttlebutt
- }
-}
-
-/*
-example:
-
-var sync = makeSync(function (name) {
- //return a scuttlebutt.
- return new Model()
-}, function (name) {
- //return a duplex stream...
- return http.get('http://whatever.com/replicate/'+name)
-})
-
-sync(name, function (_, scuttlebutt) {
- //scuttlebutt is ready!
-})
-
-*/

0 comments on commit 90cf8b2

Please sign in to comment.