Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Changing implementation to make sync with an API changes in light-tra…

…its.
  • Loading branch information...
commit 863751210cd9254d304ec7e6685339df2ab913c0 1 parent 608e39d
@Gozala authored
Showing with 46 additions and 33 deletions.
  1. +46 −33 lib/promised-traits.js
View
79 lib/promised-traits.js
@@ -1,46 +1,59 @@
'use strict'
var Q = require('q'), defer = Q.defer, when = Q.when, isPromise = Q.isPromise
-, Promised = require('promised-utils').Promised
-, Trait = require('light-traits').Trait
+var Promised = require('promised-utils').Promised
+var Trait = require('light-traits').Trait
-function PromisedTrait() {
- var TPromisedProto = Object.create(PromisedTrait.prototype)
- , TPromised = Object.create(TPromisedProto)
- , trait = Trait.apply(null, arguments)
- , keys = Object.getOwnPropertyNames(trait)
+function promisify(trait) {
+ var pmap = {}
+ var map = {}
- for (var i = 0, ii = keys.length; i < ii; i++) {
- var key = keys[i]
- , property = TPromised[key] = trait[key]
- , descriptor = TPromisedProto[key] =
- { configurable: property.configurable
- , enumerable: property.enumerable
- }
+ Object.getOwnPropertyNames(trait).forEach(function (key) {
+ var method, getter, setter, property, descriptor
+ property = trait[key]
+ descriptor = {
+ configurable: property.configurable,
+ enumerable: property.enumerable
+ }
- if (property.conflict) continue
+ // Copying descriptor as a data value.
+ pmap[key] = { value: descriptor, enumerable: true }
+ // Copying property descriptor as a data value.
+ map[key] = { value: property, enumerable: true }
- var method = 'function' == typeof property.value ? property.value : null
- , getter = 'function' == typeof property.get ? property.get : null
- , setter = 'function' == typeof property.set ? property.set : null
+ // If it is not a conflict property
+ if (!property.conflict) {
- if (property.required) {
- if (getter) descriptor.get = Promised.sync(Getter(key))
- if (setter) descriptor.set = Promised.sync(Setter(key))
- continue
- }
+ method = 'function' == typeof property.value ? property.value : null
+ getter = 'function' == typeof property.get ? property.get : null
+ setter = 'function' == typeof property.set ? property.set : null
- if (getter) descriptor.get = property.get = Promised.sync(getter)
- if (setter) descriptor.set = property.set = Promised.sync(setter)
- if (method) {
- descriptor.writable = property.writable
- descriptor.value = property.value = Promised(method)
- } else {
- descriptor.get = Promised.sync(Getter(key))
- descriptor.set = Promised.sync(Setter(key))
+ // if property is marked as required we promisify it's getter and setters.
+ if (property.required) {
+ if (getter) descriptor.get = Promised.sync(Getter(key))
+ if (setter) descriptor.set = Promised.sync(Setter(key))
+ } else {
+ if (getter) descriptor.get = property.get = Promised.sync(getter)
+ if (setter) descriptor.set = property.set = Promised.sync(setter)
+ if (method) {
+ descriptor.writable = property.writable
+ descriptor.value = property.value = Promised(method)
+ } else {
+ descriptor.get = Promised.sync(Getter(key))
+ descriptor.set = Promised.sync(Setter(key))
+ }
+ }
}
- }
- return TPromised
+ })
+
+ return Object.create(Object.create(PromisedTrait.prototype, pmap), map);
+}
+
+function PromisedTrait() {
+ return promisify(Trait.apply(null, arguments))
+}
+PromisedTrait.compose = function compose () {
+ return promisify(Trait.compose.apply(Trait, arguments))
}
PromisedTrait.required = Trait.required
PromisedTrait.prototype = Object.freeze(Object.create(Trait.prototype,
Please sign in to comment.
Something went wrong with that request. Please try again.