Skip to content

Instantly share code, notes, and snippets.

@Gozala
Created September 17, 2010 09:37
Show Gist options
  • Save Gozala/583979 to your computer and use it in GitHub Desktop.
Save Gozala/583979 to your computer and use it in GitHub Desktop.
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Jetpack.
*
* The Initial Developer of the Original Code is Mozilla
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Irakli Gozalishvili <gozala@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
"use strict";
const { Trait } = require('traits/core');
function Get(key) this[key]
function Set(key, value) this[key] = value
/**
* @see https://jetpack.mozillalabs.com/sdk/latest/docs/#module/jetpack-core/capabilities
*/
const PublicTrait = Trait.compose({
/**
* Lazy property that represents map of public property descriptors.
* This property is a trait containing only subset of the API that is
* can be safely used to construct objects that will be exposed as a
* public API.
* @see #_public
*/
get _publicTrait() {
let trait = {},
keys = Object.getOwnPropertyNames(this);
for each (let key in keys) {
if ('_' === key.charAt(0) && '__iterator__' !== key )
continue;
let property = Object.getOwnPropertyDescriptor(this, key),
descriptor = {
configurable: property.configurable,
enumerable: property.enumerable
};
if (property.get)
descriptor.get = property.get.bind(this);
if (property.set)
descriptor.set = property.set.bind(this);
if ('value' in property) {
let value = property.value;
if ('function' === typeof value) {
descriptor.value = property.value.bind(this);
descriptor.writable = property.writable;
} else {
descriptor.get = Get.bind(this, key);
descriptor.set = Set.bind(this, key);
}
}
trait[key] = descriptor;
}
Object.defineProperty(this, '_publicTrait', { value: trait });
return this._publicTrait;
},
/**
* Lazy property that holds public API of this object. All the
* methods and properties that don't start with `"_"` character
* are considered to be public. All the methods and properties
* of this property represent proxies to the same named properties
* of this object. **This is the property must be used to expose
* this object to the public in a safe manner.**
* @example
*
* var PointTrait = Trait.compose({
* constructor: Point,
* _x: 0,
* _y: 0,
* get magnitude() this._x * this._x + this._y * this._y
* }, PublicTrait);
* function Point(x, y) Trait.create({}, PointTrait)._public
*/
get _public() {
let _public = Trait.create(
this.constructor ? this.constructor.prototype : {},
this._publicTrait
);
Object.defineProperty(this, '_public', { value: Object.freeze(_public) });
return this._public;
}
});
exports.PublicTrait = PublicTrait;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment