pax_global_header00006660000000000000000000000064141763077000014517gustar00rootroot0000000000000052 comment=f4f46bf4e630485c7833353117bece3c7d496c0a class-plus-2.0.0/000077500000000000000000000000001417630770000136045ustar00rootroot00000000000000class-plus-2.0.0/.npmignore000066400000000000000000000000371417630770000156030ustar00rootroot00000000000000.gitignore node_modules/ work/ class-plus-2.0.0/README.md000066400000000000000000000367601417630770000150770ustar00rootroot00000000000000# Overview `class-plus` is a simple class builder utility, which adds support for specifying class member variables as well as mix-ins. It does this by providing a custom API that augments an ES2015 class with custom features. ## Why Native JavaScript classes (introduced in ECMAScript 2015) are a welcome addition to the language, but two things bother me about them: 1. You cannot declare properties inside the class (you have to do it in the constructor, which I do not like). - Yes, there is a [TC39 proposal](https://github.com/tc39/proposal-class-fields) for this, but it's not yet available to normal users. 2. Classes can only inherit from one single parent class. - I guess mix-ins are [technically possible](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#Mix-ins), but the syntax is confusing in my opinion. I wrote the class-plus module to provide these features to ES2015 classes using a simple API, while offering some additional niceties. The full set of features are: - Provide an easy API to generate classes with additions. - Support for adding properties right above the class definition (as close to inside it as we can get). - Support for multiple mix-ins, will merge both properties and methods from multiple classes. - Support for adding static properties. - Optional easy way to mix-in the Node.js [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter) class. - Optional hook system (async event emitters). - Optional automatic conversion of callback methods to async ones. - No dependencies # Usage Use [npm](https://www.npmjs.com/) to install the module: ``` npm install class-plus ``` Then use `require()` to load it in your code: ```javascript const Class = require('class-plus'); ``` Then call `Class()` to create classes. The function takes two arguments: an object with properties for the class, and the class definition itself (in native ES2015 format). Example: ```javascript const Animal = Class({ // class member variables nickname: '', color: '' }, class Animal { // class methods constructor(new_name, new_color) { this.nickname = new_name; this.color = new_color; } getInfo() { return("Nickname: " + this.nickname + "\nColor: " + this.color); } }); ``` This defines a class called `Animal`, with two member variables, `nickname` and `color`, a constructor and a `getInfo()` method which returns the nickname and color. Usage of this class is exactly what you would expect: ```javascript var dog = new Animal('Spot', 'Green'); console.log( dog.getInfo() ); ``` Of course, you can also access the class member variables, as all members are public. ```javascript dog.nickname = 'Skippy'; dog.color = 'Blue'; console.log( dog.getInfo() ); ``` ## Creating Subclasses To create a subclass that inherits from a base class, use the built-in `extends` keyword: ```javascript const Bear = Class({ // define a new member variable wants: 'Honey' }, class Bear extends Animal { // and a new method roar() { console.log("Roar! Give me " + this.wants + "!"); } }); ``` This defines a `Bear` class which inherits from the base `Animal` class, including its constructor. What we did is extend the base class by introducing a new member variable `wants`, and a new method `roar()`. Everything else from the base class will be present in subclass instances. ```javascript var grizzly = new Bear('Fred', 'Brown'); console.log( grizzly.getInfo() ); grizzly.wants = 'blood'; grizzly.roar(); ``` ## Calling Superclass methods You can also explicitly invoke a superclass method using the built-in `super` keyword: ```javascript const Bear = Class({ // define a new member variable wants: 'Honey' }, class Bear extends Animal { // and a new method roar() { console.log("Roar! Give me " + this.wants + "!"); } // override base class method getInfo() { // first, get info from base class var info = super.getInfo(); // append bear info and return combined info info += "\nWants: " + this.wants; return info; } }); ``` So here we are overriding the base class `getInfo()` method, but the first thing we do is call the superclass method of the same name. This is done using the `super` keyword, which points to the parent class. ## Static Members While the native built-in class system supports [static methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#Static_methods) it doesn't support non-function static *members*. Using class-plus you can define static class members (variables or methods) by using the `__static` property. These members do not become part of class instances, but instead live inside the class reference object, and must be accessed that way too. Example class definition: ```javascript const Beer = Class({ // static members __static: { types: ['Lager', 'Ale', 'Stout', 'Barleywine'] }, // class member variables name: '', type: '' }, class Beer { // standard class syntax for methods // class constructor constructor(new_name, new_type) { this.name = new_name; if (Beer.types.indexOf(new_type) == -1) throw("Type not known: " + new_type); this.type = new_type; } getInfo() { return("Name: " + this.name + "\nType: " + this.type); } }); ``` Here we define a `Beer` class which has a static member defined in the `__static` property. Anything placed there will *not* be propagated to class instances, and must be accessed using the class reference variable instead (e.g. `Beer` in the above example). As you can see in the constructor, we are checking the new type against the `types` array which is declared static, so we are getting to the list by using the syntax: `Beer.types` rather than `this.types`. If you were to change `Beer.types` later on, then *all* classes would see the changes instantly. The content is effectively shared. ## Mix-ins While achieving mix-ins (essentially multiple inheritance) is technically [possible](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#Mix-ins) using ES2015 classes, the syntax leaves much to be desired. Using class-plus you can simply merge in one or more "mix-in" classes using the `__mixins` property. This will import all the variables, methods and static members from the specified classes, excluding constructors. Example: ```javascript const Liquid = Class({ flavor: "sweet" }, class Liquid {}); const Glass = Class({ size: 8 }, class Glass {}); const Soda = Class({ __mixins: [ Liquid, Glass ] }, class Soda { drink() { console.log("Yum, " + this.size + " oz of " + this.flavor + " drink!"); } }); ``` In the above example we are importing all the variables and methods of the `Liquid` and `Glass` classes into our `Soda` class. Then, they are accessible using the normal `this` keyword, as if they were defined in the class. I often use mix-ins to spread my larger classes across multiple source files, like this: ```js const Soda = Class({ __mixins: [ require('./liquid.js'), require('./glass.js') ] }, ... ); ``` Note that mix-in properties and methods will *only* be imported if they aren't already defined in your class. Meaning, they will not clobber any existing class members. If the mix-in classes you are importing have their own parent classes, those should be separately listed in the `__mixins` array. Meaning, the prototype chain of the mix-ins is not automatically imported -- only the top-level methods and properties on the specified class are merged in. You'll need to specify parent classes if you want those merged in as well. ## Event Emitters I find myself frequently inheriting from Node's [EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter) in my classes, so I added a shortcut for it in class-plus. Simply include a property named `__events` and set it to `true`, and your class will magically become an EventEmitter. Example: ```javascript const Party = Class({ __events: true }, class Party { start() { console.log("Let's get this party started!"); this.emit('dance'); } }); var birthday = new Party(); birthday.on('dance', function() { console("I'm dancing!"); } ); birthday.start(); ``` Setting the `__events` property is equivalent to including Node's [EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter) class in your [mix-ins](#mix-ins) array: ```javascript const Party = Class({ __mixins: [ require('events').EventEmitter ] }, ... ); ``` So `__events` is really just a shortcut for that. ## Hooks Taking event listeners one step further, class-plus introduces an optional "hook" system for use in your classes, where custom events can be hooked, and you can run *asynchronous* operations in each listener. If multiple listeners are registered on a given hook, they are all fired in sequence. If any listener returns an error, the sequence is aborted, and the error passed to the original caller. Your listeners can be either callback based, or native [async](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) functions. To enable hooks in your class, simply include a `__hooks` property and set it to `true`. Example: ```js const Party = Class({ __hooks: true }, class Party { start() { console.log("The party has finally started."); } }); var birthday = new Party(); birthday.registerHook( 'prestart', function(item, callback) { // delay the party by 100ms setTimeout( function() { callback(); }, 100 ); } ); birthday.fireHook( 'prestart', "Get ready!", function(err) { // all prestart hooks completed, let's go // this will run about 100ms later birthday.start(); }); ``` The idea here is similar with events, where one or more listeners can be registered, but in this case the hooks fire in an asynchronous manner, each with a callback to advance to the next listener, or to complete the hook sequence. In fact, the whole system can be used with native [async](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) functions (in Node 8+). Example: ```js const Party = Class({ __hooks: true }, class Party { start() { console.log("The party has finally started."); } }); (async function() { var birthday = new Party(); birthday.registerHook( 'prestart', async function(item) { // do something async here await myfunc(); } ); await birthday.fireHook( 'prestart', "Get ready!"); // all async prestart hooks completed, let's go birthday.start(); })(); ``` ## Async/Await Conversion Node.js version 8 introduced native support for the [async](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function)/[await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await) pattern. If your class has callback-based methods that you want to auto-convert into async/await, simply declare a `__asyncify` property, and set it to `true`: ```js var Sleeper = Class({ __asyncify: true }, class Sleeper { sleep(milliseconds, callback) { // sleep for N milliseconds, then fire callback setTimeout( function() { callback(); }, milliseconds ); } }); ``` This will automatically detect all your callback-based methods in the class, and then convert them to async using Node's [util.promisify()](https://nodejs.org/api/util.html#util_util_promisify_original), making them instantly ready for async/await. Example usage: ```js var snooze = new Sleeper(); async function main() { await snooze.sleep( 1000 ); // waits for 1 second here console.log("This happened 1 second later!"); }; main(); ``` The automatic detection mechanism looks inside your method signatures for argument lists ending with the name `callback` (or `cb`). If you use any other variable name for the callback argument, this will skip over it. Also, class-plus will take care not asyncify a function that is already async. If you only want *some* of your methods to be asyncified, set the `__asyncify` property to an array containing all the method names. Example: ```js { // only asyncify some methods __asyncify: ["sleep"] } ``` Alternatively, you can set `__asyncify` to a [regular expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions), to match against all of your class method names. If the pattern matches, the function will be converted. Example: ```js { // only asyncify some methods __asyncify: /^(sleep|someOtherFunction)$/ } ``` Note that in order for your methods to be async-compatible, they must accept a callback as the final argument, and that callback must be called using the standard Node.js convention (i.e. `(err)` or `(err, result)`). The error *must* be the first argument sent to the callback (or false/undefined on success), and a result, if any, must be the second argument. ### Async Return Values If your callback is fired using the typical `(err, result)` arguments, such as this: ```js const Soda = Class({ __asyncify: true }, class Soda { pour(callback) { setTimeout( function() { callback( null, "8oz" ); }, 250 ); } }); ``` Then you can access the result using async in this way: ```js let drink = new Soda(); try { let result = await drink.pour(); console.log(result); } catch (err) { throw err; } ``` However, if your callback has *multiple result arguments*, like this: ```js const Soda = Class({ __asyncify: true }, class Soda { pour(callback) { setTimeout( function() { callback( null, 8, "oz" ); }, 250 ); } }); ``` They will be returned in an array which you can destruct like this: ```js let drink = new Soda(); try { let [amount, units] = await drink.pour(); console.log(amount, units); } catch (err) { throw err; } ``` Finally, for ultimate control over the async conversion, you can pre-declare the names of your callback arguments in the `__asyncify` property, by setting it to an object containing the function names as keys, and the argument names as array items. Then, the result can be awaited as a destructed object with named keys. Here is how to set this up in the class: ```js const Soda = Class({ __asyncify: { pour: ['amount', 'units'] // declare pour() callback arg names here } }, class Soda { pour(callback) { setTimeout( function() { callback( null, 8, "oz" ); // fire callback as usual }, 250 ); } }); ``` And here is how to await it: ```js let drink = new Soda(); try { let { amount, units } = await drink.pour(); console.log(amount, units); } catch (err) { throw err; } ``` The idea here is that the calling code can select *which of the arguments it wants*. For example, we can omit `units` and only fetch `amount`: ```js let { amount } = await drink.pour(); ``` # License **The MIT License** *Copyright (c) 2019 - 2022 Joseph Huckaby* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. class-plus-2.0.0/class.js000066400000000000000000000131161417630770000152510ustar00rootroot00000000000000// Simple class builder utility // Copyright (c) 2019 - 2022 Joseph Huckaby // Released under the MIT License var events = require("events"); class HookHelper { registerHook(name, handler) { // register a hook, called by plugins // hooks are different than event listeners, as they are called in async sequence if (!this.hooks) this.hooks = {}; if (!this.hooks[name]) this.hooks[name] = []; this.hooks[name].push( handler ); } removeHook(name, handler) { // remove single hook listener or all of them if (!this.hooks) this.hooks = {}; if (handler) { if (this.hooks[name]) { var idx = this.hooks[name].indexOf(handler); if (idx > -1) this.hooks[name].splice( idx, 1 ); if (!this.hooks[name].length) delete this.hooks[name]; } } else delete this.hooks[name]; } fireHook(name, thingy, callback) { // fire all listeners for a given hook // calls both sync and async listeners var self = this; if (!this.hooks) this.hooks = {}; // now do the normal async dance if (!this.hooks[name] || !this.hooks[name].length) { process.nextTick( callback ); return; } // fire hooks in async series var idx = 0; var iterator = function() { var handler = self.hooks[name][idx++]; if (!handler) return callback(); if (handler.constructor.name === "AsyncFunction") { // async style handler.call( self, thingy ).then( iterator, callback ); } else { // callback-style var nextThread = 0; handler.call( self, thingy, function(err) { if (err) return callback(err); // ensure async, to prevent call stack overflow if (nextThread) iterator(); else process.nextTick( iterator ); } ); nextThread++; } }; iterator(); } } // HookHelper const asyncify = function(origFunc, argNames) { // asyncify a class method // resolution will be array of callback arguments (err, results, etc.) // also supports classic callback calling convention return async function(...args) { var self = this; // sniff for classic callback style if (typeof(args[ args.length - 1 ]) == 'function') return origFunc.call(self, ...args); // promise/async style return new Promise( (resolve, reject) => { origFunc.call(self, ...args, function(...results) { let err = results.shift(); if (err) reject(err); else if (argNames) { var resultsObj = {}; argNames.forEach( function(name, idx) { resultsObj[name] = results[idx]; } ); resolve( resultsObj ); } else resolve( (results.length > 1) ? results : results[0] ); }); }); }; }; module.exports = function Class(args, obj) { // class builder var proto = obj.prototype; // handle static variables if (args.__static) { for (var key in args.__static) { obj[key] = args.__static[key]; } } // optional asyncify if (args.__asyncify) { if (typeof(args.__asyncify) == 'object') { if (Array.isArray(args.__asyncify)) { // specific set of methods to asyncify args.__asyncify.forEach( function(key) { if (proto[key] && !proto[key].__async && (proto[key].constructor.name !== "AsyncFunction")) { proto[key] = asyncify( proto[key] ); proto[key].__async = true; } } ); } else if (args.__asyncify instanceof RegExp) { // regular expression to match against method names Object.getOwnPropertyNames(proto).forEach( function(key) { if (!key.match(/^(__name|constructor|prototype)$/) && (typeof(proto[key]) == 'function') && key.match(args.__asyncify) && !proto[key].__async && (proto[key].constructor.name !== "AsyncFunction")) { proto[key] = asyncify( proto[key] ); proto[key].__async = true; } }); } else { // hash to define callback arg names for each async func for (var key in args.__asyncify) { if (proto[key] && !proto[key].__async && (proto[key].constructor.name !== "AsyncFunction")) { proto[key] = asyncify( proto[key], args.__asyncify[key] ); proto[key].__async = true; } } } } // is object else { // try to sniff out callback based methods using reflection Object.getOwnPropertyNames(proto).forEach( function(key) { if (!key.match(/^(__name|constructor|prototype)$/) && (typeof(proto[key]) == 'function') && (proto[key].toString().match(/^\s*\S+\s*\([^\)]*(callback|cb)\s*\)\s*\{/)) && !proto[key].__async && (proto[key].constructor.name !== "AsyncFunction")) { proto[key] = asyncify( proto[key] ); proto[key].__async = true; } }); } } // __asyncify // merge in mixins var mixins = args.__mixins || []; if (args.__events) mixins.unshift( events.EventEmitter ); if (args.__hooks) mixins.unshift( HookHelper ); for (var idx = 0, len = mixins.length; idx < len; idx++) { var class_obj = mixins[idx]; var class_proto = class_obj.prototype; if (!class_proto) throw "All items specified in __mixins must be classes."; // prototype members Object.getOwnPropertyNames(class_proto).forEach( function(key) { if (!key.match(/^(__name|constructor|prototype)$/) && !(key in proto)) { proto[key] = class_proto[key]; } }); // static members Object.getOwnPropertyNames(class_obj).forEach( function(key) { if (!key.match(/^(name|length|prototype)$/) && !(key in obj)) { obj[key] = class_obj[key]; } }); } // foreach mixin // asyncify fireHook if applicable if (args.__hooks && !proto.fireHook.__async) { proto.fireHook = asyncify( proto.fireHook ); proto.fireHook.__async = true; } // add non-meta args as prototype properties for (var key in args) { if (!key.match(/^__(static|asyncify|events|hooks)/)) { proto[key] = args[key]; } } return obj; }; class-plus-2.0.0/package.json000066400000000000000000000012351417630770000160730ustar00rootroot00000000000000{ "name": "class-plus", "version": "2.0.0", "description": "A simple module for building and extending classes, with mixins and more.", "author": "Joseph Huckaby ", "homepage": "https://github.com/jhuckaby/class-plus", "license": "MIT", "main": "class.js", "scripts": { "test": "pixl-unit test.js" }, "repository": { "type": "git", "url": "https://github.com/jhuckaby/class-plus" }, "bugs": { "url": "https://github.com/jhuckaby/class-plus/issues" }, "keywords": [ "oop", "class", "mixins", "async", "await" ], "dependencies": {}, "devDependencies": { "pixl-unit": "^1.0.0" } } class-plus-2.0.0/test.js000066400000000000000000000174551417630770000151350ustar00rootroot00000000000000// Unit tests for class-plus var Class = require('./class.js'); class GenericBaseClass { base() { return "BASE"; } } exports.tests = [ function basic(test) { var MyClass = Class({}, class Foo { foo() { return "bar"; } }); var instance = new MyClass(); test.ok( instance.foo() === "bar", "Foo does not equal bar" ); test.done(); }, function props(test) { var MyClass = Class({ prop1: "hello", __special: 12345 }, class Foo { foo() { return "bar"; } }); var instance = new MyClass(); test.ok( instance.foo() === "bar", "Foo does not equal bar" ); test.ok( instance.prop1 === "hello", "Property prop1 missing from instance" ); test.ok( instance.__special === 12345, "Property __special missing from instance" ); test.done(); }, function static(test) { var MyClass = Class({ __static: { prop1: "hello" } }, class Foo { foo() { return "bar"; } }); var instance = new MyClass(); test.ok( instance.foo() === "bar", "Foo does not equal bar" ); test.ok( MyClass.prop1 === "hello", "Static prop found in class object" ); test.ok( !instance.prop1, "Static prop should NOT exist in instance" ); test.done(); }, function inheritance(test) { var MyClass = Class({}, class Foo extends GenericBaseClass { foo() { return "bar"; } bar() { return "baz"; } }); var instance = new MyClass(); test.ok( instance.foo() === "bar", "Foo does not equal bar" ); test.ok( instance.base() === "BASE", "Foo does not have base class method" ); var MySubClass = Class({}, class Bar extends MyClass { foo() { return "bar2"; } baz() { return "zar"; } }); var instance2 = new MySubClass(); test.ok( instance2.foo() === "bar2", "Bar (subclass) foo() does not equal bar" ); test.ok( instance2.baz() === "zar", "Bar (subclass) baz() does not equal zar" ); test.ok( instance2.base() === "BASE", "Bar (subclass) base() does not have base class method" ); test.done(); }, function mixins(test) { // mixin base class and another class var MyOtherClass = Class({ prop1: "hello" }, class Other { bar() { return "baz"; } }); var MyClass = Class({ __mixins: [ GenericBaseClass, MyOtherClass ] }, class Foo { foo() { return "bar"; } }); var instance = new MyClass(); test.ok( instance.foo() === "bar", "Foo.foo() does not equal bar" ); test.ok( instance.bar() === "baz", "Foo.bar() does not equal baz" ); test.ok( instance.base() === "BASE", "Foo.base() does not equal BASE" ); test.ok( instance.prop1 === "hello", "Property missing from instance" ); test.done(); }, function events(test) { test.expect( 2 ); var MyClass = Class({ __events: true }, class Foo { foo() { return "bar"; } }); var instance = new MyClass(); test.ok( instance.foo() === "bar", "Foo does not equal bar" ); instance.on('party', function() { test.ok( true, "Went to party" ); }); instance.emit( 'party' ); test.done(); }, function hooks(test) { var MyClass = Class({ __hooks: true }, class Foo { foo() { return "bar"; } }); var instance = new MyClass(); test.ok( instance.foo() === "bar", "Foo does not equal bar" ); var went_to_party = false; instance.registerHook( 'party', function (thingy, callback) { test.ok( true, "Went to party" ); test.ok( thingy === "present", "Received a present" ); went_to_party = true; setTimeout( function() { callback(); }, 100 ); // delay completion of hook }); var went_home = false; instance.registerHook( 'party', function(thingy, callback) { test.ok( true, "Went home" ); went_home = true; callback(); // same thread }); instance.fireHook( 'party', "present", function(err) { test.ok( !err, "Unexpected error from party hook: " + err ); test.ok( went_to_party, "Party hook was not fired" ); test.ok( went_home, "Second party hook was not fired" ); test.done(); } ); }, function asyncify(test) { var MyClass = Class({ __asyncify: true }, class Foo { foo() { return "bar"; } sleep(ms, callback) { setTimeout( function() { callback(); }, ms ); } }); var instance = new MyClass(); test.ok( instance.foo() === "bar", "Foo does not equal bar" ); (async function() { var before = Date.now(); await instance.sleep( 100 ); var elapsed = Date.now() - before; // allowing for some error here, as clock corrections do happen test.ok( elapsed > 95, "Unexpected elapsed time for await sleep test: " + elapsed ); test.done(); })(); }, function asyncifyWithError(test) { test.expect(2); var MyClass = Class({ __asyncify: true }, class Foo { foo() { return "bar"; } pour(callback) { callback( new Error("frogs") ); } }); var instance = new MyClass(); test.ok( instance.foo() === "bar", "Foo does not equal bar" ); (async function() { try { var result = await instance.pour(); } catch(err) { test.ok( err.message === 'frogs', "Unexpected error message: " + err.message ); } test.done(); })(); }, function asyncifyWithSingleArg(test) { var MyClass = Class({ __asyncify: true }, class Foo { foo() { return "bar"; } pour(callback) { callback(null, "8oz"); } }); var instance = new MyClass(); test.ok( instance.foo() === "bar", "Foo does not equal bar" ); (async function() { var result = await instance.pour(); test.ok( result === "8oz", "Unexpected result: " + result ); test.done(); })(); }, function asyncifyWithMultiArgs(test) { var MyClass = Class({ __asyncify: true }, class Foo { foo() { return "bar"; } pour(callback) { callback(null, 8, "oz"); } }); var instance = new MyClass(); test.ok( instance.foo() === "bar", "Foo does not equal bar" ); (async function() { var [amount, units] = await instance.pour(); test.ok( amount === 8, "Unexpected amount: " + amount ); test.ok( units === "oz", "Unexpected units: " + units ); test.done(); })(); }, function asyncifyWithNamedArgs(test) { var MyClass = Class({ __asyncify: { pour: ['amount', 'units'] } }, class Foo { foo() { return "bar"; } pour(callback) { callback(null, 8, "oz"); } }); var instance = new MyClass(); test.ok( instance.foo() === "bar", "Foo does not equal bar" ); (async function() { var {amount, units} = await instance.pour(); test.ok( amount === 8, "Unexpected amount: " + amount ); test.ok( units === "oz", "Unexpected units: " + units ); test.done(); })(); }, function asyncifyWithNamedArgsSelective(test) { var MyClass = Class({ __asyncify: { pour: ['amount', 'units'] } }, class Foo { foo() { return "bar"; } pour(callback) { callback(null, 8, "oz"); } }); var instance = new MyClass(); test.ok( instance.foo() === "bar", "Foo does not equal bar" ); (async function() { var {amount} = await instance.pour(); test.ok( amount === 8, "Unexpected amount: " + amount ); test.done(); })(); }, function asyncHooks(test) { var MyClass = Class({ __asyncify: true, __hooks: true }, class Foo { foo() { return "bar"; } sleep(ms, callback) { setTimeout( function() { callback(); }, ms ); } }); var instance = new MyClass(); test.ok( instance.foo() === "bar", "Foo does not equal bar" ); var went_to_party = false; instance.registerHook( 'party', async function (thingy) { test.ok( true, "Went to party" ); test.ok( thingy === "present", "Received a present" ); went_to_party = true; await this.sleep(100); }); (async function() { var before = Date.now(); await instance.fireHook( 'party', "present" ); var elapsed = Date.now() - before; // allowing for some error here, as clock corrections do happen test.ok( elapsed > 95, "Unexpected elapsed time for async hook test: " + elapsed ); test.done(); })(); } ];