No Description

doorlock.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
  2. import _createClass from 'babel-runtime/helpers/createClass';
  3. /*
  4. Copyright 2013-2015 ASIAL CORPORATION
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. */
  15. var generateId = function () {
  16. var i = 0;
  17. return function () {
  18. return i++;
  19. };
  20. }();
  21. /**
  22. * Door locking system.
  23. *
  24. * @param {Object} [options]
  25. * @param {Function} [options.log]
  26. */
  27. var DoorLock = function () {
  28. function DoorLock() {
  29. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  30. _classCallCheck(this, DoorLock);
  31. this._lockList = [];
  32. this._waitList = [];
  33. this._log = options.log || function () {};
  34. }
  35. /**
  36. * Register a lock.
  37. *
  38. * @return {Function} Callback for unlocking.
  39. */
  40. _createClass(DoorLock, [{
  41. key: 'lock',
  42. value: function lock() {
  43. var _this = this;
  44. var unlock = function unlock() {
  45. _this._unlock(unlock);
  46. };
  47. unlock.id = generateId();
  48. this._lockList.push(unlock);
  49. this._log('lock: ' + unlock.id);
  50. return unlock;
  51. }
  52. }, {
  53. key: '_unlock',
  54. value: function _unlock(fn) {
  55. var index = this._lockList.indexOf(fn);
  56. if (index === -1) {
  57. throw new Error('This function is not registered in the lock list.');
  58. }
  59. this._lockList.splice(index, 1);
  60. this._log('unlock: ' + fn.id);
  61. this._tryToFreeWaitList();
  62. }
  63. }, {
  64. key: '_tryToFreeWaitList',
  65. value: function _tryToFreeWaitList() {
  66. while (!this.isLocked() && this._waitList.length > 0) {
  67. this._waitList.shift()();
  68. }
  69. }
  70. /**
  71. * Register a callback for waiting unlocked door.
  72. *
  73. * @params {Function} callback Callback on unlocking the door completely.
  74. */
  75. }, {
  76. key: 'waitUnlock',
  77. value: function waitUnlock(callback) {
  78. if (!(callback instanceof Function)) {
  79. throw new Error('The callback param must be a function.');
  80. }
  81. if (this.isLocked()) {
  82. this._waitList.push(callback);
  83. } else {
  84. callback();
  85. }
  86. }
  87. /**
  88. * @return {Boolean}
  89. */
  90. }, {
  91. key: 'isLocked',
  92. value: function isLocked() {
  93. return this._lockList.length > 0;
  94. }
  95. }]);
  96. return DoorLock;
  97. }();
  98. export default DoorLock;