@wcstack/midi 4.16.0 → 4.17.0

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
@@ -190,7 +190,9 @@ declare function getConfig(): IConfig;
190
190
  * Nothing starts on construction. `requestMIDIAccess()` can raise a permission
191
191
  * prompt, so it is deliberately command-driven (`request()`), matching the
192
192
  * `idle` / Generic Sensor family. `observe({ auto: true })` opts back into
193
- * requesting immediately.
193
+ * requesting immediately, and `observe()` after `dispose()` acquires again the
194
+ * access the last `request()` got, when no prompt can appear (an element taken
195
+ * out of the document and put back).
194
196
  *
195
197
  * The `MIDIAccess` handle itself is never published: the Core owns it and drops
196
198
  * it in `close()` / `dispose()`, as `worker` / `websocket` / `broadcast` do with
@@ -210,6 +212,8 @@ declare class MidiCore extends EventTarget {
210
212
  private _errorInfo;
211
213
  private _permissionStatus;
212
214
  private _gen;
215
+ private _pendingGen;
216
+ private _grantedSysex;
213
217
  private _ready;
214
218
  constructor(options?: IMidiOptions | null, target?: EventTarget);
215
219
  get message(): IMidiMessage | null;
@@ -230,9 +234,12 @@ declare class MidiCore extends EventTarget {
230
234
  /** Resolves once the current (or initial) access attempt settles. */
231
235
  get ready(): Promise<void>;
232
236
  /**
233
- * Store the options and, when `auto` is set, request access. Idempotent:
234
- * calling it again with new options re-applies port selection to the live
235
- * access without re-requesting.
237
+ * Store the options and decide whether to request access: when `auto` is set,
238
+ * or when `dispose()` dropped an access the last `request()` acquired and its
239
+ * permission is still granted (so no prompt can appear). A missing API is
240
+ * reported as `unsupported` here, `auto` or not. Idempotent: calling it again
241
+ * with new options re-applies port selection to the live access without
242
+ * re-requesting, and it starts nothing while a request is in flight.
236
243
  */
237
244
  observe(options: IMidiOptions): Promise<void>;
238
245
  /**
@@ -245,9 +252,11 @@ declare class MidiCore extends EventTarget {
245
252
  * `permission` / `error` state (async-io-node-guidelines.md §3.6).
246
253
  */
247
254
  request(): Promise<void>;
255
+ private _request;
248
256
  /**
249
257
  * Release the access handle and detach every listener, keeping the observed
250
- * permission state. A later `request()` re-acquires.
258
+ * permission state. `connected` goes to `false` and `devices` to `[]`, both
259
+ * published. A later `request()` re-acquires; `observe()` does not by itself.
251
260
  */
252
261
  close(): void;
253
262
  /**
@@ -265,6 +274,9 @@ declare class MidiCore extends EventTarget {
265
274
  * long as the ports are alive.
266
275
  */
267
276
  dispose(): void;
277
+ private _release;
278
+ private _reacquire;
279
+ private _queryPermission;
268
280
  private _resolveApi;
269
281
  private _messageOf;
270
282
  private _probePermission;
@@ -278,6 +290,7 @@ declare class MidiCore extends EventTarget {
278
290
  private _onMidiMessage;
279
291
  private _publishMessage;
280
292
  private _publishDevices;
293
+ private _setDevices;
281
294
  private _snapshotDevices;
282
295
  private _sameDevices;
283
296
  private _setConnected;
@@ -289,9 +302,13 @@ declare class MidiCore extends EventTarget {
289
302
  /**
290
303
  * `<wcs-midi>` — declarative Web MIDI input/output.
291
304
  *
292
- * Nothing happens on connect: `requestMIDIAccess()` can raise a permission
293
- * prompt, so access is command-driven (`command.request`). Add the `auto`
294
- * attribute to opt into requesting as soon as the element connects.
305
+ * Nothing happens on a first connect: `requestMIDIAccess()` can raise a
306
+ * permission prompt, so access is command-driven (`command.request`). Add the
307
+ * `auto` attribute to opt into requesting as soon as the element connects. An
308
+ * element whose last `request()` got access (and not `close()`d since), taken
309
+ * out and put back (a route left and entered again), requests again by itself,
310
+ * only while the permission is still granted, so no prompt can appear. Where
311
+ * the API is absent, `unsupported` is reported at connect, `auto` or not.
295
312
  */
296
313
  declare class WcsMidi extends HTMLElement {
297
314
  static hasConnectedCallbackPromise: boolean;
package/dist/index.esm.js CHANGED
@@ -58,7 +58,7 @@ function setConfig(partialConfig) {
58
58
  frozenConfig = null;
59
59
  }
60
60
 
61
- var pkg = {"name":"@wcstack/midi","version":"4.16.0"};
61
+ var pkg = {"name":"@wcstack/midi","version":"4.17.0"};
62
62
 
63
63
  /**
64
64
  * midiCapabilities.ts
@@ -205,7 +205,9 @@ const messageField = (key) => (event) => detailOf(event)?.[key] ?? null;
205
205
  * Nothing starts on construction. `requestMIDIAccess()` can raise a permission
206
206
  * prompt, so it is deliberately command-driven (`request()`), matching the
207
207
  * `idle` / Generic Sensor family. `observe({ auto: true })` opts back into
208
- * requesting immediately.
208
+ * requesting immediately, and `observe()` after `dispose()` acquires again the
209
+ * access the last `request()` got, when no prompt can appear (an element taken
210
+ * out of the document and put back).
209
211
  *
210
212
  * The `MIDIAccess` handle itself is never published: the Core owns it and drops
211
213
  * it in `close()` / `dispose()`, as `worker` / `websocket` / `broadcast` do with
@@ -257,6 +259,18 @@ class MidiCore extends EventTarget {
257
259
  // rapid disconnect→reconnect (or a close() mid-flight) never hooks listeners
258
260
  // onto a torn-down instance.
259
261
  _gen = 0;
262
+ // The generation of the request in flight: observe() starts none while it is
263
+ // the current one (a request() before the connect-time decision took it,
264
+ // timing-and-firing-contract §23.3).
265
+ _pendingGen = -1;
266
+ // The sysex of the access the last request() acquired; null before one, after
267
+ // a refused one and after close(). dispose() keeps it: observe() acquires that
268
+ // access again when its permission is still granted (an element taken out of
269
+ // the document and put back, as a route left and entered again does). Only the
270
+ // command's success counts: an access `auto` acquired is `auto`'s to decide on
271
+ // the next connect, so an `auto` the page turns off there still requests
272
+ // nothing. A refused request, `auto`'s included, forgets it.
273
+ _grantedSysex = null;
260
274
  _ready = Promise.resolve();
261
275
  constructor(options, target) {
262
276
  super();
@@ -284,15 +298,24 @@ class MidiCore extends EventTarget {
284
298
  get ready() { return this._ready; }
285
299
  // --- Public API ---
286
300
  /**
287
- * Store the options and, when `auto` is set, request access. Idempotent:
288
- * calling it again with new options re-applies port selection to the live
289
- * access without re-requesting.
301
+ * Store the options and decide whether to request access: when `auto` is set,
302
+ * or when `dispose()` dropped an access the last `request()` acquired and its
303
+ * permission is still granted (so no prompt can appear). A missing API is
304
+ * reported as `unsupported` here, `auto` or not. Idempotent: calling it again
305
+ * with new options re-applies port selection to the live access without
306
+ * re-requesting, and it starts nothing while a request is in flight.
290
307
  */
291
308
  observe(options) {
292
- const wasIdle = this._access === null;
293
309
  this.setOptions(options);
294
- if (options.auto && wasIdle) {
295
- this._ready = this.request();
310
+ if (this._access !== null || this._pendingGen === this._gen)
311
+ return this._ready;
312
+ if (options.auto || this._resolveApi() === null) {
313
+ // _request() reports a missing API at once: no prompt is involved, so the
314
+ // page learns it at connect rather than at its first request()
315
+ this._ready = this._request(false);
316
+ }
317
+ else if (this._grantedSysex !== null) {
318
+ this._ready = this._reacquire();
296
319
  }
297
320
  return this._ready;
298
321
  }
@@ -310,6 +333,11 @@ class MidiCore extends EventTarget {
310
333
  * `permission` / `error` state (async-io-node-guidelines.md §3.6).
311
334
  */
312
335
  request() {
336
+ return this._request(true);
337
+ }
338
+ // `remember`: an access the command acquired is what observe() acquires again
339
+ // after dispose(); one `auto` acquired is not. Any refusal forgets it.
340
+ _request(remember) {
313
341
  const requestAccess = this._resolveApi();
314
342
  if (!requestAccess) {
315
343
  this._setPermission(UNSUPPORTED);
@@ -318,10 +346,15 @@ class MidiCore extends EventTarget {
318
346
  return this._ready;
319
347
  }
320
348
  const gen = ++this._gen;
349
+ this._pendingGen = gen;
350
+ const sysex = this._options.sysex === true;
321
351
  this._probePermission();
322
- this._ready = requestAccess({ sysex: this._options.sysex === true }).then((access) => {
352
+ this._ready = requestAccess({ sysex }).then((access) => {
323
353
  if (gen !== this._gen)
324
354
  return;
355
+ this._pendingGen = -1;
356
+ if (remember)
357
+ this._grantedSysex = sysex;
325
358
  this._access = access;
326
359
  access.onstatechange = this._onStateChange;
327
360
  this._setError(null);
@@ -332,6 +365,8 @@ class MidiCore extends EventTarget {
332
365
  }, (error) => {
333
366
  if (gen !== this._gen)
334
367
  return;
368
+ this._pendingGen = -1;
369
+ this._grantedSysex = null;
335
370
  this._setPermission("denied");
336
371
  this._setError(this._messageOf(error), error?.name);
337
372
  });
@@ -339,16 +374,12 @@ class MidiCore extends EventTarget {
339
374
  }
340
375
  /**
341
376
  * Release the access handle and detach every listener, keeping the observed
342
- * permission state. A later `request()` re-acquires.
377
+ * permission state. `connected` goes to `false` and `devices` to `[]`, both
378
+ * published. A later `request()` re-acquires; `observe()` does not by itself.
343
379
  */
344
380
  close() {
345
- this._gen++;
346
- this._unhookInputs();
347
- if (this._access) {
348
- this._access.onstatechange = null;
349
- this._access = null;
350
- }
351
- this._setConnected(false);
381
+ this._release();
382
+ this._grantedSysex = null;
352
383
  }
353
384
  /**
354
385
  * Send a raw MIDI message to the selected output port (or the first available
@@ -375,12 +406,63 @@ class MidiCore extends EventTarget {
375
406
  * long as the ports are alive.
376
407
  */
377
408
  dispose() {
378
- this.close();
409
+ // Unlike close(), keeps what the last request() acquired: a later observe()
410
+ // acquires it again when its permission is still granted.
411
+ this._release();
379
412
  if (this._permissionStatus) {
380
413
  this._permissionStatus.removeEventListener("change", this._onPermissionChange);
381
414
  this._permissionStatus = null;
382
415
  }
383
416
  }
417
+ // --- Internal: access ---
418
+ // A disconnect publishes what it changes (timing-and-firing-contract §22).
419
+ // Without the access the ports are no longer tracked, so `devices` is emptied
420
+ // with its event: a page put back must not list a keyboard it cannot hear.
421
+ _release() {
422
+ this._gen++;
423
+ this._unhookInputs();
424
+ if (this._access) {
425
+ this._access.onstatechange = null;
426
+ this._access = null;
427
+ }
428
+ this._setConnected(false);
429
+ this._setDevices([]);
430
+ }
431
+ // observe() after dispose(): request again only when the permission for the
432
+ // sysex asked now is granted, so no prompt can appear. A request(), close()
433
+ // or dispose() while the answer is pending takes the decision (_gen).
434
+ //
435
+ // Where the browser answers, its answer decides, and one that is not
436
+ // `granted` is published: the element learned that the grant went away (a
437
+ // page reading `granted` with `connected: false` could not tell it must ask
438
+ // again). Where it does not, the outcome last inferred from request() stands,
439
+ // and only for the sysex it was inferred at (a sysex grant is a separate
440
+ // permission); nothing new is known, so nothing is published.
441
+ _reacquire() {
442
+ const gen = this._gen;
443
+ const sysex = this._options.sysex === true;
444
+ return this._queryPermission(sysex).then((answer) => {
445
+ if (gen !== this._gen)
446
+ return;
447
+ if (answer === null) {
448
+ if (this._permission === "granted" && this._grantedSysex === sysex)
449
+ return this.request();
450
+ return;
451
+ }
452
+ if (answer === "granted")
453
+ return this.request();
454
+ this._setPermission(answer);
455
+ });
456
+ }
457
+ // The Permissions API's answer for midi at this sysex; null where the browser
458
+ // gives none (no Permissions API, or the descriptor is not understood).
459
+ _queryPermission(sysex) {
460
+ // Only reached after _resolveApi() found navigator.requestMIDIAccess.
461
+ const permissions = globalThis.navigator.permissions;
462
+ if (!permissions || typeof permissions.query !== "function")
463
+ return Promise.resolve(null);
464
+ return permissions.query({ name: "midi", sysex }).then((status) => status.state, () => null);
465
+ }
384
466
  // --- Internal: API resolution ---
385
467
  // Resolved at call time, never cached: keeps the unsupported branch honest and
386
468
  // lets tests swap the global (async-io-node-guidelines.md §3.7). Read off
@@ -502,7 +584,9 @@ class MidiCore extends EventTarget {
502
584
  this._target.dispatchEvent(new CustomEvent("wcs-midi:message", { detail: message, bubbles: true }));
503
585
  }
504
586
  _publishDevices(access) {
505
- const next = this._snapshotDevices(access);
587
+ this._setDevices(this._snapshotDevices(access));
588
+ }
589
+ _setDevices(next) {
506
590
  if (this._sameDevices(next))
507
591
  return;
508
592
  // Fresh array assigned before notifying (producer snapshot contract).
@@ -683,9 +767,13 @@ function reflectBooleanAttribute(el, name, value) {
683
767
  /**
684
768
  * `<wcs-midi>` — declarative Web MIDI input/output.
685
769
  *
686
- * Nothing happens on connect: `requestMIDIAccess()` can raise a permission
687
- * prompt, so access is command-driven (`command.request`). Add the `auto`
688
- * attribute to opt into requesting as soon as the element connects.
770
+ * Nothing happens on a first connect: `requestMIDIAccess()` can raise a
771
+ * permission prompt, so access is command-driven (`command.request`). Add the
772
+ * `auto` attribute to opt into requesting as soon as the element connects. An
773
+ * element whose last `request()` got access (and not `close()`d since), taken
774
+ * out and put back (a route left and entered again), requests again by itself,
775
+ * only while the permission is still granted, so no prompt can appear. Where
776
+ * the API is absent, `unsupported` is reported at connect, `auto` or not.
689
777
  */
690
778
  class WcsMidi extends HTMLElement {
691
779
  static hasConnectedCallbackPromise = true;
@@ -817,14 +905,17 @@ class WcsMidi extends HTMLElement {
817
905
  // --- Commands ---
818
906
  request() {
819
907
  // 要求の sysex は属性が正本。接続時の判断(マイクロタスク)が Core へ渡すより先の
820
- // request() でも、接続の後に sysex が変わった後でも、いまの属性で要求するよう渡しておく
908
+ // request() でも、接続の後に sysex が変わった後でも、いまの属性で要求するよう渡しておく。
909
+ // 判断の前の request() は判断を引き取る — 要求中の Core に observe() は要求を重ねない
910
+ // (timing-and-firing-contract §23.3)
821
911
  this._core.setOptions(this._options());
822
912
  return this._core.request();
823
913
  }
824
914
  close() {
825
915
  // 接続時の判断待ちなら、その auto の要求を取り消す — 同じタスクの append → close() で、
826
916
  // 後のマイクロタスクが要求しないように(判断が同期だった頃は、始めた要求を close() が
827
- // 捨てていた)。入れ直せば、次の判断は auto に従う
917
+ // 捨てていた)。入れ直せば、次の判断は auto に従う。Core の close() は最後の request()
918
+ // が取ったアクセスも忘れるので、入れ直しでも取り直さない(明示の解放)
828
919
  if (this._connectResolve !== null)
829
920
  this._autoCancelled = true;
830
921
  this._core.close();
@@ -845,7 +936,10 @@ class WcsMidi extends HTMLElement {
845
936
  // channel)で 1 回だけ行う(timing-and-firing-contract §23)。router は遷移で同じノードを
846
937
  // 入れ直し、新しいルートの params から state が入力を書くのはマイクロタスク — その場で
847
938
  // 判断すると、前の auto / sysex で requestMIDIAccess を起こしていた(docs/seam-apps.md、
848
- // shift-desk)。
939
+ // shift-desk)。判断は Core の observe() が下す: auto なら要求し、auto でなくても、最後の
940
+ // request() でアクセスを取れていた要素は、権限がまだ granted なら取り直す(プロンプトは
941
+ // 出ない。ルートを出て戻った要素が MIDI を失っていた — 音楽室の練習用シンセの継ぎ目アプリ)。
942
+ // API が無ければ auto でなくても unsupported を報告する。
849
943
  _scheduleRequest() {
850
944
  if (this._requestPending)
851
945
  return;
@@ -882,6 +976,8 @@ class WcsMidi extends HTMLElement {
882
976
  this._connectResolve?.();
883
977
  this._connectResolve = null;
884
978
  this._autoCancelled = false;
979
+ // connected: false と devices: [] を知らせる(§22)。最後の request() が取ったアクセスは
980
+ // Core が覚えていて、入れ直しの判断が取り直す
885
981
  this._core.dispose();
886
982
  }
887
983
  attributeChangedCallback(_name, oldValue, newValue) {