iframe.io 1.0.1 → 1.0.3

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/dist/index.d.ts CHANGED
@@ -3,7 +3,6 @@ export declare type CallbackFunction = (error: boolean | string, ...args: any[])
3
3
  export declare type Listener = (payload?: any, callback?: CallbackFunction) => void;
4
4
  export declare type Options = {
5
5
  type?: PeerType;
6
- debug?: boolean;
7
6
  };
8
7
  export interface RegisteredEvents {
9
8
  [index: string]: Listener[];
@@ -16,7 +15,7 @@ export declare type Peer = {
16
15
  export declare type MessageData = {
17
16
  _event: string;
18
17
  payload: any;
19
- callback: boolean;
18
+ cid: boolean;
20
19
  };
21
20
  export declare type Message = {
22
21
  origin: string;
@@ -31,7 +30,7 @@ export default class IOF {
31
30
  debug(...args: any[]): void;
32
31
  initiate(contentWindow: MessageEventSource, iframeOrigin: string): this;
33
32
  listen(hostOrigin?: string): this;
34
- fire(_event: string, payload?: MessageData['payload'], callback?: boolean): void;
33
+ fire(_event: string, payload?: MessageData['payload'], cid?: boolean): void;
35
34
  emit(_event: string, payload?: MessageData['payload'], fn?: CallbackFunction): this;
36
35
  on(_event: string, fn: Listener): this;
37
36
  once(_event: string, fn: Listener): this;
package/dist/index.js CHANGED
@@ -23,6 +23,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
23
23
  function newObject(data) {
24
24
  return JSON.parse(JSON.stringify(data));
25
25
  }
26
+ var callbackId = function () {
27
+ var rmin = 10, rmax = 9999;
28
+ return Date.now() + String(Math.floor(Math.random() * (rmax - rmin + 1) + (rmin + 1)));
29
+ };
26
30
  var IOF = /** @class */ (function () {
27
31
  function IOF(options) {
28
32
  if (options && typeof options !== 'object')
@@ -38,7 +42,7 @@ var IOF = /** @class */ (function () {
38
42
  for (var _i = 0; _i < arguments.length; _i++) {
39
43
  args[_i] = arguments[_i];
40
44
  }
41
- this.options && this.options.debug && console.log.apply(console, args);
45
+ console.debug.apply(console, args);
42
46
  };
43
47
  IOF.prototype.initiate = function (contentWindow, iframeOrigin) {
44
48
  var _this = this;
@@ -57,7 +61,7 @@ var IOF = /** @class */ (function () {
57
61
  || typeof data !== 'object'
58
62
  || !data.hasOwnProperty('_event'))
59
63
  return;
60
- var _b = data, _event = _b._event, payload = _b.payload, callback = _b.callback;
64
+ var _b = data, _event = _b._event, payload = _b.payload, cid = _b.cid;
61
65
  _this.debug("[".concat(_this.peer.type, "] Message: ").concat(_event), payload || '');
62
66
  // Handshake or availability check events
63
67
  if (_event == 'pong') {
@@ -66,7 +70,7 @@ var IOF = /** @class */ (function () {
66
70
  return _this.debug("[".concat(_this.peer.type, "] connected"));
67
71
  }
68
72
  // Fire available event listeners
69
- _this.fire(_event, payload, callback);
73
+ _this.fire(_event, payload, cid);
70
74
  }, false);
71
75
  this.debug("[".concat(this.peer.type, "] Initiate connection: IFrame origin <").concat(iframeOrigin, ">"));
72
76
  this.emit('ping');
@@ -95,7 +99,7 @@ var IOF = /** @class */ (function () {
95
99
  // Origin different from handshaked source origin
96
100
  else if (origin !== _this.peer.origin)
97
101
  throw new Error('Invalid Origin');
98
- var _event = data._event, payload = data.payload, callback = data.callback;
102
+ var _event = data._event, payload = data.payload, cid = data.cid;
99
103
  _this.debug("[".concat(_this.peer.type, "] Message: ").concat(_event), payload || '');
100
104
  // Handshake or availability check events
101
105
  if (_event == 'ping') {
@@ -105,23 +109,23 @@ var IOF = /** @class */ (function () {
105
109
  return _this.debug("[".concat(_this.peer.type, "] connected"));
106
110
  }
107
111
  // Fire available event listeners
108
- _this.fire(_event, payload, callback);
112
+ _this.fire(_event, payload, cid);
109
113
  }, false);
110
114
  return this;
111
115
  };
112
- IOF.prototype.fire = function (_event, payload, callback) {
116
+ IOF.prototype.fire = function (_event, payload, cid) {
113
117
  var _this = this;
114
118
  // Volatile event
115
119
  if (!this.Events[_event]
116
120
  && !this.Events[_event + '--@once'])
117
121
  return this.debug("[".concat(this.peer.type, "] No <").concat(_event, "> listener defined"));
118
- var callbackFn = callback ?
122
+ var callbackFn = cid ?
119
123
  function (error) {
120
124
  var args = [];
121
125
  for (var _i = 1; _i < arguments.length; _i++) {
122
126
  args[_i - 1] = arguments[_i];
123
127
  }
124
- _this.emit(_event + '--@callback', { error: error || false, args: args });
128
+ _this.emit("".concat(_event, "--").concat(cid, "--@callback"), { error: error || false, args: args });
125
129
  return;
126
130
  } : undefined;
127
131
  var listeners = [];
@@ -135,26 +139,26 @@ var IOF = /** @class */ (function () {
135
139
  else
136
140
  listeners = this.Events[_event];
137
141
  // Fire listeners
138
- listeners.map(function (fn) { return payload ? fn(payload, callbackFn) : fn(callbackFn); });
142
+ listeners.map(function (fn) { return payload !== undefined ? fn(payload, callbackFn) : fn(callbackFn); });
139
143
  };
140
144
  IOF.prototype.emit = function (_event, payload, fn) {
141
145
  if (!this.peer.source)
142
146
  throw new Error('No Connection initiated');
143
147
  if (typeof payload == 'function') {
144
148
  fn = payload;
145
- payload = null;
149
+ payload = undefined;
146
150
  }
147
151
  // Acknowledge/callback event listener
148
- var hasCallback = false;
152
+ var cid;
149
153
  if (typeof fn === 'function') {
150
154
  var callbackFunction_1 = fn;
151
- this.once(_event + '--@callback', function (_a) {
155
+ cid = callbackId();
156
+ this.once("".concat(_event, "--").concat(cid, "--@callback"), function (_a) {
152
157
  var error = _a.error, args = _a.args;
153
158
  return callbackFunction_1.apply(void 0, __spreadArray([error], args, false));
154
159
  });
155
- hasCallback = true;
156
160
  }
157
- this.peer.source.postMessage(newObject({ _event: _event, payload: payload, callback: hasCallback }), this.peer.origin);
161
+ this.peer.source.postMessage(newObject({ _event: _event, payload: payload, cid: cid }), this.peer.origin);
158
162
  return this;
159
163
  };
160
164
  IOF.prototype.on = function (_event, fn) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "iframe.io",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
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
6
  "types": "./dist/index.d.ts",
package/src/index.ts CHANGED
@@ -6,7 +6,7 @@ export type Listener = ( payload?: any, callback?: CallbackFunction ) => void
6
6
 
7
7
  export type Options = {
8
8
  type?: PeerType
9
- debug?: boolean
9
+ // debug?: boolean
10
10
  }
11
11
 
12
12
  export interface RegisteredEvents {
@@ -22,7 +22,7 @@ export type Peer = {
22
22
  export type MessageData = {
23
23
  _event: string
24
24
  payload: any
25
- callback: boolean
25
+ cid: boolean
26
26
  }
27
27
 
28
28
  export type Message = {
@@ -35,14 +35,17 @@ function newObject( data: object ){
35
35
  return JSON.parse( JSON.stringify( data ) )
36
36
  }
37
37
 
38
- export default class IOF {
38
+ const callbackId = () => {
39
+ const rmin = 10, rmax = 9999
40
+ return Date.now() + String( Math.floor( Math.random() * ( rmax - rmin + 1 )+( rmin + 1 ) ) )
41
+ }
39
42
 
43
+ export default class IOF {
40
44
  Events: RegisteredEvents
41
45
  peer: Peer
42
46
  options: Options
43
47
 
44
48
  constructor( options: Options ){
45
-
46
49
  if( options && typeof options !== 'object' )
47
50
  throw new Error('Invalid Options')
48
51
 
@@ -54,7 +57,7 @@ export default class IOF {
54
57
  this.peer.type = options.type.toUpperCase() as PeerType
55
58
  }
56
59
 
57
- debug( ...args: any[] ){ this.options && this.options.debug && console.log( ...args ) }
60
+ debug( ...args: any[] ){ console.debug( ...args ) }
58
61
 
59
62
  initiate( contentWindow: MessageEventSource, iframeOrigin: string ){
60
63
  // Establish a connection with an iframe containing in the current window
@@ -74,7 +77,7 @@ export default class IOF {
74
77
  || typeof data !== 'object'
75
78
  || !data.hasOwnProperty('_event') ) return
76
79
 
77
- const { _event, payload, callback } = data as Message['data']
80
+ const { _event, payload, cid } = data as Message['data']
78
81
  this.debug( `[${this.peer.type}] Message: ${_event}`, payload || '' )
79
82
 
80
83
  // Handshake or availability check events
@@ -85,7 +88,7 @@ export default class IOF {
85
88
  }
86
89
 
87
90
  // Fire available event listeners
88
- this.fire( _event, payload, callback )
91
+ this.fire( _event, payload, cid )
89
92
  }, false )
90
93
 
91
94
  this.debug(`[${this.peer.type}] Initiate connection: IFrame origin <${iframeOrigin}>`)
@@ -120,7 +123,7 @@ export default class IOF {
120
123
  else if( origin !== this.peer.origin )
121
124
  throw new Error('Invalid Origin')
122
125
 
123
- const { _event, payload, callback } = data
126
+ const { _event, payload, cid } = data
124
127
  this.debug( `[${this.peer.type}] Message: ${_event}`, payload || '' )
125
128
 
126
129
  // Handshake or availability check events
@@ -133,21 +136,21 @@ export default class IOF {
133
136
  }
134
137
 
135
138
  // Fire available event listeners
136
- this.fire( _event, payload, callback )
139
+ this.fire( _event, payload, cid )
137
140
  }, false )
138
141
 
139
142
  return this
140
143
  }
141
144
 
142
- fire( _event: string, payload?: MessageData['payload'], callback?: boolean ){
145
+ fire( _event: string, payload?: MessageData['payload'], cid?: boolean ){
143
146
  // Volatile event
144
147
  if( !this.Events[ _event ]
145
148
  && !this.Events[ _event +'--@once'] )
146
149
  return this.debug(`[${this.peer.type}] No <${_event}> listener defined`)
147
150
 
148
- const callbackFn = callback ?
151
+ const callbackFn = cid ?
149
152
  ( error: boolean | string, ...args: any[] ): void => {
150
- this.emit( _event +'--@callback', { error: error || false, args } )
153
+ this.emit(`${_event}--${cid}--@callback`, { error: error || false, args } )
151
154
  return
152
155
  } : undefined
153
156
  let listeners: Listener[] = []
@@ -162,7 +165,7 @@ export default class IOF {
162
165
  else listeners = this.Events[ _event ]
163
166
 
164
167
  // Fire listeners
165
- listeners.map( fn => payload ? fn( payload, callbackFn ) : fn( callbackFn ) )
168
+ listeners.map( fn => payload !== undefined ? fn( payload, callbackFn ) : fn( callbackFn ) )
166
169
  }
167
170
 
168
171
  emit( _event: string, payload?: MessageData['payload'], fn?: CallbackFunction ){
@@ -172,19 +175,19 @@ export default class IOF {
172
175
 
173
176
  if( typeof payload == 'function' ){
174
177
  fn = payload
175
- payload = null
178
+ payload = undefined
176
179
  }
177
180
 
178
181
  // Acknowledge/callback event listener
179
- let hasCallback = false
182
+ let cid
180
183
  if( typeof fn === 'function' ){
181
184
  const callbackFunction = fn
182
185
 
183
- this.once( _event +'--@callback', ({ error, args }) => callbackFunction( error, ...args ) )
184
- hasCallback = true
186
+ cid = callbackId()
187
+ this.once(`${_event}--${cid}--@callback`, ({ error, args }) => callbackFunction( error, ...args ) )
185
188
  }
186
189
 
187
- this.peer.source.postMessage( newObject({ _event, payload, callback: hasCallback }), this.peer.origin as string )
190
+ this.peer.source.postMessage( newObject({ _event, payload, cid }), this.peer.origin as string )
188
191
 
189
192
  return this
190
193
  }