semaphore attampt 1: failure

Fortune for async-timeout-js's current commit: Curse − 凶
master
Avril 3 years ago
parent ed32e1586b
commit bc64ae9232
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -0,0 +1,53 @@
function SemaphoreDisposed(message) { this.message = message || "Semaphore was disposed"; Object.freeze(this); }
const new_id = () => {
};
apsdajod XXX: eh, this isn't working...
function Semaphore(cap)
{
function Handle(next, id)
{
this.state = next;
this.id = id || new_id();
}
Handle.create = function(resolve, reject, id) {
let handle = new Handle(this, id);
handle.state[handle.id] = (error) => error ? reject(error) : resolve();
return handle;
};
// Waiters for the seamphore
var next = {
waiters: {},
// Push a waiter into the queue and return a promise for when it completes
wait: () => {
new Promise((resolve, reject) => { // next.waiters.push(error => error ? reject(error) : resolve()))
return Handle.create(resolve, reject).bind(next);
});
},
// Pop a waiter from the queue and complete its promise (with an optional truthy error)
add: (error) => { if(next.waiters.length>0) next.waiters.shift()(error); },
// Complete all semaphores, with an optional truthy error for rejection
flush: (reject) => {
while(next.waiters.length>0) next.waiters.shift()(reject);
},
// Dispose all semaphores in the waiter
dispose: (message) => next.flush(new SemaphoreDisposed(message))
};
this.capacity = cap;
this.length = 0;
this.acquire = async () => {
};
this.release = () => {
};
}
Loading…
Cancel
Save