not really known
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.

224 lines
6.4 KiB

  1. define(["sugar-web/bus", "sugar-web/env"], function (bus, env) {
  2. 'use strict';
  3. var datastore = {};
  4. function DatastoreObject(objectId) {
  5. this.objectId = objectId;
  6. this.newMetadata = {};
  7. this.ensureObjectId = function (callback) {
  8. var that = this;
  9. env.getObjectId(function (objectId) {
  10. if (objectId !== null && that.objectId === undefined) {
  11. that.objectId = objectId;
  12. }
  13. callback();
  14. });
  15. };
  16. this.blobToText = function (blob, callback) {
  17. var reader = new FileReader();
  18. reader.onload = function (e) {
  19. callback(e.target.result);
  20. };
  21. reader.readAsText(blob);
  22. };
  23. this.blobToArrayBuffer = function (blob, callback) {
  24. var reader = new FileReader();
  25. reader.onload = function (e) {
  26. callback(e.target.result);
  27. };
  28. reader.readAsArrayBuffer(blob);
  29. };
  30. this.saveText = function (metadata, callback) {
  31. var that = this;
  32. function onSaved(error, outputStream) {
  33. var blob = new Blob([that.newDataAsText]);
  34. that.blobToArrayBuffer(blob, function (buffer) {
  35. outputStream.write(buffer);
  36. outputStream.close(callback);
  37. });
  38. }
  39. datastore.save(this.objectId, metadata, onSaved);
  40. };
  41. this.applyChanges = function (metadata, callback) {
  42. for (var key in this.newMetadata) {
  43. metadata[key] = this.newMetadata[key];
  44. }
  45. if (this.newDataAsText !== undefined) {
  46. this.saveText(metadata, callback);
  47. } else {
  48. datastore.setMetadata(this.objectId, metadata, callback);
  49. }
  50. };
  51. }
  52. DatastoreObject.prototype.getMetadata = function (callback) {
  53. var that = this;
  54. this.ensureObjectId(function () {
  55. datastore.getMetadata(that.objectId, callback);
  56. });
  57. };
  58. DatastoreObject.prototype.loadAsText = function (callback) {
  59. var that = this;
  60. var inputStream = null;
  61. var arrayBuffers = [];
  62. var metadata = null;
  63. function onRead(error, data) {
  64. if (data.byteLength === 0) {
  65. var blob = new Blob(arrayBuffers);
  66. that.blobToText(blob, function (text) {
  67. callback(null, metadata, text);
  68. });
  69. inputStream.close();
  70. return;
  71. }
  72. arrayBuffers.push(data);
  73. inputStream.read(8192, onRead);
  74. }
  75. function onLoad(error, loadedMetadata, loadedInputStream) {
  76. metadata = loadedMetadata;
  77. inputStream = loadedInputStream;
  78. inputStream.read(8192, onRead);
  79. }
  80. this.ensureObjectId(function () {
  81. datastore.load(that.objectId, onLoad);
  82. });
  83. };
  84. DatastoreObject.prototype.setMetadata = function (metadata) {
  85. for (var key in metadata) {
  86. this.newMetadata[key] = metadata[key];
  87. }
  88. };
  89. DatastoreObject.prototype.setDataAsText = function (text) {
  90. this.newDataAsText = text;
  91. };
  92. DatastoreObject.prototype.save = function (callback) {
  93. if (callback === undefined) {
  94. callback = function () {};
  95. }
  96. var that = this;
  97. function onCreated(error, objectId) {
  98. that.objectId = objectId;
  99. that.applyChanges({}, callback);
  100. }
  101. function onGotMetadata(error, metadata) {
  102. that.applyChanges(metadata, callback);
  103. }
  104. this.ensureObjectId(function () {
  105. if (that.objectId === undefined) {
  106. datastore.create(that.newMetadata, onCreated);
  107. } else {
  108. datastore.getMetadata(that.objectId, onGotMetadata);
  109. }
  110. });
  111. };
  112. datastore.DatastoreObject = DatastoreObject;
  113. datastore.setMetadata = function (objectId, metadata, callback) {
  114. function onResponseReceived(error, result) {
  115. if (callback) {
  116. if (error === null) {
  117. callback(null);
  118. } else {
  119. callback(error);
  120. }
  121. }
  122. }
  123. var params = [objectId, metadata];
  124. bus.sendMessage("datastore.set_metadata", params, onResponseReceived);
  125. };
  126. datastore.getMetadata = function (objectId, callback) {
  127. function onResponseReceived(error, result) {
  128. if (error === null) {
  129. callback(null, result[0]);
  130. } else {
  131. callback(error, null);
  132. }
  133. }
  134. var params = [objectId];
  135. bus.sendMessage("datastore.get_metadata", params, onResponseReceived);
  136. };
  137. datastore.load = function (objectId, callback) {
  138. var inputStream = bus.createInputStream();
  139. inputStream.open(function (error) {
  140. function onResponseReceived(responseError, result) {
  141. if (responseError === null) {
  142. callback(null, result[0], inputStream);
  143. } else {
  144. callback(responseError, null, null);
  145. }
  146. }
  147. var params = [objectId, inputStream.streamId];
  148. bus.sendMessage("datastore.load", params, onResponseReceived);
  149. });
  150. };
  151. datastore.create = function (metadata, callback) {
  152. function onResponseReceived(responseError, result) {
  153. if (responseError === null) {
  154. callback(null, result[0]);
  155. } else {
  156. callback(responseError, null);
  157. }
  158. }
  159. var params = [metadata];
  160. bus.sendMessage("datastore.create", params, onResponseReceived);
  161. };
  162. datastore.save = function (objectId, metadata, callback) {
  163. var outputStream = bus.createOutputStream();
  164. outputStream.open(function (error) {
  165. function onResponseReceived(responseError, result) {
  166. if (responseError === null) {
  167. callback(null, outputStream);
  168. } else {
  169. callback(responseError, null);
  170. }
  171. }
  172. var params = [objectId, metadata, outputStream.streamId];
  173. bus.sendMessage("datastore.save", params, onResponseReceived);
  174. });
  175. };
  176. return datastore;
  177. });