@nemigo/helpers 0.13.2 → 0.13.3

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.
@@ -1,5 +1,6 @@
1
+ import { AsyncLocalStorage } from "async_hooks";
1
2
  export declare abstract class AsyncSpace<TX> {
2
- private async_storage;
3
+ __async_storage: AsyncLocalStorage<TX[]>;
3
4
  getTX(): TX[];
4
5
  space<T>(call: (tx: TX[]) => Promise<T>, auto?: boolean): Promise<T>;
5
6
  commit(): Promise<void>;
@@ -1,19 +1,19 @@
1
1
  import { AsyncLocalStorage } from "async_hooks";
2
2
  export class AsyncSpace {
3
- async_storage = new AsyncLocalStorage();
3
+ __async_storage = new AsyncLocalStorage();
4
4
  getTX() {
5
- const ctx = this.async_storage.getStore();
5
+ const ctx = this.__async_storage.getStore();
6
6
  if (ctx)
7
7
  return ctx;
8
8
  throw new Error("getTX() called outside of space()");
9
9
  }
10
10
  async space(call, auto = true) {
11
- const ctx = this.async_storage.getStore();
11
+ const ctx = this.__async_storage.getStore();
12
12
  if (ctx)
13
13
  return call(ctx);
14
14
  // Создаем новое пространство
15
15
  const tx = [];
16
- const result = await this.async_storage.run(tx, () => call(tx));
16
+ const result = await this.__async_storage.run(tx, () => call(tx));
17
17
  // Автоматически выполняем транзакции, если они есть и включен автокоммит
18
18
  if (auto && tx.length > 0) {
19
19
  await this.submit(tx);
@@ -22,7 +22,7 @@ export class AsyncSpace {
22
22
  return result;
23
23
  }
24
24
  async commit() {
25
- const ctx = this.async_storage.getStore();
25
+ const ctx = this.__async_storage.getStore();
26
26
  if (!ctx)
27
27
  throw new Error("commit() called outside of space()");
28
28
  await this.submit(ctx);
package/dist/future.d.ts CHANGED
@@ -47,4 +47,5 @@ export declare class Future {
47
47
  * Функция будет обёрнута в {@link boundary} при вызове
48
48
  */
49
49
  run(call: () => CanBePromise, { type, key, delay }?: FutureOptions): void;
50
+ has(type: FutureType, key: RID): boolean;
50
51
  }
package/dist/future.js CHANGED
@@ -47,7 +47,15 @@ export class Future {
47
47
  run(call, { type = "timer", key = Symbol(), delay = this.delay } = {}) {
48
48
  if (type === "timer") {
49
49
  this.reset(type, this.timers, key);
50
- const timer = setTimeout(call, delay);
50
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
51
+ const timer = setTimeout(async () => {
52
+ try {
53
+ await call();
54
+ }
55
+ finally {
56
+ this.timers.delete(key);
57
+ }
58
+ }, delay);
51
59
  this.timers.set(key, timer);
52
60
  }
53
61
  else {
@@ -56,4 +64,8 @@ export class Future {
56
64
  this.intervals.set(key, timer);
57
65
  }
58
66
  }
67
+ has(type, key) {
68
+ const map = this.get(type);
69
+ return map.has(key);
70
+ }
59
71
  }
package/dist/lru.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { RID } from "./types.js";
1
+ import type { RID, Timestamp } from "./types.js";
2
2
  /**
3
3
  * LRU (Least Recently Used) кэш с поддержкой TTL (time-to-live)
4
4
  *
@@ -20,12 +20,12 @@ export declare class LRU<V = unknown, K extends RID = string> {
20
20
  /**
21
21
  * Максимальное количество элементов в кэше
22
22
  */
23
- private readonly max;
23
+ readonly max: number;
24
24
  /**
25
25
  * Глобальный TTL (time-to-live) для каждого элемента в {@link Timestamp}.
26
26
  * Если не задан (`undefined`), элементы не устаревают глобально
27
27
  */
28
- private readonly ttl?;
28
+ readonly ttl?: Timestamp;
29
29
  /**
30
30
  * Внутреннее хранилище кэша.
31
31
  * Хранит значения вместе с информацией о времени истечения срока их действия
package/dist/omitter.d.ts CHANGED
@@ -64,7 +64,10 @@ export type MergeOmitSchemas<S1, S2> = {
64
64
  */
65
65
  export declare class Omitter<Source, OmitSchema> {
66
66
  #private;
67
- private readonly structure;
67
+ readonly __structure: {
68
+ keys: Set<KEYS<OmitSchema>>;
69
+ nested: Map<KEYS<OmitSchema>, Omitter<any, any>>;
70
+ };
68
71
  readonly schema: OmitSchema;
69
72
  readonly zip: ZIP;
70
73
  constructor(schema: OmitSchema);
package/dist/omitter.js CHANGED
@@ -12,7 +12,7 @@ import { zipper } from "./zipper.js";
12
12
  * const result = omitter.run(source); // { a: 1, b: { d: 3 }, e: 4 }
13
13
  */
14
14
  export class Omitter {
15
- structure;
15
+ __structure;
16
16
  schema;
17
17
  zip;
18
18
  constructor(schema) {
@@ -27,19 +27,19 @@ export class Omitter {
27
27
  }
28
28
  return true;
29
29
  }));
30
- this.structure = { keys, nested };
30
+ this.__structure = { keys, nested };
31
31
  }
32
32
  /**
33
33
  * Получение хэша вложенного эмиттера
34
34
  */
35
35
  into(key) {
36
- return this.structure.nested.get(key)?.zip;
36
+ return this.__structure.nested.get(key)?.zip;
37
37
  }
38
38
  /**
39
39
  * Проверяет, останется ли ключ в итоговом объекте
40
40
  */
41
41
  left(key) {
42
- return !this.structure.keys.has(key);
42
+ return !this.__structure.keys.has(key);
43
43
  }
44
44
  /**
45
45
  * Рекурсивно удаляет ключи из объекта на основе структуры
@@ -58,7 +58,7 @@ export class Omitter {
58
58
  structure.nested.forEach((omitter, key) => {
59
59
  const nested = obj[key];
60
60
  if (nested && typeof nested === "object")
61
- omitter.#process(nested, omitter.structure);
61
+ omitter.#process(nested, omitter.__structure);
62
62
  });
63
63
  }
64
64
  else {
@@ -72,7 +72,7 @@ export class Omitter {
72
72
  if (omitter) {
73
73
  const nested = obj[key];
74
74
  if (nested && typeof nested === "object") {
75
- omitter.#process(nested, omitter.structure);
75
+ omitter.#process(nested, omitter.__structure);
76
76
  }
77
77
  }
78
78
  }
@@ -86,7 +86,7 @@ export class Omitter {
86
86
  * @returns Объект с удалёнными ключами
87
87
  */
88
88
  run(source) {
89
- this.#process(source, this.structure);
89
+ this.#process(source, this.__structure);
90
90
  return source;
91
91
  }
92
92
  }
@@ -3,7 +3,10 @@ import type { RID } from "./types.js";
3
3
  * Класс для создания простого механизма подписок
4
4
  */
5
5
  export declare class Promoter<Data> {
6
- private __subs;
6
+ __subs: Map<RID, {
7
+ call: (data: Data) => void;
8
+ once?: boolean;
9
+ }>;
7
10
  /**
8
11
  * Вызывает подписчиков
9
12
  */
package/dist/queue.d.ts CHANGED
@@ -16,12 +16,13 @@ export declare abstract class Queue<T> {
16
16
  * Добавляет элемент в очередь и запускает обработку
17
17
  *
18
18
  * @param item - Элемент для добавления в очередь
19
+ * @param run - Запуск очереди
19
20
  */
20
- protected push(item: T): void;
21
+ protected push(item: T, run?: boolean): void;
21
22
  /**
22
23
  * Запускает обработку очереди с задержкой
23
24
  */
24
- private run;
25
+ run(): void;
25
26
  /**
26
27
  * Метод для обработки элемента очереди
27
28
  *
package/dist/queue.js CHANGED
@@ -18,10 +18,12 @@ export class Queue {
18
18
  * Добавляет элемент в очередь и запускает обработку
19
19
  *
20
20
  * @param item - Элемент для добавления в очередь
21
+ * @param run - Запуск очереди
21
22
  */
22
- push(item) {
23
+ push(item, run = true) {
23
24
  this._queue.push(item);
24
- this.run();
25
+ if (run)
26
+ this.run();
25
27
  }
26
28
  /**
27
29
  * Запускает обработку очереди с задержкой
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nemigo/helpers",
3
- "version": "0.13.2",
3
+ "version": "0.13.3",
4
4
  "private": false,
5
5
  "author": {
6
6
  "name": "Vlad Logvin",