xcraft-core-utils 4.3.7 → 4.3.8

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.
package/.editorconfig CHANGED
@@ -1,9 +1,9 @@
1
- root = true
2
-
3
- [*.{js,jsx,json}]
4
- indent_style = space
5
- indent_size = 2
6
- charset = utf-8
7
- trim_trailing_whitespace = true
8
- insert_final_newline = true
9
- max_line_length = 80
1
+ root = true
2
+
3
+ [*.{js,jsx,json}]
4
+ indent_style = space
5
+ indent_size = 2
6
+ charset = utf-8
7
+ trim_trailing_whitespace = true
8
+ insert_final_newline = true
9
+ max_line_length = 80
package/.zou-flow ADDED
@@ -0,0 +1,3 @@
1
+ [update-version]
2
+ freezed = true
3
+ package-json = package.json
package/lib/locks.js CHANGED
@@ -1,106 +1,106 @@
1
- 'use strict';
2
-
3
- const watt = require('gigawatts');
4
- const locks = require('locks');
5
-
6
- class Semaphore {
7
- constructor(initialValue = 1) {
8
- this._sem = locks.createSemaphore(initialValue);
9
- watt.wrapAll(this, 'wait');
10
- }
11
-
12
- *wait(next) {
13
- yield this._sem.wait(next);
14
- }
15
-
16
- signal() {
17
- this._sem.signal();
18
- }
19
- }
20
-
21
- class Mutex {
22
- constructor() {
23
- this._mutex = locks.createMutex();
24
- watt.wrapAll(this, 'lock');
25
- }
26
-
27
- *lock(next) {
28
- yield this._mutex.lock(next.arg(0));
29
- }
30
-
31
- unlock() {
32
- this._mutex.unlock();
33
- }
34
- }
35
-
36
- class RecursiveMutex extends Mutex {
37
- constructor() {
38
- super();
39
- this._owner = null;
40
- this._refCount = 0;
41
- watt.wrapAll(this, 'lock');
42
- }
43
-
44
- *lock(owner, next) {
45
- if (!owner) {
46
- throw new Error('owner missing');
47
- }
48
-
49
- if (this._mutex.isLocked && owner === this._owner) {
50
- ++this._refCount;
51
- return; /* Same owner, continue because already locked */
52
- }
53
-
54
- yield* super.lock(next);
55
- this._owner = owner;
56
- ++this._refCount;
57
- }
58
-
59
- unlock(owner) {
60
- if (owner !== this._owner) {
61
- throw new Error('bad owner');
62
- }
63
-
64
- --this._refCount;
65
- if (this._refCount < 1) {
66
- this._owner = null;
67
- super.unlock();
68
- }
69
- }
70
- }
71
-
72
- class GetMutex {
73
- constructor() {
74
- this._mutexes = {};
75
- watt.wrapAll(this, 'lock');
76
- }
77
-
78
- _cleanupMutex(key) {
79
- if (!this._mutexes[key] || this._mutexes[key].isLocked) {
80
- return;
81
- }
82
- delete this._mutexes[key];
83
- }
84
-
85
- *lock(key, next) {
86
- if (this._mutexes[key]) {
87
- yield this._mutexes[key].lock(next.arg(0));
88
- return;
89
- }
90
-
91
- this._mutexes[key] = locks.createMutex();
92
- yield this._mutexes[key].lock(next.arg(0));
93
- }
94
-
95
- unlock(key) {
96
- this._mutexes[key].unlock();
97
- this._cleanupMutex(key);
98
- }
99
- }
100
-
101
- module.exports = {
102
- Mutex,
103
- RecursiveMutex,
104
- Semaphore,
105
- getMutex: new GetMutex(),
106
- };
1
+ 'use strict';
2
+
3
+ const watt = require('gigawatts');
4
+ const locks = require('locks');
5
+
6
+ class Semaphore {
7
+ constructor(initialValue = 1) {
8
+ this._sem = locks.createSemaphore(initialValue);
9
+ watt.wrapAll(this, 'wait');
10
+ }
11
+
12
+ *wait(next) {
13
+ yield this._sem.wait(next);
14
+ }
15
+
16
+ signal() {
17
+ this._sem.signal();
18
+ }
19
+ }
20
+
21
+ class Mutex {
22
+ constructor() {
23
+ this._mutex = locks.createMutex();
24
+ watt.wrapAll(this, 'lock');
25
+ }
26
+
27
+ *lock(next) {
28
+ yield this._mutex.lock(next.arg(0));
29
+ }
30
+
31
+ unlock() {
32
+ this._mutex.unlock();
33
+ }
34
+ }
35
+
36
+ class RecursiveMutex extends Mutex {
37
+ constructor() {
38
+ super();
39
+ this._owner = null;
40
+ this._refCount = 0;
41
+ watt.wrapAll(this, 'lock');
42
+ }
43
+
44
+ *lock(owner, next) {
45
+ if (!owner) {
46
+ throw new Error('owner missing');
47
+ }
48
+
49
+ if (this._mutex.isLocked && owner === this._owner) {
50
+ ++this._refCount;
51
+ return; /* Same owner, continue because already locked */
52
+ }
53
+
54
+ yield* super.lock(next);
55
+ this._owner = owner;
56
+ ++this._refCount;
57
+ }
58
+
59
+ unlock(owner) {
60
+ if (owner !== this._owner) {
61
+ throw new Error('bad owner');
62
+ }
63
+
64
+ --this._refCount;
65
+ if (this._refCount < 1) {
66
+ this._owner = null;
67
+ super.unlock();
68
+ }
69
+ }
70
+ }
71
+
72
+ class GetMutex {
73
+ constructor() {
74
+ this._mutexes = {};
75
+ watt.wrapAll(this, 'lock');
76
+ }
77
+
78
+ _cleanupMutex(key) {
79
+ if (!this._mutexes[key] || this._mutexes[key].isLocked) {
80
+ return;
81
+ }
82
+ delete this._mutexes[key];
83
+ }
84
+
85
+ *lock(key, next) {
86
+ if (this._mutexes[key]) {
87
+ yield this._mutexes[key].lock(next.arg(0));
88
+ return;
89
+ }
90
+
91
+ this._mutexes[key] = locks.createMutex();
92
+ yield this._mutexes[key].lock(next.arg(0));
93
+ }
94
+
95
+ unlock(key) {
96
+ this._mutexes[key].unlock();
97
+ this._cleanupMutex(key);
98
+ }
99
+ }
100
+
101
+ module.exports = {
102
+ Mutex,
103
+ RecursiveMutex,
104
+ Semaphore,
105
+ getMutex: new GetMutex(),
106
+ };
@@ -1,87 +1,87 @@
1
- 'use strict';
2
-
3
- const EventEmitter = require('events');
4
- const LinkedList = require('linked-list');
5
-
6
- class RankedCache extends EventEmitter {
7
- constructor(limit) {
8
- super();
9
-
10
- this._limit = limit;
11
- this._size = 0;
12
- this._linkedList = new LinkedList();
13
- }
14
-
15
- /**
16
- * Push a new payload or an existing item in the linked cache.
17
- *
18
- * If the limit is reached, the older item is sent by event emitter to the
19
- * `out` topic.
20
- *
21
- * Note that a RankedCache with a limit <= 0 will just return null.
22
- *
23
- * @param {*} item - Push an item in the linked cache.
24
- * @returns {*} the ranked item (always wrapper in an Item).
25
- */
26
- rank(item) {
27
- if (this._limit <= 0) {
28
- return null;
29
- }
30
-
31
- const isNew = !(item instanceof LinkedList.Item);
32
-
33
- /* Create the item on the fly if necessary. */
34
- if (isNew) {
35
- const _item = new LinkedList.Item();
36
- _item.payload = item;
37
- item = _item;
38
- }
39
-
40
- /* Limit the cache to this._limit entries. The less used item is deleted
41
- * when the limit is reached.
42
- */
43
- if (this._size === this._limit) {
44
- const prevItem = this._linkedList.head;
45
- prevItem.detach();
46
- this._size--;
47
- this.emit('out', prevItem);
48
- }
49
-
50
- if (item.list) {
51
- /* When an existing style is used, detach from its current position
52
- * and move of one step in the linked-list. The goal is to keep the less
53
- * used items in front of the list (head).
54
- */
55
- const nextItem = item.next;
56
- if (nextItem) {
57
- item.detach();
58
- nextItem.append(item);
59
- }
60
- } else {
61
- /* Add the item to the end of the list. Here, it's still not possible to
62
- * be sure that this item will be often used. Anyway, if it's not used
63
- * anymore, it will move one-by-one to the front of the list.
64
- */
65
- this._linkedList.append(item);
66
- this._size++;
67
- }
68
-
69
- return item;
70
- }
71
-
72
- /**
73
- * Clear the whole RankedCache.
74
- *
75
- * All items are sent to the 'out' event.
76
- */
77
- clear() {
78
- let item;
79
- while ((item = this._linkedList.head)) {
80
- item.detach();
81
- this._size--;
82
- this.emit('out', item);
83
- }
84
- }
85
- }
86
-
87
- module.exports = RankedCache;
1
+ 'use strict';
2
+
3
+ const EventEmitter = require('events');
4
+ const LinkedList = require('linked-list');
5
+
6
+ class RankedCache extends EventEmitter {
7
+ constructor(limit) {
8
+ super();
9
+
10
+ this._limit = limit;
11
+ this._size = 0;
12
+ this._linkedList = new LinkedList();
13
+ }
14
+
15
+ /**
16
+ * Push a new payload or an existing item in the linked cache.
17
+ *
18
+ * If the limit is reached, the older item is sent by event emitter to the
19
+ * `out` topic.
20
+ *
21
+ * Note that a RankedCache with a limit <= 0 will just return null.
22
+ *
23
+ * @param {*} item - Push an item in the linked cache.
24
+ * @returns {*} the ranked item (always wrapper in an Item).
25
+ */
26
+ rank(item) {
27
+ if (this._limit <= 0) {
28
+ return null;
29
+ }
30
+
31
+ const isNew = !(item instanceof LinkedList.Item);
32
+
33
+ /* Create the item on the fly if necessary. */
34
+ if (isNew) {
35
+ const _item = new LinkedList.Item();
36
+ _item.payload = item;
37
+ item = _item;
38
+ }
39
+
40
+ /* Limit the cache to this._limit entries. The less used item is deleted
41
+ * when the limit is reached.
42
+ */
43
+ if (this._size === this._limit) {
44
+ const prevItem = this._linkedList.head;
45
+ prevItem.detach();
46
+ this._size--;
47
+ this.emit('out', prevItem);
48
+ }
49
+
50
+ if (item.list) {
51
+ /* When an existing style is used, detach from its current position
52
+ * and move of one step in the linked-list. The goal is to keep the less
53
+ * used items in front of the list (head).
54
+ */
55
+ const nextItem = item.next;
56
+ if (nextItem) {
57
+ item.detach();
58
+ nextItem.append(item);
59
+ }
60
+ } else {
61
+ /* Add the item to the end of the list. Here, it's still not possible to
62
+ * be sure that this item will be often used. Anyway, if it's not used
63
+ * anymore, it will move one-by-one to the front of the list.
64
+ */
65
+ this._linkedList.append(item);
66
+ this._size++;
67
+ }
68
+
69
+ return item;
70
+ }
71
+
72
+ /**
73
+ * Clear the whole RankedCache.
74
+ *
75
+ * All items are sent to the 'out' event.
76
+ */
77
+ clear() {
78
+ let item;
79
+ while ((item = this._linkedList.head)) {
80
+ item.detach();
81
+ this._size--;
82
+ this.emit('out', item);
83
+ }
84
+ }
85
+ }
86
+
87
+ module.exports = RankedCache;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xcraft-core-utils",
3
- "version": "4.3.7",
3
+ "version": "4.3.8",
4
4
  "description": "Xcraft utils",
5
5
  "main": "index.js",
6
6
  "engines": {