@sound2score/player 0.0.3 → 0.0.4
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/index.js +115 -8
- package/package.json +5 -6
- package/skills/sound2score-player/SKILL.md +165 -0
- package/{src → types/src}/embed-frame.d.ts +2 -2
- package/{src → types/src}/fake-embed-frame.d.ts +1 -1
- package/{src → types/src}/iframe-embed-frame.d.ts +2 -2
- package/{src → types/src}/player.d.ts +2 -2
- package/vendor/core/LICENSE +0 -21
- package/vendor/core/disposable/index.js +0 -96
- package/vendor/core/events/index.js +0 -75
- /package/{index.d.ts → types/index.d.ts} +0 -0
- /package/{vendor → types/packages}/core/disposable/async-disposer.d.ts +0 -0
- /package/{vendor → types/packages}/core/disposable/async-resource.d.ts +0 -0
- /package/{vendor → types/packages}/core/disposable/disposables.d.ts +0 -0
- /package/{vendor → types/packages}/core/disposable/disposer.d.ts +0 -0
- /package/{vendor → types/packages}/core/disposable/index.d.ts +0 -0
- /package/{vendor → types/packages}/core/disposable/resource.d.ts +0 -0
- /package/{vendor → types/packages}/core/events/dispatcher.d.ts +0 -0
- /package/{vendor → types/packages}/core/events/eventful.d.ts +0 -0
- /package/{vendor → types/packages}/core/events/events.d.ts +0 -0
- /package/{vendor → types/packages}/core/events/index.d.ts +0 -0
- /package/{vendor → types/packages}/core/events/noop-events.d.ts +0 -0
- /package/{src → types/src}/embed-message-reader.d.ts +0 -0
- /package/{src → types/src}/embed-options-codec.d.ts +0 -0
- /package/{src → types/src}/embed-options.d.ts +0 -0
- /package/{src → types/src}/embed-url-builder.d.ts +0 -0
- /package/{src → types/src}/protocol.d.ts +0 -0
package/index.js
CHANGED
|
@@ -334,9 +334,69 @@ class EmbedUrlBuilder {
|
|
|
334
334
|
return query ? `${path}?${query}` : path;
|
|
335
335
|
}
|
|
336
336
|
}
|
|
337
|
+
// packages/core/events/dispatcher.ts
|
|
338
|
+
class Dispatcher {
|
|
339
|
+
events = this;
|
|
340
|
+
scopedEntries = new Map;
|
|
341
|
+
universalEntries = [];
|
|
342
|
+
nextId = 0;
|
|
343
|
+
nextOrder = 0;
|
|
344
|
+
on(type, listener, opts) {
|
|
345
|
+
const id = this.nextId++;
|
|
346
|
+
const entries = this.scopedEntries.get(type) ?? [];
|
|
347
|
+
entries.push({
|
|
348
|
+
kind: "scoped",
|
|
349
|
+
listener,
|
|
350
|
+
once: opts?.once ?? false,
|
|
351
|
+
id,
|
|
352
|
+
order: this.nextOrder++
|
|
353
|
+
});
|
|
354
|
+
this.scopedEntries.set(type, entries);
|
|
355
|
+
return () => this.off(id);
|
|
356
|
+
}
|
|
357
|
+
all(typeListener, opts) {
|
|
358
|
+
const id = this.nextId++;
|
|
359
|
+
this.universalEntries.push({
|
|
360
|
+
kind: "universal",
|
|
361
|
+
typeListener,
|
|
362
|
+
once: opts?.once ?? false,
|
|
363
|
+
id,
|
|
364
|
+
order: this.nextOrder++
|
|
365
|
+
});
|
|
366
|
+
return () => this.off(id);
|
|
367
|
+
}
|
|
368
|
+
dispatch(type, ...args) {
|
|
369
|
+
const entries = [
|
|
370
|
+
...this.scopedEntries.get(type) ?? [],
|
|
371
|
+
...this.universalEntries
|
|
372
|
+
].sort((left, right) => left.order - right.order);
|
|
373
|
+
const event = args[0];
|
|
374
|
+
for (const entry of entries) {
|
|
375
|
+
if (entry.kind === "scoped") {
|
|
376
|
+
entry.listener(event);
|
|
377
|
+
} else {
|
|
378
|
+
entry.typeListener(type, event);
|
|
379
|
+
}
|
|
380
|
+
if (entry.once) {
|
|
381
|
+
this.off(entry.id);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
dispose() {
|
|
386
|
+
this.scopedEntries.clear();
|
|
387
|
+
this.universalEntries.length = 0;
|
|
388
|
+
}
|
|
389
|
+
off(id) {
|
|
390
|
+
for (const [type, entries] of this.scopedEntries) {
|
|
391
|
+
const kept = entries.filter((entry) => entry.id !== id);
|
|
392
|
+
if (kept.length !== entries.length) {
|
|
393
|
+
this.scopedEntries.set(type, kept);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
this.universalEntries = this.universalEntries.filter((entry) => entry.id !== id);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
337
399
|
// src/fake-embed-frame.ts
|
|
338
|
-
import { Dispatcher } from "./vendor/core/events/index.js";
|
|
339
|
-
|
|
340
400
|
class FakeEmbedFrame {
|
|
341
401
|
dispatcher = new Dispatcher;
|
|
342
402
|
posted = [];
|
|
@@ -365,14 +425,12 @@ class FakeEmbedFrame {
|
|
|
365
425
|
}
|
|
366
426
|
}
|
|
367
427
|
// src/iframe-embed-frame.ts
|
|
368
|
-
import { Dispatcher as Dispatcher2 } from "./vendor/core/events/index.js";
|
|
369
|
-
|
|
370
428
|
class IframeEmbedFrame {
|
|
371
429
|
iframe;
|
|
372
430
|
window;
|
|
373
431
|
origin;
|
|
374
432
|
reader;
|
|
375
|
-
dispatcher = new
|
|
433
|
+
dispatcher = new Dispatcher;
|
|
376
434
|
constructor(iframe, window, origin, reader) {
|
|
377
435
|
this.iframe = iframe;
|
|
378
436
|
this.window = window;
|
|
@@ -405,14 +463,63 @@ class IframeEmbedFrame {
|
|
|
405
463
|
this.dispatcher.dispatch("load");
|
|
406
464
|
};
|
|
407
465
|
}
|
|
466
|
+
// packages/core/disposable/disposables.ts
|
|
467
|
+
class disposables {
|
|
468
|
+
constructor() {}
|
|
469
|
+
static noop() {
|
|
470
|
+
return disposables.callback(() => {});
|
|
471
|
+
}
|
|
472
|
+
static callback(dispose) {
|
|
473
|
+
return { dispose };
|
|
474
|
+
}
|
|
475
|
+
static nullable(resource) {
|
|
476
|
+
return resource ?? disposables.noop();
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
// packages/core/disposable/disposer.ts
|
|
480
|
+
class Disposer {
|
|
481
|
+
resources = [];
|
|
482
|
+
_disposed = false;
|
|
483
|
+
get disposed() {
|
|
484
|
+
return this._disposed;
|
|
485
|
+
}
|
|
486
|
+
use(resource) {
|
|
487
|
+
this.assertOpen();
|
|
488
|
+
this.resources.push(resource);
|
|
489
|
+
return resource;
|
|
490
|
+
}
|
|
491
|
+
adopt(value, dispose) {
|
|
492
|
+
this.assertOpen();
|
|
493
|
+
this.resources.push(disposables.callback(() => dispose(value)));
|
|
494
|
+
return value;
|
|
495
|
+
}
|
|
496
|
+
defer(onDispose) {
|
|
497
|
+
this.assertOpen();
|
|
498
|
+
this.resources.push(disposables.callback(onDispose));
|
|
499
|
+
}
|
|
500
|
+
dispose() {
|
|
501
|
+
if (this._disposed) {
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
this._disposed = true;
|
|
505
|
+
let resource = this.resources.pop();
|
|
506
|
+
while (resource) {
|
|
507
|
+
resource.dispose();
|
|
508
|
+
resource = this.resources.pop();
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
assertOpen() {
|
|
512
|
+
if (this._disposed) {
|
|
513
|
+
throw new Error("cannot register a resource on a disposed Disposer");
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
}
|
|
408
517
|
// src/player.ts
|
|
409
|
-
import { Disposer } from "./vendor/core/disposable/index.js";
|
|
410
|
-
import { Dispatcher as Dispatcher3 } from "./vendor/core/events/index.js";
|
|
411
518
|
var DEFAULT_ORIGIN = "https://sound2score.com";
|
|
412
519
|
|
|
413
520
|
class Player {
|
|
414
521
|
frame;
|
|
415
|
-
dispatcher = new
|
|
522
|
+
dispatcher = new Dispatcher;
|
|
416
523
|
disposer = new Disposer;
|
|
417
524
|
state = null;
|
|
418
525
|
ready = false;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sound2score/player",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Embed sound2score scores on your site and control playback from your page.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -16,16 +16,15 @@
|
|
|
16
16
|
"iframe",
|
|
17
17
|
"player"
|
|
18
18
|
],
|
|
19
|
-
"dependencies": {},
|
|
20
19
|
"publishConfig": {
|
|
21
20
|
"access": "public"
|
|
22
21
|
},
|
|
23
|
-
"main": "./index.js",
|
|
24
|
-
"types": "./index.d.ts",
|
|
25
22
|
"exports": {
|
|
26
23
|
".": {
|
|
27
|
-
"types": "./index.d.ts",
|
|
24
|
+
"types": "./types/index.d.ts",
|
|
28
25
|
"default": "./index.js"
|
|
29
26
|
}
|
|
30
|
-
}
|
|
27
|
+
},
|
|
28
|
+
"main": "./index.js",
|
|
29
|
+
"types": "./types/index.d.ts"
|
|
31
30
|
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: sound2score-player
|
|
3
|
+
description: Embed sound2score sheet music on a website and control it from the page with @sound2score/player. Use when adding a sound2score score to a site, creating or attaching its embed, passing the viewer's user ID, playing, pausing, seeking, looping, changing speed, transposing or toggling the metronome from page code, or reacting to the embed's ready, change and unavailable events.
|
|
4
|
+
version: 0.0.4
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# sound2score Player
|
|
8
|
+
|
|
9
|
+
`@sound2score/player` puts a sound2score score on a web page as an iframe and
|
|
10
|
+
lets the page drive it: play, pause, seek, loop bars, slow down, transpose and
|
|
11
|
+
click along, and hear back what the Player is doing.
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm install @sound2score/player
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Before you start
|
|
18
|
+
|
|
19
|
+
- The score's owner must be on a plan with embeds and must add your site
|
|
20
|
+
(its origin, such as `https://school.example`) to the sites in their Embeds
|
|
21
|
+
settings on sound2score. On any other site the embed shows why it can't
|
|
22
|
+
play and raises `unavailable` with reason `site`.
|
|
23
|
+
- The score ID is the last part of its address: `sound2score.com/s/<scoreId>`.
|
|
24
|
+
- Each viewer of an embed counts as one of the owner's students. Pass
|
|
25
|
+
`userId` (below) so a student counts once however many browsers they use.
|
|
26
|
+
|
|
27
|
+
## Create a new embed
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { Player } from "@sound2score/player";
|
|
31
|
+
|
|
32
|
+
const container = document.getElementById("score");
|
|
33
|
+
if (!container) {
|
|
34
|
+
throw new Error("No #score element");
|
|
35
|
+
}
|
|
36
|
+
const player = Player.create(container, {
|
|
37
|
+
scoreId: "abc123",
|
|
38
|
+
// The ID your site knows this viewer by. Sent once, when the embed loads.
|
|
39
|
+
userId: "student-42",
|
|
40
|
+
options: {
|
|
41
|
+
start: { speed: 80, bars: { from: 9, to: 16 } },
|
|
42
|
+
controls: { transpose: "off", mixer: "open" },
|
|
43
|
+
layout: { title: false },
|
|
44
|
+
look: { theme: "volt", accent: "#ff00aa" },
|
|
45
|
+
},
|
|
46
|
+
height: 600,
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
The iframe fills the container's width. Anything left out of `options`
|
|
51
|
+
keeps its default, and look fields you leave out follow the owner's style.
|
|
52
|
+
|
|
53
|
+
## Attach to an embed already on the page
|
|
54
|
+
|
|
55
|
+
An iframe pasted from sound2score's Embed dialog works too:
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import { Player } from "@sound2score/player";
|
|
59
|
+
|
|
60
|
+
const iframe = document.querySelector<HTMLIFrameElement>(
|
|
61
|
+
'iframe[src*="sound2score.com/s/"]',
|
|
62
|
+
);
|
|
63
|
+
if (!iframe) {
|
|
64
|
+
throw new Error("No sound2score embed on the page");
|
|
65
|
+
}
|
|
66
|
+
const player = Player.attach(iframe);
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
To pass a `userId` to a pasted iframe, add `u=<id>` to its `src` before it
|
|
70
|
+
loads. It can't be added later.
|
|
71
|
+
|
|
72
|
+
## Commands
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import { Player } from "@sound2score/player";
|
|
76
|
+
|
|
77
|
+
declare const player: Player;
|
|
78
|
+
|
|
79
|
+
player.play();
|
|
80
|
+
player.pause();
|
|
81
|
+
player.seek(30); // seconds from the start
|
|
82
|
+
player.goToBar(17); // bars count from 1, in playback order
|
|
83
|
+
player.setLoop({ from: 9, to: 16 }); // both bars included
|
|
84
|
+
player.setLoop(null); // stop looping
|
|
85
|
+
player.setSpeed(75); // percent of the written tempo, 25 to 200
|
|
86
|
+
player.setTranspose(-2); // semitones from as written, -12 to 12
|
|
87
|
+
player.setMetronome(true);
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
- Commands given before the score is ready wait, then run in order.
|
|
91
|
+
- Commands work even when the embed's options hide that control, so you can
|
|
92
|
+
hide the Player's own buttons and build your own.
|
|
93
|
+
- Inside a loop, `seek` and `goToBar` keep the playhead in the loop. Call
|
|
94
|
+
`setLoop(null)` first to leave it.
|
|
95
|
+
- Browsers start sound only after the viewer has interacted with the page.
|
|
96
|
+
On phones, the viewer has to tap inside the embed once before `play()`
|
|
97
|
+
makes a sound.
|
|
98
|
+
|
|
99
|
+
## Events
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
import { Player, type PlayerState } from "@sound2score/player";
|
|
103
|
+
|
|
104
|
+
declare const player: Player;
|
|
105
|
+
|
|
106
|
+
function show(state: PlayerState): void {
|
|
107
|
+
console.log(`bar ${state.bar} of ${state.bars}`, state.playing);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
player.events.on("ready", show);
|
|
111
|
+
// At most 4 times a second, whenever what the Player shows changed.
|
|
112
|
+
player.events.on("change", show);
|
|
113
|
+
player.events.on("unavailable", ({ reason, resetsAt }) => {
|
|
114
|
+
// "not-found": no such score, or it can't be embedded.
|
|
115
|
+
// "site": this site isn't one the owner allows.
|
|
116
|
+
// "capped": the owner reached their spending limit until resetsAt.
|
|
117
|
+
// "busy": too many new viewers from one network, until resetsAt.
|
|
118
|
+
console.log(reason, resetsAt === null ? null : new Date(resetsAt));
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
`PlayerState` holds `playing`, `seconds`, `duration`, `bar`, `bars`, `loop`,
|
|
123
|
+
`speed`, `semitones` and `metronome`. `player.getState()` returns the last
|
|
124
|
+
one, or null before `ready`.
|
|
125
|
+
|
|
126
|
+
Call `player.dispose()` to stop listening. The iframe stays on the page;
|
|
127
|
+
remove it yourself if you want it gone.
|
|
128
|
+
|
|
129
|
+
## What it can't do
|
|
130
|
+
|
|
131
|
+
These are deliberate:
|
|
132
|
+
|
|
133
|
+
- No `load(scoreId)`. To show another score, create a new Player.
|
|
134
|
+
- No printing, exporting or downloading the score or its recordings.
|
|
135
|
+
- No preloading recordings.
|
|
136
|
+
- `userId` is read only when the embed loads.
|
|
137
|
+
|
|
138
|
+
## Building URLs yourself
|
|
139
|
+
|
|
140
|
+
For a server-rendered page, `EmbedUrlBuilder` writes the same `src` that
|
|
141
|
+
`create` does:
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
import {
|
|
145
|
+
DEFAULT_EMBED_OPTIONS,
|
|
146
|
+
EmbedOptionsCodec,
|
|
147
|
+
EmbedUrlBuilder,
|
|
148
|
+
} from "@sound2score/player";
|
|
149
|
+
|
|
150
|
+
const urls = new EmbedUrlBuilder(
|
|
151
|
+
"https://sound2score.com",
|
|
152
|
+
new EmbedOptionsCodec(),
|
|
153
|
+
);
|
|
154
|
+
const src = urls.build(
|
|
155
|
+
"abc123",
|
|
156
|
+
{
|
|
157
|
+
...DEFAULT_EMBED_OPTIONS,
|
|
158
|
+
start: { ...DEFAULT_EMBED_OPTIONS.start, speed: 70 },
|
|
159
|
+
},
|
|
160
|
+
"student-42",
|
|
161
|
+
);
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Keep `allow="autoplay; fullscreen"` on the iframe so playback and fullscreen
|
|
165
|
+
work.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Resource } from "../
|
|
2
|
-
import type { Eventful } from "../
|
|
1
|
+
import type { Resource } from "../packages/core/disposable/index.js";
|
|
2
|
+
import type { Eventful } from "../packages/core/events/index.js";
|
|
3
3
|
import type { EmbedMessage, HostMessage } from "./protocol.js";
|
|
4
4
|
export type EmbedFrameEvents = {
|
|
5
5
|
/** A message from the embed. */
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Events } from "../
|
|
1
|
+
import { type Events } from "../packages/core/events/index.js";
|
|
2
2
|
import type { EmbedFrame, EmbedFrameEvents } from "./embed-frame.js";
|
|
3
3
|
import type { EmbedMessage, HostMessage } from "./protocol.js";
|
|
4
4
|
/** An embed in memory, for testing code that drives a Player. */
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Resource } from "../
|
|
2
|
-
import { type Events } from "../
|
|
1
|
+
import type { Resource } from "../packages/core/disposable/index.js";
|
|
2
|
+
import { type Events } from "../packages/core/events/index.js";
|
|
3
3
|
import type { EmbedFrame, EmbedFrameEvents } from "./embed-frame.js";
|
|
4
4
|
import type { EmbedMessageReader } from "./embed-message-reader.js";
|
|
5
5
|
import type { HostMessage } from "./protocol.js";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { type Resource } from "../
|
|
2
|
-
import { type Eventful, type Events } from "../
|
|
1
|
+
import { type Resource } from "../packages/core/disposable/index.js";
|
|
2
|
+
import { type Eventful, type Events } from "../packages/core/events/index.js";
|
|
3
3
|
import type { EmbedFrame } from "./embed-frame.js";
|
|
4
4
|
import { type BarRange, type EmbedControls, type EmbedLayout, type EmbedLook, type EmbedStart } from "./embed-options.js";
|
|
5
5
|
import { type PlayerState, type Unavailable } from "./protocol.js";
|
package/vendor/core/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Jared Johnson
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
// disposable/async-disposer.ts
|
|
2
|
-
class AsyncDisposer {
|
|
3
|
-
resources = [];
|
|
4
|
-
_disposed = false;
|
|
5
|
-
disposing;
|
|
6
|
-
get disposed() {
|
|
7
|
-
return this._disposed;
|
|
8
|
-
}
|
|
9
|
-
use(resource) {
|
|
10
|
-
this.assertOpen();
|
|
11
|
-
this.resources.push(resource);
|
|
12
|
-
return resource;
|
|
13
|
-
}
|
|
14
|
-
adopt(value, disposeAsync) {
|
|
15
|
-
this.assertOpen();
|
|
16
|
-
this.resources.push({ disposeAsync: () => disposeAsync(value) });
|
|
17
|
-
return value;
|
|
18
|
-
}
|
|
19
|
-
defer(onDispose) {
|
|
20
|
-
this.assertOpen();
|
|
21
|
-
this.resources.push({ disposeAsync: onDispose });
|
|
22
|
-
}
|
|
23
|
-
disposeAsync = () => {
|
|
24
|
-
this._disposed = true;
|
|
25
|
-
this.disposing ??= this.release();
|
|
26
|
-
return this.disposing;
|
|
27
|
-
};
|
|
28
|
-
async release() {
|
|
29
|
-
let resource = this.resources.pop();
|
|
30
|
-
while (resource) {
|
|
31
|
-
await resource.disposeAsync();
|
|
32
|
-
resource = this.resources.pop();
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
assertOpen() {
|
|
36
|
-
if (this._disposed) {
|
|
37
|
-
throw new Error("cannot register a resource on a disposed AsyncDisposer");
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
// disposable/disposables.ts
|
|
42
|
-
class disposables {
|
|
43
|
-
constructor() {}
|
|
44
|
-
static noop() {
|
|
45
|
-
return disposables.callback(() => {});
|
|
46
|
-
}
|
|
47
|
-
static callback(dispose) {
|
|
48
|
-
return { dispose };
|
|
49
|
-
}
|
|
50
|
-
static nullable(resource) {
|
|
51
|
-
return resource ?? disposables.noop();
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
// disposable/disposer.ts
|
|
55
|
-
class Disposer {
|
|
56
|
-
resources = [];
|
|
57
|
-
_disposed = false;
|
|
58
|
-
get disposed() {
|
|
59
|
-
return this._disposed;
|
|
60
|
-
}
|
|
61
|
-
use(resource) {
|
|
62
|
-
this.assertOpen();
|
|
63
|
-
this.resources.push(resource);
|
|
64
|
-
return resource;
|
|
65
|
-
}
|
|
66
|
-
adopt(value, dispose) {
|
|
67
|
-
this.assertOpen();
|
|
68
|
-
this.resources.push(disposables.callback(() => dispose(value)));
|
|
69
|
-
return value;
|
|
70
|
-
}
|
|
71
|
-
defer(onDispose) {
|
|
72
|
-
this.assertOpen();
|
|
73
|
-
this.resources.push(disposables.callback(onDispose));
|
|
74
|
-
}
|
|
75
|
-
dispose() {
|
|
76
|
-
if (this._disposed) {
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
this._disposed = true;
|
|
80
|
-
let resource = this.resources.pop();
|
|
81
|
-
while (resource) {
|
|
82
|
-
resource.dispose();
|
|
83
|
-
resource = this.resources.pop();
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
assertOpen() {
|
|
87
|
-
if (this._disposed) {
|
|
88
|
-
throw new Error("cannot register a resource on a disposed Disposer");
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
export {
|
|
93
|
-
AsyncDisposer,
|
|
94
|
-
Disposer,
|
|
95
|
-
disposables
|
|
96
|
-
};
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
// events/dispatcher.ts
|
|
2
|
-
class Dispatcher {
|
|
3
|
-
events = this;
|
|
4
|
-
scopedEntries = new Map;
|
|
5
|
-
universalEntries = [];
|
|
6
|
-
nextId = 0;
|
|
7
|
-
nextOrder = 0;
|
|
8
|
-
on(type, listener, opts) {
|
|
9
|
-
const id = this.nextId++;
|
|
10
|
-
const entries = this.scopedEntries.get(type) ?? [];
|
|
11
|
-
entries.push({
|
|
12
|
-
kind: "scoped",
|
|
13
|
-
listener,
|
|
14
|
-
once: opts?.once ?? false,
|
|
15
|
-
id,
|
|
16
|
-
order: this.nextOrder++
|
|
17
|
-
});
|
|
18
|
-
this.scopedEntries.set(type, entries);
|
|
19
|
-
return () => this.off(id);
|
|
20
|
-
}
|
|
21
|
-
all(typeListener, opts) {
|
|
22
|
-
const id = this.nextId++;
|
|
23
|
-
this.universalEntries.push({
|
|
24
|
-
kind: "universal",
|
|
25
|
-
typeListener,
|
|
26
|
-
once: opts?.once ?? false,
|
|
27
|
-
id,
|
|
28
|
-
order: this.nextOrder++
|
|
29
|
-
});
|
|
30
|
-
return () => this.off(id);
|
|
31
|
-
}
|
|
32
|
-
dispatch(type, ...args) {
|
|
33
|
-
const entries = [
|
|
34
|
-
...this.scopedEntries.get(type) ?? [],
|
|
35
|
-
...this.universalEntries
|
|
36
|
-
].sort((left, right) => left.order - right.order);
|
|
37
|
-
const event = args[0];
|
|
38
|
-
for (const entry of entries) {
|
|
39
|
-
if (entry.kind === "scoped") {
|
|
40
|
-
entry.listener(event);
|
|
41
|
-
} else {
|
|
42
|
-
entry.typeListener(type, event);
|
|
43
|
-
}
|
|
44
|
-
if (entry.once) {
|
|
45
|
-
this.off(entry.id);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
dispose() {
|
|
50
|
-
this.scopedEntries.clear();
|
|
51
|
-
this.universalEntries.length = 0;
|
|
52
|
-
}
|
|
53
|
-
off(id) {
|
|
54
|
-
for (const [type, entries] of this.scopedEntries) {
|
|
55
|
-
const kept = entries.filter((entry) => entry.id !== id);
|
|
56
|
-
if (kept.length !== entries.length) {
|
|
57
|
-
this.scopedEntries.set(type, kept);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
this.universalEntries = this.universalEntries.filter((entry) => entry.id !== id);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
// events/noop-events.ts
|
|
64
|
-
class NoopEvents {
|
|
65
|
-
on() {
|
|
66
|
-
return () => {};
|
|
67
|
-
}
|
|
68
|
-
all() {
|
|
69
|
-
return () => {};
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
export {
|
|
73
|
-
Dispatcher,
|
|
74
|
-
NoopEvents
|
|
75
|
-
};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|