src/redata.js
function create(loader, shouldReload = defaultShouldReload, mapper = defaultMapper, initialCtx = defaultInitialCtx) {
    // Initialise context, which is passed around and holds the lastData.
    const ctx = initialCtx; // TODO: instead of providing a full initial context, the redata() function below should receive
                            // a third argument, initialData, which is stored in the ctx. This allows for composed
                            // redatas to be fed initially.

    function redata(params, onUpdate) {
// console.log('redata()', params);
        // If should not reload the data.
        if (!shouldReload(params)) {
console.log('will not reload');
            // Inform any subscriber.
            onUpdate && onUpdate(ctx.lastData);

            // Resolve with last data.
            return Promise.resolve(ctx.lastData);
        }
console.log('will reload');
        // Data not valid, will load new data and subscribe to its updates.
        // Reset lastData, since it is no longer valid.
        ctx.lastData = { ...defaultInitialData };
// debugger;
        // Update any subscriber about new data.
        onUpdate && onUpdate(ctx.lastData);

        // Store promise reference in order to check which is the last one if multiple resolve.
        ctx.promise = load(ctx, loader, params, onUpdate);
// console.log('storing ctx promise', ctx.promise);
ctx.promise.loader = loader;
        return ctx.promise;
    }

    // Store shouldReload and mapper for future reference in redata compositions.
    redata.shouldReload = shouldReload;
    redata.map = mapper;

    return redata;
}
function create(loader, shouldReload = defaultShouldReload, mapper = defaultMapper, initialCtx = defaultInitialCtx) {
    // Initialise context, which is passed around and holds the lastData.
    const ctx = initialCtx; // TODO: instead of providing a full initial context, the redata() function below should receive
                            // a third argument, initialData, which is stored in the ctx. This allows for composed
                            // redatas to be fed initially.

    function redata(params, onUpdate) {
// console.log('redata()', params);
        // If should not reload the data.
        if (!shouldReload(params)) {
console.log('will not reload');
            // Inform any subscriber.
            onUpdate && onUpdate(ctx.lastData);

            // Resolve with last data.
            return Promise.resolve(ctx.lastData);
        }
console.log('will reload');
        // Data not valid, will load new data and subscribe to its updates.
        // Reset lastData, since it is no longer valid.
        ctx.lastData = { ...defaultInitialData };
// debugger;
        // Update any subscriber about new data.
        onUpdate && onUpdate(ctx.lastData);

        // Store promise reference in order to check which is the last one if multiple resolve.
        ctx.promise = load(ctx, loader, params, onUpdate);
// console.log('storing ctx promise', ctx.promise);
ctx.promise.loader = loader;
        return ctx.promise;
    }

    // Store shouldReload and mapper for future reference in redata compositions.
    redata.shouldReload = shouldReload;
    redata.map = mapper;

    return redata;
}
function create ( loader , shouldReload = defaultShouldReload , mapper = defaultMapper , initialCtx = defaultInitialCtx ) { // Initialise context, which is passed around and holds the lastData. const ctx = initialCtx ; // TODO: instead of providing a full initial context, the redata() function below should receive // a third argument, initialData, which is stored in the ctx. This allows for composed // redatas to be fed initially. function redata ( params , onUpdate ) { // console.log('redata()', params); // If should not reload the data. if ( ! shouldReload ( params ) ) { console . log ( 'will not reload' ) ; // Inform any subscriber. onUpdate && onUpdate ( ctx . lastData ) ; // Resolve with last data. return Promise . resolve ( ctx . lastData ) ; } console . log ( 'will reload' ) ; // Data not valid, will load new data and subscribe to its updates. // Reset lastData, since it is no longer valid. ctx . lastData = { ... defaultInitialData } ; // debugger; // Update any subscriber about new data. onUpdate && onUpdate ( ctx . lastData ) ; // Store promise reference in order to check which is the last one if multiple resolve. ctx . promise = load ( ctx , loader , params , onUpdate ) ; // console.log('storing ctx promise', ctx.promise); ctx . promise . loader = loader ; return ctx . promise ; } // Store shouldReload and mapper for future reference in redata compositions. redata . shouldReload = shouldReload ; redata . map = mapper ; return redata ; }
redata's data object definition
@typedef {Object} Data
@property {boolean} loading - true if the loader is still running, false otherwise.
@property {error} error - instance of Error in case the loader failed, undefined otherwise.
@property {*} result - result of the loader, or undefined if the loader is still running.
 
@param {function} loader - called to fetch new data
@param {function} [shouldReload = defaultShouldReload] - decides whether a redata should occur
@param {function} [mapper = defaultMapper] - maps the {@link Data} object values to your component's props
@param {object} [initialCtx = defaultInitialCtx] - starting point of redata, defines if there's any preloaded data
@returns {function} redata wrapper function
redata s data object definition