copper-sdk 0.7.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.7.1
2
+
3
+ * Add support for parallel asynchronous requests
4
+
1
5
  ## 0.7.0
2
6
 
3
7
  * Bump all dependencies as far as they can go on Node 10
@@ -68,7 +68,7 @@
68
68
  }
69
69
  }
70
70
 
71
- var version = "0.7.0";
71
+ var version = "0.7.1";
72
72
 
73
73
  var ENTITY_PATH_MAP = {
74
74
  lead: 'leads',
@@ -164,7 +164,14 @@
164
164
  _this._resolve = resolve;
165
165
  _this._reject = reject;
166
166
  });
167
+ this._id = Deferred.getNextId();
167
168
  }
169
+ Deferred.resetNextId = function () {
170
+ Deferred._nextId = 0;
171
+ };
172
+ Deferred.getNextId = function () {
173
+ return Deferred._nextId++;
174
+ };
168
175
  Object.defineProperty(Deferred.prototype, "promise", {
169
176
  get: function () {
170
177
  return this._promise;
@@ -172,6 +179,14 @@
172
179
  enumerable: false,
173
180
  configurable: true
174
181
  });
182
+ Object.defineProperty(Deferred.prototype, "id", {
183
+ get: function () {
184
+ return this._id;
185
+ },
186
+ enumerable: false,
187
+ configurable: true
188
+ });
189
+ Deferred._nextId = 0;
175
190
  return Deferred;
176
191
  }());
177
192
 
@@ -564,15 +579,19 @@
564
579
  });
565
580
  });
566
581
  };
