@etherplay/connect 0.10.0 → 0.11.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/src/popup.ts CHANGED
@@ -229,10 +229,30 @@ export function createPopupLauncher<T>() {
229
229
  }, 200);
230
230
  }
231
231
 
232
+ // The window this launch opened, so `cancel` closes ITS popup rather than whichever is
233
+ // current: a second launch replaces `currentPopup` while an older promise may still be held.
234
+ let launchedWindow: Window | undefined;
235
+
232
236
  const store = {
233
237
  subscribe: _store.subscribe,
238
+ /**
239
+ * Close the popup and SETTLE the promise.
240
+ *
241
+ * This used to be an empty `TODO`, which meant `connection.cancel()` returned the store to
242
+ * `Idle` and left the promise `connect()` was awaiting pending for good: an app doing
243
+ * `await connection.connect({type: 'email'})` and then offering a cancel button waited
244
+ * forever, holding the popup window open behind it.
245
+ *
246
+ * Rejects with `type: 'cancelation'`, which is what `connect` already reads to tell a
247
+ * cancellation (nothing to report) from a refusal (a reason the app must surface).
248
+ */
234
249
  cancel() {
235
- // TODO
250
+ try {
251
+ launchedWindow?.close();
252
+ } catch (err) {
253
+ console.error(err);
254
+ }
255
+ rejectRecovery({message: 'popup cancelled', type: 'cancelation'});
236
256
  },
237
257
  };
238
258
 
@@ -253,6 +273,7 @@ export function createPopupLauncher<T>() {
253
273
  if (!popup) {
254
274
  throw new Error(`could not open the login popup`);
255
275
  }
276
+ launchedWindow = popup;
256
277
  currentPopup = {popup, onMessage, rejectRecovery: _rejectRecovery};
257
278
  console.log(`listening to message... ${id}`);
258
279
  window.addEventListener('message', currentPopup.onMessage);