added experimental interactive promise handler removal...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2021-04-07 03:32:23 +03:00
parent 99eabf49a8
commit 174d7292ba
3 changed files with 38 additions and 13 deletions

View File

@ -236,6 +236,7 @@ object.Constructor('IterablePromise', Promise, {
var InteractivePromise =
module.InteractivePromise =
object.Constructor('InteractivePromise', Promise, {
// XXX do we need a way to remove handlers???
__message_handlers: null,
send: function(...args){
@ -257,19 +258,33 @@ object.Constructor('InteractivePromise', Promise, {
//
__new__: function(_, handler){
var handlers = []
var onmessage = function(func){
var h = obj == null ?
// NOTE: we need to get the handlers from .__message_handlers
// unless we are not fully defined yet, then use the
// bootstrap container (handlers)...
// ...since we can call onmessage(..) while the promise
// is still defined there is no way to .send(..) until it
// returns a promise object, this races here are highly
// unlikely...
handlers
: (obj.__message_handlers =
obj.__message_handlers ?? [])
handlers.push(func) }
// remove all handlers... (XXX TEST)
if(func === false){
obj == null ?
handlers.splice(0, handlers.length)
: (delete obj.__message_handlers)
// remove a specific handler... (XXX TEST)
} else if(arguments[1] === false){
var h = (obj == null ?
handlers
: (obj.__message_handlers || []))
h.splice(h.indexOf(func), 1)
// register a handler...
} else {
var h = obj == null ?
// NOTE: we need to get the handlers from .__message_handlers
// unless we are not fully defined yet, then use the
// bootstrap container (handlers)...
// ...since we can call onmessage(..) while the promise
// is still defined there is no way to .send(..) until it
// returns a promise object, this races here are highly
// unlikely...
handlers
: (obj.__message_handlers =
obj.__message_handlers ?? [])
handlers.push(func) } }
var obj = Reflect.construct(
InteractivePromise.__proto__,

View File

@ -1205,6 +1205,16 @@ handler, `<onmessage>`, used to register message handlers.
<onmessage>(<message-handler>)
```
Remove `<message-handler>`
```bnf
<onmessage>(<message-handler>, false)
```
Remove all handlers
```bnf
<onmessage>(false)
```
`<message-handler>` is called when a message is sent via
[`<promise-inter>.send()`](#promise-intersend).

View File

@ -1,6 +1,6 @@
{
"name": "ig-types",
"version": "6.0.11",
"version": "6.0.12",
"description": "Generic JavaScript types and type extensions...",
"main": "main.js",
"scripts": {