Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom creation of response object #1639

Closed
wants to merge 7 commits into from

Conversation

pnstickne
Copy link
Contributor

Per #1591

A fairly-generic method of intercepting request creation and specifying custom request objects (and configurations therein). Works for XHR / Image / Audio as well as "fine tuning" the URL or whatnot.

Usage for custom XHR headers looks like:

loader.onCreatingRequest.add(createRequest, this);

function createRequest (loader, r)
{
    if (r.requestType === 'xhr' && shouldCustomizeRequestForUrl(r.url))
    {
        var xhr = loader.createXMLHttpRequest(r.url);
        // any relevant modification to XHR / request can be done here
        // (including providing a fake proxy object or using a different URL entirely)
        // but there are some restrictions and the xhr.responseType will be overridden
        xhr.setRequestHeader('custom-header', 'value');
        r.requestObject = xhr;
    }
}

- Fixes a small bug with URL selection
- Semi-universal method of allow user code to perform "advanced"
  request configutation such as adding additional headers to XHR.

- Also works with images and audio
- Fixed bug that could cause a TypeError (thanks XDR :>)

- Made createXMLHttpRequest more friendly for consumers of the
  onCreateRequest

- Renamed onCreateRequest to onCreatingRequest
@pnstickne
Copy link
Contributor Author

Grr. "npm install failed" for Travis CI - "no 'build' task"

@photonstorm
Copy link
Collaborator

How safe is this to merge this close to release? At least I've extensively tested the current 2.3 Loader through several large client projects, but I can see lots of underlying structure changes here.

@pnstickne
Copy link
Contributor Author

I would say 'fairly safe'. Most of the structure changes come from unifying XDR+XHR code (of which I have sadly not tested for XDR, but XHR works as expected).

The building (and extraction of) the request configuration doesn't really affect the structure (excluding the basic assignment into and out of); any new semantic differences should only be observed if adding an onCreatingRequest handler and altering the supplied values.

Some of the image/audio callback code is updated for consistency and I have not run into any regression cases.

@photonstorm
Copy link
Collaborator

I'm afraid I'm going to leave this one be. I'm happy with how the Loader currently works, and will leave features like this for Lazer.

@photonstorm photonstorm closed this Feb 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants