exupery-core-internals 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ export type I_Async_Monitor = {
2
+ readonly 'report process started': () => void;
3
+ readonly 'report process finished': () => void;
4
+ };
5
+ /**
6
+ * this function helps in keeping track of ongoing async operations
7
+ * async operations are registered and when finished reported as such.
8
+ * when all ongoing operations are finished the onEnd callback is called
9
+ *
10
+ * this function is specifically useful for async map functions
11
+ *
12
+ * @param callback this callback creates a scope within which the counter is provided
13
+ * @param onEnd this callback will be called when all ongoing operations are finished
14
+ */
15
+ export declare function create_asynchronous_processes_monitor(monitoring_phase: ($: I_Async_Monitor) => void, on_all_finished: () => void): void;
@@ -1,16 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.create_async_registry = create_async_registry;
3
+ exports.create_asynchronous_processes_monitor = create_asynchronous_processes_monitor;
4
4
  /**
5
- * this function provides a callback with a counter as parameter
6
- * when the counter reaches 0, the onEnd callback is called
5
+ * this function helps in keeping track of ongoing async operations
6
+ * async operations are registered and when finished reported as such.
7
+ * when all ongoing operations are finished the onEnd callback is called
7
8
  *
8
9
  * this function is specifically useful for async map functions
9
10
  *
10
11
  * @param callback this callback creates a scope within which the counter is provided
11
- * @param onEnd this callback will be called when the counter reaches 0
12
+ * @param onEnd this callback will be called when all ongoing operations are finished
12
13
  */
