Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
R
rsvp.js
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
rsvp.js
Commits
46330d04
Commit
46330d04
authored
Feb 26, 2020
by
Romain Courteaud
1
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Queue: add a thenable argument to resolve with a Queue
parent
29822afe
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
138 additions
and
31 deletions
+138
-31
lib/rsvp/queue.js
lib/rsvp/queue.js
+24
-26
test/tests/extension_test.js
test/tests/extension_test.js
+114
-5
No files found.
lib/rsvp/queue.js
View file @
46330d04
...
@@ -12,7 +12,7 @@ function ResolvedQueueError(message) {
...
@@ -12,7 +12,7 @@ function ResolvedQueueError(message) {
ResolvedQueueError
.
prototype
=
new
Error
();
ResolvedQueueError
.
prototype
=
new
Error
();
ResolvedQueueError
.
prototype
.
constructor
=
ResolvedQueueError
;
ResolvedQueueError
.
prototype
.
constructor
=
ResolvedQueueError
;
var
Queue
=
function
()
{
var
Queue
=
function
(
thenable
)
{
var
queue
=
this
,
var
queue
=
this
,
promise_list
=
[],
promise_list
=
[],
promise
,
promise
,
...
@@ -30,6 +30,26 @@ var Queue = function() {
...
@@ -30,6 +30,26 @@ var Queue = function() {
}
}
}
}
function
checkPromise
(
next_promise
)
{
promise_list
.
push
(
next_promise
);
// Handle pop
promise_list
.
push
(
next_promise
.
then
(
function
(
fulfillmentValue
)
{
promise_list
.
splice
(
0
,
2
);
if
(
promise_list
.
length
===
0
)
{
fulfill
(
fulfillmentValue
);
}
else
{
return
fulfillmentValue
;
}
},
function
(
rejectedReason
)
{
promise_list
.
splice
(
0
,
2
);
if
(
promise_list
.
length
===
0
)
{
reject
(
rejectedReason
);
}
else
{
throw
rejectedReason
;
}
}));
}
promise
=
new
Promise
(
function
(
done
,
fail
)
{
promise
=
new
Promise
(
function
(
done
,
fail
)
{
fulfill
=
function
(
fulfillmentValue
)
{
fulfill
=
function
(
fulfillmentValue
)
{
if
(
resolved
)
{
return
;}
if
(
resolved
)
{
return
;}
...
@@ -47,13 +67,7 @@ var Queue = function() {
...
@@ -47,13 +67,7 @@ var Queue = function() {
};
};
},
canceller
);
},
canceller
);
promise_list
.
push
(
resolve
());
checkPromise
(
resolve
(
thenable
));
promise_list
.
push
(
promise_list
[
0
].
then
(
function
()
{
promise_list
.
splice
(
0
,
2
);
if
(
promise_list
.
length
===
0
)
{
fulfill
();
}
}));
queue
.
cancel
=
function
()
{
queue
.
cancel
=
function
()
{
if
(
resolved
)
{
return
;}
if
(
resolved
)
{
return
;}
...
@@ -76,25 +90,9 @@ var Queue = function() {
...
@@ -76,25 +90,9 @@ var Queue = function() {
throw
new
ResolvedQueueError
();
throw
new
ResolvedQueueError
();
}
}
next_promise
=
last_promise
.
then
(
done
,
fail
);
promise_list
.
push
(
next_promise
);
// Handle pop
// Handle pop
promise_list
.
push
(
next_promise
.
then
(
function
(
fulfillmentValue
)
{
checkPromise
(
last_promise
.
then
(
done
,
fail
));
promise_list
.
splice
(
0
,
2
);
if
(
promise_list
.
length
===
0
)
{
fulfill
(
fulfillmentValue
);
}
else
{
return
fulfillmentValue
;
}
},
function
(
rejectedReason
)
{
promise_list
.
splice
(
0
,
2
);
if
(
promise_list
.
length
===
0
)
{
reject
(
rejectedReason
);
}
else
{
throw
rejectedReason
;
}
}));
return
this
;
return
this
;
};
};
...
...
test/tests/extension_test.js
View file @
46330d04
...
@@ -2147,9 +2147,9 @@ describe("`RSVP.Queue`", function () {
...
@@ -2147,9 +2147,9 @@ describe("`RSVP.Queue`", function () {
});
});
describe
(
"
`Queue` constructor
"
,
function
()
{
describe
(
"
`Queue` constructor
"
,
function
()
{
it
(
'
should exist and have length
0
'
,
function
()
{
it
(
'
should exist and have length
1
'
,
function
()
{
assert
(
RSVP
.
Queue
);
assert
(
RSVP
.
Queue
);
assert
.
equal
(
RSVP
.
Queue
.
length
,
0
);
assert
.
equal
(
RSVP
.
Queue
.
length
,
1
);
});
});
it
(
'
should be a constructor
'
,
function
()
{
it
(
'
should be a constructor
'
,
function
()
{
...
@@ -2240,6 +2240,37 @@ describe("`RSVP.Queue`", function () {
...
@@ -2240,6 +2240,37 @@ describe("`RSVP.Queue`", function () {
setTimeout
(
function
()
{
setTimeout
(
function
()
{
assert
.
equal
(
queue
.
isFulfilled
,
true
);
assert
.
equal
(
queue
.
isFulfilled
,
true
);
assert
.
equal
(
queue
.
fulfillmentValue
,
undefined
);
done
();
},
20
);
});
it
(
'
should be fulfilled with the static argument
'
,
function
(
done
)
{
var
queue
=
new
RSVP
.
Queue
(
'
foo
'
);
setTimeout
(
function
()
{
assert
.
equal
(
queue
.
isFulfilled
,
true
);
assert
.
equal
(
queue
.
fulfillmentValue
,
'
foo
'
);
done
();
},
20
);
});
it
(
'
should be fulfilled with the thenable argument
'
,
function
(
done
)
{
var
queue
=
new
RSVP
.
Queue
(
RSVP
.
resolve
(
'
bar
'
));
setTimeout
(
function
()
{
assert
.
equal
(
queue
.
isFulfilled
,
true
);
assert
.
equal
(
queue
.
fulfillmentValue
,
'
bar
'
);
done
();
},
20
);
});
it
(
'
should be rejected with the rejected thenable argument
'
,
function
(
done
)
{
var
queue
=
new
RSVP
.
Queue
(
RSVP
.
reject
(
'
ko
'
));
setTimeout
(
function
()
{
assert
.
equal
(
queue
.
isRejected
,
true
);
assert
.
equal
(
queue
.
rejectedReason
,
'
ko
'
);
done
();
done
();
},
20
);
},
20
);
});
});
...
@@ -2272,6 +2303,22 @@ describe("`RSVP.Queue`", function () {
...
@@ -2272,6 +2303,22 @@ describe("`RSVP.Queue`", function () {
},
20
);
},
20
);
});
});
it
(
'
`thenable` value is propagated to the next entry
'
,
function
(
done
)
{
var
queue
=
new
RSVP
.
Queue
(
RSVP
.
resolve
(
'
foo
'
)),
pushed_result
;
queue
.
push
(
function
(
value
)
{
pushed_result
=
value
;
return
"
bar
"
;
});
setTimeout
(
function
()
{
assert
.
equal
(
pushed_result
,
"
foo
"
);
assert
.
equal
(
queue
.
isFulfilled
,
true
);
assert
.
equal
(
queue
.
fulfillmentValue
,
"
bar
"
);
done
();
},
20
);
});
it
(
'
resolve value is propagated to the next entry
'
,
function
(
done
)
{
it
(
'
resolve value is propagated to the next entry
'
,
function
(
done
)
{
var
queue
=
new
RSVP
.
Queue
(),
var
queue
=
new
RSVP
.
Queue
(),
pushed_result
;
pushed_result
;
...
@@ -2319,7 +2366,23 @@ describe("`RSVP.Queue`", function () {
...
@@ -2319,7 +2366,23 @@ describe("`RSVP.Queue`", function () {
},
20
);
},
20
);
});
});
it
(
'
reject cancels the remaining promise
'
,
function
(
done
)
{
it
(
'
`thenable` rejection is propagated to the next entry
'
,
function
(
done
)
{
var
queue
=
new
RSVP
.
Queue
(
RSVP
.
timeout
(
1
)),
pushed_result
;
queue
.
push
(
undefined
,
function
(
value
)
{
pushed_result
=
value
;
throw
'
bar
'
;
});
setTimeout
(
function
()
{
assert
.
equal
(
pushed_result
,
"
Timed out after 1 ms
"
);
assert
.
equal
(
queue
.
isRejected
,
true
);
assert
.
equal
(
queue
.
rejectedReason
,
"
bar
"
);
done
();
},
20
);
});
it
(
'
rejection is propagated to the next entry
'
,
function
(
done
)
{
var
queue
=
new
RSVP
.
Queue
(),
var
queue
=
new
RSVP
.
Queue
(),
pushed_result
;
pushed_result
;
queue
.
push
(
function
()
{
queue
.
push
(
function
()
{
...
@@ -2327,13 +2390,13 @@ describe("`RSVP.Queue`", function () {
...
@@ -2327,13 +2390,13 @@ describe("`RSVP.Queue`", function () {
});
});
queue
.
push
(
undefined
,
function
(
value
)
{
queue
.
push
(
undefined
,
function
(
value
)
{
pushed_result
=
value
;
pushed_result
=
value
;
throw
value
;
throw
'
bar
'
;
});
});
setTimeout
(
function
()
{
setTimeout
(
function
()
{
assert
.
equal
(
pushed_result
,
"
Timed out after 1 ms
"
);
assert
.
equal
(
pushed_result
,
"
Timed out after 1 ms
"
);
assert
.
equal
(
queue
.
isRejected
,
true
);
assert
.
equal
(
queue
.
isRejected
,
true
);
assert
.
equal
(
queue
.
rejectedReason
,
"
Timed out after 1 ms
"
);
assert
.
equal
(
queue
.
rejectedReason
,
"
bar
"
);
done
();
done
();
},
20
);
},
20
);
});
});
...
@@ -2413,6 +2476,52 @@ describe("`RSVP.Queue`", function () {
...
@@ -2413,6 +2476,52 @@ describe("`RSVP.Queue`", function () {
},
20
);
},
20
);
});
});
it
(
'
should `cancel` default `thenable`
'
,
function
(
done
)
{
var
thenable_cancel_called
=
false
,
thenable_ongoing
=
false
,
later_success_thenable_called
=
false
,
later_error_thenable_called
=
false
,
queue
=
new
RSVP
.
Queue
(
new
RSVP
.
Promise
(
function
()
{
thenable_ongoing
=
true
;
},
function
()
{
thenable_cancel_called
=
true
;
})
)
.
push
(
// Should be skipped
function
()
{
later_success_thenable_called
=
true
;},
function
()
{
later_error_thenable_called
=
true
;}
),
result
=
'
MARKER1
'
,
error
=
'
MARKER2
'
;
queue
.
then
(
function
(
e
)
{
result
=
e
;
});
queue
.
fail
(
function
(
e
)
{
error
=
e
;
});
setTimeout
(
function
()
{
assert
.
equal
(
queue
.
isRejected
,
undefined
);
assert
.
equal
(
queue
.
isFulfilled
,
undefined
);
assert
.
equal
(
thenable_ongoing
,
true
);
queue
.
cancel
();
assert
.
equal
(
thenable_cancel_called
,
true
);
setTimeout
(
function
()
{
assert
.
equal
(
queue
.
isRejected
,
true
);
assert
.
equal
(
queue
.
isFulfilled
,
undefined
);
assert
(
queue
.
rejectedReason
instanceof
RSVP
.
CancellationError
);
assert
.
equal
(
result
,
'
MARKER1
'
);
assert
(
error
instanceof
RSVP
.
CancellationError
);
assert
.
equal
(
later_success_thenable_called
,
false
);
assert
.
equal
(
later_error_thenable_called
,
false
);
done
();
},
20
);
},
20
);
});
it
(
'
should `cancel` pending success `thenable`
'
,
function
(
done
)
{
it
(
'
should `cancel` pending success `thenable`
'
,
function
(
done
)
{
var
thenable_cancel_called
=
false
,
var
thenable_cancel_called
=
false
,
thenable_ongoing
=
false
,
thenable_ongoing
=
false
,
...
...
Romain Courteaud
@romain
mentioned in merge request
erp5!1446
·
Jun 17, 2021
mentioned in merge request
erp5!1446
mentioned in merge request erp5!1446
Toggle commit list
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