/** * run single 'task' method sharing callbacks. Method MUST take callback as LAST arg. * piper is bound to an IndexFile. * * @param task {string} - task name unique to method! * @param method {function} - method to execute, gets (args, ... , callback) * @param args {Array} - args to pass to method * @param context {object} - other params to remember and sent to callback * @param callback {function} - result callback */ function piper(task, method, args, context, callback){ var readCallbacks = this.callbackQueue, memoArgs = _.rest(arguments, 2), wrappedCallback; //console.log('piper', task, [method]); // queue up if already reading file for this task if (task in readCallbacks){ readCallbacks[task].push(memoArgs); return; } readCallbacks[task] = [memoArgs]; if (!this.fd) { //console.log(' ... opening', this.filePath); this.fd = fs.openSync(this.filePath, 'r'); } // ref count so we know when to close the main index file ++this.refcount; wrappedCallback = _.partial(piper.wrapper, this, task); // call method -- replace original callback (last arg) with wrapped one method.apply(null, [].concat( args, wrappedCallback )); }
function removeFromHome() { if (!home) return; var pos = home.socks.indexOf(sockExports); if (pos == -1) return; home.socks.splice(pos, 1); // renumber the cursors of connections >= pos _.each(_.rest(home.socks, pos), function(sock, i) { sock.setCursorId(i + pos); }); _.each(home.socks, function(sock) { // remove the highest numbered cursor from each connection sock.updateCursor({id: home.socks.length}); // resend all cursors >= pos to all connections _.each(_.rest(home.socks, pos), function(cursorSock, i) { if (cursorSock != sock) { sock.updateCursor(cursorSock.getCursor()); } else { sock.updateCursor({id: i + pos}); } }); }); _.each(homes, function(home) { _.each(home.socks, function(sock) { sock.updateHomes(); }); }); if (lab) updateHomeLabParts(lab); }
Table.prototype.hashes = function() { var self = this; return _.map(_.rest(self.raw), function(row) { var line = {}; _.each(self.headers(), function(header, hindex) { line[header] = row[hindex]; }); return line; }); };
_.each(home.socks, function(sock) { // remove the highest numbered cursor from each connection sock.updateCursor({id: home.socks.length}); // resend all cursors >= pos to all connections _.each(_.rest(home.socks, pos), function(cursorSock, i) { if (cursorSock != sock) { sock.updateCursor(cursorSock.getCursor()); } else { sock.updateCursor({id: i + pos}); } }); });
function build_search_url(terms) { var url = "_search/" + _.first(terms); if (terms.length > 1) { var first = true; _.each(_.rest(terms), function(term){ var prepend = '/'; if (!first) prepend = '+'; url += prepend + term; first = false; }) } return url; }
piper.wrapper = function(self, task /*, result...*/){ var readCallbacks = self.callbackQueue, result = _.rest(arguments, 2), callback, args; // live access callbacks cache in case nested cb's // add to the array. while (args = readCallbacks[task].shift()) { callback = args.pop(); // last arg MUST be callback // console.log('>>>> pper wrapper', self.fastIndex.name, task, result.toString()) callback.apply(null, [].concat(_.flatten(args, /*shallow*/true), result)); } // now done - delete cb cache delete readCallbacks[task]; if (--self.refcount === 0) { //console.log(' ... closing', self.filePath); fs.close(self.fd); self.fd = null; } };
format: function() { if (arguments.length == 0) return null; return _.inject(_.rest(arguments), function(str, arg, i) { return str.replace(new RegExp("\\{" + i + "\\}", 'gm'), arg) }, arguments[0].toString()); }
var shift = function(array, index) { return _.rest(array, index).concat(_.first(array, index)); };
var f = function(setup) { var args = this._rsetup(setup || {}); EventEmitter.call(this); type_f.call(this, setup); if (this.init) { this.init.apply(this, _.rest(arguments)); } };