13
- function create_async_registry(registration_phase, on_all_finished) {
14
+ function create_asynchronous_processes_monitor(monitoring_phase, on_all_finished) {
14
15
  let counter = 0;
15
16
  /*
16
17
  * we need to keep track of if the registration phase is ended or not.
@@ -31,14 +32,14 @@ function create_async_registry(registration_phase, on_all_finished) {
31
32
  }
32
33
  }
33
34
  }
34
- registration_phase({
35
- register: () => {
35
+ monitoring_phase({
36
+ 'report process started': () => {
36
37
  if (on_all_finished_has_been_called) {
37
38
  throw new Error("CORE: async call done after context is ready");
38
39
  }
39
40
  counter += 1;
40
41
  },
41
- report_finished: () => {
42
+ 'report process finished': () => {
42
43
  if (counter === 0) {
43
44
  throw new Error("CORE: decrement while counter is 0");
44
45
  }
@@ -1,4 +1,4 @@
1
- import * as pt from "exupery-core-types";
1
+ import * as _et from "exupery-core-types";
2
2
  /**
3
3
  * returns a Exupery array
4
4
  * why is this not the constructor? to call a constructor, you have to use the keyword 'new'. Exupery doesn't use the concept of a class so that keyword should be avoided
@@ -6,4 +6,4 @@ import * as pt from "exupery-core-types";
6
6
  * @param source An array literal
7
7
  * @returns
8
8
  */
9
- export declare function array_literal<T>(source: readonly T[]): pt.Array<T>;
9
+ export declare function array_literal<T>(source: readonly T[]): _et.Array<T>;
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.array_literal = array_literal;
4
- const create_async_registry_1 = require("../private/create_async_registry");
5
- const cast_to_async_value_imp_1 = require("./cast_to_async_value_imp");
4
+ const create_asynchronous_processes_monitor_1 = require("../private/create_asynchronous_processes_monitor");
5
+ const create_Async_Value_1 = require("./create_Async_Value");
6
+ const create_Unsafe_Async_Value_1 = require("./create_Unsafe_Async_Value");
6
7
  const not_set_1 = require("./not_set");
7
8
  const set_1 = require("./set");
8
9
  /**
@@ -19,34 +20,54 @@ class Array_Class {
19
20
  return $v(entry);
20
21
  }));
21
22
  }
22
- async_map($v) {
23
- // const elements = source.map($v)
24
- // let _isGuaranteedToReturnAResult = true
25
- // source.forEach(($) => {
26
- // if ($)
27
- // })
28
- function array(array, $v) {
29
- const mapped = array.map($v);
30
- return (0, cast_to_async_value_imp_1.cast_to_async_value_imp)({
31
- 'execute': (on_value) => {
32
- const temp = [];
33
- (0, create_async_registry_1.create_async_registry)((registry) => {
34
- mapped.forEach((v) => {
35
- registry.register();
36
- v.__start((v) => {
37
- temp.push(v);
38
- registry.report_finished();
39
- });
23
+ map_async(on_element_value) {
24
+ const data = this.data;
25
+ return (0, create_Async_Value_1.create_Async_Value)({
26
+ 'execute': (on_array_value) => {
27
+ const temp = [];
28
+ (0, create_asynchronous_processes_monitor_1.create_asynchronous_processes_monitor)((registry) => {
29
+ data.map(on_element_value).forEach((v) => {
30
+ registry['report process started']();
31
+ v.__start((v) => {
32
+ temp.push(v);
33
+ registry['report process finished']();
40
34
  });
41
- }, () => {
42
- on_value(array_literal(temp));
43
35
  });
44
- }
45
- });
46
- }
47
- return array(this.data, $v);
36
+ }, () => {
37
+ on_array_value(array_literal(temp));
38
+ });
39
+ }
40
+ });
41
+ }
42
+ map_async_unsafe(on_element_value) {
43
+ const data = this.data;
44
+ return (0, create_Unsafe_Async_Value_1.create_Unsafe_Async_Value)({
45
+ 'execute': (on_array_value, on_array_exception) => {
46
+ const temp_values = [];
47
+ const temp_exceptions = [];
48
+ (0, create_asynchronous_processes_monitor_1.create_asynchronous_processes_monitor)((registry) => {
49
+ data.map(on_element_value).forEach((v) => {
50
+ registry['report process started']();
51
+ v.__start(($) => {
52
+ temp_values.push($);
53
+ registry['report process finished']();
54
+ }, ($) => {
55
+ temp_exceptions.push($);
56
+ registry['report process finished']();
57
+ });
58
+ });
59
+ }, () => {
60
+ if (temp_exceptions.length > 0) {
61
+ on_array_exception(array_literal(temp_exceptions));
62
+ }
63
+ else {
64
+ on_array_value(array_literal(temp_values));
65
+ }
66
+ });
67
+ }
68
+ });
48
69
  }
49
- /////////
70
+ //internal methods
50
71
  __for_each($i) {
51
72
  this.data.forEach(($) => {
52
73
  $i($);
@@ -7,4 +7,4 @@ export type Executer<T> = {
7
7
  * @param executer the function that produces the eventual value
8
8
  * @returns
9
9
  */
10
- export declare function cast_to_async_value_imp<T>(executer: Executer<T>): pt.Async_Value<T>;
10
+ export declare function create_Async_Value<T>(executer: Executer<T>): pt.Async_Value<T>;
@@ -1,12 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.cast_to_async_value_imp = cast_to_async_value_imp;
3
+ exports.create_Async_Value = create_Async_Value;
4
4
  class Async_Value_Class {
5
5
  constructor(executer) {
6
6
  this.executer = executer;
7
7
  }
8
8
  map(handle_value) {
9
- return cast_to_async_value_imp({
9
+ return create_Async_Value({
10
+ 'execute': (on_value) => {
11
+ this.executer.execute((value) => {
12
+ on_value(handle_value(value));
13
+ });
14
+ }
15
+ });
16
+ }
17
+ then(handle_value) {
18
+ return create_Async_Value({
10
19
  'execute': (on_value) => {
11
20
  this.executer.execute((value) => {
12
21
  handle_value(value).__start(on_value);
@@ -23,6 +32,6 @@ class Async_Value_Class {
23
32
  * @param executer the function that produces the eventual value
24
33
  * @returns
25
34
  */
26
- function cast_to_async_value_imp(executer) {
35
+ function create_Async_Value(executer) {
27
36
  return new Async_Value_Class(executer);
28
37
  }
@@ -0,0 +1,17 @@
1
+ import * as _et from "exupery-core-types";
2
+ /**
3
+ * this function contains the body in which the async value or exception is executed
4
+ * after the execution, either the on_value or on_exception callback will be called
5
+ * @param on_value the callback to call when a value is produced
6
+ * @param on_exception the callback to call when an error is produced
7
+ */
8
+ type Executer<T, E> = {
9
+ 'execute': (on_value: ($: T) => void, on_exception: ($: E) => void) => void;
10
+ };
11
+ /**
12
+ * returns an {@link Async_Value }
13
+ * @param executer the function that produces the eventual value
14
+ * @returns
15
+ */
16
+ export declare function create_Unsafe_Async_Value<T, E>(executer: Executer<T, E>): _et.Unsafe_Async_Value<T, E>;
17
+ export {};
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.create_Unsafe_Async_Value = create_Unsafe_Async_Value;
4
+ const create_Async_Value_1 = require("./create_Async_Value");
5
+ class Unsafe_Async_Value_Class {
6
+ constructor(executer) {
7
+ this.executer = executer;
8
+ }
9
+ map(handle_value) {
10
+ return new Unsafe_Async_Value_Class({
11
+ 'execute': (on_value, on_exception) => {
12
+ this.executer.execute(($) => {
13
+ on_value(handle_value($));
14
+ }, on_exception);
15
+ }
16
+ });
17
+ }
18
+ then(handle_value) {
19
+ return new Unsafe_Async_Value_Class({
20
+ 'execute': (new_on_value, new_on_exception) => {
21
+ this.executer.execute(($) => {
22
+ handle_value($).__start(new_on_value, new_on_exception);
23
+ }, new_on_exception);
24
+ }
25
+ });
26
+ }
27
+ if_exception_then(handle_exception) {
28
+ return new Unsafe_Async_Value_Class({
29
+ 'execute': (new_on_value, new_on_exception) => {
30
+ this.executer.execute(new_on_value, ($) => {
31
+ handle_exception($).__start(new_on_value, new_on_exception);
32
+ });
33
+ }
34
+ });
35
+ }
36
+ map_exception(handle_exception) {
37
+ return new Unsafe_Async_Value_Class({
38
+ 'execute': (on_value, on_exception) => {
39
+ this.executer.execute(on_value, ($) => {
40
+ on_exception(handle_exception($));
41
+ });
42
+ }
43
+ });
44
+ }
45
+ catch(handle_exception) {
46
+ return (0, create_Async_Value_1.create_Async_Value)({
47
+ 'execute': (on_value) => {
48
+ this.executer.execute(on_value, ($) => {
49
+ on_value(handle_exception($));
50
+ });
51
+ }
52
+ });
53
+ }
54
+ catch_and_map(handle_value, handle_exception) {
55
+ return (0, create_Async_Value_1.create_Async_Value)({
56
+ 'execute': (on_value) => {
57
+ this.executer.execute(($) => {
58
+ on_value(handle_value($));
59
+ }, ($) => {
60
+ on_value(handle_exception($));
61
+ });
62
+ }
63
+ });
64
+ }
65
+ __start(on_value, on_exception) {
66
+ this.executer.execute(on_value, on_exception);
67
+ }
68
+ }
69
+ /**
70
+ * returns an {@link Async_Value }
71
+ * @param executer the function that produces the eventual value
72
+ * @returns
73
+ */
74
+ function create_Unsafe_Async_Value(executer) {
75
+ return new Unsafe_Async_Value_Class(executer);
76
+ }
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.dictionary_literal = dictionary_literal;
4
- const create_async_registry_1 = require("../private/create_async_registry");
5
- const cast_to_async_value_imp_1 = require("./cast_to_async_value_imp");
4
+ const create_asynchronous_processes_monitor_1 = require("../private/create_asynchronous_processes_monitor");
5
+ const create_Async_Value_1 = require("./create_Async_Value");
6
+ const create_Unsafe_Async_Value_1 = require("./create_Unsafe_Async_Value");
6
7
  const set_1 = require("./set");
7
8
  const not_set_1 = require("./not_set");
8
9
  const array_literal_1 = require("./array_literal");
@@ -21,32 +22,52 @@ class Dictionary {
21
22
  };
22
23
  }));
23
24
  }
24
- async_map($v) {
25
- function imp(dictionary_as_array, $v) {
26
- const mapped = dictionary_as_array.map(($) => {
27
- return {
28
- key: $.key,
29
- value: $v($.value),
30
- };
31
- });
32
- return (0, cast_to_async_value_imp_1.cast_to_async_value_imp)({
33
- 'execute': (on_value) => {
34
- const temp = {};
35
- (0, create_async_registry_1.create_async_registry)((counter) => {
36
- mapped.map(($) => {
37
- counter.register();
38
- $.value.__start((nv) => {
39
- temp[$.key] = nv;
40
- counter.report_finished();
41
- });
25
+ map_async(on_entry_value) {
26
+ const source = this.source;
27
+ const temp = {};
28
+ return (0, create_Async_Value_1.create_Async_Value)({
29
+ 'execute': (on_dictionary_value) => {
30
+ (0, create_asynchronous_processes_monitor_1.create_asynchronous_processes_monitor)((counter) => {
31
+ source.map(($) => {
32
+ counter['report process started']();
33
+ on_entry_value($.value).__start((nv) => {
34
+ temp[$.key] = nv;
35
+ counter['report process finished']();
42
36
  });
43
- }, () => {
44
- on_value(dictionary_literal(temp));
45
37
  });
46
- }
47
- });
48
- }
49
- return imp(this.source, $v);
38
+ }, () => {
39
+ on_dictionary_value(dictionary_literal(temp));
40
+ });
41
+ }
42
+ });
43
+ }
44
+ map_async_unsafe(on_entry_value) {
45
+ const source = this.source;
46
+ const temp_values = {};
47
+ const temp_exceptions = {};
48
+ return (0, create_Unsafe_Async_Value_1.create_Unsafe_Async_Value)({
49
+ 'execute': (on_dictionary_value, on_dictionary_exception) => {
50
+ (0, create_asynchronous_processes_monitor_1.create_asynchronous_processes_monitor)((counter) => {
51
+ source.map(($) => {
52
+ counter['report process started']();
53
+ on_entry_value($.value).__start((value) => {
54
+ temp_values[$.key] = value;
55
+ counter['report process finished']();
56
+ }, (exception) => {
57
+ temp_exceptions[$.key] = exception;
58
+ counter['report process finished']();
59
+ });
60
+ });
61
+ }, () => {
62
+ if (Object.keys(temp_exceptions).length > 0) {
63
+ on_dictionary_exception(dictionary_literal(temp_exceptions));
64
+ }
65
+ else {
66
+ on_dictionary_value(dictionary_literal(temp_values));
67
+ }
68
+ });
69
+ }
70
+ });
50
71
  }
51
72
  __map_with_key($v) {
52
73
  return new Dictionary(this.source.map(($) => {
@@ -1,14 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.make_async = make_async;
4
- const cast_to_async_value_imp_1 = require("./cast_to_async_value_imp");
4
+ const create_Async_Value_1 = require("./create_Async_Value");
5
5
  /**
6
6
  * converts a regular value to a {@link Async_Value}
7
7
  * @param $ the value
8
8
  * @returns
9
9
  */
10
10
  function make_async($) {
11
- return (0, cast_to_async_value_imp_1.cast_to_async_value_imp)({
11
+ return (0, create_Async_Value_1.create_Async_Value)({
12
12
  'execute': (on_value) => {
13
13
  on_value($);
14
14
  }
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.resolve_async_tuple_2 = resolve_async_tuple_2;
4
- const cast_to_async_value_imp_1 = require("./cast_to_async_value_imp");
4
+ const create_Async_Value_1 = require("./create_Async_Value");
5
5
  function resolve_async_tuple_2(first, second) {
6
- return (0, cast_to_async_value_imp_1.cast_to_async_value_imp)({
6
+ return (0, create_Async_Value_1.create_Async_Value)({
7
7
  'execute': (on_value) => {
8
8
  let element_1_is_set = false;
9
9
  let element_2_is_set = false;
package/dist/index.d.ts CHANGED
@@ -8,7 +8,7 @@ export * from "./imp/public/switch_state";
8
8
  export * from "./imp/public/get_location_info";
9
9
  export * from "./imp/public/resolve_async_tuple_2";
10
10
  export * from "./imp/public/make_async";
11
- export * from "./imp/public/cast_to_async_value_imp";
11
+ export * from "./imp/public/create_Async_Value";
12
12
  export * from "./imp/public/set";
13
13
  export * from "./imp/public/not_set";
14
14
  export * from "./imp/public/Error";
package/dist/index.js CHANGED
@@ -24,7 +24,7 @@ __exportStar(require("./imp/public/switch_state"), exports);
24
24
  __exportStar(require("./imp/public/get_location_info"), exports);
25
25
  __exportStar(require("./imp/public/resolve_async_tuple_2"), exports);
26
26
  __exportStar(require("./imp/public/make_async"), exports);
27
- __exportStar(require("./imp/public/cast_to_async_value_imp"), exports);
27
+ __exportStar(require("./imp/public/create_Async_Value"), exports);
28
28
  __exportStar(require("./imp/public/set"), exports);
29
29
  __exportStar(require("./imp/public/not_set"), exports);
30
30
  __exportStar(require("./imp/public/Error"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exupery-core-internals",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "license": "ISC",
5
5
  "description": "",
6
6
  "author": "Corno",
@@ -15,7 +15,7 @@
15
15
  "url": "https://github.com/corno/exupery-core/issues"
16
16
  },
17
17
  "dependencies": {
18
- "exupery-core-types": "^0.1.2"
18
+ "exupery-core-types": "^0.1.4"
19
19
  },
20
20
  "devDependencies": {
21
21
  "@types/node": "^18.15.3"
@@ -1,14 +0,0 @@
1
- export type I_Async_Registry = {
2
- readonly 'register': () => void;
3
- readonly 'report_finished': () => void;
4
- };
5
- /**
6
- * this function provides a callback with a counter as parameter
7
- * when the counter reaches 0, the onEnd callback is called
8
- *
9
- * this function is specifically useful for async map functions
10
- *
11
- * @param callback this callback creates a scope within which the counter is provided
12
- * @param onEnd this callback will be called when the counter reaches 0
13
- */
14
- export declare function create_async_registry(registration_phase: ($: I_Async_Registry) => void, on_all_finished: () => void): void;