UNPKG

nanoresource-promise

Version:
324 lines (320 loc) 8.43 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/emittery.js var emittery_exports = {}; __export(emittery_exports, { NanoresourcePromise: () => NanoresourcePromise2 }); module.exports = __toCommonJS(emittery_exports); var import_emittery = __toESM(require("emittery"), 1); // src/nanoresource-cb.js var import_proc_nexttick = __toESM(require("proc-nexttick"), 1); var preopening = Symbol("opening when closing"); var opening = Symbol("opening queue"); var preclosing = Symbol("closing when inactive"); var closing = Symbol("closing queue"); var sync = Symbol("sync"); var fastClose = Symbol("fast close"); var reopen = Symbol("allow reopen"); var init = Symbol("init state"); function noop() { } var Nanoresource = class { constructor(opts) { if (!opts) opts = {}; if (opts.open) this._open = opts.open; if (opts.close) this._close = opts.close; this[init](); this[reopen] = opts.reopen || false; this[preopening] = null; this[opening] = null; this[preclosing] = null; this[closing] = null; this[sync] = false; this[fastClose] = true; } [init]() { this.opening = false; this.opened = false; this.closing = false; this.closed = false; this.actives = 0; } _open(cb) { cb(null); } _close(cb) { cb(null); } open(cb) { if (!cb) cb = noop; if (this.closing || this.closed) { if (!this[reopen]) { return (0, import_proc_nexttick.default)(cb, new Error("Resource is closed")); } if (this.closing) { if (!this[preopening]) this[preopening] = []; this[preopening].push(cb); return; } this[init](); } if (this.opened) return (0, import_proc_nexttick.default)(cb); if (this[opening]) { this[opening].push(cb); return; } this.opening = true; this[opening] = [cb]; this[sync] = true; this._open(onopen.bind(this)); this[sync] = false; } active(cb) { if (this[fastClose] && this[preclosing] || this[closing] || this.closed) { if (cb) (0, import_proc_nexttick.default)(cb, new Error("Resource is closed")); return false; } this.actives++; return true; } inactive(cb, err, val) { if (!--this.actives) { const queue = this[preclosing]; if (queue) { this[preclosing] = null; while (queue.length) this.close(queue.shift()); } } if (cb) cb(err, val); } close(allowActive, cb) { if (typeof allowActive === "function") return this.close(false, allowActive); if (!cb) cb = noop; if (allowActive) this[fastClose] = false; if (this.closed) return (0, import_proc_nexttick.default)(cb); if (this.actives || this[opening]) { if (!this[preclosing]) this[preclosing] = []; this[preclosing].push(cb); return; } if (!this.opened) { this.closed = true; (0, import_proc_nexttick.default)(cb); return; } if (this[closing]) { this[closing].push(cb); return; } this.closing = true; this[closing] = [cb]; this[sync] = true; this._close(onclose.bind(this)); this[sync] = false; } }; function onopen(err) { if (this[sync]) return (0, import_proc_nexttick.default)(onopen.bind(this), err); const oqueue = this[opening]; this[opening] = null; this.opening = false; this.opened = !err; while (oqueue.length) oqueue.shift()(err); const cqueue = this[preclosing]; if (cqueue && !this.actives) { this[preclosing] = null; while (cqueue.length) this.close(cqueue.shift()); } } function onclose(err) { if (this[sync]) return (0, import_proc_nexttick.default)(onclose.bind(this), err); const queue = this[closing]; this.closing = false; this[closing] = null; this.closed = !err; while (queue.length) queue.shift()(err); const cqueue = this[preopening]; if (cqueue) { this[preopening] = null; while (cqueue.length) this.open(cqueue.shift()); } } // src/index.js function callbackPromise() { let callback; const promise = new Promise((resolve, reject) => { callback = (err, value) => { if (err) reject(err); else resolve(value); }; }); callback.promise = promise; return callback; } var kNanoresource = Symbol("nanoresource"); var NanoresourcePromise = class { constructor(opts = {}) { const open = opts.open || this._open.bind(this); const close = opts.close || this._close.bind(this); this[kNanoresource] = new Nanoresource({ open: (cb) => processPromise(open, cb), close: (cb) => processPromise(close, cb), reopen: opts.reopen }); } get opened() { return this[kNanoresource].opened; } get opening() { return this[kNanoresource].opening; } get closed() { return this[kNanoresource].closed; } get closing() { return this[kNanoresource].closing; } get actives() { return this[kNanoresource].actives; } open() { const callback = callbackPromise(); this[kNanoresource].open(callback); return callback.promise; } close(allowActive = false) { const callback = callbackPromise(); this[kNanoresource].close(allowActive, callback); return callback.promise; } active(cb) { return this[kNanoresource].active(cb); } inactive(cb, err, value) { return this[kNanoresource].inactive(cb, err, value); } async _open() { } async _close() { } }; async function processPromise(fnPromise, cb) { try { await fnPromise(); cb(); } catch (err) { cb(err); } } // src/emittery.js var kNanoresource2 = Symbol("nanoresource"); var NanoresourcePromise2 = class { constructor(opts = {}) { const _open = opts.open || this._open.bind(this); const _close = opts.close || this._close.bind(this); this._emitOpened = null; this._emitClosed = null; this[kNanoresource2] = new NanoresourcePromise({ open: async () => { await this.emit("open"); await _open(); }, close: async () => { await this.emit("close"); await _close(); }, reopen: opts.reopen }); new import_emittery.default().bindMethods(this); } get opened() { return this[kNanoresource2].opened; } get opening() { return this[kNanoresource2].opening; } get closed() { return this[kNanoresource2].closed; } get closing() { return this[kNanoresource2].closing; } get actives() { return this[kNanoresource2].actives; } async open() { await this[kNanoresource2].open(); if (!this._emitOpened) { this._emitOpened = this.emit("opened"); this._emitClosed = null; } await this._emitOpened; } async close(allowActive) { await this[kNanoresource2].close(allowActive); if (!this._emitClosed) { this._emitClosed = this.emit("closed"); this._emitOpened = null; } await this._emitClosed; } active(cb) { return this[kNanoresource2].active(cb); } inactive(cb, err, value) { return this[kNanoresource2].inactive(cb, err, value); } async _open() { } async _close() { } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { NanoresourcePromise }); //# sourceMappingURL=emittery.cjs.map