UNPKG

snapd

Version:
196 lines (152 loc) 6.39 kB
"use strict"; /*; @module-license: The MIT License (MIT) @mit-license Copyright (@c) 2017 Richeve Siodina Bebedor @email: richeve.bebedor@gmail.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @end-module-license @module-configuration: { "package": "snapd", "path": "snapd/snapd.js", "file": "snapd.js", "module": "snapd", "author": "Richeve S. Bebedor", "contributors": [ "John Lenon Maghanoy <johnlenonmaghanoy@gmail.com>" ], "eMail": "richeve.bebedor@gmail.com", "repository": "https://github.com/volkovasystems/snapd.git",`` "test": "snapd-test.js", "global": true } @end-module-configuration @module-documentation: Wraps the function in process.nextTick and setTimeout for server based, and setTimeout for client based. Callback is optional, this will execute the procedure even if callback is not given. @end-module-documentation @include: { "asea": "asea", "harden": "harden", "kein": "kein", "letgo": "letgo", "pringe": "pringe", "protype": "protype", "shft": "shft", "truly": "truly", "zelf": "zelf" } @end-include */ var asea = require("asea"); var harden = require("harden"); var kein = require("kein"); var letgo = require("letgo"); var pringe = require("pringe"); var protype = require("protype"); var shft = require("shft"); var truly = require("truly"); var zelf = require("zelf"); var snapd = function snapd(procedure, timeout, parameter) { /*; @meta-configuration: { "procedure:required": "function", "timeout": "number", "parameter": "..." } @end-meta-configuration */ if (!protype(procedure, FUNCTION)) { throw new Error("invalid procedure"); } if (truly(timeout) && !protype(timeout, NUMBER)) { throw new Error("invalid timeout"); } timeout = timeout || 0; var self = zelf(this); parameter = shft(arguments, 2); var catcher = letgo.bind(self)(function later(callback) { parameter = parameter.concat(shft(arguments)); if (asea.server) { var delayedProcedure = setTimeout(function onTimeout(procedure, self, catcher) { if (catcher.done()) { return; } process.nextTick(function onTick() {var callback = this.callback,catcher = this.catcher,parameter = this.parameter,procedure = this.procedure,self = this.self; if (catcher.done()) { return; } try { var result = procedure.apply(self, parameter); callback(null, result); } catch (error) { callback(error); } }.bind({ "callback": callback, "catcher": catcher, "parameter": parameter, "procedure": procedure, "self": self })); /*; @note: Do not change how we bind this data. nextTick procedure has a special way of handling context. @end-note */ }, timeout, procedure, self, catcher); catcher.set("timeout", delayedProcedure); } else if (asea.client) { var _delayedProcedure = setTimeout(function onTimeout(procedure, self, catcher) { if (catcher.done()) { return; } try { var result = procedure.apply(self, parameter.concat(catcher.get("parameter"))); callback(null, result); } catch (error) { callback(error); } }, timeout, procedure, self, catcher); catcher.set("timeout", _delayedProcedure); } else { throw new Error("cannot determine platform, platform not supported"); } }); var trace = pringe.bind(self)(arguments); harden("trace", trace, catcher); if (kein(trace, snapd.cache) && !snapd.cache[trace].done()) { catcher.stop(); return snapd.cache[trace]; } catcher.on("release", function release() { if (kein(trace, snapd.cache)) { delete snapd.cache[trace]; } clearTimeout(catcher.get("timeout")); }); snapd.cache[trace] = catcher; return catcher; }; harden("cache", snapd.cache || {}, snapd); module.exports = snapd; //# sourceMappingURL=snapd.support.js.map