All files / web/bundles/pimui/js/fetcher fetcher-registry.js

96% Statements 24/25
90% Branches 9/10
85.71% Functions 6/7
96% Lines 24/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74    1183x   1183x               2352x 1162x 1162x 1162x 1162x   1162x 34860x 34860x 34860x     1162x 34860x 34860x 34860x 34860x     1162x 1162x   1162x     2352x                     38444x   38444x                                 727x 21810x          
'use strict';
 
define(['jquery', 'underscore', 'pim/base-fetcher', 'require-context'],
function ($, _, BaseFetcher, requireContext) {
    return {
        fetchers: {},
        initializePromise: null,
 
        /**
         * @return Promise
         */
        initialize: function () {
            if (null === this.initializePromise) {
                var fetcherList = __moduleConfig.fetchers
                var deferred = $.Deferred();
                var defaultFetcher = 'pim/base-fetcher'
                var fetchers = {};
 
                _.each(fetcherList, function (config, name) {
                    config = _.isString(config) ? { module: config } : config;
                    config.options = config.options || { };
                    fetchers[name] = config;
                });
 
                for (var fetcher in fetcherList) {
                    var moduleName = fetcherList[fetcher].module || defaultFetcher
                    var ResolvedModule = requireContext(moduleName);
                    fetchers[fetcher].loadedModule = new ResolvedModule(fetchers[fetcher].options)
                    fetchers[fetcher].options = fetcherList[fetcher].options
                }
 
                this.fetchers = fetchers;
                deferred.resolve();
 
                this.initializePromise = deferred.promise();
            }
 
            return this.initializePromise;
        },
 
        /**
         * Get the related fetcher for the given collection name
         *
         * @param {String} entityType
         *
         * @return Fetcher
         */
        getFetcher: function (entityType) {
            var fetcher = (this.fetchers[entityType] || this.fetchers.default)
 
            return fetcher.loadedModule;
        },
 
        /**
         * Clear the fetcher cache for the given collection name
         *
         * @param {String}         entityType
         * @param {String|integer} entity
         */
        clear: function (entityType, entity) {
            return this.getFetcher(entityType).clear(entity);
        },
 
        /**
         * Clear all fetchers cache
         */
        clearAll: function () {
            _.each(this.fetchers, function (fetcher) {
                fetcher.loadedModule.clear();
            });
        }
    };
});