Skip to content

Commit

Permalink
Remove proxy support (which will be probably available on v2)
Browse files Browse the repository at this point in the history
  • Loading branch information
fjorgemota committed May 21, 2018
1 parent 3c7b491 commit 65fc5e3
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 233 deletions.
65 changes: 1 addition & 64 deletions dist/Jimple.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
this._instances = new Map();
this._factories = new Set();
this._protected = new Set();
this._proxy = undefined;
values = isPlainObject(values) ? values : {};
Object.keys(values).forEach(function (key) {
this.set(key, values[key]);
Expand All @@ -117,7 +116,7 @@
} else if (this._instances.has(item)) {
obj = this._instances.get(item);
} else {
obj = item(this._proxy ? this._proxy : this);
obj = item(this);
if (!this._factories.has(item)) {
this._instances.set(item, obj);
}
Expand Down Expand Up @@ -178,68 +177,6 @@
checkDefined(this, key);
return this._items[key];
}
}], [{
key: "proxy",
value: function proxy(values) {
assert(typeof Proxy !== "undefined", "The actual environment does not support ES6 Proxy");
var container = new this();
// The variable 'hasValues' exists because Proxy can only really proxy
// attributes that EXIST on the object it is proxying.
// So hasValues basically contains all container keys, and all methods
// in the Container, in a way so we are able to respond correctly to
// that attribute.
var hasValues = {};
// Methods contain a dynamic list of all the methods of the application
// Which CANNOT be replaced in any way
var methods = Object.getOwnPropertyNames(Object.getPrototypeOf(container)).filter(function (key) {
return isFunction(container[key]);
});
methods.forEach(function (key) {
return hasValues[key] = 1;
});
var result = new Proxy(hasValues, {
get: function get(obj, key) {
var value = methods.includes(key) ? container[key].bind(container) : container.get(key);
if (key === "set") {
return function (k, val) {
obj[k] = 1;
return value(k, val);
};
}
return value;
},
set: function set(obj, key, value) {
assert(!methods.includes(key), "The key \"" + key + "\" isn't valid because it's the name of a method of the container");
obj[key] = 1;
container.set(key, value);
return true;
},
ownKeys: function ownKeys(obj) {
return container.keys();
},
has: function has(obj, key) {
return container.has(key);
},
getOwnPropertyDescriptor: function getOwnPropertyDescriptor(obj, key) {
if (!obj[key]) {
return undefined;
}
var isPrivate = methods.includes(key);
return {
'configurable': !isPrivate,
'writable': !isPrivate,
'enumerable': !isPrivate,
'value': isPrivate ? container[key] : container.get(key)
};
}
});
container._proxy = result;
values = isPlainObject(values) ? values : {};
Object.keys(values).forEach(function (key) {
result[key] = values[key];
});
return result;
}
}]);

return Jimple;
Expand Down
2 changes: 1 addition & 1 deletion dist/Jimple.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 1 addition & 70 deletions src/Jimple.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class Jimple {
this._instances = new Map();
this._factories = new Set();
this._protected = new Set();
this._proxy = undefined;
values = isPlainObject(values) ? values : {};
Object.keys(values).forEach(function(key) {
this.set(key, values[key]);
Expand All @@ -69,7 +68,7 @@ class Jimple {
} else if (this._instances.has(item)) {
obj = this._instances.get(item);
} else {
obj = item(this._proxy ? this._proxy : this);
obj = item(this);
if (!this._factories.has(item)) {
this._instances.set(item, obj);
}
Expand Down Expand Up @@ -164,74 +163,6 @@ class Jimple {
checkDefined(this, key);
return this._items[key];
}

/**
* Returns a proxyable object, that can be managed without calling get and
* set
* @param {Object?} [values] - An optional object whose keys and values will be associated in the container at initialization
* @throws If the platform does not support Proxy
* @return {Jimple} A proxyable Jimple contanier
*/
static proxy(values) {
assert(typeof Proxy !== "undefined", "The actual environment does not support ES6 Proxy");
let container = new this();
// The variable 'hasValues' exists because Proxy can only really proxy
// attributes that EXIST on the object it is proxying.
// So hasValues basically contains all container keys, and all methods
// in the Container, in a way so we are able to respond correctly to
// that attribute.
const hasValues = {};
// Methods contain a dynamic list of all the methods of the application
// Which CANNOT be replaced in any way
const methods = Object.getOwnPropertyNames(
Object.getPrototypeOf(container)
).filter((key) => isFunction(container[key]));
methods.forEach((key) => hasValues[key] = 1);
let result = new Proxy(hasValues, {
get(obj, key) {
let value = methods.includes(key) ?
container[key].bind(container) :
container.get(key);
if (key === "set") {
return function(k, val) {
obj[k] = 1;
return value(k, val);
}
}
return value;
},
set(obj, key, value) {
assert(!methods.includes(key), `The key "${key}" isn't valid because it's the name of a method of the container`);
obj[key] = 1;
container.set(key, value);
return true;
},
ownKeys(obj) {
return container.keys();
},
has(obj, key) {
return container.has(key);
},
getOwnPropertyDescriptor(obj, key) {
if (!obj[key]) {
return undefined;
}
let isPrivate = methods.includes(key);
return {
'configurable': !isPrivate,
'writable': !isPrivate,
'enumerable': !isPrivate,
'value': isPrivate ? container[key] : container.get(key)
}
}
});
container._proxy = result;
values = isPlainObject(values) ? values : {};
Object.keys(values).forEach(function(key) {
result[key] = values[key];
});
return result;
}
}

module.exports = Jimple;
98 changes: 0 additions & 98 deletions test/JimpleTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,104 +379,6 @@ describe("Jimple", function() {
expect(jimple.get("age")).to.equal(22);
});
});
describe("#proxy()", function() {
before(function() {
if (typeof Proxy === "undefined") {
// Skips tests of this method if Proxy is not supported..
this.skip();
}
});
it("should thrown a error on non-existent key", function() {
let jimple = Jimple.proxy();
expect(function() {
jimple["not-found-key"]
}).to.throw();
});

it("should support getting parameters", function() {
let jimple = Jimple.proxy();
jimple["age"] = 19;
expect(jimple["age"]).to.equal(19);
});
it("should support getting services", function() {
let jimple = Jimple.proxy();
jimple["age"] = function() {
return 19
};
expect(jimple["age"]).to.equal(19);
});
it("should support 'in' operator", function() {
let jimple = Jimple.proxy();
jimple["age"] = function() {
return 19
};
jimple["name"] = "xpto";
expect("age" in jimple).to.equal(true);
expect("name" in jimple).to.equal(true);
});
it("should support Object.getOwnPropertyNames", function() {
let jimple = Jimple.proxy({
"country": "Brazil"
});
jimple["age"] = function() {
return 19
};
jimple["name"] = "xpto";
expect(jimple["age"]).to.equal(19);
expect(jimple["name"]).to.equal("xpto");
expect(jimple["country"]).to.equal("Brazil");
let keys = Object.getOwnPropertyNames(jimple);
expect(keys).to.have.length(3);
expect(keys).to.include("age");
expect(keys).to.include("name");
expect(keys).to.include("country");
});
it("should support Object.getOwnPropertyDescriptor", function() {
let jimple = Jimple.proxy({
"country": "Brazil"
});
jimple.set("age", 19);
jimple["name"] = "xpto";
expect(jimple["age"]).to.equal(19);
expect(jimple["name"]).to.equal("xpto");
let age = Object.getOwnPropertyDescriptor(jimple, "age");
expect(age.writable).to.equal(true);
expect(age.value).to.equal(19);
let name = Object.getOwnPropertyDescriptor(jimple, "name");
expect(name.writable).to.equal(true);
expect(name.value).to.equal("xpto");
let country = Object.getOwnPropertyDescriptor(jimple, "country");
expect(country.writable).to.equal(true);
expect(country.value).to.equal("Brazil");
let non_existent_key = Object.getOwnPropertyDescriptor(jimple, "non-existent-key");
expect(non_existent_key).to.equal(undefined);
expect(function() {
let get = Object.getOwnPropertyDescriptor(jimple, "get");
expect(get.writable).to.equal(false);
}).to.throw();
});
it("should not overwrite methods names via set", function() {
let jimple = Jimple.proxy();
expect(jimple.get).to.be.a('function');
expect(function() {
jimple["get"] = 20
}).to.throw();
})
it("should not overwrite methods names via initial parameters", function() {
expect(function() {
let jimple = Jimple.proxy({
"get": 42
});
}).to.throw();
})
it("should work correctly with private attribute", function() {
let jimple = Jimple.proxy({
"_item": 42
});
jimple["_protected"] = (c) => c["_item"]
expect(jimple["_protected"]).to.equal(42);
})
})
describe('#provider', function() {
it("should register a provider created by the shorthand static method", function() {
let jimple = new Jimple();
Expand Down

0 comments on commit 65fc5e3

Please sign in to comment.