Permalink
Browse files

Experiments in async... too late! Callback hell it is!

  • Loading branch information...
kristianoye committed Jan 12, 2018
1 parent f49f95f commit 929ff3f656b2fa8694d190c6aea151a1ba1c0adc
Showing with 168 additions and 98 deletions.
  1. +3 −3 KMUD.njsproj
  2. +1 −1 lib/base/Interactive.js
  3. +4 −8 src/EFUNProxy.js
  4. +1 −4 src/EFUNS.js
  5. +8 −3 src/FileManager.js
  6. +32 −19 src/FileSystem.js
  7. +0 −59 src/MUDExcecutionContext.js
  8. +54 −0 src/MUDPromise.js
  9. +1 −1 src/MXC.js
  10. +12 −0 src/fs/DefaultFileSystem.js
  11. +52 −0 src/fs/FileSystemStat.js
View
@@ -472,6 +472,9 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="src\fs\DefaultFileSecurity.js" />
<Compile Include="src\fs\FileSystemStat.js">
<SubType>Code</SubType>
</Compile>
<Compile Include="src\GameServer.js">
<SubType>Code</SubType>
</Compile>
@@ -499,9 +502,6 @@
<Compile Include="src\MUDEventEmitter.js">
<SubType>Code</SubType>
</Compile>
<Compile Include="src\MUDExcecutionContext.js">
<SubType>Code</SubType>
</Compile>
<Compile Include="src\MUDGlobals.js">
<SubType>Code</SubType>
</Compile>
View
@@ -256,7 +256,7 @@ class Interactive extends Living {
}
else if (resultType === 'string') {
/* end of the line */
if (evt.verb.length > 0) this.write(evt.error);
if (evt.verb.length > 0) this.writeLine(evt.error = result);
return evt.complete(MUDEVENT_STOP);
}
else if (resultType === 'function') {
View
@@ -14,10 +14,10 @@ const
sprintf = require('sprintf').sprintf,
MUDConfig = require('./MUDConfig'),
{ SecurityError } = require('./ErrorTypes'),
MUDExecutionContext = require('./MUDExcecutionContext'),
util = require('util'),
fs = require('fs'),
vm = require('vm');
vm = require('vm'),
MXC = require('./MXC');
const
KiloByte = 1024,
@@ -1318,15 +1318,11 @@ class EFUNProxy {
* @returns {any} The result of the unguarded call.
*/
unguarded(callback) {
var result = false, context = new MUDExecutionContext();
let result = false;
try {
result = context.run(callback);
}
catch (err) {
throw err;
result = callback();
}
finally {
context.restore();
}
return typeof result === 'undefined' ? this : result;
}
View
@@ -15,8 +15,7 @@ const
FT_DIRECTORY = 1,
FT_FILE = 2,
basePath = path.resolve('src'),
libPath = path.resolve('lib'),
MUDExecutionContext = require('./MUDExcecutionContext');
libPath = path.resolve('lib');
const
PLURAL_SUFFIX = 1,
@@ -49,7 +48,6 @@ class EFUNS {
var parts = dirname.split(path.sep);
if (_async) {
var ctx = new MUDExecutionContext();
async.forEachOf(parts, (item, i, cb) => {
var dir = parts.slice(0, i + 1).join(path.sep);
if (!dir)
@@ -152,7 +150,6 @@ class EFUNS {
throw new Error('Security Violation');
if (_async) {
let context = new MUDExecutionContext();
return this.isDirectory(filepath, (isDir) => {
return fs.readdir(isDir ? filepath : _restOfPath, (err, files) => {
if (err)
View
@@ -132,9 +132,8 @@ class FileManager extends MUDEventEmitter {
if (!result)
throw new Error('Fatal: Could not locate filesystem');
return callback ?
callback(new FileSystemRequest(relPath, expr, result)) :
new FileSystemRequest(relPath, expr, result);
let req = new FileSystemRequest(relPath, expr, result);
return callback ? callback(req) : req;
}
/**
@@ -186,6 +185,12 @@ class FileManager extends MUDEventEmitter {
});
}
getStat(efuns, expr, callback) {
return this.createFileRequest(expr, req => {
return req.filesystem.getStat(req.relativePath, callback);
});
}
/**
* Check to see if the given expression is a directory,
* @param {EFUNProxy} efuns The external proxy checking the directory.
View
@@ -7,7 +7,6 @@
*/
const
MUDEventEmitter = require('./MUDEventEmitter'),
MUDExecutionContext = require('./MUDExcecutionContext'),
{ NotImplementedError } = require('./ErrorTypes'),
FS_NONE = 0, // No flags set
FS_SYNC = 1 << 0, // The filesystem supports syncronous I/O.
@@ -136,23 +135,23 @@ class FileSystem extends MUDEventEmitter {
/**
* Append data to a file; Creates file if needed.
* @param {string} path
* @param {any} content
* @param {Function=} callback
* @param {string} expr The file to append data to.
* @param {any} content The content to write to file.
* @param {function(boolean,Error):void} callback A callback to fire with success status.
*/
appendFile(path, content, callback) {
appendFile(expr, content, callback) {
return typeof callback === 'function' ?
this.assertAsync(() => this.appendFileAsync(path, content, MUDExecutionContext.awaiter(callback))) :
this.assertSync(() => this.appendFileSync(path, content));
this.assertAsync(() => this.appendFileAsync(expr, content, MXC.awaiter(callback))) :
this.assertSync(() => this.appendFileSync(expr, content));
}
/**
* Append data to a file in async mode; Creates file if needed.
* @param {string} path
* @param {any} content
* @param {function(Error):void} callback
* @param {string} expr The file to append data to.
* @param {any} content The content to write to file.
* @param {function(Error):void} callback A callback to fire with success status.
*/
appendFileAsync(path, content, callback) {
appendFileAsync(expr, content, callback) {
throw new NotImplementedError('appendFileAsync');
}
@@ -174,7 +173,7 @@ class FileSystem extends MUDEventEmitter {
createDirectory(expr, opts, callback) {
this.assertDirectories();
return typeof callback === 'function' ?
this.assertAsync(() => this.createDirectoryAsync(expr, opts, MUDExecutionContext.awaiter(callback))) :
this.assertAsync(() => this.createDirectoryAsync(expr, opts, MXC.awaiter(callback))) :
this.assertSync(() => this.createDirectorySync(expr, opts));
}
@@ -188,7 +187,7 @@ class FileSystem extends MUDEventEmitter {
createFile(expr, content, callback) {
return typeof callback === 'function' ?
this.assertAsync(() => this.createFileAsync(expr, content, MUDExecutionContext.awaiter(callback))) :
this.assertAsync(() => this.createFileAsync(expr, content, MXC.awaiter(callback))) :
this.assertSync(() => this.createFileSync(expr, content));
}
@@ -219,7 +218,7 @@ class FileSystem extends MUDEventEmitter {
*/
deleteDirectory(expr, flags, callback) {
return typeof callback === 'function' ?
this.assertAsync(() => this.deleteDirectoryAsync(expr, flags, MUDExecutionContext.awaiter(callback))) :
this.assertAsync(() => this.deleteDirectoryAsync(expr, flags, MXC.awaiter(callback))) :
this.assertSync(() => this.deleteDirectorySync(expr, flags));
}
@@ -244,7 +243,7 @@ class FileSystem extends MUDEventEmitter {
deleteFile(expr, callback) {
return typeof callback === 'function' ?
this.assertAsync(() => this.deleteFileAsync(expr, MUDExecutionContext.awaiter(callback))) :
this.assertAsync(() => this.deleteFileAsync(expr, MXC.awaiter(callback))) :
this.assertSync(() => this.deleteFileSync(expr));
}
@@ -263,6 +262,20 @@ class FileSystem extends MUDEventEmitter {
*/
getRealPath(expr) { return expr; }
getStat(expr, callback) {
return typeof callback === 'function' || callback === false ?
this.assertAsync(async () => this.getStatAsync(expr, callback)) :
this.assertSync(() => this.getStatSync(expr));
}
getStatAsync(expr) {
throw new NotImplementedError('getStatAsync');
}
getStatSync(expr) {
throw new NotImplementedError('getStatSync');
}
/**
* Translate an absolute path back into a virtual path.
* @param {string} expr The absolute path to translate.
@@ -340,7 +353,7 @@ class FileSystem extends MUDEventEmitter {
*/
loadObject(expr, args, callback) {
return typeof callback === 'function' ?
this.assertAsync(() => this.loadObjectAsync(expr, args, MUDExecutionContext.awaiter(callback))) :
this.assertAsync(() => this.loadObjectAsync(expr, args, MXC.awaiter(callback))) :
this.assertSync(() => this.loadObjectSync(expr, args));
}
@@ -375,7 +388,7 @@ class FileSystem extends MUDEventEmitter {
flags = 0;
}
return typeof callback === 'function' ?
this.assertAsync(() => this.readDirectoryAsync(expr, flags, MUDExecutionContext.awaiter(callback))) :
this.assertAsync(() => this.readDirectoryAsync(expr, flags, MXC.awaiter(callback))) :
this.assertSync(() => this.readDirectorySync(expr, flags));
}
@@ -440,7 +453,7 @@ class FileSystem extends MUDEventEmitter {
*/
stat(expr, callback) {
return typeof callback === 'function' ?
this.assertAsync(() => this.statAsync(expr, MUDExecutionContext.awaiter(callback))) :
this.assertAsync(() => this.statAsync(expr, MXC.awaiter(callback))) :
this.assertSync(() => this.statSync(expr));
}
@@ -463,7 +476,7 @@ class FileSystem extends MUDEventEmitter {
writeFile(expr, content, callback) {
return typeof callback === 'function' ?
this.assertAsync(() => this.writeFileAsync(expr, content, MUDExecutionContext.awaiter(callback))) :
this.assertAsync(() => this.writeFileAsync(expr, content, MXC.awaiter(callback))) :
this.assertSync(() => this.writeFileSync(expr, content));
}

This file was deleted.

Oops, something went wrong.
View
@@ -1 +1,55 @@
/**
*
* @param {function(Function,Function):any} action
* @param {function(any):any} success
* @param {function(Error):any} failure
*/
function tryFunctionCall(action, success, failure) ) {
try {
return action(success, failure);
}
catch (err) {
}
}
/**
*
* @param {any} action
*/
function performAction(action) {
let done = false, result = tryFunctionCall.call(this, action,
(result) => {
if (!done) {
done = true;
}
},
(error) => {
if (!done) {
done = true;
}
});
}
class MUDPromise {
/**
* Create an A+ MUD Promise
* @param {function(Function,Function):MUDPromise} action The async action to wrap.
*/
constructor(action) {
this.state = 0;
this.action = action;
this.thens = [];
performAction.call(this, action);
}
then(onSuccess, onFailure) {
}
}
let test = new MUDPromise((resolve, reject) => {
let m = parseFloat(Math.random() * 100.0);
return m > 50 ? resolve(m) : reject(new Error('Under 50'));
});
View
@@ -61,4 +61,4 @@ MXC.awaiter = function (callback) {
};
};
module.exports = MXC;
module.exports = MXC;
@@ -132,6 +132,18 @@ class DefaultFileSystem extends FileSystem {
});
}
getStatAsync(expr, callback) {
return this.translatePath(expr, fullPath => {
});
}
getStatSync(expr) {
return this.translatePath(expr, fullPath => {
});
}
/**
* Translate an absolute path back into a virtual path.
* @param {string} expr The absolute path to translate.
Oops, something went wrong.

0 comments on commit 929ff3f

Please sign in to comment.