567
- Copper.prototype._postMessage = function (type, message) {
582
+ Copper.prototype._postMessage = function (type, message, id) {
583
+ var _a;
568
584
  if (message === void 0) { message = {}; }
569
- this.win.top.postMessage(__assign(__assign({}, message), {
585
+ if (id === void 0) { id = -1; }
586
+ (_a = this.win.top) === null || _a === void 0 ? void 0 : _a.postMessage(__assign(__assign({}, message), {
570
587
  // as a credential to the parent frame, so parent frame can recoganize the origin
571
588
  instanceId: this.instanceId,
572
589
  // tell parent frame current sdk version
573
590
  version: version,
574
591
  // type of message
575
- type: type }), this.parentOrigin);
592
+ type: type,
593
+ // optional id for message
594
+ id: id }), this.parentOrigin);
576
595
  };
577
596
  Copper.prototype._listenMessage = function () {
578
597
  var _this = this;
@@ -582,7 +601,7 @@
582
601
  }
583
602
  // if type is a deferred type, we resolve it
584
603
  // otherwise we do something else
585
- _this._resolveDeferred(event.data.type, event.data);
604
+ _this._resolveDeferred(event.data.type, event.data.id, event.data);
586
605
  // if event type exists, we pass the event to SDK
587
606
  // so sdk user can subscribe those events
588
607
  if (event.data.type) {
@@ -598,12 +617,17 @@
598
617
  createArrayWhenEmpty(this.deferredQueues, queueName);
599
618
  this.deferredQueues[queueName].push(deferred);
600
619
  };
601
- Copper.prototype._resolveDeferred = function (queueName, data) {
602
- if (!this.deferredQueues[queueName]) {
620
+ Copper.prototype._resolveDeferred = function (queueName, messageId, data) {
621
+ var queue = this.deferredQueues[queueName];
622
+ if (!queue) {
603
623
  return;
604
624
  }
605
- var deferred = this.deferredQueues[queueName].shift();
625
+ var foundIdx = queue.findIndex(function (d) {
626
+ return d.id === messageId;
627
+ });
628
+ var deferred = queue[foundIdx];
606
629
  if (deferred) {
630
+ queue.splice(foundIdx, 1);
607
631
  if (data.error) {
608
632
  return deferred.reject(data.error);
609
633
  }
@@ -642,17 +666,17 @@
642
666
  Copper.prototype._createDeferredMethod = function (queueName, executor) {
643
667
  var deferred = new Deferred();
644
668
  this._enqueueDeferred(queueName, deferred);
645
- executor();
669
+ executor(deferred);
646
670
  return deferred.promise;
647
671
  };
648
672
  Copper.prototype._deferredPost = function (name, data) {
649
673
  var _this = this;
650
- return this._createDeferredMethod(name, function () {
674
+ return this._createDeferredMethod(name, function (deferred) {
651
675
  if (data) {
652
- _this._postMessage(name, data);
676
+ _this._postMessage(name, data, deferred.id);
653
677
  }
654
678
  else {
655
- _this._postMessage(name);
679
+ _this._postMessage(name, {}, deferred.id);
656
680
  }
657
681
  });
658
682
  };
@@ -1 +1 @@
1
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).Copper=e()}(this,function(){"use strict";var n=function(){return(n=Object.assign||function(t){for(var e,n=1,i=arguments.length;n<i;n++)for(var r in e=arguments[n])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t}).apply(this,arguments)};function e(t,s,a,u){return new(a=a||Promise)(function(n,e){function i(t){try{o(u.next(t))}catch(t){e(t)}}function r(t){try{o(u.throw(t))}catch(t){e(t)}}function o(t){var e;t.done?n(t.value):((e=t.value)instanceof a?e:new a(function(t){t(e)})).then(i,r)}o((u=u.apply(t,s||[])).next())})}function d(i,r){var o,s,a,u={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]},t={next:e(0),throw:e(1),return:e(2)};return"function"==typeof Symbol&&(t[Symbol.iterator]=function(){return this}),t;function e(n){return function(t){var e=[n,t];if(o)throw new TypeError("Generator is already executing.");for(;u;)try{if(o=1,s&&(a=2&e[0]?s.return:e[0]?s.throw||((a=s.return)&&a.call(s),0):s.next)&&!(a=a.call(s,e[1])).done)return a;switch(s=0,(e=a?[2&e[0],a.value]:e)[0]){case 0:case 1:a=e;break;case 4:return u.label++,{value:e[1],done:!1};case 5:u.label++,s=e[1],e=[0];continue;case 7:e=u.ops.pop(),u.trys.pop();continue;default:if(!(a=0<(a=u.trys).length&&a[a.length-1])&&(6===e[0]||2===e[0])){u=0;continue}if(3===e[0]&&(!a||a[0]<e[1]&&e[1]<a[3]))u.label=e[1];else if(6===e[0]&&u.label<a[1])u.label=a[1],a=e;else{if(!a||a[2]<=u.label){a[2]&&u.ops.pop(),u.trys.pop();continue}u.label=a[2],u.ops.push(e)}}e=r.call(i,u)}catch(t){e=[6,t],s=0}finally{o=a=0}if(5&e[0])throw e[1];return{value:e[0]?e[1]:void 0,done:!0}}}}var f,t,i="0.7.0",h={lead:"leads",person:"people",company:"companies",opportunity:"opportunities",task:"tasks",project:"projects"};(t=f=f||{}).ActivityLog="ActivityLog",t.ListView="ListView";Object.defineProperty(o.prototype,"promise",{get:function(){return this._promise},enumerable:!(t.Related="Related"),configurable:!0});var r=o;function o(){var n=this;this.resolve=function(t){n._resolve(t)},this.reject=function(t){n._reject(t)},this._promise=new Promise(function(t,e){n._resolve=t,n._reject=e})}function s(t,e){e=(e=void 0===e?"":e)||window.location.href,t=t.replace(/[[]]/g,"\\$&");t=RegExp("[?&]"+t+"(=([^&#]*)|&|#|$)").exec(e);return t&&t[2]?decodeURIComponent(t[2].replace(/\+/g," ")):""}function a(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];console.log.apply(console,t)}function u(t,e){t[e]||(t[e]=[])}function l(t,e){setTimeout(function(){t()},e=void 0===e?1e3:e)}p.prototype.save=function(){return e(this,void 0,void 0,function(){return d(this,function(t){switch(t.label){case 0:return this._onSave?[4,this._onSave(this)]:[3,2];case 1:return[2,t.sent()];case 2:return[2,null]}})})},p.prototype.toJSON=function(){return JSON.stringify(this.toObject())},p.prototype.toObject=function(){var e=this,n={};return Object.keys(this).forEach(function(t){n[t]=e[t]}),n},p.prototype._getEntityDataDefinition=function(e,n){void 0===n&&(n=[]);var i={};return Object.keys(e).forEach(function(t){~n.indexOf(t)?i[t]={value:e[t],writable:!0,enumerable:!0}:i[t]={get:function(){return e[t]},set:function(){a("property "+t+" is read only")},enumerable:!0}}),i};var c=p;function p(t,e,n,i){e=this._getEntityDataDefinition(e,n=void 0===n?[]:n);e.type={enumerable:!1,writable:!1,value:t},e._onSave={enumerable:!1,value:i},Object.defineProperties(this,e)}function y(t,e,n,i){if(void 0===i&&(i=window),this.parentOrigin=t,this.instanceId=e,this.options=n,this._win=i,this.deferredQueues={},this.events={},!this.parentOrigin||!this.instanceId)throw new TypeError("parentOrigin or instanceId is empty");this._listenMessage(),this._subscribeContextUpdated(),this._postMessage("init")}return y.init=function(){var t=s("origin"),e=s("instanceId"),n={isGlobal:"1"===s("isGlobal")};if(function(){if("undefined"!=typeof window)return window.top===window&&a("Copper should be inside an iframe, otherwise it might not work as expected."),1;a("Copper can only run in browser environment")}())return new y(t,e,n);throw Error("Environment checking does not pass.")},Object.defineProperty(y,"version",{get:function(){return i},enumerable:!1,configurable:!0}),Object.defineProperty(y.prototype,"win",{get:function(){return this._win},enumerable:!1,configurable:!0}),y.prototype.getContext=function(){return e(this,void 0,void 0,function(){var e;return d(this,function(t){switch(t.label){case 0:return[4,this._deferredPost("getContext")];case 1:return e=t.sent(),[2,this._createContextModel(e)]}})})},y.prototype.getUserInfo=function(){return e(this,void 0,void 0,function(){return d(this,function(t){return[2,this._deferredPost("getUserInfo")]})})},y.prototype.getRouteInfo=function(){return e(this,void 0,void 0,function(){return d(this,function(t){return[2,this._deferredPost("getRouteInfo")]})})},y.prototype.saveContext=function(n){return e(this,void 0,void 0,function(){var e;return d(this,function(t){switch(t.label){case 0:return[4,this._deferredPost("saveContext",{data:{entityType:n.type,entityData:n.toObject()}})];case 1:return e=t.sent(),[2,this._createContextModel(e)]}})})},y.prototype.setAppUI=function(t){this._postMessage("setUI",{data:t})},y.prototype.showModal=function(t){this._postMessage("showModal",{params:t=void 0===t?{}:t})},y.prototype.closeModal=function(){this._postMessage("closeModal")},y.prototype.publishMessage=function(t,e,n){this._postMessage("publishMessage",{target:e,data:{type:t,msg:n=void 0===n?{}:n}})},y.prototype.logActivity=function(r,o,s,a){return void 0===a&&(a=0),e(this,void 0,void 0,function(){var i;return d(this,function(t){switch(t.label){case 0:return[4,this._getCachedContext()];case 1:return i=t.sent(),e={parent:{type:(e=i).type,id:e.id},type:{category:"user",id:(n={activityType:r,details:o,activityDate:s}).activityType},details:n.details},(n=n.activityDate)&&(e.activity_date=n),i={url:"/v1/activities",method:"POST",data:e,target:{name:f.ActivityLog}},[2,this._action(i,a)]}var e,n})})},y.prototype.createEntity=function(u,c,p){return void 0===p&&(p=0),e(this,void 0,void 0,function(){var r,o,s,a=this;return d(this,function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),[4,this._getCachedContext()];case 1:return r=t.sent(),[3,3];case 2:return t.sent(),r={},[3,3];case 3:return e=r,i=(n={entityType:u,data:c}).entityType,n=n.data,o=(s={url:"/v1/"+h[i],method:"POST",data:n,target:i===e.type?{name:f.ListView,data:{entityType:i,entityData:n}}:null}).target,[4,this.api(s.url,{method:s.method,body:JSON.stringify(s.data)})];case 4:return s=t.sent(),o&&o.data&&(o.data.entityData=s,l(function(){a.refreshUI(o)},p)),[2,s]}var e,n,i})})},y.prototype.relateEntity=function(o,s,a,u){return void 0===u&&(u=0),e(this,void 0,void 0,function(){var r;return d(this,function(t){switch(t.label){case 0:return[4,this._getCachedContext()];case 1:return r=t.sent(),r={url:"/v1/"+h[n=(e={entityType:o,entityId:parseInt(s,10),data:a}).entityType]+"/"+(i=e.entityId)+"/related",method:"POST",data:{resource:e=e.data},target:n===(n=r).type&&i===n.id?{name:f.Related,data:e}:null},[2,this._action(r,u)]}var e,n,i})})},y.prototype.refreshUI=function(t){this._postMessage("refreshUI",{target:t})},y.prototype.on=function(t,e){u(this.events,t),this.events[t].push(e)},y.prototype.trigger=function(t,e){var n=this;this.events[t]&&this.events[t].forEach(function(t){t.call(n,e)})},y.prototype.api=function(t,e){if(!t)return Promise.reject({id:"sdk-api",version:y.version,detail:"url cannot be empty"});if(e&&e.body)try{JSON.parse(e.body)}catch(t){return Promise.reject({id:"sdk-api",version:y.version,detail:"body must be a valid JSON string"})}return this._deferredPost("api",{url:t,options:e})},y.prototype.navigateToEntityDetail=function(t,e){return this._deferredPost("navigateToEntityDetail",{entityType:t,entityId:e})},y.prototype.getSelectedRecords=function(t){var t=void 0===t?{}:t,e=t.pageSize,t=t.pageNumber;return this._deferredPost("getSelectedRecords",{pageSize:void 0===e?100:e,pageNumber:void 0===t?0:t})},y.prototype.getConfig=function(){return this._deferredPost("getConfig")},y.prototype._getCachedContext=function(){return e(this,void 0,void 0,function(){return d(this,function(t){switch(t.label){case 0:return this._context?[2,this._context]:[4,this.getContext()];case 1:return[2,t.sent().context]}})})},y.prototype._postMessage=function(t,e){this.win.top.postMessage(n(n({},e=void 0===e?{}:e),{instanceId:this.instanceId,version:i,type:t}),this.parentOrigin)},y.prototype._listenMessage=function(){var e=this;this.win.addEventListener("message",function(t){e._isOriginValid(t)&&(e._resolveDeferred(t.data.type,t.data),t.data.type)&&e.trigger(t.data.type,t.data.msg||t.data.data)},!1)},y.prototype._isOriginValid=function(t){return t.origin===this.parentOrigin},y.prototype._enqueueDeferred=function(t,e){u(this.deferredQueues,t),this.deferredQueues[t].push(e)},y.prototype._resolveDeferred=function(t,e){if(this.deferredQueues[t]){t=this.deferredQueues[t].shift();if(t){if(e.error)return t.reject(e.error);t.resolve(e.data)}}},y.prototype._createContextModel=function(t){var e=t.entityType,t=new c(e,t.entityData,t.editableFields,this.saveContext.bind(this));return{type:e,context:this._context=t}},y.prototype._action=function(t,i){var r=t.url,o=t.method,s=t.data,a=t.target;return e(this,void 0,void 0,function(){var e,n=this;return d(this,function(t){switch(t.label){case 0:return[4,this.api(r,{method:o,body:JSON.stringify(s)})];case 1:return e=t.sent(),a&&l(function(){n.refreshUI(a)},i),[2,e]}})})},y.prototype._createDeferredMethod=function(t,e){var n=new r;return this._enqueueDeferred(t,n),e(),n.promise},y.prototype._deferredPost=function(t,e){var n=this;return this._createDeferredMethod(t,function(){e?n._postMessage(t,e):n._postMessage(t)})},y.prototype._subscribeContextUpdated=function(){this.on("contextUpdated",function(){0})},y});
1
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).Copper=e()}(this,function(){"use strict";var r=function(){return(r=Object.assign||function(t){for(var e,n=1,i=arguments.length;n<i;n++)for(var r in e=arguments[n])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t}).apply(this,arguments)};function e(t,s,a,u){return new(a=a||Promise)(function(n,e){function i(t){try{o(u.next(t))}catch(t){e(t)}}function r(t){try{o(u.throw(t))}catch(t){e(t)}}function o(t){var e;t.done?n(t.value):((e=t.value)instanceof a?e:new a(function(t){t(e)})).then(i,r)}o((u=u.apply(t,s||[])).next())})}function p(i,r){var o,s,a,u={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]},t={next:e(0),throw:e(1),return:e(2)};return"function"==typeof Symbol&&(t[Symbol.iterator]=function(){return this}),t;function e(n){return function(t){var e=[n,t];if(o)throw new TypeError("Generator is already executing.");for(;u;)try{if(o=1,s&&(a=2&e[0]?s.return:e[0]?s.throw||((a=s.return)&&a.call(s),0):s.next)&&!(a=a.call(s,e[1])).done)return a;switch(s=0,(e=a?[2&e[0],a.value]:e)[0]){case 0:case 1:a=e;break;case 4:return u.label++,{value:e[1],done:!1};case 5:u.label++,s=e[1],e=[0];continue;case 7:e=u.ops.pop(),u.trys.pop();continue;default:if(!(a=0<(a=u.trys).length&&a[a.length-1])&&(6===e[0]||2===e[0])){u=0;continue}if(3===e[0]&&(!a||a[0]<e[1]&&e[1]<a[3]))u.label=e[1];else if(6===e[0]&&u.label<a[1])u.label=a[1],a=e;else{if(!a||a[2]<=u.label){a[2]&&u.ops.pop(),u.trys.pop();continue}u.label=a[2],u.ops.push(e)}}e=r.call(i,u)}catch(t){e=[6,t],s=0}finally{o=a=0}if(5&e[0])throw e[1];return{value:e[0]?e[1]:void 0,done:!0}}}}var f,t,o="0.7.1",h={lead:"leads",person:"people",company:"companies",opportunity:"opportunities",task:"tasks",project:"projects"};(t=f=f||{}).ActivityLog="ActivityLog",t.ListView="ListView",t.Related="Related";s.resetNextId=function(){s._nextId=0},s.getNextId=function(){return s._nextId++},Object.defineProperty(s.prototype,"promise",{get:function(){return this._promise},enumerable:!1,configurable:!0}),Object.defineProperty(s.prototype,"id",{get:function(){return this._id},enumerable:!1,configurable:!0}),s._nextId=0;var i=s;function s(){var n=this;this.resolve=function(t){n._resolve(t)},this.reject=function(t){n._reject(t)},this._promise=new Promise(function(t,e){n._resolve=t,n._reject=e}),this._id=s.getNextId()}function a(t,e){e=(e=void 0===e?"":e)||window.location.href,t=t.replace(/[[]]/g,"\\$&");t=RegExp("[?&]"+t+"(=([^&#]*)|&|#|$)").exec(e);return t&&t[2]?decodeURIComponent(t[2].replace(/\+/g," ")):""}function u(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];console.log.apply(console,t)}function n(t,e){t[e]||(t[e]=[])}function l(t,e){setTimeout(function(){t()},e=void 0===e?1e3:e)}d.prototype.save=function(){return e(this,void 0,void 0,function(){return p(this,function(t){switch(t.label){case 0:return this._onSave?[4,this._onSave(this)]:[3,2];case 1:return[2,t.sent()];case 2:return[2,null]}})})},d.prototype.toJSON=function(){return JSON.stringify(this.toObject())},d.prototype.toObject=function(){var e=this,n={};return Object.keys(this).forEach(function(t){n[t]=e[t]}),n},d.prototype._getEntityDataDefinition=function(e,n){void 0===n&&(n=[]);var i={};return Object.keys(e).forEach(function(t){~n.indexOf(t)?i[t]={value:e[t],writable:!0,enumerable:!0}:i[t]={get:function(){return e[t]},set:function(){u("property "+t+" is read only")},enumerable:!0}}),i};var c=d;function d(t,e,n,i){e=this._getEntityDataDefinition(e,n=void 0===n?[]:n);e.type={enumerable:!1,writable:!1,value:t},e._onSave={enumerable:!1,value:i},Object.defineProperties(this,e)}function y(t,e,n,i){if(void 0===i&&(i=window),this.parentOrigin=t,this.instanceId=e,this.options=n,this._win=i,this.deferredQueues={},this.events={},!this.parentOrigin||!this.instanceId)throw new TypeError("parentOrigin or instanceId is empty");this._listenMessage(),this._subscribeContextUpdated(),this._postMessage("init")}return y.init=function(){var t=a("origin"),e=a("instanceId"),n={isGlobal:"1"===a("isGlobal")};if(function(){if("undefined"!=typeof window)return window.top===window&&u("Copper should be inside an iframe, otherwise it might not work as expected."),1;u("Copper can only run in browser environment")}())return new y(t,e,n);throw Error("Environment checking does not pass.")},Object.defineProperty(y,"version",{get:function(){return o},enumerable:!1,configurable:!0}),Object.defineProperty(y.prototype,"win",{get:function(){return this._win},enumerable:!1,configurable:!0}),y.prototype.getContext=function(){return e(this,void 0,void 0,function(){var e;return p(this,function(t){switch(t.label){case 0:return[4,this._deferredPost("getContext")];case 1:return e=t.sent(),[2,this._createContextModel(e)]}})})},y.prototype.getUserInfo=function(){return e(this,void 0,void 0,function(){return p(this,function(t){return[2,this._deferredPost("getUserInfo")]})})},y.prototype.getRouteInfo=function(){return e(this,void 0,void 0,function(){return p(this,function(t){return[2,this._deferredPost("getRouteInfo")]})})},y.prototype.saveContext=function(n){return e(this,void 0,void 0,function(){var e;return p(this,function(t){switch(t.label){case 0:return[4,this._deferredPost("saveContext",{data:{entityType:n.type,entityData:n.toObject()}})];case 1:return e=t.sent(),[2,this._createContextModel(e)]}})})},y.prototype.setAppUI=function(t){this._postMessage("setUI",{data:t})},y.prototype.showModal=function(t){this._postMessage("showModal",{params:t=void 0===t?{}:t})},y.prototype.closeModal=function(){this._postMessage("closeModal")},y.prototype.publishMessage=function(t,e,n){this._postMessage("publishMessage",{target:e,data:{type:t,msg:n=void 0===n?{}:n}})},y.prototype.logActivity=function(r,o,s,a){return void 0===a&&(a=0),e(this,void 0,void 0,function(){var i;return p(this,function(t){switch(t.label){case 0:return[4,this._getCachedContext()];case 1:return i=t.sent(),e={parent:{type:(e=i).type,id:e.id},type:{category:"user",id:(n={activityType:r,details:o,activityDate:s}).activityType},details:n.details},(n=n.activityDate)&&(e.activity_date=n),i={url:"/v1/activities",method:"POST",data:e,target:{name:f.ActivityLog}},[2,this._action(i,a)]}var e,n})})},y.prototype.createEntity=function(u,c,d){return void 0===d&&(d=0),e(this,void 0,void 0,function(){var r,o,s,a=this;return p(this,function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),[4,this._getCachedContext()];case 1:return r=t.sent(),[3,3];case 2:return t.sent(),r={},[3,3];case 3:return e=r,i=(n={entityType:u,data:c}).entityType,n=n.data,o=(s={url:"/v1/"+h[i],method:"POST",data:n,target:i===e.type?{name:f.ListView,data:{entityType:i,entityData:n}}:null}).target,[4,this.api(s.url,{method:s.method,body:JSON.stringify(s.data)})];case 4:return s=t.sent(),o&&o.data&&(o.data.entityData=s,l(function(){a.refreshUI(o)},d)),[2,s]}var e,n,i})})},y.prototype.relateEntity=function(o,s,a,u){return void 0===u&&(u=0),e(this,void 0,void 0,function(){var r;return p(this,function(t){switch(t.label){case 0:return[4,this._getCachedContext()];case 1:return r=t.sent(),r={url:"/v1/"+h[n=(e={entityType:o,entityId:parseInt(s,10),data:a}).entityType]+"/"+(i=e.entityId)+"/related",method:"POST",data:{resource:e=e.data},target:n===(n=r).type&&i===n.id?{name:f.Related,data:e}:null},[2,this._action(r,u)]}var e,n,i})})},y.prototype.refreshUI=function(t){this._postMessage("refreshUI",{target:t})},y.prototype.on=function(t,e){n(this.events,t),this.events[t].push(e)},y.prototype.trigger=function(t,e){var n=this;this.events[t]&&this.events[t].forEach(function(t){t.call(n,e)})},y.prototype.api=function(t,e){if(!t)return Promise.reject({id:"sdk-api",version:y.version,detail:"url cannot be empty"});if(e&&e.body)try{JSON.parse(e.body)}catch(t){return Promise.reject({id:"sdk-api",version:y.version,detail:"body must be a valid JSON string"})}return this._deferredPost("api",{url:t,options:e})},y.prototype.navigateToEntityDetail=function(t,e){return this._deferredPost("navigateToEntityDetail",{entityType:t,entityId:e})},y.prototype.getSelectedRecords=function(t){var t=void 0===t?{}:t,e=t.pageSize,t=t.pageNumber;return this._deferredPost("getSelectedRecords",{pageSize:void 0===e?100:e,pageNumber:void 0===t?0:t})},y.prototype.getConfig=function(){return this._deferredPost("getConfig")},y.prototype._getCachedContext=function(){return e(this,void 0,void 0,function(){return p(this,function(t){switch(t.label){case 0:return this._context?[2,this._context]:[4,this.getContext()];case 1:return[2,t.sent().context]}})})},y.prototype._postMessage=function(t,e,n){var i;void 0===e&&(e={}),void 0===n&&(n=-1),null!=(i=this.win.top)&&i.postMessage(r(r({},e),{instanceId:this.instanceId,version:o,type:t,id:n}),this.parentOrigin)},y.prototype._listenMessage=function(){var e=this;this.win.addEventListener("message",function(t){e._isOriginValid(t)&&(e._resolveDeferred(t.data.type,t.data.id,t.data),t.data.type)&&e.trigger(t.data.type,t.data.msg||t.data.data)},!1)},y.prototype._isOriginValid=function(t){return t.origin===this.parentOrigin},y.prototype._enqueueDeferred=function(t,e){n(this.deferredQueues,t),this.deferredQueues[t].push(e)},y.prototype._resolveDeferred=function(t,e,n){t=this.deferredQueues[t];if(t){var i=t.findIndex(function(t){return t.id===e}),r=t[i];if(r){if(t.splice(i,1),n.error)return r.reject(n.error);r.resolve(n.data)}}},y.prototype._createContextModel=function(t){var e=t.entityType,t=new c(e,t.entityData,t.editableFields,this.saveContext.bind(this));return{type:e,context:this._context=t}},y.prototype._action=function(t,i){var r=t.url,o=t.method,s=t.data,a=t.target;return e(this,void 0,void 0,function(){var e,n=this;return p(this,function(t){switch(t.label){case 0:return[4,this.api(r,{method:o,body:JSON.stringify(s)})];case 1:return e=t.sent(),a&&l(function(){n.refreshUI(a)},i),[2,e]}})})},y.prototype._createDeferredMethod=function(t,e){var n=new i;return this._enqueueDeferred(t,n),e(n),n.promise},y.prototype._deferredPost=function(e,n){var i=this;return this._createDeferredMethod(e,function(t){i._postMessage(e,n||{},t.id)})},y.prototype._subscribeContextUpdated=function(){this.on("contextUpdated",function(){0})},y});
package/dist/defer.d.ts CHANGED
@@ -1,9 +1,14 @@
1
1
  export default class Deferred<T> {
2
+ static resetNextId(): void;
3
+ private static _nextId;
4
+ private static getNextId;
2
5
  private _promise;
3
6
  private _resolve;
4
7
  private _reject;
8
+ private _id;
5
9
  constructor();
6
10
  get promise(): Promise<T>;
11
+ get id(): number;
7
12
  resolve: (value?: T | PromiseLike<T> | undefined) => void;
8
13
  reject: (reason?: any) => void;
9
14
  }
package/es/copper-sdk.js CHANGED
@@ -62,7 +62,7 @@ function __generator(thisArg, body) {
62
62
  }
63
63
  }
64
64
 
65
- var version = "0.7.0";
65
+ var version = "0.7.1";
66
66
 
67
67
  var ENTITY_PATH_MAP = {
68
68
  lead: 'leads',
@@ -158,7 +158,14 @@ var Deferred = /** @class */ (function () {
158
158
  _this._resolve = resolve;
159
159
  _this._reject = reject;
160
160
  });
161
+ this._id = Deferred.getNextId();
161
162
  }
163
+ Deferred.resetNextId = function () {
164
+ Deferred._nextId = 0;
165
+ };
166
+ Deferred.getNextId = function () {
167
+ return Deferred._nextId++;
168
+ };
162
169
  Object.defineProperty(Deferred.prototype, "promise", {
163
170
  get: function () {
164
171
  return this._promise;
@@ -166,6 +173,14 @@ var Deferred = /** @class */ (function () {
166
173
  enumerable: false,
167
174
  configurable: true
168
175
  });
176
+ Object.defineProperty(Deferred.prototype, "id", {
177
+ get: function () {
178
+ return this._id;
179
+ },
180
+ enumerable: false,
181
+ configurable: true
182
+ });
183
+ Deferred._nextId = 0;
169
184
  return Deferred;
170
185
  }());
171
186
 
@@ -558,15 +573,19 @@ var Copper = /** @class */ (function () {
558
573
  });
559
574
  });
