You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

boardData.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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_DIR = path.join(__dirname, "../server-data/");
  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. this.file = path.join(HISTORY_DIR, "board-" + encodeURIComponent(name) + ".json");
  53. //Loads the file. This will emit the "ready" event
  54. this.load(this.file);
  55. this.on("ready", function(){
  56. that.ready = true;
  57. });
  58. };
  59. //Allows to use BoardData.emit() and BoardData.on()
  60. util.inherits(BoardData, events.EventEmitter);
  61. /** Adds data to the board */
  62. BoardData.prototype.set = function (id, data) {
  63. //KISS
  64. this.board[id] = data;
  65. this.delaySave();
  66. };
  67. /** Adds a child to an element that is already in the board
  68. * @param {string} id - Identifier of the parent element.
  69. * @param {object} child - Object containing the the values to update.
  70. * @param {boolean} [create=true] - Whether to create an empty parent if it doesn't exist
  71. * @returns {boolean} - True if the child was added, else false
  72. */
  73. BoardData.prototype.addChild = function (parentId, child, create) {
  74. if (create===undefined) create = true;
  75. var obj = this.board[parentId];
  76. if (typeof obj !== "object") {
  77. if (create) obj = this.board[parentId] = {};
  78. else return false;
  79. }
  80. if (Array.isArray(obj._children)) obj._children.push(child);
  81. else obj._children = [child];
  82. this.delaySave();
  83. return true;
  84. };
  85. /** Update the data in the board
  86. * @param {string} id - Identifier of the data to update.
  87. * @param {object} data - Object containing the the values to update.
  88. * @param {boolean} create - True if the object should be created if it's not currently in the DB.
  89. */
  90. BoardData.prototype.update = function (id, data, create) {
  91. var obj = this.board[id];
  92. if (typeof obj === "object") {
  93. for (var i in data) {
  94. obj[i] = data[i];
  95. }
  96. } else if (create || obj !== undefined) {
  97. this.board[id] = data;
  98. }
  99. this.delaySave();
  100. };
  101. /** Removes data from the board
  102. * @param {string} id - Identifier of the data to delete.
  103. */
  104. BoardData.prototype.delete = function (id) {
  105. //KISS
  106. delete this.board[id];
  107. this.delaySave();
  108. };
  109. /** Reads data from the board
  110. * @param {string} id - Identifier of the element to get.
  111. * @returns {object} The element with the given id, or undefined if no element has this id
  112. */
  113. BoardData.prototype.get = function (id, children) {
  114. return this.board[id];
  115. };
  116. /** Reads data from the board
  117. * @param {string} [id] - Identifier of the first element to get.
  118. * @param {BoardData~processData} callback - Function to be called with each piece of data read
  119. */
  120. BoardData.prototype.getAll = function (id, callback) {
  121. if (!callback) callback = id;
  122. for (var i in this.board) {
  123. if (!id || i > id) {
  124. callback(this.board[i]);
  125. }
  126. }
  127. };
  128. /**
  129. * This callback is displayed as part of the BoardData class.
  130. * Describes a function that processes data that comes from the board
  131. * @callback BoardData~processData
  132. * @param {object} data
  133. */
  134. /** Delays the triggering of auto-save by SAVE_INTERVAL seconds
  135. */
  136. BoardData.prototype.delaySave = function (file) {
  137. if (this.saveTimeoutId !== undefined) clearTimeout(this.saveTimeoutId);
  138. var that = this;
  139. this.saveTimeoutId = setTimeout(function(){that.save()}, SAVE_INTERVAL);
  140. };
  141. /** Saves the data in the board to a file.
  142. * @param {string} [file=this.file] - Path to the file where the board data will be saved.
  143. */
  144. BoardData.prototype.save = function (file) {
  145. if (!file) file = this.file;
  146. var board_txt = JSON.stringify(this.board);
  147. var that = this;
  148. fs.writeFile(file, board_txt, function (err) {
  149. if (err) that.emit("error", err);
  150. });
  151. };
  152. /** Load the data in the board from a file.
  153. * @param {string} file - Path to the file where the board data will be read.
  154. */
  155. BoardData.prototype.load = function (file) {
  156. var that = this;
  157. fs.readFile(file, function (err, data) {
  158. try {
  159. if (err) throw err;
  160. that.board = JSON.parse(data);
  161. } catch (e) {
  162. console.error("Unable to read history from "+file+". The following error occured: " + e);
  163. console.log("Creating an empty board.");
  164. that.board = {}
  165. }
  166. that.emit("ready");
  167. });
  168. };
  169. module.exports.BoardData = BoardData;