@joycostudio/susano 0.2.0 → 1.1.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/README.md +337 -112
- package/dist/index.d.mts +198 -91
- package/dist/index.d.ts +198 -91
- package/dist/index.js +239 -139
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +239 -140
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,70 +1,101 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Thin typed wrapper around `tiny-emitter`. Subclasses pin an event-map type
|
|
3
|
+
* (`{ eventName: payloadType }`) so `on`/`emit` are fully typed.
|
|
4
|
+
*/
|
|
5
|
+
declare class TypedEmitter<EventMap extends Record<string, unknown>> {
|
|
6
|
+
private readonly emitter;
|
|
7
|
+
on<K extends keyof EventMap & string>(event: K, listener: (payload: EventMap[K]) => void): () => void;
|
|
8
|
+
once<K extends keyof EventMap & string>(event: K, listener: (payload: EventMap[K]) => void): void;
|
|
9
|
+
off<K extends keyof EventMap & string>(event: K, listener?: (payload: EventMap[K]) => void): void;
|
|
10
|
+
protected emit<K extends keyof EventMap & string>(event: K, payload: EventMap[K]): void;
|
|
11
|
+
}
|
|
2
12
|
|
|
3
|
-
type
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
13
|
+
type LoaderStatus = 'idle' | 'loading' | 'loaded' | 'error';
|
|
14
|
+
/** Events emitted by a {@link SusanoLoader} over the content fetch lifecycle. */
|
|
15
|
+
type SusanoLoaderEventMap<T> = {
|
|
16
|
+
/** Content fetch settled successfully. Payload is the loader itself (read `.content`). */
|
|
17
|
+
loaded: SusanoLoader<T>;
|
|
18
|
+
/** Fetch progress advanced. Payload is the loader itself (read `.progress`). */
|
|
19
|
+
progress: SusanoLoader<T>;
|
|
20
|
+
/** Content fetch failed. Payload is the underlying error. */
|
|
21
|
+
error: unknown;
|
|
7
22
|
};
|
|
8
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Base loader. Owns and caches a single piece of raw **content** per URL (the expensive fetch),
|
|
25
|
+
* deduped and idempotent. It does NOT run `postprocess` — that is a per-call projection applied
|
|
26
|
+
* by `Susano` (`_runLoad`). If you want to cache a transformed result, encode the transform in a
|
|
27
|
+
* loader subclass so its output becomes the cached content.
|
|
28
|
+
*
|
|
29
|
+
* Subclasses add their own construction args as a second constructor parameter (see `LoaderRegistry`).
|
|
30
|
+
*/
|
|
31
|
+
declare class SusanoLoader<T> extends TypedEmitter<SusanoLoaderEventMap<T>> {
|
|
9
32
|
url: string;
|
|
10
33
|
loaded: boolean;
|
|
34
|
+
status: LoaderStatus;
|
|
11
35
|
content: T;
|
|
12
|
-
config: SusanoLoaderConfig<T, R>;
|
|
13
|
-
weight: number;
|
|
14
36
|
progress: number;
|
|
15
|
-
promise: Promise<
|
|
37
|
+
promise: Promise<T>;
|
|
16
38
|
private _resolve;
|
|
17
39
|
private _reject;
|
|
18
|
-
constructor(url: string
|
|
19
|
-
load()
|
|
40
|
+
constructor(url: string);
|
|
41
|
+
/** `true` while a content load is in flight (`status === 'loading'`). */
|
|
42
|
+
get loading(): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Load (or join) the content fetch. Idempotent: repeat calls return the same promise without
|
|
45
|
+
* re-fetching.
|
|
46
|
+
*
|
|
47
|
+
* An in-flight fetch can never be preempted: while `status === 'loading'`, every call joins the
|
|
48
|
+
* same promise regardless of `cache`. This keeps the invariant that at most one fetch runs per
|
|
49
|
+
* URL, so resetting the promise can never orphan a consumer already awaiting it. `cache: false`
|
|
50
|
+
* therefore acts on a *completed* load — it busts the cached content and forces a fresh fetch —
|
|
51
|
+
* not on concurrent work. Resolves with the raw content `T`; postprocessing happens per call in
|
|
52
|
+
* `Susano`.
|
|
53
|
+
*/
|
|
54
|
+
load(cache?: boolean): Promise<T>;
|
|
55
|
+
/** Subclasses implement the raw fetch here, calling `_onLoaded` / `_onError` / `_onProgress`. */
|
|
56
|
+
protected _load(): void;
|
|
20
57
|
_onLoaded(): void;
|
|
21
|
-
private _finalize;
|
|
22
58
|
_onError(error: unknown): void;
|
|
23
59
|
private _resetPromise;
|
|
24
60
|
_onProgress(value: number): void;
|
|
25
|
-
/**
|
|
26
|
-
* This is ment to be used as a way to attach to master loader events
|
|
27
|
-
*/
|
|
28
|
-
_appendConfig(cnfg: SusanoLoaderConfig<T, R>): void;
|
|
29
|
-
}
|
|
30
|
-
interface ISusanoLoader<T, R = T> {
|
|
31
|
-
load: () => Promise<R>;
|
|
32
61
|
}
|
|
33
62
|
|
|
34
|
-
type SusanoImageLoaderConfig
|
|
63
|
+
type SusanoImageLoaderConfig = {
|
|
35
64
|
srcSet?: string;
|
|
36
65
|
sizes?: string;
|
|
37
66
|
};
|
|
38
|
-
declare class ImageLoader
|
|
67
|
+
declare class ImageLoader extends SusanoLoader<HTMLImageElement> {
|
|
39
68
|
static type: "image";
|
|
40
69
|
srcSet?: string;
|
|
41
70
|
sizes?: string;
|
|
42
|
-
constructor(url: string, cnfg?: SusanoImageLoaderConfig
|
|
43
|
-
|
|
71
|
+
constructor(url: string, cnfg?: SusanoImageLoaderConfig);
|
|
72
|
+
protected _load(): void;
|
|
44
73
|
}
|
|
45
74
|
|
|
46
75
|
type LoadEvent$1 = 'canplay' | 'canplaythrough';
|
|
47
|
-
type SusanoVideoLoaderConfig
|
|
76
|
+
type SusanoVideoLoaderConfig = {
|
|
48
77
|
video?: HTMLVideoElement;
|
|
49
78
|
loadEvent?: LoadEvent$1;
|
|
50
79
|
};
|
|
51
|
-
declare class VideoLoader
|
|
80
|
+
declare class VideoLoader extends SusanoLoader<HTMLVideoElement> {
|
|
52
81
|
static type: "video";
|
|
53
82
|
loadEvent: LoadEvent$1;
|
|
54
|
-
|
|
55
|
-
|
|
83
|
+
private _attempt;
|
|
84
|
+
constructor(url: string, cnfg?: SusanoVideoLoaderConfig);
|
|
85
|
+
protected _load(): void;
|
|
56
86
|
}
|
|
57
87
|
|
|
58
88
|
type LoadEvent = 'canplay' | 'canplaythrough';
|
|
59
|
-
type SusanoAudioLoaderConfig
|
|
89
|
+
type SusanoAudioLoaderConfig = {
|
|
60
90
|
audio?: HTMLAudioElement;
|
|
61
91
|
loadEvent?: LoadEvent;
|
|
62
92
|
};
|
|
63
|
-
declare class AudioLoader
|
|
93
|
+
declare class AudioLoader extends SusanoLoader<HTMLAudioElement> {
|
|
64
94
|
static type: "audio";
|
|
65
95
|
loadEvent: LoadEvent;
|
|
66
|
-
|
|
67
|
-
|
|
96
|
+
private _attempt;
|
|
97
|
+
constructor(url: string, cnfg?: SusanoAudioLoaderConfig);
|
|
98
|
+
protected _load(): void;
|
|
68
99
|
}
|
|
69
100
|
|
|
70
101
|
type GenericLoadFn = (config: {
|
|
@@ -73,81 +104,157 @@ type GenericLoadFn = (config: {
|
|
|
73
104
|
error: (error: Error) => void;
|
|
74
105
|
progress: (progress: number) => void;
|
|
75
106
|
}) => void;
|
|
76
|
-
type SusanoGenericLoaderConfig
|
|
107
|
+
type SusanoGenericLoaderConfig = {
|
|
77
108
|
loadFn: GenericLoadFn;
|
|
78
109
|
};
|
|
79
|
-
declare class GenericLoader
|
|
110
|
+
declare class GenericLoader extends SusanoLoader<any> {
|
|
80
111
|
static type: "generic";
|
|
81
112
|
loadFn: GenericLoadFn;
|
|
82
|
-
constructor(url: string, cnfg: SusanoGenericLoaderConfig
|
|
83
|
-
|
|
113
|
+
constructor(url: string, cnfg: SusanoGenericLoaderConfig);
|
|
114
|
+
protected _load(): void;
|
|
84
115
|
}
|
|
85
116
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
};
|
|
117
|
+
/** One tracked load within a batch: a content loader and its per-call result promise. */
|
|
118
|
+
type BatchItem = {
|
|
119
|
+
loader: SusanoLoader<unknown>;
|
|
120
|
+
promise: Promise<unknown>;
|
|
91
121
|
};
|
|
92
|
-
type
|
|
93
|
-
[K in keyof T]: T[K]['loader'];
|
|
94
|
-
};
|
|
95
|
-
type ProgressEventArgs = {
|
|
122
|
+
type BatchProgressEventArgs = {
|
|
96
123
|
value: number;
|
|
97
|
-
|
|
98
|
-
|
|
124
|
+
/** The loader that just settled, or `null` for a batch-level tick (e.g. an empty batch). */
|
|
125
|
+
loader: SusanoLoader<unknown> | null;
|
|
126
|
+
batch: Batch;
|
|
127
|
+
};
|
|
128
|
+
type BatchErrorEventArgs = {
|
|
129
|
+
loader: SusanoLoader<unknown>;
|
|
130
|
+
error: unknown;
|
|
131
|
+
batch: Batch;
|
|
132
|
+
};
|
|
133
|
+
/** Events emitted by a {@link Batch} over its lifecycle. */
|
|
134
|
+
type BatchEventMap = {
|
|
135
|
+
/** An item settled (or an empty batch ticked). */
|
|
136
|
+
progress: BatchProgressEventArgs;
|
|
137
|
+
/** An item failed. Fail-soft: the batch still advances. */
|
|
138
|
+
error: BatchErrorEventArgs;
|
|
139
|
+
/** Every item settled. One-shot. */
|
|
140
|
+
completed: Batch;
|
|
141
|
+
};
|
|
142
|
+
type BatchHandlers = {
|
|
143
|
+
onProgress?: (progress: BatchProgressEventArgs) => void;
|
|
144
|
+
onCompleted?: (batch: Batch) => void;
|
|
145
|
+
onError?: (error: BatchErrorEventArgs) => void;
|
|
99
146
|
};
|
|
100
147
|
/**
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
148
|
+
* Tracks progress and completion for a fixed set of loads, independent of other loading activity.
|
|
149
|
+
* Each batch owns its own counter and completion promise, so concurrent batches (and lazy
|
|
150
|
+
* `susano.load()` calls) never interfere.
|
|
151
|
+
*
|
|
152
|
+
* Completion is the resolution of `Promise.all` over the set — one-shot by construction. A failed
|
|
153
|
+
* item is fail-soft: it surfaces via `onError` / the `'error'` event and still advances the batch.
|
|
104
154
|
*/
|
|
105
|
-
declare class
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
active: InstanceType<T[keyof T]['loader']>[];
|
|
109
|
-
items: Map<string, InstanceType<T[keyof T]['loader']>>;
|
|
155
|
+
declare class Batch extends TypedEmitter<BatchEventMap> {
|
|
156
|
+
readonly items: BatchItem[];
|
|
157
|
+
readonly loadLength: number;
|
|
110
158
|
loadCount: number;
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
159
|
+
progress: number;
|
|
160
|
+
completed: boolean;
|
|
161
|
+
readonly promise: Promise<Batch>;
|
|
162
|
+
private _counted;
|
|
163
|
+
private _resolve;
|
|
164
|
+
constructor(items: BatchItem[], handlers?: BatchHandlers);
|
|
165
|
+
private _start;
|
|
166
|
+
/** Count an item at most once so the progress fraction can't be inflated by repeat settles. */
|
|
167
|
+
private _tick;
|
|
168
|
+
private _finish;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Per-call options. `postprocess` is the projection from cached content `T` to this call's
|
|
173
|
+
* result `R` — it runs **every call**, never cached. `onProgress` observes the shared content
|
|
174
|
+
* fetch; `onLoaded` / `onError` fire for this call's result.
|
|
175
|
+
*/
|
|
176
|
+
type LoadOptions<T, R> = {
|
|
177
|
+
postprocess?: (content: T, loader: SusanoLoader<T>) => R | Promise<R>;
|
|
178
|
+
onLoaded?: (result: R, loader: SusanoLoader<T>) => void;
|
|
179
|
+
onProgress?: (value: number, loader: SusanoLoader<T>) => void;
|
|
180
|
+
onError?: (error: unknown, loader: SusanoLoader<T>) => void;
|
|
181
|
+
};
|
|
182
|
+
/**
|
|
183
|
+
* The common shape of any loader class: constructs a `SusanoLoader` from a `url` plus that
|
|
184
|
+
* loader's own construction args. The two `any`s are deliberate and contained to this bound:
|
|
185
|
+
* - `args` — each loader narrows it to its own config; the precise type is recovered per call via
|
|
186
|
+
* `ConstructorParameters<R[K]>` in `LoadConfig` / `BatchEntry`.
|
|
187
|
+
* - `SusanoLoader<any>` — `SusanoLoader<T>` is *invariant* in `T` (it holds `(value: T) => void`
|
|
188
|
+
* resolve/reject callbacks), so no single concrete content type bounds every loader. `any` admits
|
|
189
|
+
* them all here; per-call typing stays precise via `ContentOf<R, K>`.
|
|
190
|
+
*/
|
|
191
|
+
type AnyLoaderConstructor = new (url: string, args?: any) => SusanoLoader<any>;
|
|
192
|
+
/** The loader registry: a map from `type` key to loader class. The single source of truth. */
|
|
193
|
+
type LoaderRegistry = Record<string, AnyLoaderConstructor>;
|
|
194
|
+
/** The content type produced by the loader registered under `K`. */
|
|
195
|
+
type ContentOf<R extends LoaderRegistry, K extends keyof R> = InstanceType<R[K]> extends SusanoLoader<infer C> ? C : never;
|
|
196
|
+
/** Construction args + cache flag for a load. */
|
|
197
|
+
type LoadConfig<R extends LoaderRegistry, K extends keyof R> = {
|
|
198
|
+
type: K;
|
|
199
|
+
loaderArgs?: ConstructorParameters<R[K]>[1];
|
|
200
|
+
/** Opt out of dedup; force a fresh content fetch that overwrites the cached content. Defaults to `true`. */
|
|
201
|
+
cache?: boolean;
|
|
202
|
+
};
|
|
203
|
+
/** A single entry in a {@link Susano.batch} call — construction + per-call options, typed per `type`. */
|
|
204
|
+
type BatchEntry<R extends LoaderRegistry> = {
|
|
205
|
+
[K in keyof R]: LoadConfig<R, K> & {
|
|
206
|
+
url: string;
|
|
207
|
+
} & LoadOptions<ContentOf<R, K>, unknown>;
|
|
208
|
+
}[keyof R];
|
|
209
|
+
/**
|
|
210
|
+
* Registry + per-URL **content** cache. The loader map passed to the constructor is the single
|
|
211
|
+
* source of truth: its keys are the valid `type`s and its values are the loader classes, so
|
|
212
|
+
* `load()` / `batch()` are fully inferred — no separate type map, no drift.
|
|
213
|
+
*
|
|
214
|
+
* Content (the expensive fetch) is deduped per URL; a `postprocess` is a per-call projection
|
|
215
|
+
* (see `_runLoad`), so two call sites can derive different results from one shared fetch.
|
|
216
|
+
*
|
|
217
|
+
* @template R The loader registry — `{ [type]: LoaderClass }`.
|
|
218
|
+
*/
|
|
219
|
+
declare class Susano<R extends LoaderRegistry> {
|
|
220
|
+
private loaders;
|
|
221
|
+
items: Map<string, SusanoLoader<unknown>>;
|
|
222
|
+
constructor(loaders: R);
|
|
223
|
+
/** The shared content loader cached for `url`, if one exists. Shorthand for `items.get(url)`. */
|
|
224
|
+
get(url: string): SusanoLoader<unknown> | undefined;
|
|
225
|
+
/**
|
|
226
|
+
* Get-or-create the content loader for `url`, typed to the loader registered under `type`.
|
|
227
|
+
* The one unavoidable cast: `items` is keyed by URL (a string), which can't carry the content
|
|
228
|
+
* type, so a cache hit is asserted back to the loader's known type.
|
|
229
|
+
*/
|
|
230
|
+
private _resolve;
|
|
231
|
+
/**
|
|
232
|
+
* Load one asset immediately and return a promise of *this call's result* (content after this
|
|
233
|
+
* call's `postprocess`). The content fetch is deduped per URL. Reach the shared content loader —
|
|
234
|
+
* for raw content or events — via `susano.items.get(url)`.
|
|
235
|
+
*/
|
|
236
|
+
load<K extends keyof R, Result = ContentOf<R, K>>(url: string, cnfg: LoadConfig<R, K> & LoadOptions<ContentOf<R, K>, Result>): Promise<Result>;
|
|
237
|
+
/**
|
|
238
|
+
* Run a single load against a shared content loader: ensure content (deduped), then apply this
|
|
239
|
+
* call's `postprocess` to produce the result. Two calls for the same URL share one content fetch
|
|
240
|
+
* but each get their own result promise. Stays a function of (loader, options) — it never touches
|
|
241
|
+
* the registry.
|
|
242
|
+
*/
|
|
243
|
+
private _runLoad;
|
|
244
|
+
/**
|
|
245
|
+
* Load a fixed set and track *only* that set's progress/completion, independent of other
|
|
246
|
+
* loading activity. Content fetches are deduped across entries; each entry's `postprocess` runs
|
|
247
|
+
* per call. Auto-starts; returns a {@link Batch} exposing `.promise`, `.progress`, `.completed`.
|
|
248
|
+
*/
|
|
249
|
+
batch(entries: BatchEntry<R>[], handlers?: BatchHandlers): Batch;
|
|
131
250
|
}
|
|
132
251
|
declare const susano: Susano<{
|
|
133
|
-
image:
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
video: {
|
|
138
|
-
type: "video";
|
|
139
|
-
loader: typeof VideoLoader;
|
|
140
|
-
};
|
|
141
|
-
audio: {
|
|
142
|
-
type: "audio";
|
|
143
|
-
loader: typeof AudioLoader;
|
|
144
|
-
};
|
|
145
|
-
generic: {
|
|
146
|
-
type: "generic";
|
|
147
|
-
loader: typeof GenericLoader;
|
|
148
|
-
};
|
|
252
|
+
image: typeof ImageLoader;
|
|
253
|
+
video: typeof VideoLoader;
|
|
254
|
+
audio: typeof AudioLoader;
|
|
255
|
+
generic: typeof GenericLoader;
|
|
149
256
|
}>;
|
|
150
257
|
|
|
151
258
|
declare const VERSION: string;
|
|
152
259
|
|
|
153
|
-
export { AudioLoader, type
|
|
260
|
+
export { type AnyLoaderConstructor, AudioLoader, Batch, type BatchEntry, type BatchErrorEventArgs, type BatchEventMap, type BatchHandlers, type BatchItem, type BatchProgressEventArgs, type ContentOf, type GenericLoadFn, GenericLoader, ImageLoader, type LoadConfig, type LoadOptions, type LoaderRegistry, type LoaderStatus, Susano, type SusanoAudioLoaderConfig, type SusanoGenericLoaderConfig, type SusanoImageLoaderConfig, SusanoLoader, type SusanoLoaderEventMap, type SusanoVideoLoaderConfig, VERSION, VideoLoader, susano };
|