@lsdsoftware/utils 1.0.4 → 1.0.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,6 @@
1
+ declare const _default: {
2
+ abortable1(): Promise<void>;
3
+ abortable2(): Promise<void>;
4
+ abortable3(): Promise<void>;
5
+ };
6
+ export default _default;
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const abortable_1 = require("./abortable");
13
+ const assert = require("assert");
14
+ function task(checkpoint, output) {
15
+ return __awaiter(this, void 0, void 0, function* () {
16
+ output.push(1);
17
+ yield new Promise(f => setTimeout(f, 100));
18
+ checkpoint();
19
+ output.push(2);
20
+ yield new Promise(f => setTimeout(f, 100));
21
+ checkpoint();
22
+ output.push(3);
23
+ yield new Promise(f => setTimeout(f, 100));
24
+ });
25
+ }
26
+ exports.default = {
27
+ abortable1() {
28
+ return __awaiter(this, void 0, void 0, function* () {
29
+ const [abort, abortPromise, checkpoint] = (0, abortable_1.makeAbortable)();
30
+ const timer = setTimeout(() => abort("Canceled"), 10000);
31
+ const output = [];
32
+ yield Promise.race([
33
+ abortPromise,
34
+ task(checkpoint, output)
35
+ .finally(() => clearTimeout(timer))
36
+ ]);
37
+ assert(output.length == 3 &&
38
+ output[0] == 1 &&
39
+ output[1] == 2 &&
40
+ output[2] == 3);
41
+ });
42
+ },
43
+ abortable2() {
44
+ return __awaiter(this, void 0, void 0, function* () {
45
+ const [abort, abortPromise, checkpoint] = (0, abortable_1.makeAbortable)();
46
+ setTimeout(() => abort("Canceled"), 150);
47
+ const output = [];
48
+ yield Promise.race([
49
+ abortPromise.catch(err => Promise.reject(err + " by promise")),
50
+ task(checkpoint, output)
51
+ ]).then(() => assert(false), err => assert(err == "Canceled by promise"));
52
+ assert(output.length == 2 &&
53
+ output[0] == 1 &&
54
+ output[1] == 2);
55
+ });
56
+ },
57
+ abortable3() {
58
+ return __awaiter(this, void 0, void 0, function* () {
59
+ const [abort, abortPromise, checkpoint] = (0, abortable_1.makeAbortable)();
60
+ setTimeout(() => abort("Canceled"), 150);
61
+ const output = [];
62
+ yield Promise.race([
63
+ abortPromise.catch(err => new Promise(f => "Never")),
64
+ task(checkpoint, output)
65
+ ]).then(() => assert(false), err => assert(err == "Canceled"));
66
+ assert(output.length == 2 &&
67
+ output[0] == 1 &&
68
+ output[1] == 2);
69
+ });
70
+ },
71
+ };
@@ -10,7 +10,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  const line_reader_test_1 = require("./line-reader.test");
13
- run(Object.assign({}, line_reader_test_1.default))
13
+ const semaphore_test_1 = require("./semaphore.test");
14
+ const abortable_test_1 = require("./abortable.test");
15
+ run(Object.assign(Object.assign(Object.assign({}, line_reader_test_1.default), semaphore_test_1.default), abortable_test_1.default))
14
16
  .catch(console.error);