560
575
  };
561
- Copper.prototype._postMessage = function (type, message) {
576
+ Copper.prototype._postMessage = function (type, message, id) {
577
+ var _a;
562
578
  if (message === void 0) { message = {}; }
563
- this.win.top.postMessage(__assign(__assign({}, message), {
579
+ if (id === void 0) { id = -1; }
580
+ (_a = this.win.top) === null || _a === void 0 ? void 0 : _a.postMessage(__assign(__assign({}, message), {
564
581
  // as a credential to the parent frame, so parent frame can recoganize the origin
565
582
  instanceId: this.instanceId,
566
583
  // tell parent frame current sdk version
567
584
  version: version,
568
585
  // type of message
569
- type: type }), this.parentOrigin);
586
+ type: type,
587
+ // optional id for message
588
+ id: id }), this.parentOrigin);
570
589
  };
571
590
  Copper.prototype._listenMessage = function () {
572
591
  var _this = this;
@@ -576,7 +595,7 @@ var Copper = /** @class */ (function () {
576
595
  }
577
596
  // if type is a deferred type, we resolve it
578
597
  // otherwise we do something else
579
- _this._resolveDeferred(event.data.type, event.data);
598
+ _this._resolveDeferred(event.data.type, event.data.id, event.data);
580
599
  // if event type exists, we pass the event to SDK
581
600
  // so sdk user can subscribe those events
582
601
  if (event.data.type) {
@@ -592,12 +611,17 @@ var Copper = /** @class */ (function () {
592
611
  createArrayWhenEmpty(this.deferredQueues, queueName);
593
612
  this.deferredQueues[queueName].push(deferred);
594
613
  };
595
- Copper.prototype._resolveDeferred = function (queueName, data) {
596
- if (!this.deferredQueues[queueName]) {
614
+ Copper.prototype._resolveDeferred = function (queueName, messageId, data) {
615
+ var queue = this.deferredQueues[queueName];
616
+ if (!queue) {
597
617
  return;
598
618
  }
599
- var deferred = this.deferredQueues[queueName].shift();
619
+ var foundIdx = queue.findIndex(function (d) {
620
+ return d.id === messageId;
621
+ });
622
+ var deferred = queue[foundIdx];
600
623
  if (deferred) {
624
+ queue.splice(foundIdx, 1);
601
625
  if (data.error) {
602
626
  return deferred.reject(data.error);
603
627
  }
@@ -636,17 +660,17 @@ var Copper = /** @class */ (function () {
636
660
  Copper.prototype._createDeferredMethod = function (queueName, executor) {
637
661
  var deferred = new Deferred();
638
662
  this._enqueueDeferred(queueName, deferred);
639
- executor();
663
+ executor(deferred);
640
664
  return deferred.promise;
641
665
  };
642
666
  Copper.prototype._deferredPost = function (name, data) {
643
667
  var _this = this;
644
- return this._createDeferredMethod(name, function () {
668
+ return this._createDeferredMethod(name, function (deferred) {
645
669
  if (data) {
646
- _this._postMessage(name, data);
670
+ _this._postMessage(name, data, deferred.id);
647
671
  }
648
672
  else {
649
- _this._postMessage(name);
673
+ _this._postMessage(name, {}, deferred.id);
650
674
  }
651
675
  });
652
676
  };
package/es/defer.d.ts CHANGED
@@ -1,9 +1,14 @@
1
1
  export default class Deferred<T> {
2
+ static resetNextId(): void;
3
+ private static _nextId;
4
+ private static getNextId;
2
5
  private _promise;
3
6
  private _resolve;
4
7
  private _reject;
8
+ private _id;
5
9
  constructor();
6
10
  get promise(): Promise<T>;
11
+ get id(): number;
7
12
  resolve: (value?: T | PromiseLike<T> | undefined) => void;
8
13
  reject: (reason?: any) => void;
9
14
  }
package/lib/copper-sdk.js CHANGED
@@ -64,7 +64,7 @@ function __generator(thisArg, body) {
64
64
  }
65
65
  }
66
66
 
67
- var version = "0.7.0";
67
+ var version = "0.7.1";
68
68
 
69
69
  var ENTITY_PATH_MAP = {
70
70
  lead: 'leads',
@@ -160,7 +160,14 @@ var Deferred = /** @class */ (function () {
160
160
  _this._resolve = resolve;
161
161
  _this._reject = reject;
162
162
  });
163
+ this._id = Deferred.getNextId();
163
164
  }
165
+ Deferred.resetNextId = function () {
166
+ Deferred._nextId = 0;
167
+ };
168
+ Deferred.getNextId = function () {
169
+ return Deferred._nextId++;
170
+ };
164
171
  Object.defineProperty(Deferred.prototype, "promise", {
165
172
  get: function () {
166
173
  return this._promise;
@@ -168,6 +175,14 @@ var Deferred = /** @class */ (function () {
168
175
  enumerable: false,
169
176
  configurable: true
170
177
  });
178
+ Object.defineProperty(Deferred.prototype, "id", {
179
+ get: function () {
180
+ return this._id;
181
+ },
182
+ enumerable: false,
183
+ configurable: true
184
+ });
185
+ Deferred._nextId = 0;
171
186
  return Deferred;
172
187
  }());
173
188
 
@@ -560,15 +575,19 @@ var Copper = /** @class */ (function () {
560
575
  });
561
576
  });
562
577
  };
563
- Copper.prototype._postMessage = function (type, message) {
578
+ Copper.prototype._postMessage = function (type, message, id) {
579
+ var _a;
564
580
  if (message === void 0) { message = {}; }
565
- this.win.top.postMessage(__assign(__assign({}, message), {
581
+ if (id === void 0) { id = -1; }
582
+ (_a = this.win.top) === null || _a === void 0 ? void 0 : _a.postMessage(__assign(__assign({}, message), {
566
583
  // as a credential to the parent frame, so parent frame can recoganize the origin
567
584
  instanceId: this.instanceId,
568
585
  // tell parent frame current sdk version
569
586
  version: version,
570
587
  // type of message
571
- type: type }), this.parentOrigin);
588
+ type: type,
589
+ // optional id for message
590
+ id: id }), this.parentOrigin);
572
591
  };
573
592
  Copper.prototype._listenMessage = function () {
574
593
  var _this = this;
@@ -578,7 +597,7 @@ var Copper = /** @class */ (function () {
578
597
  }
579
598
  // if type is a deferred type, we resolve it
580
599
  // otherwise we do something else
581
- _this._resolveDeferred(event.data.type, event.data);
600
+ _this._resolveDeferred(event.data.type, event.data.id, event.data);
582
601
  // if event type exists, we pass the event to SDK
583
602
  // so sdk user can subscribe those events
584
603
  if (event.data.type) {
@@ -594,12 +613,17 @@ var Copper = /** @class */ (function () {
594
613
  createArrayWhenEmpty(this.deferredQueues, queueName);
595
614
  this.deferredQueues[queueName].push(deferred);
596
615
  };
597
- Copper.prototype._resolveDeferred = function (queueName, data) {
598
- if (!this.deferredQueues[queueName]) {
616
+ Copper.prototype._resolveDeferred = function (queueName, messageId, data) {
617
+ var queue = this.deferredQueues[queueName];
618
+ if (!queue) {
599
619
  return;
600
620
  }
601
- var deferred = this.deferredQueues[queueName].shift();
621
+ var foundIdx = queue.findIndex(function (d) {
622
+ return d.id === messageId;
623
+ });
624
+ var deferred = queue[foundIdx];
602
625
  if (deferred) {
626
+ queue.splice(foundIdx, 1);
603
627
  if (data.error) {
604
628
  return deferred.reject(data.error);
605
629
  }
@@ -638,17 +662,17 @@ var Copper = /** @class */ (function () {
638
662
  Copper.prototype._createDeferredMethod = function (queueName, executor) {
639
663
  var deferred = new Deferred();
640
664
  this._enqueueDeferred(queueName, deferred);
641
- executor();
665
+ executor(deferred);
642
666
  return deferred.promise;
643
667
  };
644
668
  Copper.prototype._deferredPost = function (name, data) {
645
669
  var _this = this;
646
- return this._createDeferredMethod(name, function () {
670
+ return this._createDeferredMethod(name, function (deferred) {
647
671
  if (data) {
648
- _this._postMessage(name, data);
672
+ _this._postMessage(name, data, deferred.id);
649
673
  }
650
674
  else {
651
- _this._postMessage(name);
675
+ _this._postMessage(name, {}, deferred.id);
652
676
  }
653
677
  });
654
678
  };
package/lib/defer.d.ts CHANGED
@@ -1,9 +1,14 @@
1
1
  export default class Deferred<T> {
2
+ static resetNextId(): void;
3
+ private static _nextId;
4
+ private static getNextId;
2
5
  private _promise;
3
6
  private _resolve;
4
7
  private _reject;
8
+ private _id;
5
9
  constructor();
6
10
  get promise(): Promise<T>;
11
+ get id(): number;
7
12
  resolve: (value?: T | PromiseLike<T> | undefined) => void;
8
13
  reject: (reason?: any) => void;
9
14
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "copper-sdk",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "description": "Copper App SDK",
5
5
  "main": "lib/copper-sdk.js",
6
6
  "module": "es/copper-sdk.js",
package/src/defer.ts CHANGED
@@ -1,20 +1,35 @@
1
1
  export default class Deferred<T> {
2
+ public static resetNextId() {
3
+ Deferred._nextId = 0;
4
+ }
5
+
6
+ private static _nextId: number = 0;
7
+
8
+ private static getNextId() {
9
+ return Deferred._nextId++;
10
+ }
2
11
 
3
12
  private _promise: Promise<T>;
4
13
  private _resolve: (value?: T | PromiseLike<T>) => void;
5
14
  private _reject: (reason?: any) => void;
15
+ private _id: number;
6
16
 
7
17
  constructor() {
8
18
  this._promise = new Promise<T>((resolve, reject) => {
9
19
  this._resolve = resolve;
10
20
  this._reject = reject;
11
21
  });
22
+ this._id = Deferred.getNextId();
12
23
  }
13
24
 
14
25
  public get promise(): Promise<T> {
15
26
  return this._promise;
16
27
  }
17
28
 
29
+ public get id(): number {
30
+ return this._id;
31
+ }
32
+
18
33
  public resolve = (value?: T | PromiseLike<T>): void => {
19
34
  this._resolve(value);
20
35
  }
@@ -22,5 +37,4 @@ export default class Deferred<T> {
22
37
  public reject = (reason?: any): void => {
23
38
  this._reject(reason);
24
39
  }
25
-
26
40
  }
package/src/index.ts CHANGED
@@ -252,8 +252,8 @@ export default class Copper {
252
252
  return context;
253
253
  }
254
254
 
255
- private _postMessage(type: string, message: { [name: string]: any } = {}): void {
256
- this.win.top.postMessage(
255
+ private _postMessage(type: string, message: { [name: string]: any } = {}, id: number = -1): void {
256
+ this.win.top?.postMessage(
257
257
  {
258
258
  // actual messages
259
259
  ...message,
@@ -263,6 +263,8 @@ export default class Copper {
263
263
  version,
264
264
  // type of message
265
265
  type,
266
+ // optional id for message
267
+ id,
266
268
  },
267
269
  this.parentOrigin,
268
270
  );
@@ -278,7 +280,7 @@ export default class Copper {
278
280
 
279
281
  // if type is a deferred type, we resolve it
280
282
  // otherwise we do something else
281
- this._resolveDeferred(event.data.type, event.data as IMessageData);
283
+ this._resolveDeferred(event.data.type, event.data.id, event.data as IMessageData);
282
284
 
283
285
  // if event type exists, we pass the event to SDK
284
286
  // so sdk user can subscribe those events
@@ -300,12 +302,19 @@ export default class Copper {
300
302
  this.deferredQueues[queueName].push(deferred);
301
303
  }
302
304
 
303
- private _resolveDeferred(queueName: string, data: IMessageData): void {
304
- if (!this.deferredQueues[queueName]) {
305
+ private _resolveDeferred(queueName: string, messageId: number, data: IMessageData): void {
306
+ const queue = this.deferredQueues[queueName];
307
+ if (!queue) {
305
308
  return;
306
309
  }
307
- const deferred = this.deferredQueues[queueName].shift();
310
+
311
+ const foundIdx = queue.findIndex((d) => {
312
+ return d.id === messageId;
313
+ });
314
+
315
+ const deferred = queue[foundIdx];
308
316
  if (deferred) {
317
+ queue.splice(foundIdx, 1);
309
318
  if (data.error) {
310
319
  return deferred.reject(data.error);
311
320
  }
@@ -344,19 +353,22 @@ export default class Copper {
344
353
  return result;
345
354
  }
346
355
 
347
- private _createDeferredMethod(queueName: string, executor: () => any): Promise<any> {
356
+ private _createDeferredMethod(
357
+ queueName: string,
358
+ executor: (deferred: Deferred<any>) => any,
359
+ ): Promise<any> {
348
360
  const deferred = new Deferred<any>();
349
361
  this._enqueueDeferred(queueName, deferred);
350
- executor();
362
+ executor(deferred);
351
363
  return deferred.promise;
352
364
  }
353
365
 
354
366
  private _deferredPost(name: string, data?: any): Promise<any> {
355
- return this._createDeferredMethod(name, () => {
367
+ return this._createDeferredMethod(name, (deferred) => {
356
368
  if (data) {
357
- this._postMessage(name, data);
369
+ this._postMessage(name, data, deferred.id);
358
370
  } else {
359
- this._postMessage(name);
371
+ this._postMessage(name, {}, deferred.id);
360
372
  }
361
373
  });
362
374
  }