iframe.io 0.0.2 → 0.0.6

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.
Files changed (2) hide show
  1. package/dist/index.js +50 -23
  2. package/package.json +6 -3
package/dist/index.js CHANGED
@@ -11,6 +11,9 @@ var __assign = (this && this.__assign) || function () {
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
13
  Object.defineProperty(exports, "__esModule", { value: true });
14
+ function newObject(data) {
15
+ return JSON.parse(JSON.stringify(data));
16
+ }
14
17
  var IFrameIO = /** @class */ (function () {
15
18
  function IFrameIO(options) {
16
19
  if (options && typeof options !== 'object')
@@ -45,21 +48,20 @@ var IFrameIO = /** @class */ (function () {
45
48
  || typeof data !== 'object'
46
49
  || !data.hasOwnProperty('_event'))
47
50
  return;
48
- var _b = data, _event = _b._event, payload = _b.payload;
51
+ var _b = data, _event = _b._event, payload = _b.payload, callback = _b.callback;
49
52
  _this.debug("[".concat(_this.peer.type, "] Message: ").concat(_event), payload || '');
50
53
  // Handshake or availability check events
51
- if (_event == 'pong')
52
- return;
53
- // Volatile event
54
- if (!_this.Events[_event])
55
- return _this.debug("[".concat(_this.peer.type, "] No <").concat(_event, "> listener defined"));
56
- // Trigger listeners
57
- _this.Events[_event].map(function (fn) { return fn(payload); });
58
- // Delete once event listeners
59
- delete _this.Events[_event + '--@once'];
54
+ if (_event == 'pong') {
55
+ // Content Window is connected to iframe
56
+ _this.trigger('connect');
57
+ return _this.debug("[".concat(_this.peer.type, "] connected"));
58
+ }
59
+ // Trigger available event listeners
60
+ _this.trigger(_event, payload, callback);
60
61
  }, false);
61
62
  this.debug("[".concat(this.peer.type, "] Initiate connection: IFrame origin <").concat(iframeOrigin, ">"));
62
63
  this.emit('ping');
64
+ return this;
63
65
  };
64
66
  IFrameIO.prototype.listen = function (hostOrigin) {
65
67
  // Listening to connection from the content window
@@ -84,19 +86,41 @@ var IFrameIO = /** @class */ (function () {
84
86
  // Origin different from handshaked source origin
85
87
  else if (origin !== _this.peer.origin)
86
88
  throw new Error('Invalid Origin');
87
- var _event = data._event, payload = data.payload;
89
+ var _event = data._event, payload = data.payload, callback = data.callback;
88
90
  _this.debug("[".concat(_this.peer.type, "] Message: ").concat(_event), payload || '');
89
91
  // Handshake or availability check events
90
- if (_event == 'ping')
91
- return _this.emit('pong');
92
- // Volatile event
93
- if (!_this.Events[_event])
94
- return _this.debug("[".concat(_this.peer.type, "] No <").concat(_event, "> listener defined"));
95
- // Trigger listeners
96
- _this.Events[_event].map(function (fn) { return fn(payload); });
97
- // Delete once event listeners
98
- delete _this.Events[_event + '--@once'];
92
+ if (_event == 'ping') {
93
+ _this.emit('pong');
94
+ // Iframe is connected to content window
95
+ _this.trigger('connect');
96
+ return _this.debug("[".concat(_this.peer.type, "] connected"));
97
+ }
98
+ // Trigger available event listeners
99
+ _this.trigger(_event, payload, callback);
99
100
  }, false);
101
+ return this;
102
+ };
103
+ IFrameIO.prototype.trigger = function (_event, payload, callback) {
104
+ var _this = this;
105
+ // Volatile event
106
+ if (!this.Events[_event]
107
+ && !this.Events[_event + '--@once'])
108
+ return this.debug("[".concat(this.peer.type, "] No <").concat(_event, "> listener defined"));
109
+ var callbackFn = callback ?
110
+ function (error, response) {
111
+ _this.emit(_event + '--@callback', { error: error, response: response });
112
+ return;
113
+ } : undefined;
114
+ // Trigger listeners
115
+ if (this.Events[_event + '--@once']) {
116
+ // Once triggable event
117
+ _event += '--@once';
118
+ this.Events[_event].map(function (fn) { return fn(payload, callbackFn); });
119
+ // Delete once event listeners after triggered
120
+ delete this.Events[_event];
121
+ }
122
+ else
123
+ this.Events[_event].map(function (fn) { return fn(payload, callbackFn); });
100
124
  };
101
125
  IFrameIO.prototype.emit = function (_event, payload, fn) {
102
126
  if (!this.peer.source)
@@ -105,10 +129,13 @@ var IFrameIO = /** @class */ (function () {
105
129
  fn = payload;
106
130
  payload = null;
107
131
  }
108
- this.peer.source.postMessage(JSON.parse(JSON.stringify({ _event: _event, payload: payload })), this.peer.origin);
109
132
  // Acknowledge/callback event listener
110
- if (typeof fn == 'function')
111
- this.once(_event, fn);
133
+ var hasCallback = false;
134
+ if (typeof fn == 'function') {
135
+ this.once(_event + '--@callback', fn);
136
+ hasCallback = true;
137
+ }
138
+ this.peer.source.postMessage(newObject({ _event: _event, payload: payload, callback: hasCallback }), this.peer.origin);
112
139
  return this;
113
140
  };
114
141
  IFrameIO.prototype.on = function (_event, fn) {
package/package.json CHANGED
@@ -1,12 +1,15 @@
1
1
  {
2
2
  "name": "iframe.io",
3
- "version": "0.0.2",
3
+ "version": "0.0.6",
4
4
  "description": "Easy and friendly API to connect and interact between content window and its containing iframe",
5
5
  "main": "./dist/index.js",
6
- "type": "commonjs",
7
6
  "types": "./src/index.d.ts",
8
7
  "exports": {
9
- "require": "./dist/index.js"
8
+ ".": {
9
+ "import": "./dist/index.js",
10
+ "require": "./dist/index.js",
11
+ "types": "./src/index.d.ts"
12
+ }
10
13
  },
11
14
  "private": false,
12
15
  "scripts": {