15
17
  function run(tests) {
16
18
  return __awaiter(this, void 0, void 0, function* () {
@@ -1,6 +1,6 @@
1
1
  declare const _default: {
2
- lineSplitStream1(): Promise<void>;
3
- lineSplitStream2(): Promise<void>;
4
- lineSplitStream3(): Promise<void>;
2
+ lineReader1(): Promise<void>;
3
+ lineReader2(): Promise<void>;
4
+ lineReader3(): Promise<void>;
5
5
  };
6
6
  export default _default;
@@ -12,7 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  const line_reader_1 = require("./line-reader");
13
13
  const assert = require("assert");
14
14
  exports.default = {
15
- lineSplitStream1() {
15
+ lineReader1() {
16
16
  return __awaiter(this, void 0, void 0, function* () {
17
17
  const lines = [];
18
18
  const splitter = (0, line_reader_1.makeLineReader)(line => lines.push(line));
@@ -23,7 +23,7 @@ exports.default = {
23
23
  lines[2] == "And this is a line as well");
24
24
  });
25
25
  },
26
- lineSplitStream2() {
26
+ lineReader2() {
27
27
  return __awaiter(this, void 0, void 0, function* () {
28
28
  const lines = [];
29
29
  const splitter = (0, line_reader_1.makeLineReader)(line => lines.push(line));
@@ -33,7 +33,7 @@ exports.default = {
33
33
  lines[1] == "This is a line");
34
34
  });
35
35
  },
36
- lineSplitStream3() {
36
+ lineReader3() {
37
37
  return __awaiter(this, void 0, void 0, function* () {
38
38
  const lines = [];
39
39
  const splitter = (0, line_reader_1.makeLineReader)(line => lines.push(line));
package/dist/semaphore.js CHANGED
@@ -19,8 +19,8 @@ function makeSemaphore(count) {
19
19
  count--;
20
20
  else
21
21
  yield new Promise(f => waiters.push(f));
22
- checkpoint === null || checkpoint === void 0 ? void 0 : checkpoint();
23
22
  try {
23
+ checkpoint === null || checkpoint === void 0 ? void 0 : checkpoint();
24
24
  return yield task();
25
25
  }
26
26
  finally {
@@ -0,0 +1,6 @@
1
+ declare const _default: {
2
+ semaphore1(): Promise<void>;
3
+ semaphore2(): Promise<void>;
4
+ semaphore3(): Promise<void>;
5
+ };
6
+ export default _default;
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const semaphore_1 = require("./semaphore");
13
+ const assert = require("assert");
14
+ exports.default = {
15
+ semaphore1() {
16
+ return __awaiter(this, void 0, void 0, function* () {
17
+ const sema = (0, semaphore_1.makeSemaphore)(1);
18
+ const output = [];
19
+ yield Promise.all([
20
+ sema.runTask(() => __awaiter(this, void 0, void 0, function* () {
21
+ output.push(1);
22
+ yield new Promise(f => setTimeout(f, 100));
23
+ output.push(2);
24
+ })),
25
+ sema.runTask(() => __awaiter(this, void 0, void 0, function* () {
26
+ output.push(3);
27
+ yield new Promise(f => setTimeout(f, 50));
28
+ output.push(4);
29
+ }))
30
+ ]);
31
+ assert(output.length == 4 &&
32
+ output[0] == 1 &&
33
+ output[1] == 2 &&
34
+ output[2] == 3 &&
35
+ output[3] == 4);
36
+ });
37
+ },
38
+ semaphore2() {
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ const sema = (0, semaphore_1.makeSemaphore)(2);
41
+ const output = [];
42
+ yield Promise.all([
43
+ sema.runTask(() => __awaiter(this, void 0, void 0, function* () {
44
+ output.push(1);
45
+ yield new Promise(f => setTimeout(f, 100));
46
+ output.push(2);
47
+ })),
48
+ sema.runTask(() => __awaiter(this, void 0, void 0, function* () {
49
+ output.push(3);
50
+ yield new Promise(f => setTimeout(f, 50));
51
+ output.push(4);
52
+ }))
53
+ ]);
54
+ assert(output.length == 4 &&
55
+ output[0] == 1 &&
56
+ output[1] == 3 &&
57
+ output[2] == 4 &&
58
+ output[3] == 2);
59
+ });
60
+ },
61
+ semaphore3() {
62
+ return __awaiter(this, void 0, void 0, function* () {
63
+ const sema = (0, semaphore_1.makeSemaphore)(1);
64
+ const output = [];
65
+ yield Promise.all([
66
+ sema.runTask(() => __awaiter(this, void 0, void 0, function* () {
67
+ output.push(1);
68
+ yield new Promise(f => setTimeout(f, 100));
69
+ output.push(2);
70
+ }), () => {
71
+ throw new Error("Canceled");
72
+ }).then(() => assert(false), err => assert(err.message == "Canceled")),
73
+ sema.runTask(() => __awaiter(this, void 0, void 0, function* () {
74
+ output.push(3);
75
+ yield new Promise(f => setTimeout(f, 50));
76
+ output.push(4);
77
+ }))
78
+ ]);
79
+ assert(output.length == 2 &&
80
+ output[0] == 3 &&
81
+ output[1] == 4);
82
+ });
83
+ },
84
+ };
@@ -0,0 +1,4 @@
1
+ export declare function mockTimer(): {
2
+ sleep(duration: number): Promise<void>;
3
+ run(): void;
4
+ };
package/dist/utils.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mockTimer = void 0;
4
+ function mockTimer() {
5
+ const queue = [];
6
+ return {
7
+ sleep(duration) {
8
+ return new Promise(f => queue.push({ callback: f, duration }));
9
+ },
10
+ run() {
11
+ queue.sort((a, b) => a.duration - b.duration);
12
+ for (const { callback } of queue)
13
+ callback();
14
+ }
15
+ };
16
+ }
17
+ exports.mockTimer = mockTimer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lsdsoftware/utils",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "Useful JavaScript utilities",
5
5
  "main": "dist/index.js",
6
6
  "files": [