UNPKG

exupery-core-async

Version:
327 lines (326 loc) 10.2 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ut = exports.uq = exports.upi = exports.up = exports.gt = exports.gq = exports.gp = exports.eh = void 0; const _ei = __importStar(require("exupery-core-internals")); const initialize_unguaranteed_procedure_1 = require("./algorithms/procedure/initialize_unguaranteed_procedure"); const create_asynchronous_processes_monitor_1 = require("./create_asynchronous_processes_monitor"); /** * * @param action gpi * @param error_transform gt */ const eh = (action, error_transform) => { return ($) => { action(error_transform($)).__start(() => { }); }; }; exports.eh = eh; var gp; (function (gp) { /** * * @param action gpi * @param query g.q * @returns */ gp.action = (action, query) => { return { __start: (on_finished) => { //run the query query.__start((query_result) => { //run the action action(query_result).__start(on_finished); }); } }; }; })(gp || (exports.gp = gp = {})); var gq; (function (gq) { /** * * @param query_result qr * @returns */ gq.fixed = (query_result) => { return { __start: (on_finished) => { on_finished(query_result); } }; }; /** * * @param the_query gqi * @param parameters g.q * @param result_transformation gt * @returns */ gq.g = (the_query, parameters, result_transformation) => { return { __start: (on_finished) => { parameters.__start((qr_in) => { the_query(qr_in).__start((result) => { on_finished(result_transformation(result)); }); }); } }; }; })(gq || (exports.gq = gq = {})); var gt; (function (gt) { /** * * @param the_transformation gt * @returns */ gt.g = (the_transformation) => { return ($) => the_transformation($); }; })(gt || (exports.gt = gt = {})); var up; (function (up) { /** * * @param action upi * @param query u.q */ up.action = (action, query) => { return { __start: (on_success, on_error) => { //run the query query.__start((query_result) => { //run the action action(query_result).__start(on_success, (error) => { //transform the error on_error(error); }); }, on_error); } }; }; /** * * @param steps up[] * @returns */ up.sequence = (steps) => { return { __start: (on_success, on_error) => { const length = _ei.array_literal(steps).__get_length(); const runStep = (index) => { if (index >= length) { on_success(); return; } steps[index].__start(() => runStep(index + 1), on_error); }; runStep(0); } }; }; /** * * @param the_array up[] * @param aggregate_exceptions gt * @returns */ up.array = (the_array, aggregate_exceptions) => { return { __start: (on_success, on_error) => { const exceptions = []; (0, create_asynchronous_processes_monitor_1.create_asynchronous_processes_monitor)((monitor) => { the_array.map(($) => { monitor['report process started'](); $.__start(() => { monitor['report process finished'](); }, (e) => { exceptions.push(e); monitor['report process finished'](); }); }); }, () => { if (exceptions.length === 0) { on_success(); } else { on_error(aggregate_exceptions(_ei.array_literal(exceptions))); } }); } }; }; /** * * @param the_dictionary dict<up> * @param aggregate_exceptions gt * @returns */ up.dictionary = (the_dictionary, aggregate_exceptions) => { return { __start: (on_success, on_error) => { const exceptions = {}; (0, create_asynchronous_processes_monitor_1.create_asynchronous_processes_monitor)((monitor) => { the_dictionary.map(($, key) => { monitor['report process started'](); $.__start(() => { monitor['report process finished'](); }, (e) => { exceptions[key] = e; monitor['report process finished'](); }); }); }, () => { if (Object.keys(exceptions).length === 0) { on_success(); } else { on_error(aggregate_exceptions(_ei.dictionary_literal(exceptions))); } }); } }; }; })(up || (exports.up = up = {})); var upi; (function (upi) { /** * * @param action gpi */ upi.g = (action) => ($) => { return (0, initialize_unguaranteed_procedure_1.__create_unguaranteed_procedure)({ 'execute': (on_succes, on_error) => { action($).__start(on_succes); } }); }; /** * * @param action upi */ upi.u = (action, error_transform, error_handler) => ($) => { return (0, initialize_unguaranteed_procedure_1.__create_unguaranteed_procedure)({ 'execute': (on_succes, on_error) => { action($).__start(on_succes, (error) => { if (error_handler !== undefined) { error_handler(error); } on_error(error_transform(error)); }); } }); }; })(upi || (exports.upi = upi = {})); var uq; (function (uq) { /** * * @param query_result qr * @returns */ uq.fixed = (query_result) => { return { __start: (on_success, on_error) => { on_success(query_result); } }; }; /** * unguaranteed query * @param the_query uqi * @param parameters u.q * @param result_transformation ut * @param error_transform gt * @param error_handler eh * @returns */ uq.u = (the_query, parameters, result_transformation, error_transform, error_handler) => { return { __start: (on_success, on_error) => { parameters.__start((qr_in) => { the_query(qr_in).__start((result) => { result_transformation(result).process((x) => on_success(x), on_error); }, (error) => { if (error_handler !== undefined) { error_handler(error); } on_error(error_transform(error)); }); }, on_error); } }; }; /** * guaranteed query * @param the_query gqi * @param parameters u.q * @param result_transformation ut */ uq.g = (the_query, parameters, result_transformation) => { return { __start: (on_success, on_error) => { parameters.__start((x) => { the_query(x).__start(($) => { result_transformation($).process((x) => on_success(x), on_error); }); }, on_error); } }; }; })(uq || (exports.uq = uq = {})); var ut; (function (ut) { /** * * @param the_transformation ut * @param error_transform gt * @param error_handler eh * @returns */ ut.u = (the_transformation, error_transform, error_handler) => { return ($) => { const result = the_transformation($); return result.map((out) => out, (transformation_error) => { //run the error handler if (error_handler !== undefined) { error_handler(transformation_error); } return error_transform(transformation_error); }); }; }; /** * * @param the_transformation gt */ ut.g = (the_transformation) => { return ($) => { return _ei.transformation.successful(the_transformation($)); }; }; })(ut || (exports.ut = ut = {}));