import $msg from 'message-tag'; import type { Proxyable } from 'proxy-extend'; import { Status, Loadable, itemKey, statusKey, LoadableProxy, asReady, asFailed } from './Loadable.js'; // A *loader* is a function that takes a resource (the current state), and returns a promise for // a new (loaded) resource (so always async, by definition). export type Loader = (resource : Loadable) => Promise>; // A *loader creator* is a function that returns a loader. export type LoaderCreator = (...args : Array) => Loader; export const resourceKey = Symbol.for('lifecycle.loadable.loader-resource'); // Version of `Error` that keeps a reference to the resource export class LoadError extends Error { readonly [resourceKey] : Loadable; constructor(reason : unknown, resource : Loadable) { let message = reason; if (reason instanceof Error) { message = reason.message; } super($msg`Loading failed: ${message}`); this[resourceKey] = resource; } } /* // type Fulfill = (resolve : (result : unknown) => void, reject : (reason : unknown) => void) => void; type Resolver = (value ?: T | PromiseLike) => void; type Rejecter = (reason ?: any) => void; type PromiseExecutor = (resolve : Resolver, reject : Rejecter) => void; // Extended version of `Promise` that works with loadable resources. // Note: although the ES6 spec allows extending Promise, babel by default does not support it. Can // use the `transform-builtin-extend` plugin. // https://github.com/babel/babel/issues/1120 // // Note: should extending Promise become an issue, we could always fall back to just implementing // the "thenable" interface (i.e. just a method named `then()`). export class LoadablePromise extends Promise> { // Set the species to regular `Promise`, so that `then()` chaining will not try to create // a new LoadablePromise (which fails due to lack of information given to the constructor). static [Symbol.species] = Promise; // Create from existing promise static from(resource : Loadable, promise : Promise>) : LoadablePromise { return new LoadablePromise((resolve, reject) => { promise.then(resolve, reject); }, resource); } private fulfilled : boolean = false; public readonly resource : Loadable; constructor( executor : (resolve : (resource : Loadable) => void, reject : (resource : Loadable) => void) => void, resource : Loadable ) { super((resolve : Resolver>, reject : Rejecter) => { // Note: need to do this check here, because we cannot do it before `super()`, but we also want // it before the call to `executor()`. if (!resource[statusKey].loading) { throw new TypeError($msg`Expected resource with status loading, given ${resource[statusKey]}`); } executor( (resource : Loadable) => { if (!resource[statusKey].ready) { throw new TypeError($msg`Expected resource with status ready, given ${resource[statusKey]}`); } this.fulfilled = true; resolve(resource); }, (resource : Loadable) => { if (!resource[statusKey].error) { throw new TypeError($msg`Expected resource with status failed, given ${resource[statusKey]}`); } this.fulfilled = true; reject(new LoadError(resource[statusKey].error, resource)); }, ); }); this.resource = resource; } // Similar to `then()`, but will be called: // - Once, synchronously, with the resource in loading state, *if* the resource is not already fulfilled // (resolved or rejected) synchronously. // - Second, when the resource is fulfilled, with the resource in the corresponding state (ready/failed). // In addition, `subscribe` does not distinguish between resolved/rejected, it only takes // one function which is called regardless of the result (check the `status` instead). subscribe(this : LoadablePromise, subscriber : (resource : Loadable) => void) : typeof this { if (!this.fulfilled) { // Note: should be guaranteed to be in loading state (by the check in the constructor) subscriber(this.resource); } this.then( (resourceReady : Loadable) => { subscriber(resourceReady); }, (reason : LoadError) => { subscriber(reason.resource); }, ); return this; } } */ export type PromiseWithResource = PromiseLike> & { [resourceKey] : Loadable, //subscribe : () => ..., // TODO }; export const fromPromise = ( promise : PromiseLike, resource ?: Loadable ) : PromiseWithResource => { // Use the given resource, or use a LoadableProxy by default if none given const resourceLoading : Loadable = resource ?? LoadableProxy(undefined, { loading: true }); const promiseWithResource : PromiseWithResource = Object.assign( promise.then( (result : T) => asReady(resourceLoading, result), (reason : Error) => { throw new LoadError(reason, asFailed(resourceLoading, reason)); }, ), { [resourceKey]: resourceLoading, subscribe(subscriber : (resource : Loadable) => void) { subscriber(resourceLoading); promise.then( (result : T) => subscriber(asReady(resourceLoading, result)), (reason : Error) => subscriber(asFailed(resourceLoading, reason)), ); }, }, ); return promiseWithResource; };