diff --git a/src/core/Application.js b/src/core/Application.js index 7e952cb..2fcc729 100644 --- a/src/core/Application.js +++ b/src/core/Application.js @@ -39,6 +39,7 @@ * @param {boolean} [options.legacy=false] - If true Pixi will aim to ensure compatibility * with older / less advanced devices. If you experience unexplained flickering try setting this to true. * @param {boolean} [options.sharedTicker=false] - `true` to use PIXI.ticker.shared, `false` to create new ticker. + * @param {boolean} [options.sharedLoader=false] - `true` to use PIXI.loaders.shared, `false` to create new Loader. */ constructor(options, arg2, arg3, arg4, arg5) { @@ -53,10 +54,15 @@ }, arg3); } - // Set the default options - options = Object.assign({ + /** + * The default options, so we mixin functionality later. + * @member {object} + * @protected + */ + this._options = options = Object.assign({ sharedTicker: false, forceCanvas: false, + sharedLoader: false, }, options); /** @@ -167,5 +173,7 @@ this.renderer.destroy(removeView); this.renderer = null; + + this._options = null; } } diff --git a/src/core/Application.js b/src/core/Application.js index 7e952cb..2fcc729 100644 --- a/src/core/Application.js +++ b/src/core/Application.js @@ -39,6 +39,7 @@ * @param {boolean} [options.legacy=false] - If true Pixi will aim to ensure compatibility * with older / less advanced devices. If you experience unexplained flickering try setting this to true. * @param {boolean} [options.sharedTicker=false] - `true` to use PIXI.ticker.shared, `false` to create new ticker. + * @param {boolean} [options.sharedLoader=false] - `true` to use PIXI.loaders.shared, `false` to create new Loader. */ constructor(options, arg2, arg3, arg4, arg5) { @@ -53,10 +54,15 @@ }, arg3); } - // Set the default options - options = Object.assign({ + /** + * The default options, so we mixin functionality later. + * @member {object} + * @protected + */ + this._options = options = Object.assign({ sharedTicker: false, forceCanvas: false, + sharedLoader: false, }, options); /** @@ -167,5 +173,7 @@ this.renderer.destroy(removeView); this.renderer = null; + + this._options = null; } } diff --git a/src/deprecation.js b/src/deprecation.js index b888ad5..33daeec 100644 --- a/src/deprecation.js +++ b/src/deprecation.js @@ -998,67 +998,73 @@ }, }); -Object.defineProperties(loaders.Resource.prototype, { - isJson: { - get() - { - warn('The isJson property is deprecated, please use `resource.type === Resource.TYPE.JSON`.'); +if (loaders.Loader) +{ + const Resource = loaders.Resource; + const Loader = loaders.Loader; - return this.type === loaders.Loader.Resource.TYPE.JSON; + Object.defineProperties(Resource.prototype, { + isJson: { + get() + { + warn('The isJson property is deprecated, please use `resource.type === Resource.TYPE.JSON`.'); + + return this.type === Resource.TYPE.JSON; + }, }, - }, - isXml: { - get() - { - warn('The isXml property is deprecated, please use `resource.type === Resource.TYPE.XML`.'); + isXml: { + get() + { + warn('The isXml property is deprecated, please use `resource.type === Resource.TYPE.XML`.'); - return this.type === loaders.Loader.Resource.TYPE.XML; + return this.type === Resource.TYPE.XML; + }, }, - }, - isImage: { - get() - { - warn('The isImage property is deprecated, please use `resource.type === Resource.TYPE.IMAGE`.'); + isImage: { + get() + { + warn('The isImage property is deprecated, please use `resource.type === Resource.TYPE.IMAGE`.'); - return this.type === loaders.Loader.Resource.TYPE.IMAGE; + return this.type === Resource.TYPE.IMAGE; + }, }, - }, - isAudio: { - get() - { - warn('The isAudio property is deprecated, please use `resource.type === Resource.TYPE.AUDIO`.'); + isAudio: { + get() + { + warn('The isAudio property is deprecated, please use `resource.type === Resource.TYPE.AUDIO`.'); - return this.type === loaders.Loader.Resource.TYPE.AUDIO; + return this.type === Resource.TYPE.AUDIO; + }, }, - }, - isVideo: { - get() - { - warn('The isVideo property is deprecated, please use `resource.type === Resource.TYPE.VIDEO`.'); + isVideo: { + get() + { + warn('The isVideo property is deprecated, please use `resource.type === Resource.TYPE.VIDEO`.'); - return this.type === loaders.Loader.Resource.TYPE.VIDEO; + return this.type === Resource.TYPE.VIDEO; + }, }, - }, -}); + }); -Object.defineProperties(loaders.Loader.prototype, { - before: { - get() - { - warn('The before() method is deprecated, please use pre().'); + Object.defineProperties(Loader.prototype, { + before: { + get() + { + warn('The before() method is deprecated, please use pre().'); - return this.pre; + return this.pre; + }, }, - }, - after: { - get() - { - warn('The after() method is deprecated, please use use().'); + after: { + get() + { + warn('The after() method is deprecated, please use use().'); - return this.use; + return this.use; + }, }, - }, -}); + }); +} /** * @name PIXI.interaction.interactiveTarget#defaultCursor diff --git a/src/core/Application.js b/src/core/Application.js index 7e952cb..2fcc729 100644 --- a/src/core/Application.js +++ b/src/core/Application.js @@ -39,6 +39,7 @@ * @param {boolean} [options.legacy=false] - If true Pixi will aim to ensure compatibility * with older / less advanced devices. If you experience unexplained flickering try setting this to true. * @param {boolean} [options.sharedTicker=false] - `true` to use PIXI.ticker.shared, `false` to create new ticker. + * @param {boolean} [options.sharedLoader=false] - `true` to use PIXI.loaders.shared, `false` to create new Loader. */ constructor(options, arg2, arg3, arg4, arg5) { @@ -53,10 +54,15 @@ }, arg3); } - // Set the default options - options = Object.assign({ + /** + * The default options, so we mixin functionality later. + * @member {object} + * @protected + */ + this._options = options = Object.assign({ sharedTicker: false, forceCanvas: false, + sharedLoader: false, }, options); /** @@ -167,5 +173,7 @@ this.renderer.destroy(removeView); this.renderer = null; + + this._options = null; } } diff --git a/src/deprecation.js b/src/deprecation.js index b888ad5..33daeec 100644 --- a/src/deprecation.js +++ b/src/deprecation.js @@ -998,67 +998,73 @@ }, }); -Object.defineProperties(loaders.Resource.prototype, { - isJson: { - get() - { - warn('The isJson property is deprecated, please use `resource.type === Resource.TYPE.JSON`.'); +if (loaders.Loader) +{ + const Resource = loaders.Resource; + const Loader = loaders.Loader; - return this.type === loaders.Loader.Resource.TYPE.JSON; + Object.defineProperties(Resource.prototype, { + isJson: { + get() + { + warn('The isJson property is deprecated, please use `resource.type === Resource.TYPE.JSON`.'); + + return this.type === Resource.TYPE.JSON; + }, }, - }, - isXml: { - get() - { - warn('The isXml property is deprecated, please use `resource.type === Resource.TYPE.XML`.'); + isXml: { + get() + { + warn('The isXml property is deprecated, please use `resource.type === Resource.TYPE.XML`.'); - return this.type === loaders.Loader.Resource.TYPE.XML; + return this.type === Resource.TYPE.XML; + }, }, - }, - isImage: { - get() - { - warn('The isImage property is deprecated, please use `resource.type === Resource.TYPE.IMAGE`.'); + isImage: { + get() + { + warn('The isImage property is deprecated, please use `resource.type === Resource.TYPE.IMAGE`.'); - return this.type === loaders.Loader.Resource.TYPE.IMAGE; + return this.type === Resource.TYPE.IMAGE; + }, }, - }, - isAudio: { - get() - { - warn('The isAudio property is deprecated, please use `resource.type === Resource.TYPE.AUDIO`.'); + isAudio: { + get() + { + warn('The isAudio property is deprecated, please use `resource.type === Resource.TYPE.AUDIO`.'); - return this.type === loaders.Loader.Resource.TYPE.AUDIO; + return this.type === Resource.TYPE.AUDIO; + }, }, - }, - isVideo: { - get() - { - warn('The isVideo property is deprecated, please use `resource.type === Resource.TYPE.VIDEO`.'); + isVideo: { + get() + { + warn('The isVideo property is deprecated, please use `resource.type === Resource.TYPE.VIDEO`.'); - return this.type === loaders.Loader.Resource.TYPE.VIDEO; + return this.type === Resource.TYPE.VIDEO; + }, }, - }, -}); + }); -Object.defineProperties(loaders.Loader.prototype, { - before: { - get() - { - warn('The before() method is deprecated, please use pre().'); + Object.defineProperties(Loader.prototype, { + before: { + get() + { + warn('The before() method is deprecated, please use pre().'); - return this.pre; + return this.pre; + }, }, - }, - after: { - get() - { - warn('The after() method is deprecated, please use use().'); + after: { + get() + { + warn('The after() method is deprecated, please use use().'); - return this.use; + return this.use; + }, }, - }, -}); + }); +} /** * @name PIXI.interaction.interactiveTarget#defaultCursor diff --git a/src/index.js b/src/index.js index c553427..77da490 100644 --- a/src/index.js +++ b/src/index.js @@ -20,6 +20,14 @@ import { utils } from './core'; utils.mixins.performMixins(); +/** + * Alias for {@link PIXI.loaders.shared}. + * @name loader + * @memberof PIXI + * @type {PIXI.loader.Loader} + */ +const loader = loaders.shared || null; + export { accessibility, extract, @@ -30,18 +38,8 @@ mesh, particles, prepare, + loader, }; -/** - * A premade instance of the loader that can be used to load resources. - * - * @name loader - * @memberof PIXI - * @property {PIXI.loaders.Loader} - */ -const loader = loaders && loaders.Loader ? new loaders.Loader() : null; // check is there in case user excludes loader lib - -export { loader }; - // Always export pixi globally. global.PIXI = exports; // eslint-disable-line diff --git a/src/core/Application.js b/src/core/Application.js index 7e952cb..2fcc729 100644 --- a/src/core/Application.js +++ b/src/core/Application.js @@ -39,6 +39,7 @@ * @param {boolean} [options.legacy=false] - If true Pixi will aim to ensure compatibility * with older / less advanced devices. If you experience unexplained flickering try setting this to true. * @param {boolean} [options.sharedTicker=false] - `true` to use PIXI.ticker.shared, `false` to create new ticker. + * @param {boolean} [options.sharedLoader=false] - `true` to use PIXI.loaders.shared, `false` to create new Loader. */ constructor(options, arg2, arg3, arg4, arg5) { @@ -53,10 +54,15 @@ }, arg3); } - // Set the default options - options = Object.assign({ + /** + * The default options, so we mixin functionality later. + * @member {object} + * @protected + */ + this._options = options = Object.assign({ sharedTicker: false, forceCanvas: false, + sharedLoader: false, }, options); /** @@ -167,5 +173,7 @@ this.renderer.destroy(removeView); this.renderer = null; + + this._options = null; } } diff --git a/src/deprecation.js b/src/deprecation.js index b888ad5..33daeec 100644 --- a/src/deprecation.js +++ b/src/deprecation.js @@ -998,67 +998,73 @@ }, }); -Object.defineProperties(loaders.Resource.prototype, { - isJson: { - get() - { - warn('The isJson property is deprecated, please use `resource.type === Resource.TYPE.JSON`.'); +if (loaders.Loader) +{ + const Resource = loaders.Resource; + const Loader = loaders.Loader; - return this.type === loaders.Loader.Resource.TYPE.JSON; + Object.defineProperties(Resource.prototype, { + isJson: { + get() + { + warn('The isJson property is deprecated, please use `resource.type === Resource.TYPE.JSON`.'); + + return this.type === Resource.TYPE.JSON; + }, }, - }, - isXml: { - get() - { - warn('The isXml property is deprecated, please use `resource.type === Resource.TYPE.XML`.'); + isXml: { + get() + { + warn('The isXml property is deprecated, please use `resource.type === Resource.TYPE.XML`.'); - return this.type === loaders.Loader.Resource.TYPE.XML; + return this.type === Resource.TYPE.XML; + }, }, - }, - isImage: { - get() - { - warn('The isImage property is deprecated, please use `resource.type === Resource.TYPE.IMAGE`.'); + isImage: { + get() + { + warn('The isImage property is deprecated, please use `resource.type === Resource.TYPE.IMAGE`.'); - return this.type === loaders.Loader.Resource.TYPE.IMAGE; + return this.type === Resource.TYPE.IMAGE; + }, }, - }, - isAudio: { - get() - { - warn('The isAudio property is deprecated, please use `resource.type === Resource.TYPE.AUDIO`.'); + isAudio: { + get() + { + warn('The isAudio property is deprecated, please use `resource.type === Resource.TYPE.AUDIO`.'); - return this.type === loaders.Loader.Resource.TYPE.AUDIO; + return this.type === Resource.TYPE.AUDIO; + }, }, - }, - isVideo: { - get() - { - warn('The isVideo property is deprecated, please use `resource.type === Resource.TYPE.VIDEO`.'); + isVideo: { + get() + { + warn('The isVideo property is deprecated, please use `resource.type === Resource.TYPE.VIDEO`.'); - return this.type === loaders.Loader.Resource.TYPE.VIDEO; + return this.type === Resource.TYPE.VIDEO; + }, }, - }, -}); + }); -Object.defineProperties(loaders.Loader.prototype, { - before: { - get() - { - warn('The before() method is deprecated, please use pre().'); + Object.defineProperties(Loader.prototype, { + before: { + get() + { + warn('The before() method is deprecated, please use pre().'); - return this.pre; + return this.pre; + }, }, - }, - after: { - get() - { - warn('The after() method is deprecated, please use use().'); + after: { + get() + { + warn('The after() method is deprecated, please use use().'); - return this.use; + return this.use; + }, }, - }, -}); + }); +} /** * @name PIXI.interaction.interactiveTarget#defaultCursor diff --git a/src/index.js b/src/index.js index c553427..77da490 100644 --- a/src/index.js +++ b/src/index.js @@ -20,6 +20,14 @@ import { utils } from './core'; utils.mixins.performMixins(); +/** + * Alias for {@link PIXI.loaders.shared}. + * @name loader + * @memberof PIXI + * @type {PIXI.loader.Loader} + */ +const loader = loaders.shared || null; + export { accessibility, extract, @@ -30,18 +38,8 @@ mesh, particles, prepare, + loader, }; -/** - * A premade instance of the loader that can be used to load resources. - * - * @name loader - * @memberof PIXI - * @property {PIXI.loaders.Loader} - */ -const loader = loaders && loaders.Loader ? new loaders.Loader() : null; // check is there in case user excludes loader lib - -export { loader }; - // Always export pixi globally. global.PIXI = exports; // eslint-disable-line diff --git a/src/loaders/index.js b/src/loaders/index.js index 39669be..daa1fc4 100644 --- a/src/loaders/index.js +++ b/src/loaders/index.js @@ -1,3 +1,6 @@ +import Application from '../core/Application'; +import Loader from './loader'; + /** * This namespace contains APIs which extends the {@link https://github.com/englercj/resource-loader resource-loader} module * for loading assets, data, and other resources dynamically. @@ -11,7 +14,7 @@ * }); * @namespace PIXI.loaders */ -export { default as Loader } from './loader'; +export { Loader }; export { default as bitmapFontParser, parse as parseBitmapFontData } from './bitmapFontParser'; export { default as spritesheetParser, getResourcePath } from './spritesheetParser'; export { default as textureParser } from './textureParser'; @@ -23,3 +26,55 @@ * @memberof PIXI.loaders */ export { Resource } from 'resource-loader'; + +/** + * A premade instance of the loader that can be used to load resources. + * @name shared + * @memberof PIXI.loaders + * @type {PIXI.loaders.Loader} + */ +const shared = new Loader(); + +shared.destroy = () => +{ + // protect destroying shared loader +}; + +export { shared }; + +// Mixin the loader construction +const AppPrototype = Application.prototype; + +AppPrototype._loader = null; + +/** + * Loader instance to help with asset loading. + * @name PIXI.Application#loader + * @type {PIXI.loaders.Loader} + */ +Object.defineProperty(AppPrototype, 'loader', { + get() + { + if (!this._loader) + { + const sharedLoader = this._options.sharedLoader; + + this._loader = sharedLoader ? shared : new Loader(); + } + + return this._loader; + }, +}); + +// Override the destroy function +// making sure to destroy the current Loader +AppPrototype._parentDestroy = AppPrototype.destroy; +AppPrototype.destroy = function destroy() +{ + if (this._loader) + { + this._loader.destroy(); + this._loader = null; + } + this._parentDestroy(); +}; diff --git a/src/core/Application.js b/src/core/Application.js index 7e952cb..2fcc729 100644 --- a/src/core/Application.js +++ b/src/core/Application.js @@ -39,6 +39,7 @@ * @param {boolean} [options.legacy=false] - If true Pixi will aim to ensure compatibility * with older / less advanced devices. If you experience unexplained flickering try setting this to true. * @param {boolean} [options.sharedTicker=false] - `true` to use PIXI.ticker.shared, `false` to create new ticker. + * @param {boolean} [options.sharedLoader=false] - `true` to use PIXI.loaders.shared, `false` to create new Loader. */ constructor(options, arg2, arg3, arg4, arg5) { @@ -53,10 +54,15 @@ }, arg3); } - // Set the default options - options = Object.assign({ + /** + * The default options, so we mixin functionality later. + * @member {object} + * @protected + */ + this._options = options = Object.assign({ sharedTicker: false, forceCanvas: false, + sharedLoader: false, }, options); /** @@ -167,5 +173,7 @@ this.renderer.destroy(removeView); this.renderer = null; + + this._options = null; } } diff --git a/src/deprecation.js b/src/deprecation.js index b888ad5..33daeec 100644 --- a/src/deprecation.js +++ b/src/deprecation.js @@ -998,67 +998,73 @@ }, }); -Object.defineProperties(loaders.Resource.prototype, { - isJson: { - get() - { - warn('The isJson property is deprecated, please use `resource.type === Resource.TYPE.JSON`.'); +if (loaders.Loader) +{ + const Resource = loaders.Resource; + const Loader = loaders.Loader; - return this.type === loaders.Loader.Resource.TYPE.JSON; + Object.defineProperties(Resource.prototype, { + isJson: { + get() + { + warn('The isJson property is deprecated, please use `resource.type === Resource.TYPE.JSON`.'); + + return this.type === Resource.TYPE.JSON; + }, }, - }, - isXml: { - get() - { - warn('The isXml property is deprecated, please use `resource.type === Resource.TYPE.XML`.'); + isXml: { + get() + { + warn('The isXml property is deprecated, please use `resource.type === Resource.TYPE.XML`.'); - return this.type === loaders.Loader.Resource.TYPE.XML; + return this.type === Resource.TYPE.XML; + }, }, - }, - isImage: { - get() - { - warn('The isImage property is deprecated, please use `resource.type === Resource.TYPE.IMAGE`.'); + isImage: { + get() + { + warn('The isImage property is deprecated, please use `resource.type === Resource.TYPE.IMAGE`.'); - return this.type === loaders.Loader.Resource.TYPE.IMAGE; + return this.type === Resource.TYPE.IMAGE; + }, }, - }, - isAudio: { - get() - { - warn('The isAudio property is deprecated, please use `resource.type === Resource.TYPE.AUDIO`.'); + isAudio: { + get() + { + warn('The isAudio property is deprecated, please use `resource.type === Resource.TYPE.AUDIO`.'); - return this.type === loaders.Loader.Resource.TYPE.AUDIO; + return this.type === Resource.TYPE.AUDIO; + }, }, - }, - isVideo: { - get() - { - warn('The isVideo property is deprecated, please use `resource.type === Resource.TYPE.VIDEO`.'); + isVideo: { + get() + { + warn('The isVideo property is deprecated, please use `resource.type === Resource.TYPE.VIDEO`.'); - return this.type === loaders.Loader.Resource.TYPE.VIDEO; + return this.type === Resource.TYPE.VIDEO; + }, }, - }, -}); + }); -Object.defineProperties(loaders.Loader.prototype, { - before: { - get() - { - warn('The before() method is deprecated, please use pre().'); + Object.defineProperties(Loader.prototype, { + before: { + get() + { + warn('The before() method is deprecated, please use pre().'); - return this.pre; + return this.pre; + }, }, - }, - after: { - get() - { - warn('The after() method is deprecated, please use use().'); + after: { + get() + { + warn('The after() method is deprecated, please use use().'); - return this.use; + return this.use; + }, }, - }, -}); + }); +} /** * @name PIXI.interaction.interactiveTarget#defaultCursor diff --git a/src/index.js b/src/index.js index c553427..77da490 100644 --- a/src/index.js +++ b/src/index.js @@ -20,6 +20,14 @@ import { utils } from './core'; utils.mixins.performMixins(); +/** + * Alias for {@link PIXI.loaders.shared}. + * @name loader + * @memberof PIXI + * @type {PIXI.loader.Loader} + */ +const loader = loaders.shared || null; + export { accessibility, extract, @@ -30,18 +38,8 @@ mesh, particles, prepare, + loader, }; -/** - * A premade instance of the loader that can be used to load resources. - * - * @name loader - * @memberof PIXI - * @property {PIXI.loaders.Loader} - */ -const loader = loaders && loaders.Loader ? new loaders.Loader() : null; // check is there in case user excludes loader lib - -export { loader }; - // Always export pixi globally. global.PIXI = exports; // eslint-disable-line diff --git a/src/loaders/index.js b/src/loaders/index.js index 39669be..daa1fc4 100644 --- a/src/loaders/index.js +++ b/src/loaders/index.js @@ -1,3 +1,6 @@ +import Application from '../core/Application'; +import Loader from './loader'; + /** * This namespace contains APIs which extends the {@link https://github.com/englercj/resource-loader resource-loader} module * for loading assets, data, and other resources dynamically. @@ -11,7 +14,7 @@ * }); * @namespace PIXI.loaders */ -export { default as Loader } from './loader'; +export { Loader }; export { default as bitmapFontParser, parse as parseBitmapFontData } from './bitmapFontParser'; export { default as spritesheetParser, getResourcePath } from './spritesheetParser'; export { default as textureParser } from './textureParser'; @@ -23,3 +26,55 @@ * @memberof PIXI.loaders */ export { Resource } from 'resource-loader'; + +/** + * A premade instance of the loader that can be used to load resources. + * @name shared + * @memberof PIXI.loaders + * @type {PIXI.loaders.Loader} + */ +const shared = new Loader(); + +shared.destroy = () => +{ + // protect destroying shared loader +}; + +export { shared }; + +// Mixin the loader construction +const AppPrototype = Application.prototype; + +AppPrototype._loader = null; + +/** + * Loader instance to help with asset loading. + * @name PIXI.Application#loader + * @type {PIXI.loaders.Loader} + */ +Object.defineProperty(AppPrototype, 'loader', { + get() + { + if (!this._loader) + { + const sharedLoader = this._options.sharedLoader; + + this._loader = sharedLoader ? shared : new Loader(); + } + + return this._loader; + }, +}); + +// Override the destroy function +// making sure to destroy the current Loader +AppPrototype._parentDestroy = AppPrototype.destroy; +AppPrototype.destroy = function destroy() +{ + if (this._loader) + { + this._loader.destroy(); + this._loader = null; + } + this._parentDestroy(); +}; diff --git a/src/loaders/loader.js b/src/loaders/loader.js index 767a411..59b8000 100644 --- a/src/loaders/loader.js +++ b/src/loaders/loader.js @@ -90,6 +90,15 @@ { Loader._pixiMiddleware.push(fn); } + + /** + * Destroy the loader, removes references. + */ + destroy() + { + this.removeAllListeners(); + this.reset(); + } } // Copy EE3 prototype (mixin)