Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
promised-value
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
tools
promised-value
Commits
bfffb16f
Commit
bfffb16f
authored
Oct 31, 2018
by
Vadym Gidulian
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '2-isready-configurable'
parents
5fec5ac0
76973523
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
734 additions
and
5 deletions
+734
-5
index.js
src/index.js
+9
-4
test.js
test/test.js
+46
-1
yarn.lock
yarn.lock
+679
-0
No files found.
src/index.js
View file @
bfffb16f
...
@@ -7,8 +7,8 @@ function PromisedValue(initialValue) {
...
@@ -7,8 +7,8 @@ function PromisedValue(initialValue) {
throw
new
TypeError
(
"Cannot call a class as a function"
);
throw
new
TypeError
(
"Cannot call a class as a function"
);
}
}
let
initialized
=
false
;
let
initialized
=
false
;
let
value
=
initialValue
;
let
value
=
initialValue
;
let
resolvePromise
;
let
resolvePromise
;
let
willBeReady
=
new
Promise
(
resolve
=>
{
let
willBeReady
=
new
Promise
(
resolve
=>
{
...
@@ -17,7 +17,8 @@ function PromisedValue(initialValue) {
...
@@ -17,7 +17,8 @@ function PromisedValue(initialValue) {
Object
.
defineProperties
(
this
,
{
Object
.
defineProperties
(
this
,
{
isReady
:
{
isReady
:
{
enumerable
:
true
,
configurable
:
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
);
}
}
...
...
test/test.js
View file @
bfffb16f
...
@@ -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
();
});
});
yarn.lock
View file @
bfffb16f
This source diff could not be displayed because it is too large. You can
view the blob
instead.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment