Skip to content

Instantly share code, notes, and snippets.

@modeswitch
Created March 10, 2014 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save modeswitch/9459595 to your computer and use it in GitHub Desktop.
Save modeswitch/9459595 to your computer and use it in GitHub Desktop.
FileSystem interface generation
var methods = [
'open',
'close',
'mkdir',
'rmdir',
'stat',
'fstat',
'link',
'unlink',
'read',
'readFile',
'write',
'writeFile',
'appendFile',
'exists',
'lseek',
'readdir',
'rename',
'readlink',
'symlink',
'lstat',
'truncate',
'ftruncate',
'utimes',
'futimes',
'setxattr',
'getxattr',
'fsetxattr',
'fgetxattr',
'removexattr',
'fremovexattr',
];
function FileSystemInterface(obj, call) {
methods.forEach(function(method_name) {
this[method_name] = call.bind(this, method_name);
}.bind(this));
}
var FS = new FileSystemInterface(fs, function(method_name) {
callback = maybeCallback(arguments[arguments.length - 1]);
var fs = this;
var error = fs.queueOrRun(
function() {
var context = fs.provider.openReadWriteContext();
function complete() {
context.close();
callback.apply(fs, arguments);
}
var args = [fs, context];
args.concat(Array.prototype.slice.call(arguments, 1, arguments.length - 1));
args.concat(complete);
fs[method_name].apply(args);
}
);
if(error) callback(error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment