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
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Romain Courteaud
rsvp.js
Commits
595f1186
Commit
595f1186
authored
Dec 17, 2019
by
Romain Courteaud
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make RSVP.hash cancellable
Cancel all subpromises as soon as cancelled.
parent
cde40f5c
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
143 additions
and
24 deletions
+143
-24
lib/rsvp/hash.js
lib/rsvp/hash.js
+47
-24
test/tests/extension_test.js
test/tests/extension_test.js
+96
-0
No files found.
lib/rsvp/hash.js
View file @
595f1186
import
{
defer
}
from
"
./defer
"
;
import
{
Promise
}
from
"
./promise
"
;
function
size
(
object
)
{
var
s
=
0
;
...
...
@@ -11,38 +11,61 @@ function size(object) {
}
function
hash
(
promises
)
{
var
results
=
{},
deferred
=
defer
(),
remaining
=
size
(
promises
);
function
canceller
()
{
var
promise
,
key
;
for
(
key
in
promises
)
{
if
(
promises
.
hasOwnProperty
(
key
))
{
promise
=
promises
[
key
];
if
(
promise
&&
typeof
promise
.
then
===
'
function
'
&&
typeof
promise
.
cancel
===
'
function
'
)
{
promise
.
cancel
();
}
}
}
}
return
new
Promise
(
function
(
resolve
,
reject
)
{
var
results
=
{},
remaining
=
size
(
promises
),
promise
;
if
(
remaining
===
0
)
{
deferred
.
resolve
({}
);
resolve
(
results
);
}
var
resolver
=
function
(
prop
)
{
function
resolver
(
key
)
{
return
function
(
value
)
{
resolveAll
(
prop
,
value
);
};
resolveAll
(
key
,
value
);
};
}
var
resolveAll
=
function
(
prop
,
value
)
{
results
[
prop
]
=
value
;
function
resolveAll
(
key
,
value
)
{
results
[
key
]
=
value
;
if
(
--
remaining
===
0
)
{
deferred
.
resolve
(
results
);
resolve
(
results
);
}
}
};
var
rejectAll
=
function
(
error
)
{
deferred
.
reject
(
error
);
};
function
cancelAll
(
rejectionValue
)
{
reject
(
rejectionValue
);
canceller
();
}
for
(
var
prop
in
promises
)
{
if
(
promises
[
prop
]
&&
typeof
promises
[
prop
].
then
===
'
function
'
)
{
promises
[
prop
].
then
(
resolver
(
prop
),
rejectAll
);
promise
=
promises
[
prop
];
if
(
promise
&&
typeof
promise
.
then
===
'
function
'
)
{
promise
.
then
(
resolver
(
prop
),
cancelAll
);
}
else
{
resolveAll
(
prop
,
promises
[
prop
]
);
resolveAll
(
prop
,
promise
);
}
}
return
deferred
.
promise
;
},
canceller
);
}
export
{
hash
};
test/tests/extension_test.js
View file @
595f1186
...
...
@@ -564,6 +564,102 @@ describe("RSVP extensions", function() {
});
});
specify
(
'
cancel the array of promise as soon as cancelled
'
,
function
(
done
)
{
var
firstResolver
,
secondResolver
;
var
first
=
new
RSVP
.
Promise
(
function
(
resolve
,
reject
)
{
firstResolver
=
{
resolve
:
resolve
,
reject
:
reject
};
});
var
second
=
new
RSVP
.
Promise
(
function
(
resolve
,
reject
)
{
secondResolver
=
{
resolve
:
resolve
,
reject
:
reject
};
});
setTimeout
(
function
()
{
firstResolver
.
reject
({});
},
0
);
setTimeout
(
function
()
{
secondResolver
.
resolve
(
true
);
},
5000
);
var
all_promise
=
RSVP
.
hash
({
one
:
"
nonPromiseValue
"
,
two
:
first
,
three
:
second
});
all_promise
.
cancel
();
setTimeout
(
function
()
{
assert
(
first
.
isRejected
);
assert
(
first
.
rejectedReason
instanceof
RSVP
.
CancellationError
);
assert
(
second
.
isRejected
);
assert
(
second
.
rejectedReason
instanceof
RSVP
.
CancellationError
);
done
();
},
20
);
});
specify
(
'
cancel the array of promise as soon as rejected
'
,
function
(
done
)
{
var
firstResolver
,
secondResolver
;
var
first
=
new
RSVP
.
Promise
(
function
(
resolve
,
reject
)
{
firstResolver
=
{
resolve
:
resolve
,
reject
:
reject
};
});
var
second
=
new
RSVP
.
Promise
(
function
(
resolve
,
reject
)
{
secondResolver
=
{
resolve
:
resolve
,
reject
:
reject
};
});
setTimeout
(
function
()
{
firstResolver
.
reject
(
"
Foo
"
);
},
0
);
setTimeout
(
function
()
{
secondResolver
.
resolve
(
true
);
},
5000
);
var
all_promise
=
RSVP
.
hash
({
one
:
"
nonPromiseValue
"
,
two
:
first
,
three
:
second
});
setTimeout
(
function
()
{
assert
(
first
.
isRejected
);
assert
.
equal
(
first
.
rejectedReason
,
"
Foo
"
);
assert
(
second
.
isRejected
);
assert
(
second
.
rejectedReason
instanceof
RSVP
.
CancellationError
);
done
();
},
20
);
});
specify
(
'
cancel with non cancellable thenables
'
,
function
(
done
)
{
var
firstResolver
,
secondResolver
;
var
first
=
new
RSVP
.
Promise
(
function
(
resolve
,
reject
)
{
firstResolver
=
{
resolve
:
resolve
,
reject
:
reject
};
});
var
second
=
new
RSVP
.
Promise
(
function
(
resolve
,
reject
)
{
secondResolver
=
{
resolve
:
resolve
,
reject
:
reject
};
});
setTimeout
(
function
()
{
firstResolver
.
reject
({});
},
0
);
setTimeout
(
function
()
{
secondResolver
.
resolve
(
true
);
},
5000
);
var
all_promise
=
RSVP
.
hash
({
one
:
{
"
then
"
:
function
()
{
return
;
}},
two
:
first
,
three
:
second
});
all_promise
.
cancel
();
setTimeout
(
function
()
{
assert
(
first
.
isRejected
);
assert
(
first
.
rejectedReason
instanceof
RSVP
.
CancellationError
);
assert
(
second
.
isRejected
);
assert
(
second
.
rejectedReason
instanceof
RSVP
.
CancellationError
);
done
();
},
20
);
});
});
describe
(
"
RSVP.all
"
,
function
()
{
...
...
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