import type { Proxyable, ProxyableExternal } from 'proxy-extend'; import { isProxyable } from 'proxy-extend'; import * as LoadableDefs from './interfaces/Loadable.js'; import * as LoaderDefs from './interfaces/Loader.js'; export type Status = LoadableDefs.Status; export type Loadable = LoadableDefs.Loadable; export type { Proxyable }; export type LoadableProxy = LoadableDefs.LoadableProxyT; // Wrap up the definitions in a single `Loadable` object, which can also be invocated as a function (uses Proxy) export const Loadable = Object.assign( (item ?: undefined | T, status : Partial = {}) => LoadableDefs.LoadableProxy(item, status), { // Keys item: LoadableDefs.itemKey as typeof LoadableDefs.itemKey, // Prevent widening to `symbol` status: LoadableDefs.statusKey as typeof LoadableDefs.statusKey, // Prevent widening to `symbol` construct: LoadableDefs.constructKey as typeof LoadableDefs.constructKey, // Prevent widening to `symbol` isStatus: LoadableDefs.isStatus, isLoadable: LoadableDefs.isLoadable, // Constructors Record: LoadableDefs.LoadableRecord, Proxy: LoadableDefs.LoadableProxy, // Accessor functions getItem: (resource : Loadable) => resource[LoadableDefs.itemKey], getStatus: (resource : Loadable) => resource[LoadableDefs.statusKey], // Updater functions update: LoadableDefs.update, updateItem: LoadableDefs.updateItem, updateStatus: LoadableDefs.updateStatus, asPending: LoadableDefs.asPending, asLoading: LoadableDefs.asLoading, asReady: LoadableDefs.asReady, asFailed: LoadableDefs.asFailed, }, ); // Shorthand exports export const status: typeof LoadableDefs.statusKey = LoadableDefs.statusKey; // Prevent widening to `symbol` export const isStatus = LoadableDefs.isStatus; export const isLoadable = LoadableDefs.isLoadable; // Utilities export { isProxyable }; /* export { LoadError, LoadablePromise, }; export const loader = ( item : Loadable, executor : (resolve : (item : Loadable) => void, reject : (item : Loadable) => void) => void, ) => new LoadablePromise(executor, item); */ export const Loader = { resource: LoaderDefs.resourceKey, fromPromise: LoaderDefs.fromPromise, }; export type PromiseWithResource = LoaderDefs.PromiseWithResource; export default Loadable;