Commit 76973523 authored by Vadym Gidulian's avatar Vadym Gidulian

Closed #2 (Make `isReady` configurable)

parent 5fec5ac0
......@@ -7,8 +7,8 @@ function PromisedValue(initialValue) {
throw new TypeError("Cannot call a class as a function");
}
let initialized = false;
let value = initialValue;
let initialized = false;
let value = initialValue;
let resolvePromise;
let willBeReady = new Promise(resolve => {
......@@ -17,7 +17,8 @@ function PromisedValue(initialValue) {
Object.defineProperties(this, {
isReady: {
enumerable: true,
configurable: true,
enumerable: true,
get() {
return initialized;
}
......@@ -32,8 +33,12 @@ function PromisedValue(initialValue) {
value = newValue;
if (!initialized) {
resolvePromise(value);
try {
this.isReady = true; // in case of defined setter
} catch (e) {/*ignore*/}
initialized = true;
resolvePromise(value);
} else {
willBeReady = Promise.resolve(value);
}
......
......@@ -3,10 +3,11 @@ const test = require('ava');
const PV = require('../dist/index');
test.cb('Promised value', t => {
t.plan(6);
t.plan(8);
const v = new PV(42);
v.willBeReady.then(value => {
t.is(v.isReady, true);
t.is(value, 123);
});
......@@ -19,6 +20,7 @@ test.cb('Promised value', t => {
t.is(v.value, 123);
v.willBeReady.then(value => {
t.is(v.isReady, true);
t.is(value, 123);
t.end();
});
......@@ -54,3 +56,46 @@ test('Property descriptors', t => {
t.false(v.hasOwnProperty('value'));
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