-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scheduled): Adds schduled creation method
The `scheduled` creation method is meant to create an observable with a scheduler. The goal of this PR is to set us up to remove scheduling from very common code paths like `from` and `of`, amongst others. This deprecates versions of `from`, `concat`, `merge`, `startWith` and `endWith` that allow passing a scheduler. This PR also simplifies a few common code paths.
- Loading branch information
Showing
22 changed files
with
436 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * as _ from 'lodash'; | ||
|
||
function stringify(x: any): string { | ||
return JSON.stringify(x, function (key: string, value: any) { | ||
if (Array.isArray(value)) { | ||
return '[' + value | ||
.map(function (i) { | ||
return '\n\t' + stringify(i); | ||
}) + '\n]'; | ||
} | ||
return value; | ||
}) | ||
.replace(/\\"/g, '"') | ||
.replace(/\\t/g, '\t') | ||
.replace(/\\n/g, '\n'); | ||
} | ||
|
||
function deleteErrorNotificationStack(marble: any) { | ||
const { notification } = marble; | ||
if (notification) { | ||
const { kind, error } = notification; | ||
if (kind === 'E' && error instanceof Error) { | ||
notification.error = { name: error.name, message: error.message }; | ||
} | ||
} | ||
return marble; | ||
} | ||
|
||
export function observableMatcher(actual: any, expected: any) { | ||
if (Array.isArray(actual) && Array.isArray(expected)) { | ||
actual = actual.map(deleteErrorNotificationStack); | ||
expected = expected.map(deleteErrorNotificationStack); | ||
const passed = _.isEqual(actual, expected); | ||
if (passed) { | ||
return; | ||
} | ||
|
||
let message = '\nExpected \n'; | ||
actual.forEach((x: any) => message += `\t${stringify(x)}\n`); | ||
|
||
message += '\t\nto deep equal \n'; | ||
expected.forEach((x: any) => message += `\t${stringify(x)}\n`); | ||
|
||
chai.assert(passed, message); | ||
} else { | ||
chai.assert.deepEqual(actual, expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { scheduled, of } from 'rxjs'; | ||
import { TestScheduler } from 'rxjs/testing'; | ||
import { lowerCaseO } from '../helpers/test-helper'; | ||
import { observableMatcher } from '../helpers/observableMatcher'; | ||
import { expect } from 'chai'; | ||
|
||
describe('scheduled', () => { | ||
let testScheduler: TestScheduler; | ||
|
||
beforeEach(() => { | ||
testScheduler = new TestScheduler(observableMatcher); | ||
}); | ||
|
||
it('should schedule a sync observable', () => { | ||
const input = of('a', 'b', 'c'); | ||
testScheduler.run(({ expectObservable }) => { | ||
expectObservable(scheduled(input, testScheduler)).toBe('(abc|)'); | ||
}); | ||
}); | ||
|
||
it('should schedule an array', () => { | ||
const input = ['a', 'b', 'c']; | ||
testScheduler.run(({ expectObservable }) => { | ||
expectObservable(scheduled(input, testScheduler)).toBe('(abc|)'); | ||
}); | ||
}); | ||
|
||
it('should schedule an iterable', () => { | ||
const input = 'abc'; // strings are iterables | ||
testScheduler.run(({ expectObservable }) => { | ||
expectObservable(scheduled(input, testScheduler)).toBe('(abc|)'); | ||
}); | ||
}); | ||
|
||
it('should schedule an observable-like', () => { | ||
const input = lowerCaseO('a', 'b', 'c'); // strings are iterables | ||
testScheduler.run(({ expectObservable }) => { | ||
expectObservable(scheduled(input, testScheduler)).toBe('(abc|)'); | ||
}); | ||
}); | ||
|
||
it('should schedule a promise', done => { | ||
const results: any[] = []; | ||
const input = Promise.resolve('x'); // strings are iterables | ||
scheduled(input, testScheduler).subscribe({ | ||
next(value) { results.push(value); }, | ||
complete() { results.push('done'); }, | ||
}); | ||
|
||
expect(results).to.deep.equal([]); | ||
|
||
// Promises force async, so we can't schedule synchronously, no matter what. | ||
testScheduler.flush(); | ||
expect(results).to.deep.equal([]); | ||
|
||
Promise.resolve().then(() => { | ||
// NOW it should work, as the other promise should have resolved. | ||
testScheduler.flush(); | ||
expect(results).to.deep.equal(['x', 'done']); | ||
done(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.