Blame view

lib/mutex.js 1.49 KB
cba7eef40   Adhidarma Hadiwinoto   Separate locks fu...
1
2
3
4
5
6
  'use strict';
  
  const locks = require('locks');
  
  const mutexWaitForOK = locks.createMutex();
  const mutexCommand = locks.createMutex();
0bdac2f9c   Adhidarma Hadiwinoto   Bersih kirim sms ...
7
  const mutexSubCommand = locks.createMutex();
cba7eef40   Adhidarma Hadiwinoto   Separate locks fu...
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  function setLockWaitForOK() {
      return new Promise((resolve) => {
          mutexWaitForOK.lock(() => {
              resolve(true);
          });
      });
  }
  
  function releaseLockWaitForOK() {
      try {
          mutexWaitForOK.unlock();
      } catch (e) {
          //
      }
  }
  
  function setLockWaitForCommand() {
      return new Promise((resolve) => {
          mutexCommand.lock(() => {
              resolve(true);
          });
      });
  }
0bdac2f9c   Adhidarma Hadiwinoto   Bersih kirim sms ...
31
32
33
  function tryLockWaitForCommand() {
      return mutexCommand.tryLock();
  }
cba7eef40   Adhidarma Hadiwinoto   Separate locks fu...
34
  function releaseLockWaitForCommand() {
0bdac2f9c   Adhidarma Hadiwinoto   Bersih kirim sms ...
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
      try {
          mutexCommand.unlock();
      } catch (e) {
          //
      }
  }
  
  function setLockWaitForSubCommand() {
      return new Promise((resolve) => {
          mutexSubCommand.lock(() => {
              resolve(true);
          });
      });
  }
  
  function releaseLockWaitForSubCommand() {
2db546a87   Adhidarma Hadiwinoto   Regex parser
51
      // mutexSubCommand.unlock();
0bdac2f9c   Adhidarma Hadiwinoto   Bersih kirim sms ...
52

0bdac2f9c   Adhidarma Hadiwinoto   Bersih kirim sms ...
53
54
55
56
57
      try {
          mutexSubCommand.unlock();
      } catch (e) {
          //
      }
cba7eef40   Adhidarma Hadiwinoto   Separate locks fu...
58
59
60
61
62
63
64
  }
  
  exports.setLockWaitForOK = setLockWaitForOK;
  exports.releaseLockWaitForOK = releaseLockWaitForOK;
  
  exports.setLockWaitForCommand = setLockWaitForCommand;
  exports.releaseLockWaitForCommand = releaseLockWaitForCommand;
0bdac2f9c   Adhidarma Hadiwinoto   Bersih kirim sms ...
65
66
67
68
  exports.tryLockWaitForCommand = tryLockWaitForCommand;
  
  exports.setLockWaitForSubCommand = setLockWaitForSubCommand;
  exports.releaseLockWaitForSubCommand = releaseLockWaitForSubCommand;