Skip to content

Commit

Permalink
Experiments in async... too late! Callback hell it is!
Browse files Browse the repository at this point in the history
  • Loading branch information
kristianoye committed Jan 12, 2018
1 parent f49f95f commit 929ff3f
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 98 deletions.
6 changes: 3 additions & 3 deletions KMUD.njsproj
Expand Up @@ -472,6 +472,9 @@
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Include="src\fs\DefaultFileSecurity.js" /> <Compile Include="src\fs\DefaultFileSecurity.js" />
<Compile Include="src\fs\FileSystemStat.js">
<SubType>Code</SubType>
</Compile>
<Compile Include="src\GameServer.js"> <Compile Include="src\GameServer.js">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
Expand Down Expand Up @@ -499,9 +502,6 @@
<Compile Include="src\MUDEventEmitter.js"> <Compile Include="src\MUDEventEmitter.js">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Include="src\MUDExcecutionContext.js">
<SubType>Code</SubType>
</Compile>
<Compile Include="src\MUDGlobals.js"> <Compile Include="src\MUDGlobals.js">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
Expand Down
2 changes: 1 addition & 1 deletion lib/base/Interactive.js
Expand Up @@ -256,7 +256,7 @@ class Interactive extends Living {
} }
else if (resultType === 'string') { else if (resultType === 'string') {
/* end of the line */ /* 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); return evt.complete(MUDEVENT_STOP);
} }
else if (resultType === 'function') { else if (resultType === 'function') {
Expand Down
12 changes: 4 additions & 8 deletions src/EFUNProxy.js
Expand Up @@ -14,10 +14,10 @@ const
sprintf = require('sprintf').sprintf, sprintf = require('sprintf').sprintf,
MUDConfig = require('./MUDConfig'), MUDConfig = require('./MUDConfig'),
{ SecurityError } = require('./ErrorTypes'), { SecurityError } = require('./ErrorTypes'),
MUDExecutionContext = require('./MUDExcecutionContext'),
util = require('util'), util = require('util'),
fs = require('fs'), fs = require('fs'),
vm = require('vm'); vm = require('vm'),
MXC = require('./MXC');


const const
KiloByte = 1024, KiloByte = 1024,
Expand Down Expand Up @@ -1318,15 +1318,11 @@ class EFUNProxy {
* @returns {any} The result of the unguarded call. * @returns {any} The result of the unguarded call.
*/ */
unguarded(callback) { unguarded(callback) {
var result = false, context = new MUDExecutionContext(); let result = false;
try { try {
result = context.run(callback); result = callback();
}
catch (err) {
throw err;
} }
finally { finally {
context.restore();
} }
return typeof result === 'undefined' ? this : result; return typeof result === 'undefined' ? this : result;
} }
Expand Down
5 changes: 1 addition & 4 deletions src/EFUNS.js
Expand Up @@ -15,8 +15,7 @@ const
FT_DIRECTORY = 1, FT_DIRECTORY = 1,
FT_FILE = 2, FT_FILE = 2,
basePath = path.resolve('src'), basePath = path.resolve('src'),
libPath = path.resolve('lib'), libPath = path.resolve('lib');
MUDExecutionContext = require('./MUDExcecutionContext');


const const
PLURAL_SUFFIX = 1, PLURAL_SUFFIX = 1,
Expand Down Expand Up @@ -49,7 +48,6 @@ class EFUNS {
var parts = dirname.split(path.sep); var parts = dirname.split(path.sep);


if (_async) { if (_async) {
var ctx = new MUDExecutionContext();
async.forEachOf(parts, (item, i, cb) => { async.forEachOf(parts, (item, i, cb) => {
var dir = parts.slice(0, i + 1).join(path.sep); var dir = parts.slice(0, i + 1).join(path.sep);
if (!dir) if (!dir)
Expand Down Expand Up @@ -152,7 +150,6 @@ class EFUNS {
throw new Error('Security Violation'); throw new Error('Security Violation');


if (_async) { if (_async) {
let context = new MUDExecutionContext();
return this.isDirectory(filepath, (isDir) => { return this.isDirectory(filepath, (isDir) => {
return fs.readdir(isDir ? filepath : _restOfPath, (err, files) => { return fs.readdir(isDir ? filepath : _restOfPath, (err, files) => {
if (err) if (err)
Expand Down
11 changes: 8 additions & 3 deletions src/FileManager.js
Expand Up @@ -132,9 +132,8 @@ class FileManager extends MUDEventEmitter {
if (!result) if (!result)
throw new Error('Fatal: Could not locate filesystem'); throw new Error('Fatal: Could not locate filesystem');


return callback ? let req = new FileSystemRequest(relPath, expr, result);
callback(new FileSystemRequest(relPath, expr, result)) : return callback ? callback(req) : req;
new FileSystemRequest(relPath, expr, result);
} }


/** /**
Expand Down Expand Up @@ -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, * Check to see if the given expression is a directory,
* @param {EFUNProxy} efuns The external proxy checking the directory. * @param {EFUNProxy} efuns The external proxy checking the directory.
Expand Down
51 changes: 32 additions & 19 deletions src/FileSystem.js
Expand Up @@ -7,7 +7,6 @@
*/ */
const const
MUDEventEmitter = require('./MUDEventEmitter'), MUDEventEmitter = require('./MUDEventEmitter'),
MUDExecutionContext = require('./MUDExcecutionContext'),
{ NotImplementedError } = require('./ErrorTypes'), { NotImplementedError } = require('./ErrorTypes'),
FS_NONE = 0, // No flags set FS_NONE = 0, // No flags set
FS_SYNC = 1 << 0, // The filesystem supports syncronous I/O. FS_SYNC = 1 << 0, // The filesystem supports syncronous I/O.
Expand Down Expand Up @@ -136,23 +135,23 @@ class FileSystem extends MUDEventEmitter {


/** /**
* Append data to a file; Creates file if needed. * Append data to a file; Creates file if needed.
* @param {string} path * @param {string} expr The file to append data to.
* @param {any} content * @param {any} content The content to write to file.
* @param {Function=} callback * @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' ? return typeof callback === 'function' ?
this.assertAsync(() => this.appendFileAsync(path, content, MUDExecutionContext.awaiter(callback))) : this.assertAsync(() => this.appendFileAsync(expr, content, MXC.awaiter(callback))) :
this.assertSync(() => this.appendFileSync(path, content)); this.assertSync(() => this.appendFileSync(expr, content));
} }


/** /**
* Append data to a file in async mode; Creates file if needed. * Append data to a file in async mode; Creates file if needed.
* @param {string} path * @param {string} expr The file to append data to.
* @param {any} content * @param {any} content The content to write to file.
* @param {function(Error):void} callback * @param {function(Error):void} callback A callback to fire with success status.
*/ */
appendFileAsync(path, content, callback) { appendFileAsync(expr, content, callback) {
throw new NotImplementedError('appendFileAsync'); throw new NotImplementedError('appendFileAsync');
} }


Expand All @@ -174,7 +173,7 @@ class FileSystem extends MUDEventEmitter {
createDirectory(expr, opts, callback) { createDirectory(expr, opts, callback) {
this.assertDirectories(); this.assertDirectories();
return typeof callback === 'function' ? 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)); this.assertSync(() => this.createDirectorySync(expr, opts));
} }


Expand All @@ -188,7 +187,7 @@ class FileSystem extends MUDEventEmitter {


createFile(expr, content, callback) { createFile(expr, content, callback) {
return typeof callback === 'function' ? 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)); this.assertSync(() => this.createFileSync(expr, content));
} }


Expand Down Expand Up @@ -219,7 +218,7 @@ class FileSystem extends MUDEventEmitter {
*/ */
deleteDirectory(expr, flags, callback) { deleteDirectory(expr, flags, callback) {
return typeof callback === 'function' ? 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)); this.assertSync(() => this.deleteDirectorySync(expr, flags));
} }


Expand All @@ -244,7 +243,7 @@ class FileSystem extends MUDEventEmitter {


deleteFile(expr, callback) { deleteFile(expr, callback) {
return typeof callback === 'function' ? 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)); this.assertSync(() => this.deleteFileSync(expr));
} }


Expand All @@ -263,6 +262,20 @@ class FileSystem extends MUDEventEmitter {
*/ */
getRealPath(expr) { return expr; } 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. * Translate an absolute path back into a virtual path.
* @param {string} expr The absolute path to translate. * @param {string} expr The absolute path to translate.
Expand Down Expand Up @@ -340,7 +353,7 @@ class FileSystem extends MUDEventEmitter {
*/ */
loadObject(expr, args, callback) { loadObject(expr, args, callback) {
return typeof callback === 'function' ? 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)); this.assertSync(() => this.loadObjectSync(expr, args));
} }


Expand Down Expand Up @@ -375,7 +388,7 @@ class FileSystem extends MUDEventEmitter {
flags = 0; flags = 0;
} }
return typeof callback === 'function' ? 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)); this.assertSync(() => this.readDirectorySync(expr, flags));
} }


Expand Down Expand Up @@ -440,7 +453,7 @@ class FileSystem extends MUDEventEmitter {
*/ */
stat(expr, callback) { stat(expr, callback) {
return typeof callback === 'function' ? 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)); this.assertSync(() => this.statSync(expr));
} }


Expand All @@ -463,7 +476,7 @@ class FileSystem extends MUDEventEmitter {


writeFile(expr, content, callback) { writeFile(expr, content, callback) {
return typeof callback === 'function' ? 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.assertSync(() => this.writeFileSync(expr, content));
} }


Expand Down
59 changes: 0 additions & 59 deletions src/MUDExcecutionContext.js

This file was deleted.

54 changes: 54 additions & 0 deletions src/MUDPromise.js
@@ -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'));
});
2 changes: 1 addition & 1 deletion src/MXC.js
Expand Up @@ -61,4 +61,4 @@ MXC.awaiter = function (callback) {
}; };
}; };


module.exports = MXC; module.exports = MXC;
12 changes: 12 additions & 0 deletions src/fs/DefaultFileSystem.js
Expand Up @@ -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. * Translate an absolute path back into a virtual path.
* @param {string} expr The absolute path to translate. * @param {string} expr The absolute path to translate.
Expand Down

0 comments on commit 929ff3f

Please sign in to comment.