promise.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Promise Library v1.1 for GUN DB
  2. * Turn any part of a gun chain into a promise, that you can then use
  3. * .then().catch() pattern.
  4. * In normal gun doing var item = gun.get('someKey'), gun returns a reference
  5. * to the someKey synchroneously. Using a reference is quite helpful in making
  6. * graph structures, so I have chosen to follow the following paradigm.
  7. * Whenever a promise is resolved, gun will return an object of data, I will
  8. * wrap that data in an object together with the reference like so:
  9. * {ref: gunRef, data: data}.
  10. * This code is freely given in the spirit of open source MIT license.
  11. * Author: Jachen Duschletta / 2019
  12. */
  13. // Get window or node Gun instance
  14. var Gun = (typeof window !== "undefined")? window.Gun : require('../gun');
  15. /*
  16. * Function promOnce
  17. * @param limit - due to promises resolving too fast if we do not set a timer
  18. * we will not be able receive any data back from gun before returning the promise
  19. * works both following a Chain.get and a Chain.map (limit only applies to map)
  20. * If no limit is chosen, defaults to 100 ms (quite sufficient to fetch about 2000 nodes or more)
  21. * @param opt - option object
  22. * @return {ref: gunReference, data: object / string (data), key: string (soulOfData)}
  23. */
  24. Gun.chain.promOnce = async function (limit, opt) {
  25. var gun = this, cat = gun._;
  26. if(!limit){limit = 100}
  27. if(cat.subs){
  28. var array = [];
  29. gun.map().once((data, key)=>{
  30. var gun = this;
  31. array.push(new Promise((res, rej)=>{
  32. res({ref: gun, data:data, key:key});
  33. })
  34. )
  35. }, opt);
  36. await sleep(limit);
  37. return Promise.all(array)
  38. } else {
  39. return (new Promise((res, rej)=>{
  40. gun.once(function (data, key) {
  41. var gun = this;
  42. res({ref:gun,data:data,key:key});
  43. }, opt);
  44. }))
  45. }
  46. var chain = gun.chain();
  47. return chain;
  48. }
  49. function sleep (limit) {
  50. return (new Promise((res, rej)=>{
  51. setTimeout(res, limit);
  52. }))
  53. }
  54. /*
  55. * Function promPut
  56. * @param item (string / object) - item to be put to that key in the chain
  57. * @param opt - option object
  58. * @return object - Returns an object with the ref to that node that was just
  59. * created as well as the 'ack' which acknowledges the put was succesful
  60. * object {ref: gunReference, ack: acknowledgmentObject}
  61. * If put had an error we can catch the return via .catch
  62. */
  63. Gun.chain.promPut = async function (item, opt) {
  64. var gun = this;
  65. return (new Promise((res, rej)=>{
  66. gun.put(item, function(ack) {
  67. if(ack.err){console.log(ack.err); ack.ok=-1; res({ref:gun, ack:ack})}
  68. res({ref:gun, ack:ack});
  69. }, opt);
  70. }))
  71. }
  72. /*
  73. * Function promSet
  74. * @param item (string / object) - item to be set into a list at this key
  75. * @param opt - option object
  76. * @return object - Returns object with the ref to that node that was just
  77. * created as well as the 'ack' which acknowledges the set was succesful
  78. * object {ref: gunReference, ack: acknowledgmentObject}
  79. * If set had an error we can catch the return via .catch
  80. */
  81. Gun.chain.promSet = async function(item, opt){
  82. var gun = this, soul;
  83. var cb = cb || function(){};
  84. opt = opt || {}; opt.item = opt.item || item;
  85. return (new Promise(async function (res,rej) {
  86. if(soul = Gun.node.soul(item)){ item = Gun.obj.put({}, soul, Gun.val.link.ify(soul)) }
  87. if(!Gun.is(item)){
  88. if(Gun.obj.is(item)){;
  89. item = await gun.back(-1).get(soul = soul || Gun.node.soul(item) || gun.back('opt.uuid')()).promPut(item);
  90. item = item.ref;
  91. }
  92. res(gun.get(soul || (Gun.state.lex() + Gun.text.random(7))).promPut(item));
  93. }
  94. item.get(function(soul, o, msg){
  95. var ack = {};
  96. if(!soul){ rej({ack:{err: Gun.log('Only a node can be linked! Not "' + msg.put + '"!')}} ) }
  97. gun.put(Gun.obj.put({}, soul, Gun.val.link.ify(soul)), cb, opt);
  98. },true);
  99. res({ref:item, ack:{ok:0}});
  100. }))
  101. }
  102. /*
  103. * Function promOn
  104. * @param callback (function) - function to be called upon changes to data
  105. * @param option (object) - {change: true} only allow changes to trigger the callback
  106. * @return - data and key
  107. * subscribes callback to data
  108. */
  109. Gun.chain.promOn = async function (callback, option) {
  110. var gun = this;
  111. return (new Promise((res, rej)=>{
  112. gun.on(function (data, key){
  113. callback(data, key);
  114. res(data, key);
  115. }, option);
  116. }));
  117. }