diff --git a/.gitignore b/.gitignore index 1bff2e8..145537b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /build/ /temp/ +/.geany diff --git a/lib/plankton/plankton.d.ts b/lib/plankton/plankton.d.ts new file mode 100644 index 0000000..d1bd34f --- /dev/null +++ b/lib/plankton/plankton.d.ts @@ -0,0 +1,735 @@ +/** + * @author fenris + */ +type int = number; +/** + * @author fenris + */ +type float = number; +/** + * @author fenris + */ +type type_time = { + hours: int; + minutes: int; + seconds: int; +}; +declare var process: any; +declare var require: any; +declare class Buffer { + constructor(x: string, modifier?: string); + toString(modifier?: string): string; +} +declare namespace lib_plankton.base { + /** + * @author fenris + */ + function environment(): string; +} +/** + * @author fenris + */ +type type_pseudopointer = { + value: type_value; +}; +/** + * @author fenris + */ +declare function pseudopointer_null(): type_pseudopointer; +/** + * @author fenris + */ +declare function pseudopointer_make(value: type_value): type_pseudopointer; +/** + * @author fenris + */ +declare function pseudopointer_isset(pseudopointer: type_pseudopointer): boolean; +/** + * @author fenris + */ +declare function pseudopointer_read(pseudopointer: type_pseudopointer): type_value; +/** + * @author fenris + */ +declare function pseudopointer_write(pseudopointer: type_pseudopointer, value: type_value): void; +/** + * @author fenris + */ +declare var instance_verbosity: int; +/** + * @desc the ability to check for equality with another element of the same domain + * @author fenris + */ +interface interface_collatable { + /** + * @author fenris + */ + _collate(value: type_value): boolean; +} +/** + * @author fenris + */ +declare function instance_collate(value1: (type_value & { + _collate?: ((value: type_value) => boolean); +}), value2: type_value): boolean; +/** + * @desc the ability to compare with another element of the same domain for determining if the first is "smaller than or equal to" the latter + * @author fenris + */ +interface interface_comparable { + /** + * @author fenris + */ + _compare(value: type_value): boolean; +} +/** + * @author fenris + */ +declare function instance_compare(value1: (type_value & { + _compare: ((value: type_value) => boolean); +}), value2: type_value): boolean; +/** + * @desc the ability to create an exact copy + * @author fenris + */ +interface interface_cloneable { + /** + * @author fenris + */ + _clone(): type_value; +} +/** + * @author fenris + */ +declare function instance_clone(value: (type_value & { + _clone?: (() => type_value); +})): type_value; +/** + * @author fenris + */ +interface interface_hashable { + /** + * @author fenris + */ + _hash(): string; +} +/** + * @desc the ability to generate a string out of the element, which identifies it to a high degree + * @author fenris + */ +declare function instance_hash(value: (type_value & { + _hash?: (() => string); +})): string; +/** + * @author fenris + */ +interface interface_showable { + /** + * @author fenris + */ + _show(): string; +} +/** + * @desc the ability to map the element to a textual representation (most likely not injective) + * @author fenris + */ +declare function instance_show(value: (type_value & { + _show?: (() => string); +})): string; +/** + * @author frac + */ +interface interface_decorator { + /** + * @author frac + */ + core: type_core; +} +/** + * @author frac + */ +declare class class_observer { + /** + * @author frac + */ + protected counter: int; + /** + * @author frac + */ + protected actions: { + [id: string]: (information: Object) => void; + }; + /** + * @author frac + */ + protected buffer: Array; + /** + * @author frac + */ + constructor(); + /** + * @author frac + */ + empty(): boolean; + /** + * @author frac + */ + flush(): void; + /** + * @author frac + */ + set(id: string, action: (information: Object) => void): void; + /** + * @author frac + */ + del(id: string): void; + /** + * @author frac + */ + add(action: (information: Object) => void): void; + /** + * @author frac + */ + notify(information?: Object, delayed?: boolean): void; + /** + * @author frac + */ + rollout(): void; +} +/** + * @author frac + */ +/** + * @author frac + */ +/** + * @author frac + */ +declare class class_error extends Error { + /** + * @author frac + */ + protected suberrors: Array; + /** + * @author frac + */ + protected mess: string; + /** + * @author frac + */ + constructor(message: string, suberrors?: Array); + /** + * @override + * @author frac + */ + toString(): string; +} +declare namespace lib_plankton.base { + /** + * returns the current UNIX timestamp + * + * @author fenris + */ + function get_current_timestamp(rounded?: boolean): int; + /** + */ + function object_merge(core: Record, mantle: Record): Record; +} +declare namespace lib_plankton.pod { + /** + * @author fenris + */ + type type_pod = { + kind: ("empty" | "filled"); + value?: type_value; + }; + /** + * @author fenris + */ + function make_empty(): type_pod; + /** + * @author fenris + */ + function make_filled(value: type_value): type_pod; + /** + * whether the pod is filled + * + * @author fenris + */ + function is_filled(pod: type_pod): boolean; + /** + * return the value, stored in the pod-wrapper + * + * @author fenris + */ + function cull(pod: type_pod): type_value; + /** + * to pass on a empty-pod or to use a filled-pod + * + * @author fenris + */ + function propagate(pod: type_pod, function_: ((value: type_value) => type_value_)): type_pod; + /** + * @author fenris + */ + function distinguish(pod: type_pod, function_empty: (() => type_result), function_filled: ((value: type_value) => type_result)): type_result; + /** + */ + function show(pod: type_pod, options?: { + show_value?: ((value: type_value) => string); + }): string; +} +declare namespace lib_plankton.pod { + /** + */ + class class_pod { + private subject; + private constructor(); + is_empty(): boolean; + is_filled(): boolean; + cull(): type_value; + show(show_value?: any): string; + toString(): string; + propagate(function_: ((value: type_value) => type_value_)): class_pod; + distinguish(function_empty: (() => type_result), function_filled: ((value: type_value) => type_result)): type_result; + } +} +declare namespace lib_plankton.call { + /** + * @author fenris + */ + type type_executor = ((resolve: (result?: type_result) => any, reject?: (reason?: type_reason) => void) => void); + /** + * @author fenris + */ + function executor_resolve(result: type_result): type_executor; + /** + * @author fenris + */ + function executor_reject(reason: type_reason): type_executor; + /** + * @author fenris + */ + function executor_transform(executor: type_executor, transform_result: (result_from: type_result_from) => type_result_to, transform_reason: (error_from: type_error_from) => type_error_to): type_executor; + /** + * @author fenris + */ + function executor_transform_default(executor: type_executor, transform_result: (result_from: type_result_from) => type_result_to, wrap_string?: string): type_executor; + /** + * @author fenris + */ + function executor_compose_sequential(first: type_executor, second: (result: type_result_first) => type_executor): type_executor; + /** + * @author fenris + */ + function executor_chain(state: type_state, executors: Array<(state: type_state) => type_executor>): type_executor; + /** + * @author fenris + */ + function executor_first(executors: Array>): type_executor>; + /** + * @author fenris + */ + function executor_condense(executors: Array>): type_executor, Error>; + /** + * @author fenris + * @deprecated use condense + */ + function executor_filter(executors: Array>, predicate: (element: type_element) => boolean): type_executor, Error>; + /** + * @author fenris + * @deprecated use condense + */ + function executor_map(executors: Array>, transformator: (element1: type_element1) => type_element2): type_executor, Error>; + /** + * @author fenris + * @deprecated use condense + */ + function executor_reduce(executors: Array>, initial: type_result, accumulator: (result: type_result, element: type_element) => type_result): type_executor; +} +declare namespace lib_plankton.call { + /** + * @author fenris + */ + type type_promise = Promise; + /** + * @author fenris + */ + function promise_reject(reason: type_reason): type_promise; + /** + * @author fenris + */ + function promise_resolve(result: type_result): type_promise; + /** + * @author fenris + */ + function promise_make(executor: (resolve: (result?: type_result) => void, reject: (reason?: type_reason) => void) => void): type_promise; + /** + * @author fenris + */ + function promise_then_close(promise: type_promise, resolver: (result: type_result) => void, rejector: (reason: type_reason) => void): void; + /** + * @author fenris + */ + function promise_then_append(promise: type_promise, resolver: (result: type_result) => type_promise, rejector?: (reason: type_reason) => type_promise): type_promise; + /** + * @author fenris + */ + function promise_all(promises: Array>): type_promise, type_reason>; + /** + * @author fenris + */ + function promise_chain(promises: Array<(input: type_result) => type_promise>, start?: type_result): type_promise; + /** + * @author fenris + */ + function promise_condense(promises: Array<() => type_promise>): type_promise, type_reason>; + /** + * @author fenris + */ + function promise_group(promises: { + [name: string]: () => type_promise; + }, serial?: boolean): type_promise<{ + [name: string]: any; + }, type_reason>; + /** + * @author fenris + */ + function promise_wrap(promise: type_promise, transformator_result: (reason: type_result_inner) => type_result_outer, transformator_reason?: (reason: type_reason) => type_reason): type_promise; + /** + * @author fenris + */ + /** + * @author fenris + */ + /** + * @author fenris + */ + function promise_attach(state: { + [name: string]: any; + }, promise: type_promise, name: string): type_promise<{ + [name: string]: any; + }, type_reason>; + /** + * @author fenris + */ + function promise_delay(promise: type_promise, delay: int): type_promise; + /** + * @author fenris + */ + function promise_to_executor(promise: type_promise): type_executor; +} +declare namespace lib_plankton.call { + /** + */ + class CancellablePromise extends Promise { + /** + */ + private cancelled; + /** + */ + private interval; + /** + */ + private subject; + /** + */ + constructor(executor: ((resolve: any, reject: any) => void)); + /** + */ + private clear; + /** + */ + then(onfulfilled?: ((value: type_result) => (type_next_resolved | PromiseLike)), onrejected?: ((reason: any) => (type_next_rejected | PromiseLike))): Promise; + /** + */ + catch(x: any): Promise; + /** + */ + cancel(): void; + } +} +declare namespace lib_plankton.call { + /** + * @author fenris + */ + enum enum_initializer_state { + initial = 0, + waiting = 1, + successful = 2, + failed = 3 + } + /** + * @author fenris + */ + type type_initializer = { + fetcher: (() => type_promise); + state?: enum_initializer_state; + queue: Array<{ + resolve: ((result?: type_result) => void); + reject: ((reason?: type_reason) => void); + }>; + result?: type_result; + reason?: type_reason; + }; + /** + * @author fenris + */ + function initializer_make(fetcher: (() => type_promise)): type_initializer; + /** + * @author fenris + */ + function initializer_reset(subject: type_initializer): void; + /** + * @author fenris + */ + function initializer_state(subject: type_initializer): enum_initializer_state; + /** + * @author fenris + */ + function initializer_get(subject: type_initializer): type_promise; +} +declare namespace lib_plankton.call { + /** + * @author fenris + */ + type type_deferral = { + representation: (input: type_input) => Promise; + }; + /** + * @author fenris + * @desc activates the deferral and handles its output according to a given procedure + * @param {(value : type_value)=>void} procedure a function which receives the output of the deferral as argument + */ + function deferral_use(deferral: type_deferral, input: type_input, procedure: (output: type_output) => void): void; + /** + * @author fenris + * @desc creates a deferral-subject (similar to "new Promise", where "convey" reflects "resolve"/"reject") + */ + function deferral_make(handler: (input: type_input, convey: (output: type_output) => void) => void): type_deferral; + /** + * @author fenris + * @desc wraps a simple function into a deferral (similar to "Promise.resolve"/"Promise.reject") + */ + function deferral_wrap(function_: (input: type_input) => type_output): type_deferral; + /** + * @author fenris + */ + function deferral_id(): type_deferral; + /** + * @author fenris + */ + function deferral_const(value: type_value): type_deferral; + /** + * @author fenris + */ + function deferral_delay(output: type_output, delay: int): type_deferral; + /** + * @author fenris + * @desc connects two deferrals to form a new one; the output of the first is taken as input for the second + * (similar to "Promise.then" when passing a function which returns a new promise) + * @param {type_deferral} first a simple deferral + * @param {(value1 : type_value1)=>type_deferral} second a function depending from a value returning a deferral + */ + function deferral_compose_serial(first: type_deferral, second: type_deferral): type_deferral; + /** + * @author fenris + */ + function deferral_compose_parallel({ "left": deferral_left, "right": deferral_right, }: { + left: type_deferral; + right: type_deferral; + }): type_deferral; + /** + * @author fenris + * @desc repeatedly applied serial composition + */ + function deferral_chain(members: Array>): type_deferral; + /** + * @author fenris + */ +} +declare namespace lib_plankton.call { + /** + * @author fenris + */ + class class_deferral { + /** + * @author fenris + */ + private subject; + /** + * @author fenris + */ + private constructor(); + /** + * @author fenris + */ + private static _cram; + /** + * @author fenris + */ + private static _tear; + /** + * @author fenris + */ + static make(handler: (input: type_input, convey: (value: type_output) => void) => void): class_deferral; + /** + * @author fenris + */ + use(input: type_input, procedure: (value: type_output) => void): void; + /** + * @author fenris + */ + compose_serial(second: class_deferral): class_deferral; + /** + * @author fenris + */ + static chain(members: Array>): class_deferral; + /** + * @author fenris + */ + static wrap(function_: (input: type_input) => type_output): class_deferral; + /** + * @author fenris + */ + static const_(value: type_value): class_deferral; + /** + * @author fenris + */ + static delay(output: type_output, delay: int): class_deferral; + } +} +declare namespace lib_plankton.call { + /** + * converts the "arguments"-map into an array + * + * @param {Object} args + * @author fenris + */ + function args2list(args: any): Array; + /** + * just the empty function; useful for some callbacks etc. + * + * @author fenris + */ + function nothing(): void; + /** + * just the identity; useful for some callbacks etc.; defined as function instead of const for using type parameters + * + * @author fenris + */ + function id(x: type_value): type_value; + /** + * just the identity; useful for some callbacks etc. + * + * @author fenris + */ + function const_(x: type_value): ((y: any) => type_value); + /** + * composes two functions (i.e. returns a function that return the result of the successive execution of both input-functions) + * + * @param {function} function_f + * @param {function} function_g + * @author fenris + */ + function compose(function_f: ((type_x: any) => type_y), function_g: ((type_y: any) => type_z)): ((value: type_x) => type_z); + /** + * transforms a function with sequential input into a function with leveled input; example: add(2,3) = curryfy(add)(2)(3) + * + * @param {function} f + * @return {function} the currified version of the in put function + * @author fenris + */ + function curryfy(f: Function): Function; + /** + * @author fenris + */ + function convey(value: any, functions: Array): any; + /** + * @author fenris + */ + function timeout(procedure: (() => void), delay: int): int; + /** + * a definition for a value being "defined" + * + * @author neuc + */ + function is_def(obj: type_value, options?: { + null_is_valid?: boolean; + }): boolean; + /** + * returns the value if set and, when a type is specified, if the type is correct, if not return default_value + * + * @author neuc + */ + function def_val(value: any, default_value: any, options?: { + type?: (null | string); + null_is_valid?: boolean; + }): any; + /** + * provides the call for an attribute of a class as a regular function; useful for processing lists of objects + * + * @param {string} name the name of the attribute + * @return {function} + * @author fenris + */ + function attribute(name: string): ((object: type_object) => type_attribute); + /** + * provides a method of a class as a regular function; useful for processing lists of objects + * + * @param {string} name the name of the method + * @return {function} + * @author fenris + */ + function method(name: string): ((object: type_object) => type_output); + /** + * @author fenris + */ + type type_coproduct = { + kind: string; + data?: any; + }; + /** + * @author fenris + */ + function distinguish(coproduct: type_coproduct, handlers: Record type_output)>, options?: { + fallback?: (null | ((coproduct?: type_coproduct) => type_output)); + }): type_output; + /** + * Promise version of "setTimeout" + * + * @author fenris + */ + function defer(seconds: float, action: (() => type_result)): Promise; + /** + * for rate_limit_check + * + * @author fenris + */ + type type_mana_snapshot = { + timestamp: float; + value: float; + }; + /** + * rate limiting algorithm, based on the idea of mana (magic power) in video games: + * - an actor has a fixed mana capacity, i.e. the maximum amount of available power + * - an actor has a fixed rate of mana regeneration, i.e. how fast the power is filled up (linear growth) + * - an action has a defined mana heft, i.e. how much power is required and deducted in order to execute it + * - mana states are represented by snapshots, i.e. the amount of power at a certain point in time + * + * @author fenris + */ + function rate_limit_check(setup: { + capacity: float; + regeneration_rate: float; + get_snapshot: (() => Promise<(null | type_mana_snapshot)>); + set_snapshot: ((snapshot: type_mana_snapshot) => Promise); + update_snapshot: ((timestamp: float, value_increment: float) => Promise); + }, heft: float): Promise<{ + granted: boolean; + seconds: (null | float); + }>; +} diff --git a/lib/plankton/plankton.js b/lib/plankton/plankton.js new file mode 100644 index 0000000..c66955b --- /dev/null +++ b/lib/plankton/plankton.js @@ -0,0 +1,1729 @@ +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +/* +This file is part of »bacterio-plankton:base«. + +Copyright 2016-2023 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' + + +»bacterio-plankton:base« is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +»bacterio-plankton:base« is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with »bacterio-plankton:base«. If not, see . + */ +// } +/* +This file is part of »bacterio-plankton:base«. + +Copyright 2016-2023 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' + + +»bacterio-plankton:base« is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +»bacterio-plankton:base« is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with »bacterio-plankton:base«. If not, see . + */ +; +var lib_plankton; +(function (lib_plankton) { + var base; + (function (base) { + /** + * @author fenris + */ + function environment() { + return "node"; + } + base.environment = environment; + })(base = lib_plankton.base || (lib_plankton.base = {})); +})(lib_plankton || (lib_plankton = {})); +/* +This file is part of »bacterio-plankton:base«. + +Copyright 2016-2023 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' + + +»bacterio-plankton:base« is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +»bacterio-plankton:base« is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with »bacterio-plankton:base«. If not, see . + */ +/** + * @author fenris + */ +/*export*/ function pseudopointer_null() { + return { + "value": null + }; +} +/** + * @author fenris + */ +/*export*/ function pseudopointer_make(value) { + return { + "value": value + }; +} +/** + * @author fenris + */ +/*export*/ function pseudopointer_isset(pseudopointer) { + return (pseudopointer.value != null); +} +/** + * @author fenris + */ +/*export*/ function pseudopointer_read(pseudopointer) { + if (pseudopointer.value != null) { + return pseudopointer.value; + } + else { + var message = "nullpointer dereferencation"; + throw (new Error(message)); + } +} +/** + * @author fenris + */ +/*export*/ function pseudopointer_write(pseudopointer, value) { + pseudopointer.value = value; +} +/* +This file is part of »bacterio-plankton:base«. + +Copyright 2016-2023 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' + + +»bacterio-plankton:base« is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +»bacterio-plankton:base« is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with »bacterio-plankton:base«. If not, see . + */ +/** + * @author fenris + */ +var instance_verbosity = 0; +/** + * @author fenris + */ +function instance_collate(value1, value2) { + if (typeof (value1) === "object") { + if (value1 == null) { + return (value2 == null); + } + else { + if ("_collate" in value1) { + return value1["_collate"](value2); + } + else { + throw (new Error("[collate]" + " " + "object has no such method")); + } + } + } + else { + if (instance_verbosity >= 1) { + // lib_plankton.log.warn("[collate]" + " " + "primitive value; using default implementation"); + } + return (value1 === value2); + } +} +/** + * @author fenris + */ +function instance_compare(value1, value2) { + if (typeof (value1) === "object") { + if ("_compare" in value1) { + return value1["_compare"](value2); + } + else { + throw (new Error("[compare]" + " " + "object has no such method")); + } + } + else { + if (instance_verbosity >= 1) { + // lib_plankton.log.warn("[compare]" + " " + "primitive value; using default implementation"); + } + return (value1 <= value2); + } +} +/** + * @author fenris + */ +function instance_clone(value) { + if (typeof (value) === "object") { + if ("_clone" in value) { + return value["_clone"](); + } + else { + throw (new Error("[clone]" + " " + "object has no such method")); + } + } + else { + if (instance_verbosity >= 1) { + // lib_plankton.log.warn("[clone]" + " " + "primitive value; using default implementation"); + } + return value; + } +} +/** + * @desc the ability to generate a string out of the element, which identifies it to a high degree + * @author fenris + */ +function instance_hash(value) { + if (typeof (value) === "object") { + if ("_hash" in value) { + return value["_hash"](); + } + else { + throw (new Error("[hash]" + " " + "object has no such method")); + } + } + else { + if (instance_verbosity >= 1) { + // lib_plankton.log.warn("[hash]" + " " + "primitive value; using default implementation"); + } + return String(value); + } +} +/** + * @desc the ability to map the element to a textual representation (most likely not injective) + * @author fenris + */ +function instance_show(value) { + if (typeof (value) === "object") { + if (value == null) { + return "NULL"; + } + else { + if ("_show" in value) { + return value["_show"](); + } + else { + // throw (new Error("[show]" + " " + "object has no such method")); + return JSON.stringify(value); + } + } + } + else { + if (instance_verbosity >= 1) { + // lib_plankton.log.warn("[show]" + " " + "primitive value; using default implementation"); + } + return String(value); + } +} +/* +This file is part of »bacterio-plankton:base«. + +Copyright 2016-2023 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' + + +»bacterio-plankton:base« is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +»bacterio-plankton:base« is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with »bacterio-plankton:base«. If not, see . + */ +/** + * @author frac + */ +var class_observer = /** @class */ (function () { + /** + * @author frac + */ + function class_observer() { + this.counter = 0; + this.actions = {}; + this.buffer = []; + } + /** + * @author frac + */ + class_observer.prototype.empty = function () { + return (Object.keys(this.actions).length == 0); + }; + /** + * @author frac + */ + class_observer.prototype.flush = function () { + this.actions = {}; + }; + /** + * @author frac + */ + class_observer.prototype.set = function (id, action) { + this.actions[id] = action; + }; + /** + * @author frac + */ + class_observer.prototype.del = function (id) { + delete this.actions[id]; + }; + /** + * @author frac + */ + class_observer.prototype.add = function (action) { + this.set((this.counter++).toString(), action); + }; + /** + * @author frac + */ + class_observer.prototype.notify = function (information, delayed) { + var _this = this; + if (information === void 0) { information = {}; } + if (delayed === void 0) { delayed = false; } + if (delayed) { + this.buffer.push(information); + } + else { + Object.keys(this.actions).forEach(function (id) { return _this.actions[id](information); }); + } + }; + /** + * @author frac + */ + class_observer.prototype.rollout = function () { + var _this = this; + this.buffer.forEach(function (information) { return _this.notify(information, false); }); + this.buffer = []; + }; + return class_observer; +}()); +/** + * @author frac + */ +/* +export interface interface_readable { + + |** + * @author frac + *| + read() : type_executor; + +} + */ +/** + * @author frac + */ +/* +export interface interface_writeable { + + |** + * @author frac + *| + write(value : type_value) : type_executor; + +} + */ +/* +This file is part of »bacterio-plankton:base«. + +Copyright 2016-2023 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' + + +»bacterio-plankton:base« is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +»bacterio-plankton:base« is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with »bacterio-plankton:base«. If not, see . + */ +/** + * @author frac + */ +var class_error = /** @class */ (function (_super) { + __extends(class_error, _super); + /** + * @author frac + */ + function class_error(message, suberrors) { + if (suberrors === void 0) { suberrors = []; } + var _this = _super.call(this, message) || this; + _this.suberrors = suberrors; + _this.mess = message; + return _this; + } + /** + * @override + * @author frac + */ + class_error.prototype.toString = function () { + return ( /*super.toString()*/this.mess + " " + ("[" + this.suberrors.map(function (x) { return x.toString(); }).join(",") + "]")); + }; + return class_error; +}(Error)); +var lib_plankton; +(function (lib_plankton) { + var base; + (function (base) { + /** + * returns the current UNIX timestamp + * + * @author fenris + */ + function get_current_timestamp(rounded) { + if (rounded === void 0) { rounded = false; } + var x = (Date.now() / 1000); + return (rounded ? Math.round(x) : x); + ; + } + base.get_current_timestamp = get_current_timestamp; + /** + */ + function object_merge(core, mantle) { + return Object.assign(core, mantle); + } + base.object_merge = object_merge; + })(base = lib_plankton.base || (lib_plankton.base = {})); +})(lib_plankton || (lib_plankton = {})); +/* +This file is part of »bacterio-plankton:pod«. + +Copyright 2016-2023 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' + + +»bacterio-plankton:pod« is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +»bacterio-plankton:pod« is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with »bacterio-plankton:pod«. If not, see . + */ +var lib_plankton; +(function (lib_plankton) { + var pod; + (function (pod_1) { + /** + * @author fenris + */ + function make_empty() { + return { + "kind": "empty" + }; + } + pod_1.make_empty = make_empty; + /** + * @author fenris + */ + function make_filled(value) { + return { + "kind": "filled", + "value": value + }; + } + pod_1.make_filled = make_filled; + /** + * whether the pod is filled + * + * @author fenris + */ + function is_filled(pod) { + return (pod.kind === "filled"); + } + pod_1.is_filled = is_filled; + /** + * return the value, stored in the pod-wrapper + * + * @author fenris + */ + function cull(pod) { + if (!is_filled(pod)) { + throw (new Error("cull from empty")); + } + else { + return pod.value; + } + } + pod_1.cull = cull; + /** + * to pass on a empty-pod or to use a filled-pod + * + * @author fenris + */ + function propagate(pod, function_) { + if (!is_filled(pod)) { + return make_empty(); + } + else { + return make_filled(function_(pod.value)); + } + } + pod_1.propagate = propagate; + /** + * @author fenris + */ + function distinguish(pod, function_empty, function_filled) { + return ((!is_filled(pod)) + ? function_empty() + : function_filled(pod.value)); + } + pod_1.distinguish = distinguish; + /** + */ + function show(pod, options = {}) { + options = Object.assign({ + "show_value": value => String(value), + }, options); + if (!is_filled(pod)) { + return "<·>"; + } + else { + return ("<- " + options.show_value(pod.value) + " ->"); + } + } + pod_1.show = show; + })(pod = lib_plankton.pod || (lib_plankton.pod = {})); +})(lib_plankton || (lib_plankton = {})); +/* +This file is part of »bacterio-plankton:pod«. + +Copyright 2016-2023 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' + + +»bacterio-plankton:pod« is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +»bacterio-plankton:pod« is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with »bacterio-plankton:pod«. If not, see . + */ +var lib_plankton; +(function (lib_plankton) { + var pod; + (function (pod) { + /** + */ + class class_pod { + constructor(subject) { this.subject = subject; } + is_empty() { return (!pod.is_filled(this.subject)); } + is_filled() { return pod.is_filled(this.subject); } + cull() { return pod.cull(this.subject); } + show(show_value = undefined) { return pod.show(this.subject, show_value); } + toString() { return this.show(); } + propagate(function_) { return new class_pod(pod.propagate(this.subject, function_)); } + distinguish(function_empty, function_filled) { return pod.distinguish(this.subject, function_empty, function_filled); } + } + pod.class_pod = class_pod; + })(pod = lib_plankton.pod || (lib_plankton.pod = {})); +})(lib_plankton || (lib_plankton = {})); +/* +This file is part of »bacterio-plankton:call«. + +Copyright 2016-2023 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' + + +»bacterio-plankton:call« is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +»bacterio-plankton:call« is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with »bacterio-plankton:call«. If not, see . + */ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var lib_plankton; +(function (lib_plankton) { + var call; + (function (call) { + /** + * @author fenris + */ + function executor_resolve(result) { + return ((resolve, reject) => resolve(result)); + } + call.executor_resolve = executor_resolve; + /** + * @author fenris + */ + function executor_reject(reason) { + return ((resolve, reject) => reject(reason)); + } + call.executor_reject = executor_reject; + /** + * @author fenris + */ + function executor_transform(executor, transform_result, transform_reason) { + return ((resolve, reject) => { + executor(result => resolve(transform_result(result)), reason => reject(transform_reason(reason))); + }); + } + call.executor_transform = executor_transform; + /** + * @author fenris + */ + function executor_transform_default(executor, transform_result, wrap_string = null) { + let transform_reason = (error => ((wrap_string == null) ? error : new class_error(wrap_string, [error]))); + return (executor_transform(executor, transform_result, transform_reason)); + } + call.executor_transform_default = executor_transform_default; + /** + * @author fenris + */ + function executor_compose_sequential(first, second) { + return ((resolve, reject) => { + first(result => { + second(result)(resolve, reject); + }, reason => { + reject(reason); + }); + }); + } + call.executor_compose_sequential = executor_compose_sequential; + /** + * @author fenris + */ + function executor_chain(state, executors) { + return ((resolve, reject) => { + if (executors.length == 0) { + return resolve(state); + } + else { + return executors[0](state)(result => { + executor_chain(result, executors.slice(1))(resolve, reject); + }, reject); + } + }); + /* + */ + /* + if (executors.length == 0) { + return executor_resolve(state); + } + else if (executors.length == 1) { + return executors[0](state); + } + else { + return ( + executor_chain( + state, + [ + state => (resolve, reject) => executors[0](state)(result => executors[1](result)(resolve, reject), reject) + ].concat(executors.slice(2)) + ) + ); + } + */ + /* + return ( + executors.reduce( + (chain, current) => executor_compose_sequential(chain, current, deferred), + executor_resolve(state) + ) + ); + */ + } + call.executor_chain = executor_chain; + /** + * @author fenris + */ + function executor_first(executors) { + /* + return ( + (resolve, reject) => { + if (executors.length == 0) { + reject(new Error("all failed")); + } + else { + executors[0]( + result => { + resolve(result); + }, + reason => { + executor_first(executors.slice(1))(resolve, reject); + } + ) + } + } + ); + */ + return ((resolve, reject) => { + executor_chain([], executors.map(executor => reasons => (resolve_, reject_) => { + executor(result => reject_(result), reason => resolve_(reasons.concat([reason]))); + }))(errors => reject(errors), result => resolve(result)); + }); + } + call.executor_first = executor_first; + /** + * @author fenris + */ + function executor_condense(executors) { + return (executor_chain([], executors.map(executor => result => (resolve, reject) => { + executor(element => resolve(result.concat([element])), reject); + }))); + } + call.executor_condense = executor_condense; + /** + * @author fenris + * @deprecated use condense + */ + function executor_filter(executors, predicate) { + return (executor_chain([], executors.map(executor => result => (resolve, reject) => { + executor(element => resolve(predicate(element) ? result.concat([element]) : result), reject); + }))); + } + call.executor_filter = executor_filter; + /** + * @author fenris + * @deprecated use condense + */ + function executor_map(executors, transformator) { + return (executor_chain([], executors.map(executor => result => (resolve, reject) => { + executor(element1 => resolve(result.concat([transformator(element1)])), reject); + }))); + } + call.executor_map = executor_map; + /** + * @author fenris + * @deprecated use condense + */ + function executor_reduce(executors, initial, accumulator) { + return (executor_chain(initial, executors.map(executor => result => (resolve, reject) => { + executor(element => resolve(accumulator(result, element)), reject); + }))); + } + call.executor_reduce = executor_reduce; + })(call = lib_plankton.call || (lib_plankton.call = {})); +})(lib_plankton || (lib_plankton = {})); +/* +This file is part of »bacterio-plankton:call«. + +Copyright 2016-2023 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' + + +»bacterio-plankton:call« is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +»bacterio-plankton:call« is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with »bacterio-plankton:call«. If not, see . + */ +var lib_plankton; +(function (lib_plankton) { + var call; + (function (call) { + /** + * @author fenris + */ + function promise_reject(reason) { + return Promise.reject(reason); + } + call.promise_reject = promise_reject; + /** + * @author fenris + */ + function promise_resolve(result) { + return Promise.resolve(result); + } + call.promise_resolve = promise_resolve; + /** + * @author fenris + */ + function promise_make(executor) { + return (new Promise(executor)); + } + call.promise_make = promise_make; + /** + * @author fenris + */ + function promise_then_close(promise, resolver, rejector) { + promise.then(resolver, rejector); + } + call.promise_then_close = promise_then_close; + /** + * @author fenris + */ + function promise_then_append(promise, resolver, rejector = null) { + if (rejector == null) { + rejector = (reason) => promise_reject(reason); + } + return (promise.then(resolver, rejector)); + } + call.promise_then_append = promise_then_append; + /** + * @author fenris + */ + function promise_all(promises) { + return Promise.all(promises); + } + call.promise_all = promise_all; + /** + * @author fenris + */ + function promise_chain(promises, start = undefined) { + return (promises.reduce((chain, promise) => promise_then_append(chain, promise), promise_resolve(start))); + } + call.promise_chain = promise_chain; + /** + * @author fenris + */ + function promise_condense(promises) { + return (promise_chain(promises.map(promise => result => promise_then_append(promise(), element => promise_resolve(result.concat([element])))), [])); + } + call.promise_condense = promise_condense; + /** + * @author fenris + */ + function promise_group(promises, serial = false) { + const decorate = function (promise, name) { + return (() => promise_then_append(promise(), value => promise_resolve({ "key": name, "value": value }))); + }; + const convert = function (array) { + let object = {}; + array.forEach(({ "key": key, "value": value }) => { object[key] = value; }); + return object; + }; + if (serial) { + return (promise_then_append(promise_condense(Object.keys(promises) + .map(name => decorate(promises[name], name))), list => promise_resolve(convert(list)))); + } + else { + return (promise_then_append(promise_all(Object.keys(promises) + .map(name => decorate(promises[name], name)) + .map(promise => promise())), list => promise_resolve(convert(list)))); + } + } + call.promise_group = promise_group; + /** + * @author fenris + */ + function promise_wrap(promise, transformator_result, transformator_reason = lib_plankton.call.id) { + return (promise_make((resolve, reject) => { + promise_then_close(promise, result => resolve(transformator_result(result)), reason => reject(transformator_reason(reason))); + })); + } + call.promise_wrap = promise_wrap; + /** + * @author fenris + */ + /* + export function promise_show(label : string) : (result : type_result)=>type_promise { + return ( + result => promise_make( + (resolve, reject) => { + // lib_plankton.log.info(label + ": " + instance_show(result)); + process.stdout.write(label + ": " + instance_show(result)); + resolve(result); + } + ) + ); + } + */ + /** + * @author fenris + */ + /* + export function promise_log(result : type_result) : (result : type_result)=>type_promise { + return promise_show("log"); + } + */ + /** + * @author fenris + */ + function promise_attach(state, promise, name) { + return (promise_wrap(promise, result => { + state[name] = result; + return state; + })); + } + call.promise_attach = promise_attach; + /** + * @author fenris + */ + function promise_delay(promise, delay) { + return promise_make((resolve, reject) => { + call.timeout(() => { + promise_then_close(promise, resolve, reject); + return null; + }, delay); + }); + } + call.promise_delay = promise_delay; + /** + * @author fenris + */ + function promise_to_executor(promise) { + return ((resolve, reject) => promise.then(resolve, reject)); + } + call.promise_to_executor = promise_to_executor; + })(call = lib_plankton.call || (lib_plankton.call = {})); +})(lib_plankton || (lib_plankton = {})); +/* +This file is part of »bacterio-plankton:call«. + +Copyright 2016-2023 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' + + +»bacterio-plankton:call« is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +»bacterio-plankton:call« is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with »bacterio-plankton:call«. If not, see . + */ +var lib_plankton; +(function (lib_plankton) { + var call; + (function (call) { + /** + */ + class CancellablePromise extends Promise { + /** + */ + constructor(executor) { + super((resolve, reject) => { }); + this.subject = (new Promise((resolve, reject) => { + Promise.race([ + new Promise(executor), + new Promise((resolve_, reject_) => { + this.interval = setInterval(() => { + if (!this.cancelled) { + // do nothing + } + else { + reject_(new Error("cancelled")); + this.clear(); + } + }, 0); + }), + ]) + .then(resolve, reject); + })); + this.cancelled = false; + this.interval = null; + } + /** + */ + clear() { + if (this.interval === null) { + // do nothing + } + else { + clearInterval(this.interval); + this.interval = null; + } + } + /** + */ + then(onfulfilled, onrejected) { + this.clear(); + return this.subject.then(onfulfilled, onrejected); + } + /** + */ + catch(x) { + this.clear(); + return this.subject.catch(x); + } + /** + */ + cancel() { + this.cancelled = true; + this.clear(); + } + } + call.CancellablePromise = CancellablePromise; + })(call = lib_plankton.call || (lib_plankton.call = {})); +})(lib_plankton || (lib_plankton = {})); +/* +This file is part of »bacterio-plankton:call«. + +Copyright 2016-2023 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' + + +»bacterio-plankton:call« is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +»bacterio-plankton:call« is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with »bacterio-plankton:call«. If not, see . + */ +var lib_plankton; +(function (lib_plankton) { + var call; + (function (call) { + /** + * @author fenris + */ + let enum_initializer_state; + (function (enum_initializer_state) { + enum_initializer_state[enum_initializer_state["initial"] = 0] = "initial"; + enum_initializer_state[enum_initializer_state["waiting"] = 1] = "waiting"; + enum_initializer_state[enum_initializer_state["successful"] = 2] = "successful"; + enum_initializer_state[enum_initializer_state["failed"] = 3] = "failed"; + })(enum_initializer_state = call.enum_initializer_state || (call.enum_initializer_state = {})); + /** + * @author fenris + */ + function initializer_make(fetcher) { + let subject = { + "fetcher": fetcher, + "state": enum_initializer_state.initial, + "queue": [], + "result": undefined, + "reason": undefined, + }; + return subject; + } + call.initializer_make = initializer_make; + /** + * @author fenris + */ + function initializer_actuate(subject) { + switch (subject.state) { + case enum_initializer_state.successful: { + subject.queue.forEach(entry => entry.resolve(subject.result)); + break; + } + case enum_initializer_state.failed: { + subject.queue.forEach(entry => entry.reject(subject.reason)); + break; + } + default: { + throw (new Error(`unhandled state ${subject.state}`)); + break; + } + } + } + /** + * @author fenris + */ + function initializer_reset(subject) { + subject.state = enum_initializer_state.initial; + subject.queue = []; + } + call.initializer_reset = initializer_reset; + /** + * @author fenris + */ + function initializer_state(subject) { + return subject.state; + } + call.initializer_state = initializer_state; + /** + * @author fenris + */ + function initializer_get(subject) { + switch (subject.state) { + case enum_initializer_state.initial: { + subject.state = enum_initializer_state.waiting; + return (call.promise_make((resolve, reject) => { + subject.queue.push({ "resolve": resolve, "reject": reject }); + subject.fetcher().then(result => { + subject.state = enum_initializer_state.successful; + subject.result = result; + initializer_actuate(subject); + }, reason => { + subject.state = enum_initializer_state.failed; + subject.reason = reason; + initializer_actuate(subject); + }); + })); + break; + } + case enum_initializer_state.waiting: { + return (call.promise_make((resolve, reject) => { + subject.queue.push({ "resolve": resolve, "reject": reject }); + })); + break; + } + case enum_initializer_state.successful: { + return (call.promise_resolve(subject.result)); + break; + } + case enum_initializer_state.failed: { + return (call.promise_reject(subject.reason)); + break; + } + default: { + throw (new Error(`unhandled state ${subject.state}`)); + break; + } + } + } + call.initializer_get = initializer_get; + /** + * @author fenris + */ + function initializer_get_sync(subject) { + switch (subject.state) { + case enum_initializer_state.successful: { + return subject.result; + break; + } + case enum_initializer_state.failed: { + throw subject.reason; + break; + } + default: { + throw (new Error(`unhandled state ${subject.state}`)); + break; + } + } + } + /** + * @author fenris + */ + function initializer_set_sync(subject, result) { + switch (subject.state) { + case enum_initializer_state.successful: { + subject.result = result; + break; + } + case enum_initializer_state.failed: { + subject.state = enum_initializer_state.successful; + subject.result = result; + break; + } + default: { + throw (new Error(`unhandled state ${subject.state}`)); + break; + } + } + } + })(call = lib_plankton.call || (lib_plankton.call = {})); +})(lib_plankton || (lib_plankton = {})); +/* +This file is part of »bacterio-plankton:call«. + +Copyright 2016-2023 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' + + +»bacterio-plankton:call« is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +»bacterio-plankton:call« is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with »bacterio-plankton:call«. If not, see . + */ +var lib_plankton; +(function (lib_plankton) { + var call; + (function (call) { + /* + The core idea of this library is to provide means for asynchronous program flow. The old-school way to do is, + is to use callbacks. While this approach is simple and easy to understand, it has some disadvantages. As an + attempt to relief and improve this, the promise-system was introduced. In principle it solves most of the + problems found in the callback-approach; however it has some downsides as well: + + - Convolution of multiple principles + Promises unite the ideas of asynchronous program flow and error handling. + + - Instant execution + Creating a promise results in the instant execution of the given executor prodecure. While this might be + convenient in some cases, it can be quite disturbing and counter-intuitive in others. + + - Broken typing + The Promise system doesn't distinguish between an appending "then" (i.e. passing a function, which returns a + new promise) and a closing "then" (i.e. passing a function, which has no return value). On top of that it + allows returning simple values in an appending "then", which results in an implicit call of the executors + "resolve"-function. The price for these "pragmatic" features is that the whole system can't be typed well. + And even though JavaScript is not a strictly typed language, it was a quite questionable decision to design + the promise system in a way, which breaks typing from the start. + + The deferral-system forseeks to solve these issues while retaining the advantages of the promise-system. + */ + /** + * @author fenris + * @desc activates the deferral and handles its output according to a given procedure + * @param {(value : type_value)=>void} procedure a function which receives the output of the deferral as argument + */ + function deferral_use(deferral, input, procedure) { + deferral.representation(input).then(value => { + procedure(value); + }, reason => { + throw reason; + }); + } + call.deferral_use = deferral_use; + /** + * @author fenris + * @desc creates a deferral-subject (similar to "new Promise", where "convey" reflects "resolve"/"reject") + */ + function deferral_make(handler) { + return ({ + "representation": ((input) => (new Promise((resolve, reject) => { + handler(input, resolve); + }))) + }); + } + call.deferral_make = deferral_make; + /** + * @author fenris + * @desc wraps a simple function into a deferral (similar to "Promise.resolve"/"Promise.reject") + */ + function deferral_wrap(function_) { + return (deferral_make((input, convey) => convey(function_(input)))); + } + call.deferral_wrap = deferral_wrap; + /** + * @author fenris + */ + function deferral_id() { + return (deferral_make((input, convey) => convey(input))); + } + call.deferral_id = deferral_id; + /** + * @author fenris + */ + function deferral_const(value) { + return (deferral_make((input, convey) => convey(value))); + } + call.deferral_const = deferral_const; + /** + * @author fenris + */ + function deferral_delay(output, delay) { + return (deferral_make((input, convey) => { + setTimeout(() => convey(output), delay); + })); + } + call.deferral_delay = deferral_delay; + /** + * @author fenris + * @desc connects two deferrals to form a new one; the output of the first is taken as input for the second + * (similar to "Promise.then" when passing a function which returns a new promise) + * @param {type_deferral} first a simple deferral + * @param {(value1 : type_value1)=>type_deferral} second a function depending from a value returning a deferral + */ + function deferral_compose_serial(first, second) { + return { + "representation": ((input) => first.representation(input).then((between) => second.representation(between))) + }; + } + call.deferral_compose_serial = deferral_compose_serial; + /** + * @author fenris + */ + function deferral_compose_parallel({ "left": deferral_left, "right": deferral_right, }) { + return (deferral_make((input, convey) => { + let object = { + "left": lib_plankton.pod.make_empty(), + "right": lib_plankton.pod.make_empty(), + }; + let finish = function () { + if (lib_plankton.pod.is_filled(object.left) + && + lib_plankton.pod.is_filled(object.right)) { + let result = { + "left": lib_plankton.pod.cull(object.left), + "right": lib_plankton.pod.cull(object.right), + }; + convey(result); + } + else { + // do nothing + } + }; + deferral_use(deferral_left, input, output_left => { + object.left = lib_plankton.pod.make_filled(output_left); + finish(); + }); + deferral_use(deferral_right, input, output_right => { + object.right = lib_plankton.pod.make_filled(output_right); + finish(); + }); + })); + } + call.deferral_compose_parallel = deferral_compose_parallel; + /** + * @author fenris + * @desc repeatedly applied serial composition + */ + function deferral_chain(members) { + return (members.reduce( + // (result, current) => deferral_compose_serial(result, current), + deferral_compose_serial, deferral_id())); + } + call.deferral_chain = deferral_chain; + /** + * @author fenris + */ + /* + export function deferral_bunch( + members : {[name : string] : type_deferral} + ) : type_deferral { + + } + */ + })(call = lib_plankton.call || (lib_plankton.call = {})); +})(lib_plankton || (lib_plankton = {})); +/* +This file is part of »bacterio-plankton:call«. + +Copyright 2016-2023 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' + + +»bacterio-plankton:call« is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +»bacterio-plankton:call« is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with »bacterio-plankton:call«. If not, see . + */ +var lib_plankton; +(function (lib_plankton) { + var call; + (function (call) { + /** + * @author fenris + */ + class class_deferral { + /** + * @author fenris + */ + constructor(subject) { + this.subject = subject; + } + /** + * @author fenris + */ + static _cram(subject) { + return (new class_deferral(subject)); + } + /** + * @author fenris + */ + static _tear(instance) { + return instance.subject; + } + /** + * @author fenris + */ + static make(handler) { + return (class_deferral._cram(call.deferral_make(handler))); + } + /** + * @author fenris + */ + use(input, procedure) { + return (call.deferral_use(class_deferral._tear(this), input, procedure)); + } + /** + * @author fenris + */ + compose_serial(second) { + return (class_deferral._cram(call.deferral_compose_serial(class_deferral._tear(this), class_deferral._tear(second)))); + } + /** + * @author fenris + */ + static chain(members) { + return (class_deferral._cram(call.deferral_chain(members.map(member => class_deferral._tear(member))))); + } + /** + * @author fenris + */ + static wrap(function_) { + return (class_deferral._cram(call.deferral_wrap(function_))); + } + /** + * @author fenris + */ + static const_(value) { + return (class_deferral._cram(call.deferral_const(value))); + } + /** + * @author fenris + */ + static delay(output, delay) { + return (class_deferral._cram(call.deferral_delay(output, delay))); + } + } + call.class_deferral = class_deferral; + })(call = lib_plankton.call || (lib_plankton.call = {})); +})(lib_plankton || (lib_plankton = {})); +/* +This file is part of »bacterio-plankton:call«. + +Copyright 2016-2023 'Christian Fraß, Christian Neubauer, Martin Springwald GbR' + + +»bacterio-plankton:call« is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +»bacterio-plankton:call« is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with »bacterio-plankton:call«. If not, see . + */ +var lib_plankton; +(function (lib_plankton) { + var call; + (function (call) { + /** + * converts the "arguments"-map into an array + * + * @param {Object} args + * @author fenris + */ + function args2list(args) { + return Object.keys(args).map(key => args[key]); + } + call.args2list = args2list; + /** + * just the empty function; useful for some callbacks etc. + * + * @author fenris + */ + function nothing() { + } + call.nothing = nothing; + /** + * just the identity; useful for some callbacks etc.; defined as function instead of const for using type parameters + * + * @author fenris + */ + function id(x) { + return x; + } + call.id = id; + /** + * just the identity; useful for some callbacks etc. + * + * @author fenris + */ + function const_(x) { + return (y => x); + } + call.const_ = const_; + /** + * composes two functions (i.e. returns a function that return the result of the successive execution of both input-functions) + * + * @param {function} function_f + * @param {function} function_g + * @author fenris + */ + function compose(function_f, function_g) { + return (function (x) { + // return function_g(function_f(x)); + return function_g(function_f.apply(function_f, args2list(arguments))); + }); + } + call.compose = compose; + /** + * @author fenris + */ + function curryfy_real(f, n) { + switch (n) { + case 0: { + throw (new Error("[curryfy] impossible")); + // break; + } + case 1: { + return f; + // break; + } + default: { + return (function (x) { + return (curryfy_real(function () { return f.apply(f, [x].concat(args2list(arguments))); }, n - 1)); + }); + // break; + } + } + } + /** + * transforms a function with sequential input into a function with leveled input; example: add(2,3) = curryfy(add)(2)(3) + * + * @param {function} f + * @return {function} the currified version of the in put function + * @author fenris + */ + function curryfy(f) { + return curryfy_real(f, f.length); + } + call.curryfy = curryfy; + /** + * @author fenris + */ + function convey(value, functions) { + let result = value; + functions.forEach(function_ => { + result = function_(result); + }); + return result; + } + call.convey = convey; + /** + * @author fenris + */ + function timeout(procedure, delay) { + return ( + /*window.*/ setTimeout(procedure, delay)); + } + call.timeout = timeout; + /** + * a definition for a value being "defined" + * + * @author neuc + */ + function is_def(obj, options = {}) { + options = Object.assign({ + "null_is_valid": false, + }, options); + return (!((typeof (obj) === "undefined") + || + (!options.null_is_valid && (obj === null)))); + } + call.is_def = is_def; + /** + * returns the value if set and, when a type is specified, if the type is correct, if not return default_value + * + * @author neuc + */ + function def_val(value, default_value, options = {}) { + options = Object.assign({ + "type": null, + "null_is_valid": false, + }, options); + if (is_def(value, { "null_is_valid": options.null_is_valid }) + && + (is_def(options.type) + ? ((typeof (value) === options.type) + || + ((value === null) + && + options.null_is_valid)) + : true)) { + return value; + } + else { + return default_value; + } + } + call.def_val = def_val; + ; + /** + * provides the call for an attribute of a class as a regular function; useful for processing lists of objects + * + * @param {string} name the name of the attribute + * @return {function} + * @author fenris + */ + function attribute(name) { + return (object => object[name]); + } + call.attribute = attribute; + /** + * provides a method of a class as a regular function; useful for processing lists of objects + * + * @param {string} name the name of the method + * @return {function} + * @author fenris + */ + function method(name) { + return (function (object) { return object[name].apply(object, args2list(arguments).slice(1)); }); + } + call.method = method; + /** + * @author fenris + */ + function distinguish(coproduct, handlers, options = {}) { + options = Object.assign({ + "fallback": null, + }, options); + if (coproduct.kind in handlers) { + const handler = handlers[coproduct.kind]; + return handler(coproduct.data); + } + else { + const message = ("unhandled kind '" + coproduct.kind + "'"); + if (options.fallback !== null) { + console.warn(message); + return options.fallback(coproduct); + } + else { + throw (new Error(message)); + } + } + } + call.distinguish = distinguish; + /** + * Promise version of "setTimeout" + * + * @author fenris + */ + function defer(seconds, action) { + return (new Promise((resolve, reject) => { + setTimeout(() => resolve(action()), Math.floor(seconds * 1000)); + })); + } + call.defer = defer; + /** + * rate limiting algorithm, based on the idea of mana (magic power) in video games: + * - an actor has a fixed mana capacity, i.e. the maximum amount of available power + * - an actor has a fixed rate of mana regeneration, i.e. how fast the power is filled up (linear growth) + * - an action has a defined mana heft, i.e. how much power is required and deducted in order to execute it + * - mana states are represented by snapshots, i.e. the amount of power at a certain point in time + * + * @author fenris + */ + function rate_limit_check(setup, heft) { + return __awaiter(this, void 0, void 0, function* () { + if (heft > setup.capacity) { + return Promise.resolve({ + "granted": false, + "seconds": null, + }); + } + else { + // get current value + const current_timestamp = (Date.now() / 1000); + const old_snapshot_raw = (yield setup.get_snapshot()); + const old_snapshot = (old_snapshot_raw !== null && old_snapshot_raw !== void 0 ? old_snapshot_raw : { "timestamp": current_timestamp, "value": setup.capacity }); + const seconds_passed = (current_timestamp - old_snapshot.timestamp); + const current_value = Math.min(setup.capacity, (old_snapshot.value + + + (setup.regeneration_rate + * + seconds_passed))); + // analyze + if (current_value < heft) { + // too less + const seconds_needed = ((setup.regeneration_rate <= 0) + ? null + : ((heft - old_snapshot.value) / setup.regeneration_rate)); + return Promise.resolve({ + "granted": false, + "seconds": ((seconds_needed === null) ? null : (seconds_needed - seconds_passed)), + }); + } + else { + // enough -> update snapshot + const new_value = (current_value - heft); + // set_snapshot + if (old_snapshot_raw === null) { + yield setup.set_snapshot({ "timestamp": current_timestamp, "value": new_value }); + } + else { + yield setup.update_snapshot(current_timestamp, (new_value - old_snapshot.value)); + } + return Promise.resolve({ + "granted": true, + "seconds": 0, + }); + } + } + }); + } + call.rate_limit_check = rate_limit_check; + })(call = lib_plankton.call || (lib_plankton.call = {})); +})(lib_plankton || (lib_plankton = {})); diff --git a/source/elements/base.ts b/source/elements/base.ts index d151d29..117e380 100644 --- a/source/elements/base.ts +++ b/source/elements/base.ts @@ -1,27 +1,8 @@ -/* -type type_element_text = { - content : string; -}; - -type type_element_group = { - members : Array; -}; - -type type_element = ( - type_element_text - | - type_element_group -); - */ - /** */ -interface type_element +interface type_element extends lib_plankton.call.type_coproduct { - render_html( - ) : string - ; } diff --git a/source/elements/implementations/group.ts b/source/elements/implementations/group.ts index 0e7a0fb..094b629 100644 --- a/source/elements/implementations/group.ts +++ b/source/elements/implementations/group.ts @@ -1,51 +1,30 @@ + +/** + */ +type type_element_group_data = { + members : type_element; +}; + + /** */ class type_element_group implements type_element { - - /** - */ - private members : Array; - - - /** - */ - public constructor( - members : Array - ) - { - this.members = members; - } - - - /** - * @implementation - */ - public render_html( - ) : string - { - return ( - "
\n" - + - ( - this.members - .map(x => x.render_html()) - .join("") - ) - + - "
\n" - ); - } - + public readonly kind : string = "group"; + public readonly data : type_element_group_data; + public constructor(data : type_element_group_data) {this.data = data;} } +/** + */ element_kind_register( "group", (data, sub) => ( new type_element_group( - data["members"].map(x => sub(x)) + { + "members": data["members"].map(x => sub(x)) + } ) ) ); - diff --git a/source/elements/implementations/text.ts b/source/elements/implementations/text.ts index 74a5cde..e3a5a80 100644 --- a/source/elements/implementations/text.ts +++ b/source/elements/implementations/text.ts @@ -1,47 +1,30 @@ + +/** + */ +type type_element_text_data = { + content : string; +}; + + /** */ class type_element_text implements type_element { - - /** - */ - private content : string; - - - /** - */ - public constructor( - content : string - ) - { - this.content = content; - } - - - /** - * @implementation - */ - public render_html( - ) : string - { - return ( - "" - + - this.content - + - "\n" - ); - } - + public readonly kind : string = "text"; + public readonly data : type_element_text_data; + public constructor(data : type_element_text_data) {this.data = data;} } +/** + */ element_kind_register( "text", (data) => ( new type_element_text( - data["content"] + { + "content": data["content"], + } ) ) ); - diff --git a/source/main.ts b/source/main.ts index ce78753..3b5ac1c 100644 --- a/source/main.ts +++ b/source/main.ts @@ -21,7 +21,8 @@ function main() : void } } ); - const html : string = element.render_html(); + const output : type_output = new type_output_html({}); + const html : string = output.render_element(element); process.stdout.write(html + "\n"); } diff --git a/source/outputs/base.ts b/source/outputs/base.ts index 41f52ff..8b5e080 100644 --- a/source/outputs/base.ts +++ b/source/outputs/base.ts @@ -1,4 +1,15 @@ -interface interface_output + +/** + */ +interface type_output extends lib_plankton.call.type_coproduct { + + /** + */ + render_element( + element : type_element + ) : type_result + ; + } diff --git a/source/outputs/implementations/html.ts b/source/outputs/implementations/html.ts index de436a8..82c99e8 100644 --- a/source/outputs/implementations/html.ts +++ b/source/outputs/implementations/html.ts @@ -1 +1,46 @@ -class + +/** + */ +class type_output_html implements type_output +{ + public readonly kind : string = "html"; + public readonly data : {}; + public constructor(data : {}) {this.data = data;} + + /** + */ + public render_element( + element : type_element + ) : string + { + return lib_plankton.call.distinguish( + element, + { + "text": ({"content": content}) => ( + "" + + + content + + + "" + ), + "group": ({"members": members}) => ( + "
\n" + + + members.map(x => this.render_element(x)).join("") + + + "
\n" + ), + }, + { + "fallback": (element) => ( + "
"
+					+
+					JSON.stringify(element, undefined, "    ")
+					+
+					"
" + ), + } + ); + } + +} diff --git a/source/outputs/implementations/json.ts b/source/outputs/implementations/json.ts new file mode 100644 index 0000000..93a6eb7 --- /dev/null +++ b/source/outputs/implementations/json.ts @@ -0,0 +1,26 @@ + +/** + */ +class type_output_json implements type_output +{ + public readonly kind : string = "json"; + public readonly data : {}; + public constructor(data : {}) {this.data = data;} + + /** + */ + public render_element( + element : type_element + ) : string + { + return lib_plankton.call.distinguish( + element, + { + }, + { + "fallback": (element) => JSON.stringify(element, undefined, "\t"), + } + ); + } + +} diff --git a/source/outputs/implementations/raw.ts b/source/outputs/implementations/raw.ts deleted file mode 100644 index e69de29..0000000 diff --git a/tools/makefile b/tools/makefile index b4bac1e..73caec7 100644 --- a/tools/makefile +++ b/tools/makefile @@ -1,5 +1,6 @@ ## vars +dir_lib := lib dir_source := source dir_temp := temp dir_build := build @@ -21,16 +22,20 @@ cmd_cat := cat _default: ${dir_build}/sd ${dir_temp}/sd-unlinked.js: \ + ${dir_lib}/plankton/plankton.d.ts \ ${dir_source}/base.ts \ ${dir_source}/elements/base.ts \ ${dir_source}/elements/implementations/text.ts \ ${dir_source}/elements/implementations/group.ts \ + ${dir_source}/outputs/base.ts \ + ${dir_source}/outputs/implementations/json.ts \ + ${dir_source}/outputs/implementations/html.ts \ ${dir_source}/main.ts @ ${cmd_log} "compiling …" @ ${cmd_mkdir} $(dir $@) - @ ${cmd_tsc} $^ --outFile $@ + @ ${cmd_tsc} --lib es2020 $^ --outFile $@ -${dir_build}/sd: ${dir_temp}/sd-unlinked.js +${dir_build}/sd: ${dir_lib}/plankton/plankton.js ${dir_temp}/sd-unlinked.js @ ${cmd_log} "linking …" @ ${cmd_mkdir} $(dir $@) @ ${cmd_echo} "#!/usr/bin/env node" > $@ diff --git a/tools/update-plankton b/tools/update-plankton new file mode 100755 index 0000000..da129fd --- /dev/null +++ b/tools/update-plankton @@ -0,0 +1,20 @@ +#!/usr/bin/env sh + +## consts + +dir="lib/plankton" + + +## vars + +modules="" +modules="${modules} base" +modules="${modules} call" + + +## exec + +mkdir -p ${dir} +cd ${dir} +ptk bundle node ${modules} +cd - > /dev/null