rindexed.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. ;(function(){
  2. var Gun = (typeof window !== "undefined")? window.Gun : require('../gun');
  3. Gun.on('create', function(root){
  4. this.to.next(root);
  5. root.opt.store = root.opt.store || Store(root.opt);
  6. });
  7. function Store(opt){
  8. opt = opt || {};
  9. opt.file = String(opt.file || 'radata');
  10. var db = null;
  11. opt.indexedDB = opt.indexedDB || window.indexedDB;
  12. // Initialize indexedDB. Version 1.
  13. var request = opt.indexedDB.open(opt.file, 1)
  14. // Create schema. onupgradeneeded is called only when DB is first created or when the DB version increases.
  15. request.onupgradeneeded = function(event){
  16. var db = event.target.result;
  17. db.createObjectStore(opt.file);
  18. }
  19. // onsuccess is called when the DB is ready.
  20. request.onsuccess = function(){
  21. db = request.result;
  22. }
  23. request.onerror = function(event){
  24. console.log('ERROR: RAD IndexedDB generic error:', event);
  25. };
  26. var store = function Store(){}, u;
  27. store.put = function(file, data, cb){
  28. cb = cb || function(){};
  29. var doPut = function(){
  30. // Start a transaction. The transaction will be automaticallt closed when the last success/error handler took no new action.
  31. var transaction = db.transaction([opt.file], 'readwrite');
  32. // Add or update data.
  33. var radStore = transaction.objectStore(opt.file);
  34. var putRequest = radStore.put(data, file);
  35. putRequest.onsuccess = radStore.onsuccess = transaction.onsuccess = function(){
  36. //console.log('RAD IndexedDB put transaction was succesful.');
  37. cb(null, 1);
  38. };
  39. putRequest.onabort = radStore.onabort = transaction.onabort = function(){
  40. var es = 'ERROR: RAD IndexedDB put transaction was aborted.';
  41. console.log(es);
  42. cb(es, undefined);
  43. };
  44. putRequest.onerror = radStore.onerror = transaction.onerror = function(event){
  45. var es = 'ERROR: RAD IndexedDB put transaction was in error: ' + JSON.stringify(event)
  46. console.log(es);
  47. cb(es, undefined);
  48. };
  49. }
  50. if(!db){
  51. waitDbReady(doGet, 100, function(){
  52. var es = 'ERROR: Timeout: RAD IndexedDB not ready.';
  53. console.log(es);
  54. cb(es, undefined);
  55. }, 10)
  56. } else {
  57. doPut();
  58. }
  59. };
  60. store.get = function(file, cb){
  61. cb = cb || function(){};
  62. var doGet = function(){
  63. // Start a transaction. The transaction will be automaticallt closed when the last success/error handler took no new action.
  64. var transaction = db.transaction([opt.file], 'readwrite');
  65. // Read data.
  66. var radStore = transaction.objectStore(opt.file);
  67. var getRequest = radStore.get(file);
  68. getRequest.onsuccess = function(){
  69. //console.log('RAD IndexedDB get transaction was succesful.');
  70. cb(null, getRequest.result);
  71. };
  72. getRequest.onabort = function(){
  73. var es = 'ERROR: RAD IndexedDB get transaction was aborted.';
  74. console.log(es);
  75. cb(es, undefined);
  76. };
  77. getRequest.onerror = function(event){
  78. var es = 'ERROR: RAD IndexedDB get transaction was in error: ' + JSON.stringify(event)
  79. console.log(es);
  80. cb(es, undefined);
  81. };
  82. }
  83. if(!db){
  84. waitDbReady(doGet, 100, function(){
  85. var es = 'ERROR: Timeout: RAD IndexedDB not ready.';
  86. console.log(es);
  87. cb(es, undefined);
  88. }, 10)
  89. } else {
  90. doGet();
  91. }
  92. };
  93. var waitDbReady = function(readyFunc, checkInterval, timeoutFunc, timeoutSecs){
  94. var startTime = new Date();
  95. var checkFunc = function(){
  96. if(db){
  97. readyFunc();
  98. } else {
  99. if((new Date() - startTime) / 1000 >= timeoutSecs){
  100. timeoutFunc();
  101. } else {
  102. setTimeout(checkFunc, checkInterval);
  103. }
  104. }
  105. };
  106. checkFunc();
  107. };
  108. return store;
  109. }
  110. if(Gun.window){
  111. Gun.window.RindexedDB = Store;
  112. } else {
  113. module.exports = Store;
  114. }
  115. }());