reworked the task ticket...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-11-26 22:56:23 +03:00
parent 8b74989622
commit 697bdbcc1a
3 changed files with 75 additions and 21 deletions

View File

@ -2,6 +2,9 @@
*
*
*
* XXX need ability to extend event to implement proxy events...
* ...i.e. .on(..) / ... get called on one object but the handler
* bound on a different object via a proxy event method...
* XXX is types/events the right place for this???
* XXX should we have .pre/.post events???
* XXX should we propogate event handling to parent/overloaded events???
@ -271,6 +274,14 @@ function(name, options={}){
}) }
// XXX
var ProxyEvent =
module.ProxyEvent =
function(name, target, options={}){
// XXX
}
//---------------------------------------------------------------------
// Mixins...

View File

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

View File

@ -330,18 +330,27 @@ object.Constructor('Queue', Array, {
//
// Make the ticket more usable...
//
// NOTE: this is not intended for direct use...
var TaskTicket =
module.TaskTicket =
object.Constructor('TaskTicket', {
//module.TaskTicket =
object.Constructor('TaskTicket', Promise, {
__data: null,
title: null,
get state(){
return this.__data.state },
resolve: function(...args){
this.__data.resolve(...args)
if(this.__data.state == 'pending'){
this.__data.state = 'resolved'
this.__data.resolve(...args) }
return this },
reject: function(...args){
this.__data.reject(...args)
if(this.__data.state == 'pending'){
this.__data.state = 'rejected'
this.__data.reject(...args) }
return this },
onmessage: function(msg, func){
this.__data.onmessage(
@ -352,12 +361,42 @@ object.Constructor('TaskTicket', {
&& func(...args) })
return this },
__init__: function(title, resolve, reject, onmessage){
this.title = title
Object.defineProperty(this, '__data', {
value: {resolve, reject, onmessage},
then: Promise.iter.prototype.then,
__new__: function(_, title, resolve, reject, onmessage){
var handlers
var resolver = arguments[1]
var obj = Reflect.construct(
TaskTicket.__proto__,
[function(resolve, reject){
handlers = {resolve, reject}
// NOTE: this is here to support builtin .then(..)
typeof(resolver) == 'function'
&& resolver(resolve, reject) }],
TaskTicket)
if(typeof(resolver) == 'function'){
return obj }
// bind this to external resolve/reject...
obj.then(
function(){
resolve(...arguments) },
function(){
reject(...arguments) })
// setup state...
obj.title = title
Object.defineProperty(obj, '__data', {
value: {
resolve: handlers.resolve,
reject: handlers.reject,
onmessage,
state: 'pending',
},
enumerable: false,
}) },
})
return obj },
})
@ -419,6 +458,7 @@ object.Constructor('TaskManager', Array, events.EventMixin('flat', {
// events...
//
// XXX need to be able to bind to proxy events...
done: events.PureEvent('done'),
error: events.PureEvent('error'),
tasksDone: events.PureEvent('tasksDone'),
@ -430,21 +470,13 @@ object.Constructor('TaskManager', Array, events.EventMixin('flat', {
// .Task(title, task)
// -> task-handler
//
//
// Create a function task...
// .Task(func, ..)
// .Task(title, func, ..)
// -> task-handler
//
// func(tiket, ..)
//
//
// tiket:
// {
// title: <title>,
// resolve: <resolve(..)>,
// reject: <reject(..)>,
// onmessage: <onmessage(..)>
// }
// func(ticket, ..)
//
//
// A task can be:
@ -453,9 +485,20 @@ object.Constructor('TaskManager', Array, events.EventMixin('flat', {
// - function
// - Promise instance
//
// The task-manager is a Promise.interactive(..) instance with
// TaskMixin added.
//
// NOTE: the args are passed only if
// The ticket is a TaskTicket instance, see it for reference...
//
//
// NOTE: only function tasks accept args.
// NOTE: the task is started as soon as it is accepted.
// NOTE: tasks trigger events only on the task-manager instance that
// they were created in...
// XXX try and make all event handlers to be registered in the
// task itself and all the manager events just be proxies
// to tasks...
// ...this needs to work with all the event methods...
Task: function(title, task, ...args){
var that = this