Skip to content

Commit

Permalink
feat(http): allow lazy (cancellable) requests
Browse files Browse the repository at this point in the history
PR #388

ISSUES CLOSED: #355
  • Loading branch information
storybook-safe-bot authored and staltz committed Aug 15, 2016
1 parent 3ea362d commit 2417bd6
Show file tree
Hide file tree
Showing 11 changed files with 278 additions and 4 deletions.
34 changes: 34 additions & 0 deletions http/src/MainHTTPSource.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions http/src/MainHTTPSource.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

154 changes: 154 additions & 0 deletions http/src/http-driver.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions http/src/http-driver.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions http/src/http-driver.ts
Expand Up @@ -128,14 +128,17 @@ function normalizeRequestInput(reqOptions: RequestInput): RequestOptions {
function makeRequestInputToResponse$(runStreamAdapter: StreamAdapter) {
return function requestInputToResponse$(reqInput: RequestInput): MemoryStream<Response> & ResponseStream {
let response$ = createResponse$(reqInput).remember();
/* tslint:disable:no-empty */
response$.addListener({next: () => {}, error: () => {}, complete: () => {}});
/* tslint:enable:no-empty */
let reqOptions = softNormalizeRequestInput(reqInput);
if (!reqOptions.lazy) {
/* tslint:disable:no-empty */
response$.addListener({next: () => {}, error: () => {}, complete: () => {}});
/* tslint:enable:no-empty */
}
response$ = (runStreamAdapter) ?
runStreamAdapter.adapt(response$, XStreamAdapter.streamSubscribe) :
response$;
Object.defineProperty(response$, 'request', <PropertyDescriptor> {
value: softNormalizeRequestInput(reqInput),
value: reqOptions,
writable: false,
});
return <MemoryStream<Response> & ResponseStream> response$;
Expand Down
2 changes: 2 additions & 0 deletions http/src/interfaces.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions http/src/interfaces.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions http/src/interfaces.ts
Expand Up @@ -20,6 +20,7 @@ export interface RequestOptions {
withCredentials?: boolean;
redirects?: number;
category?: string;
lazy?: boolean;
_error?: any;
_namespace?: Array<string>;
}
Expand Down
21 changes: 21 additions & 0 deletions http/src/isolate.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions http/src/isolate.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions http/test/node.js
Expand Up @@ -39,6 +39,61 @@ describe('HTTP Driver in Node.js', function () {
}
);

it('should not auto-execute lazy request without listening to response stream',
function(done) {
function main() {
return {
HTTP: Rx.Observable.of({
url: uri + '/pet',
method: 'POST',
send: {name: 'Woof', species: 'Dog'},
lazy: true
})
}
}

var output = Cycle(main, { HTTP: makeHTTPDriver() });
globalSandbox.petPOSTResponse = null;
output.run();

setTimeout(function () {
assert.strictEqual(globalSandbox.petPOSTResponse, null);
done();
}, 250);
}
);

it('should execute lazy HTTP request when listening to response stream',
function(done) {
function main() {
return {
HTTP: Rx.Observable.of({
url: uri + '/pet',
method: 'POST',
send: {name: 'Woof', species: 'Dog'},
lazy: true
})
}
}

var output = Cycle(main, { HTTP: makeHTTPDriver() });
globalSandbox.petPOSTResponse = null;

output.sources.HTTP.response$$
.mergeAll()
.subscribe();

output.run();

setTimeout(function () {
assert.notStrictEqual(globalSandbox.petPOSTResponse, null);
assert.strictEqual(globalSandbox.petPOSTResponse, 'added Woof the Dog');
globalSandbox.petPOSTResponse = null;
done();
}, 250);
}
);

it('should add request options object to each response',
function(done) {
function main() {
Expand Down

0 comments on commit 2417bd6

Please sign in to comment.