Commit bfffb16f authored by Vadym Gidulian's avatar Vadym Gidulian

Merge branch '2-isready-configurable'

parents 5fec5ac0 76973523
...@@ -17,6 +17,7 @@ function PromisedValue(initialValue) { ...@@ -17,6 +17,7 @@ function PromisedValue(initialValue) {
Object.defineProperties(this, { Object.defineProperties(this, {
isReady: { isReady: {
configurable: true,
enumerable: true, enumerable: true,
get() { get() {
return initialized; return initialized;
...@@ -32,8 +33,12 @@ function PromisedValue(initialValue) { ...@@ -32,8 +33,12 @@ function PromisedValue(initialValue) {
value = newValue; value = newValue;
if (!initialized) { if (!initialized) {
resolvePromise(value); try {
this.isReady = true; // in case of defined setter
} catch (e) {/*ignore*/}
initialized = true; initialized = true;
resolvePromise(value);
} else { } else {
willBeReady = Promise.resolve(value); willBeReady = Promise.resolve(value);
} }
......
...@@ -3,10 +3,11 @@ const test = require('ava'); ...@@ -3,10 +3,11 @@ const test = require('ava');
const PV = require('../dist/index'); const PV = require('../dist/index');
test.cb('Promised value', t => { test.cb('Promised value', t => {
t.plan(6); t.plan(8);
const v = new PV(42); const v = new PV(42);
v.willBeReady.then(value => { v.willBeReady.then(value => {
t.is(v.isReady, true);
t.is(value, 123); t.is(value, 123);
}); });
...@@ -19,6 +20,7 @@ test.cb('Promised value', t => { ...@@ -19,6 +20,7 @@ test.cb('Promised value', t => {
t.is(v.value, 123); t.is(v.value, 123);
v.willBeReady.then(value => { v.willBeReady.then(value => {
t.is(v.isReady, true);
t.is(value, 123); t.is(value, 123);
t.end(); t.end();
}); });
...@@ -54,3 +56,46 @@ test('Property descriptors', t => { ...@@ -54,3 +56,46 @@ test('Property descriptors', t => {
t.false(v.hasOwnProperty('value')); t.false(v.hasOwnProperty('value'));
t.true(Object.keys(v).toString() === ['isReady', 'willBeReady'].toString()); t.true(Object.keys(v).toString() === ['isReady', 'willBeReady'].toString());
}); });
test.cb('isReady descriptor', t => {
t.plan(15);
const v = new PV(42);
let descriptor = Object.getOwnPropertyDescriptor(v, 'isReady');
t.true(descriptor.configurable);
Object.defineProperty(v, 'isReady', {
configurable: true,
enumerable: true,
get() { // 4 times
t.pass();
return descriptor.get.call(v);
},
set(newValue) { // 1 time
t.false(descriptor.get.call(v));
t.true(newValue);
}
});
v.willBeReady.then(value => {
t.is(v.isReady, true);
t.is(value, 123);
});
t.is(v.isReady, false);
t.is(v.value, 42);
v.value = 123;
t.is(v.isReady, true);
t.is(v.value, 123);
v.willBeReady.then(value => {
t.is(v.isReady, true);
t.is(value, 123);
t.end();
});
});
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment