Skip to content

Commit

Permalink
Fix up documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
englercj committed Dec 1, 2019
1 parent 7f5d668 commit d4fb58c
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 51 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"test": "npm run test-dev -- --single-run",
"pretest-dev": "npm run build",
"test-dev": "karma start test/karma.conf.js",
"docs": "typedoc --out docs --mode modules",
"docs": "typedoc",
"prepublishOnly": "npm run build",
"predeploy": "rimraf ./docs && npm run docs",
"deploy": "gh-pages -d docs",
Expand Down
53 changes: 30 additions & 23 deletions src/Loader.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
import parseUri from 'parse-uri';
import { Signal } from 'type-signals';
import { AsyncQueue } from './async/AsyncQueue';
import { Resource, OnCompleteSignal as OnResourceCompleteSignal } from './Resource';
import { Resource } from './Resource';
import { ILoadConfig } from './load_strategies/AbstractLoadStrategy';
import { eachSeries } from './async/eachSeries';

// some constants
const MAX_PROGRESS = 100;
const rgxExtractUrlHash = /(#[\w-]+)?$/;

export type ResourceMap = Partial<Record<string, Resource>>;
/**
* @category Type Aliases
*/
export namespace Loader
{
export type ResourceMap = Partial<Record<string, Resource>>;

export type OnProgressSignal = (loader: Loader, resource: Resource) => void;
export type OnErrorSignal = (errMessage: string, loader: Loader, resource: Resource) => void;
export type OnLoadSignal = (loader: Loader, resource: Resource) => void;
export type OnStartSignal = (loader: Loader) => void;
export type OnCompleteSignal = (loader: Loader, resources: ResourceMap) => void;
export type OnProgressSignal = (loader: Loader, resource: Resource) => void;
export type OnErrorSignal = (errMessage: string, loader: Loader, resource: Resource) => void;
export type OnLoadSignal = (loader: Loader, resource: Resource) => void;
export type OnStartSignal = (loader: Loader) => void;
export type OnCompleteSignal = (loader: Loader, resources: ResourceMap) => void;

export type MiddlewareFn = (resource: Resource, next: () => void) => void;
export type UrlResolverFn = (url: string, parsed: ReturnType<typeof parseUri>) => string;
export type MiddlewareFn = (resource: Resource, next: () => void) => void;
export type UrlResolverFn = (url: string, parsed: ReturnType<typeof parseUri>) => string;
}

export interface Middleware
interface Middleware
{
fn: MiddlewareFn;
fn: Loader.MiddlewareFn;
priority: number;
}

Expand All @@ -44,14 +50,15 @@ export interface IAddOptions extends ILoadConfig
name?: string;

// Callback to add an an onComplete signal istener.
onComplete?: OnResourceCompleteSignal;
onComplete?: Resource.OnCompleteSignal;

// Parent resource this newly added resource is a child of.
parentResource?: Resource;
}

/**
* Manages the state and loading of multiple resources to load.
* @preferred
*/
export class Loader
{
Expand All @@ -65,7 +72,7 @@ export class Loader
* can be used to modify the url just prior to `baseUrl` and `defaultQueryString`
* being applied.
*/
urlResolver: UrlResolverFn | null = null;
urlResolver: Loader.UrlResolverFn | null = null;

/**
* The progress percent of the loader going through the queue.
Expand Down Expand Up @@ -100,34 +107,34 @@ export class Loader
defaultQueryString = '';

/**
* All the resources for this loader keyed by name.
* All the resources for this loader keyed by name, or URL if no name was given.
*/
resources: ResourceMap = {};
resources: Loader.ResourceMap = {};

/**
* Dispatched once per errored resource.
*/
onError = new Signal<OnErrorSignal>();
readonly onError: Signal<Loader.OnErrorSignal> = new Signal<Loader.OnErrorSignal>();

/**
* Dispatched once per loaded resource.
*/
onLoad = new Signal<OnLoadSignal>();
readonly onLoad: Signal<Loader.OnLoadSignal> = new Signal<Loader.OnLoadSignal>();

/**
* Dispatched when the loader begins to process the queue.
*/
onStart = new Signal<OnStartSignal>();
readonly onStart: Signal<Loader.OnStartSignal> = new Signal<Loader.OnStartSignal>();

/**
* Dispatched when the queued resources all load.
*/
onComplete = new Signal<OnCompleteSignal>();
readonly onComplete: Signal<Loader.OnCompleteSignal> = new Signal<Loader.OnCompleteSignal>();

/**
* Dispatched once per loaded or errored resource.
*/
onProgress = new Signal<OnProgressSignal>();
readonly onProgress: Signal<Loader.OnProgressSignal> = new Signal<Loader.OnProgressSignal>();

/**
* The base url for all resources loaded by this loader.
Expand Down Expand Up @@ -324,7 +331,7 @@ export class Loader
* A lower priority value will make the function run earlier.
* That is, priority 30 is run before priority 50.
*/
use(fn: MiddlewareFn, priority: number = Loader.DefaultMiddlewarePriority): this
use(fn: Loader.MiddlewareFn, priority: number = Loader.DefaultMiddlewarePriority): this
{
this._middleware.push({ fn, priority });
this._middleware.sort((a, b) => a.priority - b.priority);
Expand Down Expand Up @@ -365,7 +372,7 @@ export class Loader
/**
* Starts loading the queued resources.
*/
load(cb?: OnCompleteSignal): this
load(cb?: Loader.OnCompleteSignal): this
{
if (typeof cb === 'function')
this.onComplete.once(cb);
Expand Down Expand Up @@ -544,7 +551,7 @@ export class Loader
* A lower priority value will make the function run earlier.
* That is, priority 30 is run before priority 50.
*/
static use(fn: MiddlewareFn, priority = Loader.DefaultMiddlewarePriority): typeof Loader
static use(fn: Loader.MiddlewareFn, priority = Loader.DefaultMiddlewarePriority): typeof Loader
{
Loader._defaultMiddleware.push({ fn, priority });
Loader._defaultMiddleware.sort((a, b) => a.priority - b.priority);
Expand Down
27 changes: 17 additions & 10 deletions src/Resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,25 @@ import { XhrLoadStrategy } from './load_strategies/XhrLoadStrategy';
import { ResourceType, ResourceState } from './resource_type';
import { getExtension } from './utilities';

export type OnStartSignal = (resource: Resource) => void;
export type OnErrorSignal = (resource: Resource) => void;
export type OnCompleteSignal = (resource: Resource) => void;
export type OnProgressSignal = (resource: Resource, percent: number) => void;

export interface IResourceOptions extends ILoadConfig
{
strategy?: AbstractLoadStrategy | AbstractLoadStrategyCtor;
}

/**
* @category Type Aliases
*/
export namespace Resource
{
export type OnStartSignal = (resource: Resource) => void;
export type OnErrorSignal = (resource: Resource) => void;
export type OnCompleteSignal = (resource: Resource) => void;
export type OnProgressSignal = (resource: Resource, percent: number) => void;
}

/**
* Manages the state and loading of a resource and all child resources.
* @preferred
*/
export class Resource
{
Expand Down Expand Up @@ -89,7 +96,7 @@ export class Resource
/**
* Dispatched when the resource beings to load.
*/
readonly onStart = new Signal<OnStartSignal>();
readonly onStart: Signal<Resource.OnStartSignal> = new Signal<Resource.OnStartSignal>();

/**
* Dispatched each time progress of this resource load updates.
Expand All @@ -98,18 +105,18 @@ export class Resource
* is being loaded on a modern browser, using XHR, and the remote server
* properly sets Content-Length headers, then this will be available.
*/
readonly onProgress = new Signal<OnProgressSignal>();
readonly onProgress: Signal<Resource.OnProgressSignal> = new Signal<Resource.OnProgressSignal>();

/**
* Dispatched once this resource has loaded, if there was an error it will
* be in the `error` property.
*/
readonly onComplete = new Signal<OnCompleteSignal>();
readonly onComplete: Signal<Resource.OnCompleteSignal> = new Signal<Resource.OnCompleteSignal>();

/**
* Dispatched after this resource has had all the *after* middleware run on it.
*/
readonly onAfterMiddleware = new Signal<OnCompleteSignal>();
readonly onAfterMiddleware: Signal<Resource.OnCompleteSignal> = new Signal<Resource.OnCompleteSignal>();

/**
* The data that was loaded by the resource. The type of this member is
Expand Down Expand Up @@ -148,7 +155,7 @@ export class Resource
*
* @ignore
*/
_onCompleteBinding: SignalBinding<OnCompleteSignal> | null = null;
_onCompleteBinding: SignalBinding<Resource.OnCompleteSignal> | null = null;

private _strategy: AbstractLoadStrategy;
private _state = ResourceState.NotStarted;
Expand Down
10 changes: 5 additions & 5 deletions src/async/AsyncQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ export class AsyncQueue<T>
private _started = false;
private _tasks: ITask<T>[] = [];

readonly onSaturated = new Signal<OnSaturatedSignal>();
readonly onUnsaturated = new Signal<OnUnsaturatedSignal>();
readonly onEmpty = new Signal<OnEmptySignal>();
readonly onDrain = new Signal<OnDrainSignal>();
readonly onError = new Signal<OnErrorSignal<T>>();
readonly onSaturated: Signal<OnSaturatedSignal> = new Signal<OnSaturatedSignal>();
readonly onUnsaturated: Signal<OnUnsaturatedSignal> = new Signal<OnUnsaturatedSignal>();
readonly onEmpty: Signal<OnEmptySignal> = new Signal<OnEmptySignal>();
readonly onDrain: Signal<OnDrainSignal> = new Signal<OnDrainSignal>();
readonly onError: Signal<OnErrorSignal<T>> = new Signal<OnErrorSignal<T>>();

constructor(readonly worker: IWorker<T>, public concurrency = 1)
{
Expand Down
1 change: 1 addition & 0 deletions src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Loader } from './Loader';
import { Resource } from './Resource';
import { ResourceType, ResourceState } from './resource_type';

// TODO: Hide this stuff and only expose for tests
import { AsyncQueue } from './async/AsyncQueue';
import { eachSeries } from './async/eachSeries';
import { getExtension } from './utilities';
Expand Down
5 changes: 0 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,3 @@ export { XhrLoadStrategy } from './load_strategies/XhrLoadStrategy';
export { Loader } from './Loader';
export { Resource } from './Resource';
export { ResourceType, ResourceState } from './resource_type';

import { AsyncQueue } from './async/AsyncQueue';
import { eachSeries } from './async/eachSeries';

export const async = { AsyncQueue, eachSeries };
25 changes: 18 additions & 7 deletions src/load_strategies/AbstractLoadStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { Signal } from 'type-signals';
import { ResourceType } from '../resource_type';

export type OnErrorSignal = (errMessage: string) => void;
export type OnCompleteSignal = (type: ResourceType, data: any) => void;
export type OnProgressSignal = (percent: number) => void;

export interface ILoadConfig
{
// The url for this resource, relative to the baseUrl of this loader.
Expand All @@ -21,18 +17,33 @@ export interface ILoadConfig
timeout?: number;
}

/**
* @category Type Aliases
*/
export namespace AbstractLoadStrategy
{
export type OnErrorSignal = (errMessage: string) => void;
export type OnCompleteSignal = (type: ResourceType, data: any) => void;
export type OnProgressSignal = (percent: number) => void;
}

/**
* Base load strategy interface that all custom load strategies
* are expected to inherit from and implement.
* @preferred
*/
export abstract class AbstractLoadStrategy<C extends ILoadConfig = ILoadConfig>
{
/**
* Dispatched when the resource fails to load.
*/
readonly onError = new Signal<OnErrorSignal>();
readonly onError: Signal<AbstractLoadStrategy.OnErrorSignal> = new Signal<AbstractLoadStrategy.OnErrorSignal>();

/**
* Dispatched once this resource has loaded, if there was an error it will
* be in the `error` property.
*/
readonly onComplete = new Signal<OnCompleteSignal>();
readonly onComplete: Signal<AbstractLoadStrategy.OnCompleteSignal> = new Signal<AbstractLoadStrategy.OnCompleteSignal>();

/**
* Dispatched each time progress of this resource load updates.
Expand All @@ -41,7 +52,7 @@ export abstract class AbstractLoadStrategy<C extends ILoadConfig = ILoadConfig>
* is being loaded on a modern browser, using XHR, and the remote server
* properly sets Content-Length headers, then this will be available.
*/
readonly onProgress = new Signal<OnProgressSignal>();
readonly onProgress: Signal<AbstractLoadStrategy.OnProgressSignal> = new Signal<AbstractLoadStrategy.OnProgressSignal>();

constructor(readonly config: C)
{ }
Expand Down
19 changes: 19 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@
"sourceMap": true,
"strict": true
},
"typedocOptions": {
"mode": "file",
"out": "docs",
"excludeExternals": true,
"excludeNotExported": true,
"excludePrivate": true,
"toc": [
"AbstractLoadStrategy",
"AudioLoadStrategy",
"ImageLoadStrategy",
"MediaElementLoadStrategy",
"VideoLoadStrategy",
"XhrLoadStrategy",
"Loader",
"Resource",
"ResourceType",
"ResourceState"
]
},
"include": [
"src/**/*",
"types/**/*"
Expand Down

0 comments on commit d4fb58c

Please sign in to comment.