Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

boardData.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * WHITEBOPHIR SERVER
  3. *********************************************************
  4. * @licstart The following is the entire license notice for the
  5. * JavaScript code in this page.
  6. *
  7. * Copyright (C) 2013-2014 Ophir LOJKINE
  8. *
  9. *
  10. * The JavaScript code in this page is free software: you can
  11. * redistribute it and/or modify it under the terms of the GNU
  12. * General Public License (GNU GPL) as published by the Free Software
  13. * Foundation, either version 3 of the License, or (at your option)
  14. * any later version. The code is distributed WITHOUT ANY WARRANTY;
  15. * without even the implied warranty of MERCHANTABILITY or FITNESS
  16. * FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
  17. *
  18. * As additional permission under GNU GPL version 3 section 7, you
  19. * may distribute non-source (e.g., minimized or compacted) forms of
  20. * that code without the copy of the GNU GPL normally required by
  21. * section 4, provided you include this license notice and a URL
  22. * through which recipients can access the Corresponding Source.
  23. *
  24. * @licend
  25. * @module boardData
  26. */
  27. var fs = require('fs'),
  28. path = require("path"),
  29. util = require("util"),
  30. events = require("events");
  31. /** @constant
  32. @type {string}
  33. @default
  34. Path to the file where boards will be saved by default
  35. */
  36. var HISTORY_FILE = path.join(__dirname, "../server-data/history.txt");
  37. /** @constant
  38. @type {Number}
  39. @default
  40. Number of seconds of inactivity after which the board should be saved to a file
  41. */
  42. var SAVE_INTERVAL = 1000 * 2; //Save every 2 seconds of inactivity
  43. /**
  44. * Represents a board.
  45. * @constructor
  46. */
  47. var BoardData = function(name) {
  48. var that = this;
  49. this.name = name;
  50. this.board = {};
  51. this.ready = false;
  52. //Loads the file. This will emit the "ready" event
  53. this.load(HISTORY_FILE);
  54. this.on("ready", function(){
  55. that.ready = true;
  56. });
  57. };
  58. //Allows to use BoardData.emit() and BoardData.on()
  59. util.inherits(BoardData, events.EventEmitter);
  60. /** Adds data to the board */
  61. BoardData.prototype.set = function (id, data) {
  62. //KISS
  63. this.board[id] = data;
  64. this.delaySave();
  65. };
  66. /** Adds a child to an element that is already in the board
  67. * @param {string} id - Identifier of the parent element.
  68. * @param {object} child - Object containing the the values to update.
  69. * @param {boolean} [create=true] - Whether to create an empty parent if it doesn't exist
  70. * @returns {boolean} - True if the child was added, else false
  71. */
  72. BoardData.prototype.addChild = function (parentId, child, create) {
  73. if (create===undefined) create = true;
  74. var obj = this.board[parentId];
  75. if (typeof obj !== "object") {
  76. if (create) obj = this.board[parentId] = {};
  77. else return false;
  78. }
  79. if (Array.isArray(obj._children)) obj._children.push(child);
  80. else obj._children = [child];
  81. this.delaySave();
  82. return true;
  83. };
  84. /** Update the data in the board
  85. * @param {string} id - Identifier of the data to update.
  86. * @param {object} data - Object containing the the values to update.
  87. * @param {boolean} create - True if the object should be created if it's not currently in the DB.
  88. */
  89. BoardData.prototype.update = function (id, data, create) {
  90. var obj = this.board[id];
  91. if (typeof obj === "object") {
  92. for (var i in data) {
  93. obj[i] = data[i];
  94. }
  95. } else if (create || obj !== undefined) {
  96. this.board[id] = data;
  97. }
  98. this.delaySave();
  99. };
  100. /** Removes data from the board
  101. * @param {string} id - Identifier of the data to delete.
  102. */
  103. BoardData.prototype.delete = function (id) {
  104. //KISS
  105. delete this.board[id];
  106. this.delaySave();
  107. };
  108. /** Reads data from the board
  109. * @param {string} id - Identifier of the element to get.
  110. * @returns {object} The element with the given id, or undefined if no element has this id
  111. */
  112. BoardData.prototype.get = function (id, children) {
  113. return this.board[id];
  114. };
  115. /** Reads data from the board
  116. * @param {string} [id] - Identifier of the first element to get.
  117. * @param {BoardData~processData} callback - Function to be called with each piece of data read
  118. */
  119. BoardData.prototype.getAll = function (id, callback) {
  120. if (!callback) callback = id;
  121. for (var i in this.board) {
  122. if (!id || i > id) {
  123. callback(this.board[i]);
  124. }
  125. }
  126. };
  127. /**
  128. * This callback is displayed as part of the BoardData class.
  129. * Describes a function that processes data that comes from the board
  130. * @callback BoardData~processData
  131. * @param {object} data
  132. */
  133. /** Delays the triggering of auto-save by SAVE_INTERVAL seconds
  134. */
  135. BoardData.prototype.delaySave = function (file) {
  136. if (this.saveTimeoutId !== undefined) clearTimeout(this.saveTimeoutId);
  137. var that = this;
  138. this.saveTimeoutId = setTimeout(function(){that.save()}, SAVE_INTERVAL);
  139. };
  140. /** Saves the data in the board to a file.
  141. * @param {string} [file=HISTORY_FILE] - Path to the file where the board data will be saved.
  142. */
  143. BoardData.prototype.save = function (file) {
  144. if (!file) file = HISTORY_FILE;
  145. var board_txt = JSON.stringify(this.board);
  146. fs.writeFile(file, board_txt, function (err) {
  147. if (err) this.emit("error", err);
  148. });
  149. };
  150. /** Load the data in the board from a file.
  151. * @param {string} file - Path to the file where the board data will be read.
  152. */
  153. BoardData.prototype.load = function (file) {
  154. var that = this;
  155. fs.readFile(file, function (err, data) {
  156. try {
  157. if (err) throw err;
  158. that.board = JSON.parse(data);
  159. } catch (e) {
  160. console.error("Unable to read history from "+file+". The following error occured: " + e);
  161. console.log("Creating an empty board.");
  162. that.board = {}
  163. }
  164. that.emit("ready");
  165. });
  166. };
  167. module.exports.BoardData = BoardData;