copper-sdk 0.6.3 → 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 ADDED
@@ -0,0 +1,7 @@
1
+ ## 0.7.1
2
+
3
+ * Add support for parallel asynchronous requests
4
+
5
+ ## 0.7.0
6
+
7
+ * Bump all dependencies as far as they can go on Node 10
package/README.md CHANGED
@@ -1,10 +1,8 @@
1
1
  Copper App SDK
2
2
  ====================
3
3
 
4
- [![Travis branch](https://img.shields.io/travis/ProsperWorks/copper-sdk/master.svg?style=flat-square)](https://travis-ci.org/ProsperWorks/copper-sdk)
5
4
  [![npm](https://img.shields.io/npm/v/copper-sdk.svg?style=flat-square)](https://www.npmjs.com/package/copper-sdk)
6
- [![Test Coverage](https://api.codeclimate.com/v1/badges/d8ef6bf3d4669616c465/test_coverage)](https://codeclimate.com/github/ProsperWorks/copper-sdk/test_coverage)
7
- [![Maintainability](https://api.codeclimate.com/v1/badges/d8ef6bf3d4669616c465/maintainability)](https://codeclimate.com/github/ProsperWorks/copper-sdk/maintainability)
5
+
8
6
 
9
7
  The javascript SDK provides client-side functionalities for adding embedded app to the Copper web client.
10
8
 
@@ -51,6 +49,9 @@ https://docs.copper.com/copper-sdk
51
49
 
52
50
  ## Development
53
51
  ### Prerequisites
52
+
53
+ We recommend Node 10 or newer.
54
+
54
55
  Confirm all packages have been installed before development. Yarn will be used for this particular project.
55
56
 
56
57
  ```bash
@@ -60,8 +61,10 @@ yarn
60
61
  ### Developing SDK
61
62
  ```bash
62
63
  ## watch changes
64
+ yarn dev
65
+
66
+ ## or run separately
63
67
  yarn dev:es
64
- # or
65
68
  yarn dev:commonjs
66
69
 
67
70
  ## build
@@ -2,21 +2,21 @@
2
2
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
3
  typeof define === 'function' && define.amd ? define(factory) :
4
4
  (global = global || self, global.Copper = factory());
5
- }(this, function () { 'use strict';
5
+ }(this, (function () { 'use strict';
6
6
 
7
7
  /*! *****************************************************************************
8
- Copyright (c) Microsoft Corporation. All rights reserved.
9
- Licensed under the Apache License, Version 2.0 (the "License"); you may not use
10
- this file except in compliance with the License. You may obtain a copy of the
11
- License at http://www.apache.org/licenses/LICENSE-2.0
8
+ Copyright (c) Microsoft Corporation.
12
9
 
13
- THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
- KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15
- WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16
- MERCHANTABLITY OR NON-INFRINGEMENT.
10
+ Permission to use, copy, modify, and/or distribute this software for any
11
+ purpose with or without fee is hereby granted.
17
12
 
18
- See the Apache Version 2.0 License for specific language governing permissions
19
- and limitations under the License.
13
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
14
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
16
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
17
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
18
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19
+ PERFORMANCE OF THIS SOFTWARE.
20
20
  ***************************************************************************** */
21
21
 
22
22
  var __assign = function() {
@@ -31,10 +31,11 @@
31
31
  };
32
32
 
33
33
  function __awaiter(thisArg, _arguments, P, generator) {
34
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
34
35
  return new (P || (P = Promise))(function (resolve, reject) {
35
36
  function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
36
37
  function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
37
- function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
38
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
38
39
  step((generator = generator.apply(thisArg, _arguments || [])).next());
39
40
  });
40
41
  }
@@ -67,7 +68,7 @@
67
68
  }
68
69
  }
69
70
 
70
- var version = "0.6.3";
71
+ var version = "0.7.1";
71
72
 
72
73
  var ENTITY_PATH_MAP = {
73
74
  lead: 'leads',
@@ -163,14 +164,29 @@
163
164
  _this._resolve = resolve;
164
165
  _this._reject = reject;
165
166
  });
167
+ this._id = Deferred.getNextId();
166
168
  }
169
+ Deferred.resetNextId = function () {
170
+ Deferred._nextId = 0;
171
+ };
172
+ Deferred.getNextId = function () {
173
+ return Deferred._nextId++;
174
+ };
167
175
  Object.defineProperty(Deferred.prototype, "promise", {
168
176
  get: function () {
169
177
  return this._promise;
170
178
  },
171
- enumerable: true,
179
+ enumerable: false,
172
180
  configurable: true
173
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;
174
190
  return Deferred;
175
191
  }());
176
192
 
@@ -339,14 +355,14 @@
339
355
  get: function () {
340
356
  return version;
341
357
  },
342
- enumerable: true,
358
+ enumerable: false,
343
359
  configurable: true
344
360
  });
345
361
  Object.defineProperty(Copper.prototype, "win", {
346
362
  get: function () {
347
363
  return this._win;
348
364
  },
349
- enumerable: true,
365
+ enumerable: false,
350
366
  configurable: true
351
367
  });
352
368
  Copper.prototype.getContext = function () {
@@ -563,15 +579,19 @@
563
579
  });
564
580
  });
565
581
  };
566
- Copper.prototype._postMessage = function (type, message) {
582
+ Copper.prototype._postMessage = function (type, message, id) {
583
+ var _a;
567
584
  if (message === void 0) { message = {}; }
568
- this.win.top.postMessage(__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), {
569
587
  // as a credential to the parent frame, so parent frame can recoganize the origin
570
588
  instanceId: this.instanceId,
571
589
  // tell parent frame current sdk version
572
590
  version: version,
573
591
  // type of message
574
- type: type }), this.parentOrigin);
592
+ type: type,
593
+ // optional id for message
594
+ id: id }), this.parentOrigin);
575
595
  };
576
596
  Copper.prototype._listenMessage = function () {
577
597
  var _this = this;
@@ -581,7 +601,7 @@
581
601
  }
582
602
  // if type is a deferred type, we resolve it
583
603
  // otherwise we do something else
584
- _this._resolveDeferred(event.data.type, event.data);
604
+ _this._resolveDeferred(event.data.type, event.data.id, event.data);
585
605
  // if event type exists, we pass the event to SDK
586
606
  // so sdk user can subscribe those events
587
607
  if (event.data.type) {
@@ -597,12 +617,17 @@
597
617
  createArrayWhenEmpty(this.deferredQueues, queueName);
598
618
  this.deferredQueues[queueName].push(deferred);
599
619
  };
600
- Copper.prototype._resolveDeferred = function (queueName, data) {
601
- if (!this.deferredQueues[queueName]) {
620
+ Copper.prototype._resolveDeferred = function (queueName, messageId, data) {
621
+ var queue = this.deferredQueues[queueName];
622
+ if (!queue) {
602
623
  return;
603
624
  }
604
- var deferred = this.deferredQueues[queueName].shift();
625
+ var foundIdx = queue.findIndex(function (d) {
626
+ return d.id === messageId;
627
+ });
628
+ var deferred = queue[foundIdx];
605
629
  if (deferred) {
630
+ queue.splice(foundIdx, 1);
606
631
  if (data.error) {
607
632
  return deferred.reject(data.error);
608
633
  }
@@ -641,17 +666,17 @@
641
666
  Copper.prototype._createDeferredMethod = function (queueName, executor) {
642
667
  var deferred = new Deferred();
643
668
  this._enqueueDeferred(queueName, deferred);
644
- executor();
669
+ executor(deferred);
645
670
  return deferred.promise;
646
671
  };
647
- Copper.prototype._deferredPost = function (name$$1, data) {
672
+ Copper.prototype._deferredPost = function (name, data) {
648
673
  var _this = this;
649
- return this._createDeferredMethod(name$$1, function () {
674
+ return this._createDeferredMethod(name, function (deferred) {
650
675
  if (data) {
651
- _this._postMessage(name$$1, data);
676
+ _this._postMessage(name, data, deferred.id);
652
677
  }
653
678
  else {
654
- _this._postMessage(name$$1);
679
+ _this._postMessage(name, {}, deferred.id);
655
680
  }
656
681
  });
657
682
  };
@@ -667,4 +692,4 @@
667
692
 
668
693
  return Copper;
669
694
 
670
- }));
695
+ })));
@@ -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(o,s,a,u){return new(a||(a=Promise))(function(t,e){function n(t){try{r(u.next(t))}catch(t){e(t)}}function i(t){try{r(u.throw(t))}catch(t){e(t)}}function r(e){e.done?t(e.value):new a(function(t){t(e.value)}).then(n,i)}r((u=u.apply(o,s||[])).next())})}function f(n,i){var r,o,s,t,a={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]};return t={next:e(0),throw:e(1),return:e(2)},"function"==typeof Symbol&&(t[Symbol.iterator]=function(){return this}),t;function e(e){return function(t){return function(e){if(r)throw new TypeError("Generator is already executing.");for(;a;)try{if(r=1,o&&(s=2&e[0]?o.return:e[0]?o.throw||((s=o.return)&&s.call(o),0):o.next)&&!(s=s.call(o,e[1])).done)return s;switch(o=0,s&&(e=[2&e[0],s.value]),e[0]){case 0:case 1:s=e;break;case 4:return a.label++,{value:e[1],done:!1};case 5:a.label++,o=e[1],e=[0];continue;case 7:e=a.ops.pop(),a.trys.pop();continue;default:if(!(s=0<(s=a.trys).length&&s[s.length-1])&&(6===e[0]||2===e[0])){a=0;continue}if(3===e[0]&&(!s||s[0]<e[1]&&e[1]<s[3])){a.label=e[1];break}if(6===e[0]&&a.label<s[1]){a.label=s[1],s=e;break}if(s&&a.label<s[2]){a.label=s[2],a.ops.push(e);break}s[2]&&a.ops.pop(),a.trys.pop();continue}e=i.call(n,a)}catch(t){e=[6,t],o=0}finally{r=s=0}if(5&e[0])throw e[1];return{value:e[0]?e[1]:void 0,done:!0}}([e,t])}}}var h,t,r="0.6.3",l={lead:"leads",person:"people",company:"companies",opportunity:"opportunities",task:"tasks",project:"projects"};(t=h||(h={})).ActivityLog="ActivityLog",t.ListView="ListView",t.Related="Related";var o=function(){function t(){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})}return Object.defineProperty(t.prototype,"promise",{get:function(){return this._promise},enumerable:!0,configurable:!0}),t}();function s(t,e){void 0===e&&(e=""),e||(e=window.location.href),t=t.replace(/[[]]/g,"\\$&");var n=RegExp("[?&]"+t+"(=([^&#]*)|&|#|$)").exec(e);return n&&n[2]?decodeURIComponent(n[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 y(t,e){void 0===e&&(e=1e3),setTimeout(function(){t()},e)}var c=function(){function t(t,e,n,i){void 0===n&&(n=[]);var r=this._getEntityDataDefinition(e,n);r.type={enumerable:!1,writable:!1,value:t},r._onSave={enumerable:!1,value:i},Object.defineProperties(this,r)}return t.prototype.save=function(){return e(this,void 0,void 0,function(){return f(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]}})})},t.prototype.toJSON=function(){return JSON.stringify(this.toObject())},t.prototype.toObject=function(){var e=this,n={};return Object.keys(this).forEach(function(t){n[t]=e[t]}),n},t.prototype._getEntityDataDefinition=function(e,n){void 0===n&&(n=[]);var i={};return Object.keys(e).forEach(function(t){i[t]=-1==n.indexOf(t)?{get:function(){return e[t]},set:function(){a("property "+t+" is read only")},enumerable:!0}:{value:e[t],writable:!0,enumerable:!0}}),i},t}();return function(){function i(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 i.init=function(){var t=s("origin"),e=s("instanceId"),n={isGlobal:"1"===s("isGlobal")};if(!("undefined"==typeof window?(a("Copper can only run in browser environment"),0):(window.top===window&&a("Copper should be inside an iframe, otherwise it might not work as expected."),1)))throw Error("Environment checking does not pass.");return new i(t,e,n)},Object.defineProperty(i,"version",{get:function(){return r},enumerable:!0,configurable:!0}),Object.defineProperty(i.prototype,"win",{get:function(){return this._win},enumerable:!0,configurable:!0}),i.prototype.getContext=function(){return e(this,void 0,void 0,function(){var e;return f(this,function(t){switch(t.label){case 0:return[4,this._deferredPost("getContext")];case 1:return e=t.sent(),[2,this._createContextModel(e)]}})})},i.prototype.getUserInfo=function(){return e(this,void 0,void 0,function(){return f(this,function(t){return[2,this._deferredPost("getUserInfo")]})})},i.prototype.getRouteInfo=function(){return e(this,void 0,void 0,function(){return f(this,function(t){return[2,this._deferredPost("getRouteInfo")]})})},i.prototype.saveContext=function(n){return e(this,void 0,void 0,function(){var e;return f(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)]}})})},i.prototype.setAppUI=function(t){this._postMessage("setUI",{data:t})},i.prototype.showModal=function(t){void 0===t&&(t={}),this._postMessage("showModal",{params:t})},i.prototype.closeModal=function(){this._postMessage("closeModal")},i.prototype.publishMessage=function(t,e,n){void 0===n&&(n={}),this._postMessage("publishMessage",{target:e,data:{type:t,msg:n}})},i.prototype.logActivity=function(s,a,u,c){return void 0===c&&(c=0),e(this,void 0,void 0,function(){var o;return f(this,function(t){switch(t.label){case 0:return[4,this._getCachedContext()];case 1:return o=t.sent(),r={parent:{type:(e=o).type,id:e.id},type:{category:"user",id:(n={activityType:s,details:a,activityDate:u}).activityType},details:n.details},(i=n.activityDate)&&(r.activity_date=i),[2,this._action({url:"/v1/activities",method:"POST",data:r,target:{name:h.ActivityLog}},c)]}var e,n,i,r})})},i.prototype.createEntity=function(c,p,d){return void 0===d&&(d=0),e(this,void 0,void 0,function(){var r,o,s,a,u=this;return f(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 s=(o={url:"/v1/"+l[n=(e={entityType:c,data:p}).entityType],method:"POST",data:i=e.data,target:n===r.type?{name:h.ListView,data:{entityType:n,entityData:i}}:null}).target,[4,this.api(o.url,{method:o.method,body:JSON.stringify(o.data)})];case 4:return a=t.sent(),s&&s.data&&(s.data.entityData=a,y(function(){u.refreshUI(s)},d)),[2,a]}var e,n,i})})},i.prototype.relateEntity=function(a,u,c,p){return void 0===p&&(p=0),e(this,void 0,void 0,function(){var s;return f(this,function(t){switch(t.label){case 0:return[4,this._getCachedContext()];case 1:return s=t.sent(),e=s,[2,this._action({url:"/v1/"+l[i=(n={entityType:a,entityId:parseInt(u,10),data:c}).entityType]+"/"+(r=n.entityId)+"/related",method:"POST",data:{resource:o=n.data},target:i===e.type&&r===e.id?{name:h.Related,data:o}:null},p)]}var e,n,i,r,o})})},i.prototype.refreshUI=function(t){this._postMessage("refreshUI",{target:t})},i.prototype.on=function(t,e){u(this.events,t),this.events[t].push(e)},i.prototype.trigger=function(t,e){var n=this;this.events[t]&&this.events[t].forEach(function(t){t.call(n,e)})},i.prototype.api=function(t,e){if(!t)return Promise.reject({id:"sdk-api",version:i.version,detail:"url cannot be empty"});if(e&&e.body)try{JSON.parse(e.body)}catch(t){return Promise.reject({id:"sdk-api",version:i.version,detail:"body must be a valid JSON string"})}return this._deferredPost("api",{url:t,options:e})},i.prototype.navigateToEntityDetail=function(t,e){return this._deferredPost("navigateToEntityDetail",{entityType:t,entityId:e})},i.prototype.getSelectedRecords=function(t){var e=void 0===t?{}:t,n=e.pageSize,i=e.pageNumber;return this._deferredPost("getSelectedRecords",{pageSize:void 0===n?100:n,pageNumber:void 0===i?0:i})},i.prototype.getConfig=function(){return this._deferredPost("getConfig")},i.prototype._getCachedContext=function(){return e(this,void 0,void 0,function(){return f(this,function(t){switch(t.label){case 0:return this._context?[2,this._context]:[4,this.getContext()];case 1:return[2,t.sent().context]}})})},i.prototype._postMessage=function(t,e){void 0===e&&(e={}),this.win.top.postMessage(n({},e,{instanceId:this.instanceId,version:r,type:t}),this.parentOrigin)},i.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)},i.prototype._isOriginValid=function(t){return t.origin===this.parentOrigin},i.prototype._enqueueDeferred=function(t,e){u(this.deferredQueues,t),this.deferredQueues[t].push(e)},i.prototype._resolveDeferred=function(t,e){if(this.deferredQueues[t]){var n=this.deferredQueues[t].shift();if(n){if(e.error)return n.reject(e.error);n.resolve(e.data)}}},i.prototype._createContextModel=function(t){var e=t.entityType,n=new c(e,t.entityData,t.editableFields,this.saveContext.bind(this));return{type:e,context:this._context=n}},i.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 f(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&&y(function(){n.refreshUI(a)},i),[2,e]}})})},i.prototype._createDeferredMethod=function(t,e){var n=new o;return this._enqueueDeferred(t,n),e(),n.promise},i.prototype._deferredPost=function(t,e){var n=this;return this._createDeferredMethod(t,function(){e?n._postMessage(t,e):n._postMessage(t)})},i.prototype._subscribeContextUpdated=function(){var t=this;this.on("contextUpdated",function(){t._context=null})},i}()});
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
- readonly promise: Promise<T>;
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/dist/index.d.ts CHANGED
@@ -7,7 +7,7 @@ export default class Copper {
7
7
  private options;
8
8
  private _win;
9
9
  static init(): Copper;
10
- static readonly version: string;
10
+ static get version(): string;
11
11
  /**
12
12
  * store deferred queues by name
13
13
  */
@@ -23,7 +23,7 @@ export default class Copper {
23
23
  * @memberof Copper
24
24
  */
25
25
  constructor(parentOrigin: string, instanceId: string, options: ISdkInitOptions, _win?: any);
26
- readonly win: Window;
26
+ get win(): Window;
27
27
  getContext(): Promise<IContextData>;
28
28
  getUserInfo(): Promise<any>;
29
29
  getRouteInfo(): Promise<any>;
package/es/copper-sdk.js CHANGED
@@ -1,16 +1,16 @@
1
1
  /*! *****************************************************************************
2
- Copyright (c) Microsoft Corporation. All rights reserved.
3
- Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4
- this file except in compliance with the License. You may obtain a copy of the
5
- License at http://www.apache.org/licenses/LICENSE-2.0
2
+ Copyright (c) Microsoft Corporation.
6
3
 
7
- THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8
- KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
9
- WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
10
- MERCHANTABLITY OR NON-INFRINGEMENT.
4
+ Permission to use, copy, modify, and/or distribute this software for any
5
+ purpose with or without fee is hereby granted.
11
6
 
12
- See the Apache Version 2.0 License for specific language governing permissions
13
- and limitations under the License.
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
+ PERFORMANCE OF THIS SOFTWARE.
14
14
  ***************************************************************************** */
15
15
 
16
16
  var __assign = function() {
@@ -25,10 +25,11 @@ var __assign = function() {
25
25
  };
26
26
 
27
27
  function __awaiter(thisArg, _arguments, P, generator) {
28
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
28
29
  return new (P || (P = Promise))(function (resolve, reject) {
29
30
  function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
30
31
  function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
31
- function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
32
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
32
33
  step((generator = generator.apply(thisArg, _arguments || [])).next());
33
34
  });
34
35
  }
@@ -61,7 +62,7 @@ function __generator(thisArg, body) {
61
62
  }
62
63
  }
63
64
 
64
- var version = "0.6.3";
65
+ var version = "0.7.1";
65
66
 
66
67
  var ENTITY_PATH_MAP = {
67
68
  lead: 'leads',
@@ -157,14 +158,29 @@ var Deferred = /** @class */ (function () {
157
158
  _this._resolve = resolve;
158
159
  _this._reject = reject;
159
160
  });
161
+ this._id = Deferred.getNextId();
160
162
  }
163
+ Deferred.resetNextId = function () {
164
+ Deferred._nextId = 0;
165
+ };
166
+ Deferred.getNextId = function () {
167
+ return Deferred._nextId++;
168
+ };
161
169
  Object.defineProperty(Deferred.prototype, "promise", {
162
170
  get: function () {
163
171
  return this._promise;
164
172
  },
165
- enumerable: true,
173
+ enumerable: false,
166
174
  configurable: true
167
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;
168
184
  return Deferred;
169
185
  }());
170
186
 
@@ -333,14 +349,14 @@ var Copper = /** @class */ (function () {
333
349
  get: function () {
334
350
  return version;
335
351
  },
336
- enumerable: true,
352
+ enumerable: false,
337
353
  configurable: true
338
354
  });
339
355
  Object.defineProperty(Copper.prototype, "win", {
340
356
  get: function () {
341
357
  return this._win;
342
358
  },
343
- enumerable: true,
359
+ enumerable: false,
344
360
  configurable: true
345
361
  });
346
362
  Copper.prototype.getContext = function () {
@@ -557,15 +573,19 @@ var Copper = /** @class */ (function () {
557
573
  });
558
574
  });
559
575
  };
560
- Copper.prototype._postMessage = function (type, message) {
576
+ Copper.prototype._postMessage = function (type, message, id) {
577
+ var _a;
561
578
  if (message === void 0) { message = {}; }
562
- this.win.top.postMessage(__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), {
563
581
  // as a credential to the parent frame, so parent frame can recoganize the origin
564
582
  instanceId: this.instanceId,
565
583
  // tell parent frame current sdk version
566
584
  version: version,
567
585
  // type of message
568
- type: type }), this.parentOrigin);
586
+ type: type,
587
+ // optional id for message
588
+ id: id }), this.parentOrigin);
569
589
  };
570
590
  Copper.prototype._listenMessage = function () {
571
591
  var _this = this;
@@ -575,7 +595,7 @@ var Copper = /** @class */ (function () {
575
595
  }
576
596
  // if type is a deferred type, we resolve it
577
597
  // otherwise we do something else
578
- _this._resolveDeferred(event.data.type, event.data);
598
+ _this._resolveDeferred(event.data.type, event.data.id, event.data);
579
599
  // if event type exists, we pass the event to SDK
580
600
  // so sdk user can subscribe those events
581
601
  if (event.data.type) {
@@ -591,12 +611,17 @@ var Copper = /** @class */ (function () {
591
611
  createArrayWhenEmpty(this.deferredQueues, queueName);
592
612
  this.deferredQueues[queueName].push(deferred);
593
613
  };
594
- Copper.prototype._resolveDeferred = function (queueName, data) {
595
- if (!this.deferredQueues[queueName]) {
614
+ Copper.prototype._resolveDeferred = function (queueName, messageId, data) {
615
+ var queue = this.deferredQueues[queueName];
616
+ if (!queue) {
596
617
  return;
597
618
  }
598
- var deferred = this.deferredQueues[queueName].shift();
619
+ var foundIdx = queue.findIndex(function (d) {
620
+ return d.id === messageId;
621
+ });
622
+ var deferred = queue[foundIdx];
599
623
  if (deferred) {
624
+ queue.splice(foundIdx, 1);
600
625
  if (data.error) {
601
626
  return deferred.reject(data.error);
602
627
  }
@@ -635,17 +660,17 @@ var Copper = /** @class */ (function () {
635
660
  Copper.prototype._createDeferredMethod = function (queueName, executor) {
636
661
  var deferred = new Deferred();
637
662
  this._enqueueDeferred(queueName, deferred);
638
- executor();
663
+ executor(deferred);
639
664
  return deferred.promise;
640
665
  };
641
- Copper.prototype._deferredPost = function (name$$1, data) {
666
+ Copper.prototype._deferredPost = function (name, data) {
642
667
  var _this = this;
643
- return this._createDeferredMethod(name$$1, function () {
668
+ return this._createDeferredMethod(name, function (deferred) {
644
669
  if (data) {
645
- _this._postMessage(name$$1, data);
670
+ _this._postMessage(name, data, deferred.id);
646
671
  }
647
672
  else {
648
- _this._postMessage(name$$1);
673
+ _this._postMessage(name, {}, deferred.id);
649
674
  }
650
675
  });
651
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
- readonly promise: Promise<T>;
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/index.d.ts CHANGED
@@ -7,7 +7,7 @@ export default class Copper {
7
7
  private options;
8
8
  private _win;
9
9
  static init(): Copper;
10
- static readonly version: string;
10
+ static get version(): string;
11
11
  /**
12
12
  * store deferred queues by name
13
13
  */
@@ -23,7 +23,7 @@ export default class Copper {
23
23
  * @memberof Copper
24
24
  */
25
25
  constructor(parentOrigin: string, instanceId: string, options: ISdkInitOptions, _win?: any);
26
- readonly win: Window;
26
+ get win(): Window;
27
27
  getContext(): Promise<IContextData>;
28
28
  getUserInfo(): Promise<any>;
29
29
  getRouteInfo(): Promise<any>;
package/lib/copper-sdk.js CHANGED
@@ -1,18 +1,18 @@
1
1
  'use strict';
2
2
 
3
3
  /*! *****************************************************************************
4
- Copyright (c) Microsoft Corporation. All rights reserved.
5
- Licensed under the Apache License, Version 2.0 (the "License"); you may not use
6
- this file except in compliance with the License. You may obtain a copy of the
7
- License at http://www.apache.org/licenses/LICENSE-2.0
4
+ Copyright (c) Microsoft Corporation.
8
5
 
9
- THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
10
- KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
11
- WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
12
- MERCHANTABLITY OR NON-INFRINGEMENT.
6
+ Permission to use, copy, modify, and/or distribute this software for any
7
+ purpose with or without fee is hereby granted.
13
8
 
14
- See the Apache Version 2.0 License for specific language governing permissions
15
- and limitations under the License.
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
+ PERFORMANCE OF THIS SOFTWARE.
16
16
  ***************************************************************************** */
17
17
 
18
18
  var __assign = function() {
@@ -27,10 +27,11 @@ var __assign = function() {
27
27
  };
28
28
 
29
29
  function __awaiter(thisArg, _arguments, P, generator) {
30
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
30
31
  return new (P || (P = Promise))(function (resolve, reject) {
31
32
  function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
32
33
  function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
33
- function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
34
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
34
35
  step((generator = generator.apply(thisArg, _arguments || [])).next());
35
36
  });
36
37
  }
@@ -63,7 +64,7 @@ function __generator(thisArg, body) {
63
64
  }
64
65
  }
65
66
 
66
- var version = "0.6.3";
67
+ var version = "0.7.1";
67
68
 
68
69
  var ENTITY_PATH_MAP = {
69
70
  lead: 'leads',
@@ -159,14 +160,29 @@ var Deferred = /** @class */ (function () {
159
160
  _this._resolve = resolve;
160
161
  _this._reject = reject;
161
162
  });
163
+ this._id = Deferred.getNextId();
162
164
  }
165
+ Deferred.resetNextId = function () {
166
+ Deferred._nextId = 0;
167
+ };
168
+ Deferred.getNextId = function () {
169
+ return Deferred._nextId++;
170
+ };
163
171
  Object.defineProperty(Deferred.prototype, "promise", {
164
172
  get: function () {
165
173
  return this._promise;
166
174
  },
167
- enumerable: true,
175
+ enumerable: false,
168
176
  configurable: true
169
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;
170
186
  return Deferred;
171
187
  }());
172
188
 
@@ -335,14 +351,14 @@ var Copper = /** @class */ (function () {
335
351
  get: function () {
336
352
  return version;
337
353
  },
338
- enumerable: true,
354
+ enumerable: false,
339
355
  configurable: true
340
356
  });
341
357
  Object.defineProperty(Copper.prototype, "win", {
342
358
  get: function () {
343
359
  return this._win;
344
360
  },
345
- enumerable: true,
361
+ enumerable: false,
346
362
  configurable: true
347
363
  });
348
364
  Copper.prototype.getContext = function () {
@@ -559,15 +575,19 @@ var Copper = /** @class */ (function () {
559
575
  });
560
576
  });
561
577
  };
562
- Copper.prototype._postMessage = function (type, message) {
578
+ Copper.prototype._postMessage = function (type, message, id) {
579
+ var _a;
563
580
  if (message === void 0) { message = {}; }
564
- this.win.top.postMessage(__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), {
565
583
  // as a credential to the parent frame, so parent frame can recoganize the origin
566
584
  instanceId: this.instanceId,
567
585
  // tell parent frame current sdk version
568
586
  version: version,
569
587
  // type of message
570
- type: type }), this.parentOrigin);
588
+ type: type,
589
+ // optional id for message
590
+ id: id }), this.parentOrigin);
571
591
  };
572
592
  Copper.prototype._listenMessage = function () {
573
593
  var _this = this;
@@ -577,7 +597,7 @@ var Copper = /** @class */ (function () {
577
597
  }
578
598
  // if type is a deferred type, we resolve it
579
599
  // otherwise we do something else
580
- _this._resolveDeferred(event.data.type, event.data);
600
+ _this._resolveDeferred(event.data.type, event.data.id, event.data);
581
601
  // if event type exists, we pass the event to SDK
582
602
  // so sdk user can subscribe those events
583
603
  if (event.data.type) {
@@ -593,12 +613,17 @@ var Copper = /** @class */ (function () {
593
613
  createArrayWhenEmpty(this.deferredQueues, queueName);
594
614
  this.deferredQueues[queueName].push(deferred);
595
615
  };
596
- Copper.prototype._resolveDeferred = function (queueName, data) {
597
- if (!this.deferredQueues[queueName]) {
616
+ Copper.prototype._resolveDeferred = function (queueName, messageId, data) {
617
+ var queue = this.deferredQueues[queueName];
618
+ if (!queue) {
598
619
  return;
599
620
  }
600
- var deferred = this.deferredQueues[queueName].shift();
621
+ var foundIdx = queue.findIndex(function (d) {
622
+ return d.id === messageId;
623
+ });
624
+ var deferred = queue[foundIdx];
601
625
  if (deferred) {
626
+ queue.splice(foundIdx, 1);
602
627
  if (data.error) {
603
628
  return deferred.reject(data.error);
604
629
  }
@@ -637,17 +662,17 @@ var Copper = /** @class */ (function () {
637
662
  Copper.prototype._createDeferredMethod = function (queueName, executor) {
638
663
  var deferred = new Deferred();
639
664
  this._enqueueDeferred(queueName, deferred);
640
- executor();
665
+ executor(deferred);
641
666
  return deferred.promise;
642
667
  };
643
- Copper.prototype._deferredPost = function (name$$1, data) {
668
+ Copper.prototype._deferredPost = function (name, data) {
644
669
  var _this = this;
645
- return this._createDeferredMethod(name$$1, function () {
670
+ return this._createDeferredMethod(name, function (deferred) {
646
671
  if (data) {
647
- _this._postMessage(name$$1, data);
672
+ _this._postMessage(name, data, deferred.id);
648
673
  }
649
674
  else {
650
- _this._postMessage(name$$1);
675
+ _this._postMessage(name, {}, deferred.id);
651
676
  }
652
677
  });
653
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
- readonly promise: Promise<T>;
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/index.d.ts CHANGED
@@ -7,7 +7,7 @@ export default class Copper {
7
7
  private options;
8
8
  private _win;
9
9
  static init(): Copper;
10
- static readonly version: string;
10
+ static get version(): string;
11
11
  /**
12
12
  * store deferred queues by name
13
13
  */
@@ -23,7 +23,7 @@ export default class Copper {
23
23
  * @memberof Copper
24
24
  */
25
25
  constructor(parentOrigin: string, instanceId: string, options: ISdkInitOptions, _win?: any);
26
- readonly win: Window;
26
+ get win(): Window;
27
27
  getContext(): Promise<IContextData>;
28
28
  getUserInfo(): Promise<any>;
29
29
  getRouteInfo(): Promise<any>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "copper-sdk",
3
- "version": "0.6.3",
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",
@@ -50,11 +50,11 @@
50
50
  "homepage": "https://github.com/ProsperWorks/copper-sdk#readme",
51
51
  "devDependencies": {
52
52
  "@types/chai": "^4.1.7",
53
- "@types/mocha": "^5.2.5",
53
+ "@types/mocha": "^8.2.2",
54
54
  "@types/sinon": "^7.0.3",
55
55
  "chai": "^4.1.2",
56
- "concurrently": "^4.1.0",
57
- "cross-env": "^5.1.3",
56
+ "concurrently": "^6.5.1",
57
+ "cross-env": "^7.0.3",
58
58
  "gitbook-cli": "^2.3.2",
59
59
  "gitbook-plugin-anker-enable": "0.0.4",
60
60
  "gitbook-plugin-edit-link": "^2.0.2",
@@ -64,23 +64,22 @@
64
64
  "gitbook-plugin-prism-ext": "^3.1.0",
65
65
  "karma": "^3.1.4",
66
66
  "karma-chai": "^0.1.0",
67
- "karma-chrome-launcher": "^2.2.0",
68
- "karma-coverage-istanbul-reporter": "^2.0.4",
69
- "karma-mocha": "^1.3.0",
67
+ "karma-chrome-launcher": "^3.2.0",
68
+ "karma-coverage-istanbul-reporter": "^3.0.3",
69
+ "karma-mocha": "^2.0.1",
70
70
  "karma-mocha-reporter": "^2.2.5",
71
- "karma-rollup-preprocessor": "^6.1.2",
71
+ "karma-rollup-preprocessor": "^7.0.8",
72
72
  "karma-sinon": "^1.0.5",
73
- "mocha": "^5.0.1",
74
- "rimraf": "^2.6.3",
75
- "rollup": "0.68.2",
76
- "rollup-plugin-istanbul": "^2.0.1",
77
- "rollup-plugin-json": "^3.1.0",
73
+ "mocha": "^8.4.0",
74
+ "rimraf": "^3.0.2",
75
+ "rollup": "^1.32.1",
76
+ "rollup-plugin-istanbul": "^3.0.0",
77
+ "rollup-plugin-json": "^4.0.0",
78
78
  "rollup-plugin-replace": "^2.0.0",
79
- "rollup-plugin-typescript2": "^0.19.0",
79
+ "rollup-plugin-typescript2": "^0.30.0",
80
80
  "rollup-plugin-uglify": "^6.0.1",
81
- "sinon": "^7.2.2",
81
+ "sinon": "^13.0.2",
82
82
  "tslint": "^5.12.1",
83
83
  "typescript": "^3.2.2"
84
- },
85
- "dependencies": {}
84
+ }
86
85
  }
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
  }