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

[kbn/std] add observable helpers to aid with rxjs 7 upgrade #79752

Merged
merged 21 commits into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
ea946c7
[kbn/std] add observable helpers to aid with rxjs 7 upgrade
spalger Oct 6, 2020
798cdbc
remove unused imports
spalger Oct 6, 2020
64dcdd2
replace snapshot
spalger Oct 6, 2020
43ca49a
Merge branch 'master' of github.com:elastic/kibana into implement/obs…
spalger Oct 6, 2020
c41a01c
add tests for observable helpers
spalger Oct 6, 2020
fab8139
fix copy-paste typo
spalger Oct 6, 2020
209d8c9
Merge branch 'master' of github.com:elastic/kibana into implement/obs…
spalger Oct 7, 2020
de1bad1
Merge branch 'master' of github.com:elastic/kibana into implement/obs…
spalger Oct 7, 2020
6880d55
list specific exports to prevent bundling tslib
spalger Oct 7, 2020
252268f
Merge branch 'master' of github.com:elastic/kibana into implement/obs…
spalger Oct 7, 2020
02a666f
Merge branch 'master' of github.com:elastic/kibana into implement/obs…
spalger Oct 7, 2020
436fdab
use export * now that tslib is shared
spalger Oct 7, 2020
e2a1c27
Merge branch 'master' of github.com:elastic/kibana into implement/obs…
spalger Oct 7, 2020
7c964c7
Merge branch 'master' of github.com:elastic/kibana into implement/obs…
spalger Oct 8, 2020
b5fb731
limit helpers to those needed for rxjs 7 migration
spalger Oct 8, 2020
1e94a61
Merge branch 'master' of github.com:elastic/kibana into implement/obs…
spalger Oct 8, 2020
664a160
update license text
spalger Oct 8, 2020
36c4d18
Merge branch 'master' of github.com:elastic/kibana into implement/obs…
spalger Oct 8, 2020
8b08791
remove duplicate bootstrap notice
spalger Oct 8, 2020
0c40b3e
fix notice generation to exclude build directories in x-pack plugins
spalger Oct 8, 2020
d45630b
update tests to account for actual RxJS 7 implementation
spalger Oct 8, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/kbn-optimizer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ const config = OptimizerConfig.create({
dist: true
});

await runOptimizer(config)
.pipe(logOptimizerState(log, config))
.toPromise();
await lastValueFrom(
runOptimizer(config).pipe(logOptimizerState(log, config))
);
```

This is essentially what we're doing in [`script/build_kibana_platform_plugins`][Cli] and the new [build system task][BuildTask].
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-optimizer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@babel/core": "^7.11.6",
"@kbn/babel-preset": "1.0.0",
"@kbn/dev-utils": "1.0.0",
"@kbn/std": "1.0.0",
"@kbn/ui-shared-deps": "1.0.0",
"autoprefixer": "^9.7.4",
"babel-loader": "^8.0.6",
Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-optimizer/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import 'source-map-support/register';
import Path from 'path';

import { REPO_ROOT } from '@kbn/utils';
import { lastValueFrom } from '@kbn/std';
import { run, createFlagError, CiStatsReporter } from '@kbn/dev-utils';

import { logOptimizerState } from './log_optimizer_state';
Expand Down Expand Up @@ -136,7 +137,7 @@ run(
update$ = update$.pipe(reportOptimizerStats(reporter, config, log));
}

await update$.pipe(logOptimizerState(log, config)).toPromise();
await lastValueFrom(update$.pipe(logOptimizerState(log, config)));

if (updateLimits) {
updateBundleLimits(log, config);
Expand Down
77 changes: 39 additions & 38 deletions packages/kbn-optimizer/src/common/event_stream_helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@
*/

import * as Rx from 'rxjs';
import { toArray, take } from 'rxjs/operators';
import { take } from 'rxjs/operators';
import { allValuesFrom } from '@kbn/std';

import { summarizeEventStream } from './event_stream_helpers';

it('emits each state with each event, ignoring events when summarizer returns undefined', async () => {
const event$ = Rx.of(1, 2, 3, 4, 5);
const initial = 0;
const values = await summarizeEventStream(event$, initial, (state, event) => {
if (event % 2) {
return state + event;
}
})
.pipe(toArray())
.toPromise();
const values = await allValuesFrom(
summarizeEventStream(event$, initial, (state, event) => {
if (event % 2) {
return state + event;
}
})
);

expect(values).toMatchInlineSnapshot(`
Array [
Expand All @@ -57,15 +58,15 @@ it('emits each state with each event, ignoring events when summarizer returns un
it('interleaves injected events when source is synchronous', async () => {
const event$ = Rx.of(1, 7);
const initial = 0;
const values = await summarizeEventStream(event$, initial, (state, event, injectEvent) => {
if (event < 5) {
injectEvent(event + 2);
}
const values = await allValuesFrom(
summarizeEventStream(event$, initial, (state, event, injectEvent) => {
if (event < 5) {
injectEvent(event + 2);
}

return state + event;
})
.pipe(toArray())
.toPromise();
return state + event;
})
);

expect(values).toMatchInlineSnapshot(`
Array [
Expand Down Expand Up @@ -95,15 +96,15 @@ it('interleaves injected events when source is synchronous', async () => {
it('interleaves injected events when source is asynchronous', async () => {
const event$ = Rx.of(1, 7, Rx.asyncScheduler);
const initial = 0;
const values = await summarizeEventStream(event$, initial, (state, event, injectEvent) => {
if (event < 5) {
injectEvent(event + 2);
}
const values = await allValuesFrom(
summarizeEventStream(event$, initial, (state, event, injectEvent) => {
if (event < 5) {
injectEvent(event + 2);
}

return state + event;
})
.pipe(toArray())
.toPromise();
return state + event;
})
);

expect(values).toMatchInlineSnapshot(`
Array [
Expand Down Expand Up @@ -133,17 +134,17 @@ it('interleaves injected events when source is asynchronous', async () => {
it('interleaves mulitple injected events in order', async () => {
const event$ = Rx.of(1);
const initial = 0;
const values = await summarizeEventStream(event$, initial, (state, event, injectEvent) => {
if (event < 10) {
injectEvent(10);
injectEvent(20);
injectEvent(30);
}

return state + event;
})
.pipe(toArray())
.toPromise();
const values = await allValuesFrom(
summarizeEventStream(event$, initial, (state, event, injectEvent) => {
if (event < 10) {
injectEvent(10);
injectEvent(20);
injectEvent(30);
}

return state + event;
})
);

expect(values).toMatchInlineSnapshot(`
Array [
Expand Down Expand Up @@ -179,9 +180,9 @@ it('stops an infinite stream when unsubscribed', async () => {
return prev + event;
});

const values = await summarizeEventStream(event$, initial, summarize)
.pipe(take(11), toArray())
.toPromise();
const values = await allValuesFrom(
summarizeEventStream(event$, initial, summarize).pipe(take(11))
);

expect(values).toMatchInlineSnapshot(`
Array [
Expand Down
11 changes: 6 additions & 5 deletions packages/kbn-optimizer/src/common/rxjs_helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import * as Rx from 'rxjs';
import { toArray, map } from 'rxjs/operators';
import { lastValueFrom } from '@kbn/std';

import { pipeClosure, debounceTimeBuffer, maybeMap, maybe } from './rxjs_helpers';

Expand All @@ -36,21 +37,21 @@ describe('pipeClosure()', () => {
toArray()
);

await expect(foo$.toPromise()).resolves.toMatchInlineSnapshot(`
await expect(lastValueFrom(foo$)).resolves.toMatchInlineSnapshot(`
Array [
1,
2,
3,
]
`);
await expect(foo$.toPromise()).resolves.toMatchInlineSnapshot(`
await expect(lastValueFrom(foo$)).resolves.toMatchInlineSnapshot(`
Array [
2,
4,
6,
]
`);
await expect(foo$.toPromise()).resolves.toMatchInlineSnapshot(`
await expect(lastValueFrom(foo$)).resolves.toMatchInlineSnapshot(`
Array [
3,
6,
Expand All @@ -64,7 +65,7 @@ describe('maybe()', () => {
it('filters out undefined values from the stream', async () => {
const foo$ = Rx.of(1, undefined, 2, undefined, 3).pipe(maybe(), toArray());

await expect(foo$.toPromise()).resolves.toEqual([1, 2, 3]);
await expect(lastValueFrom(foo$)).resolves.toEqual([1, 2, 3]);
});
});

Expand All @@ -75,7 +76,7 @@ describe('maybeMap()', () => {
toArray()
);

await expect(foo$.toPromise()).resolves.toEqual([1, 3, 5]);
await expect(lastValueFrom(foo$)).resolves.toEqual([1, 3, 5]);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { inspect } from 'util';

import cpy from 'cpy';
import del from 'del';
import { toArray, tap, filter } from 'rxjs/operators';
import { tap, filter } from 'rxjs/operators';
import { REPO_ROOT } from '@kbn/utils';
import { ToolingLog } from '@kbn/dev-utils';
import {
Expand All @@ -34,6 +34,7 @@ import {
logOptimizerState,
readLimits,
} from '@kbn/optimizer';
import { allValuesFrom } from '@kbn/std';

const TMP_DIR = Path.resolve(__dirname, '../__fixtures__/__tmp__');
const MOCK_REPO_SRC = Path.resolve(__dirname, '../__fixtures__/mock_repo');
Expand Down Expand Up @@ -83,13 +84,12 @@ it('builds expected bundles, saves bundle counts to metadata', async () => {

expect(config).toMatchSnapshot('OptimizerConfig');

const msgs = await runOptimizer(config)
.pipe(
const msgs = await allValuesFrom(
runOptimizer(config).pipe(
logOptimizerState(log, config),
filter((x) => x.event?.type !== 'worker stdio'),
toArray()
filter((x) => x.event?.type !== 'worker stdio')
)
.toPromise();
);

const assert = (statement: string, truth: boolean, altStates?: OptimizerUpdate[]) => {
if (!truth) {
Expand Down Expand Up @@ -208,17 +208,16 @@ it('uses cache on second run and exist cleanly', async () => {
dist: false,
});

const msgs = await runOptimizer(config)
.pipe(
const msgs = await allValuesFrom(
runOptimizer(config).pipe(
tap((state) => {
if (state.event?.type === 'worker stdio') {
// eslint-disable-next-line no-console
console.log('worker', state.event.stream, state.event.line);
}
}),
toArray()
})
)
.toPromise();
);

expect(msgs.map((m) => m.state.phase)).toMatchInlineSnapshot(`
Array [
Expand All @@ -240,7 +239,7 @@ it('prepares assets for distribution', async () => {
dist: true,
});

await runOptimizer(config).pipe(logOptimizerState(log, config), toArray()).toPromise();
await allValuesFrom(runOptimizer(config).pipe(logOptimizerState(log, config)));

expectFileMatchesSnapshotWithCompression('plugins/foo/target/public/foo.plugin.js', 'foo bundle');
expectFileMatchesSnapshotWithCompression(
Expand Down
30 changes: 8 additions & 22 deletions packages/kbn-optimizer/src/integration_tests/bundle_cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import Path from 'path';

import cpy from 'cpy';
import del from 'del';
import { toArray } from 'rxjs/operators';
import { createAbsolutePathSerializer } from '@kbn/dev-utils';
import { allValuesFrom } from '@kbn/std';

import { getMtimes } from '../optimizer/get_mtimes';
import { OptimizerConfig } from '../optimizer/optimizer_config';
Expand Down Expand Up @@ -78,9 +78,7 @@ it('emits "bundle cached" event when everything is updated', async () => {
bundleRefExportIds: [],
});

const cacheEvents = await getBundleCacheEvent$(config, optimizerCacheKey)
.pipe(toArray())
.toPromise();
const cacheEvents = await allValuesFrom(getBundleCacheEvent$(config, optimizerCacheKey));

expect(cacheEvents).toMatchInlineSnapshot(`
Array [
Expand Down Expand Up @@ -119,9 +117,7 @@ it('emits "bundle not cached" event when cacheKey is up to date but caching is d
bundleRefExportIds: [],
});

const cacheEvents = await getBundleCacheEvent$(config, optimizerCacheKey)
.pipe(toArray())
.toPromise();
const cacheEvents = await allValuesFrom(getBundleCacheEvent$(config, optimizerCacheKey));

expect(cacheEvents).toMatchInlineSnapshot(`
Array [
Expand Down Expand Up @@ -160,9 +156,7 @@ it('emits "bundle not cached" event when optimizerCacheKey is missing', async ()
bundleRefExportIds: [],
});

const cacheEvents = await getBundleCacheEvent$(config, optimizerCacheKey)
.pipe(toArray())
.toPromise();
const cacheEvents = await allValuesFrom(getBundleCacheEvent$(config, optimizerCacheKey));

expect(cacheEvents).toMatchInlineSnapshot(`
Array [
Expand Down Expand Up @@ -201,9 +195,7 @@ it('emits "bundle not cached" event when optimizerCacheKey is outdated, includes
bundleRefExportIds: [],
});

const cacheEvents = await getBundleCacheEvent$(config, optimizerCacheKey)
.pipe(toArray())
.toPromise();
const cacheEvents = await allValuesFrom(getBundleCacheEvent$(config, optimizerCacheKey));

expect(cacheEvents).toMatchInlineSnapshot(`
Array [
Expand Down Expand Up @@ -247,9 +239,7 @@ it('emits "bundle not cached" event when bundleRefExportIds is outdated, include
bundleRefExportIds: ['plugin/bar/public'],
});

const cacheEvents = await getBundleCacheEvent$(config, optimizerCacheKey)
.pipe(toArray())
.toPromise();
const cacheEvents = await allValuesFrom(getBundleCacheEvent$(config, optimizerCacheKey));

expect(cacheEvents).toMatchInlineSnapshot(`
Array [
Expand Down Expand Up @@ -292,9 +282,7 @@ it('emits "bundle not cached" event when cacheKey is missing', async () => {
bundleRefExportIds: [],
});

const cacheEvents = await getBundleCacheEvent$(config, optimizerCacheKey)
.pipe(toArray())
.toPromise();
const cacheEvents = await allValuesFrom(getBundleCacheEvent$(config, optimizerCacheKey));

expect(cacheEvents).toMatchInlineSnapshot(`
Array [
Expand Down Expand Up @@ -333,9 +321,7 @@ it('emits "bundle not cached" event when cacheKey is outdated', async () => {

jest.spyOn(bundle, 'createCacheKey').mockImplementation(() => 'new');

const cacheEvents = await getBundleCacheEvent$(config, optimizerCacheKey)
.pipe(toArray())
.toPromise();
const cacheEvents = await allValuesFrom(getBundleCacheEvent$(config, optimizerCacheKey));

expect(cacheEvents).toMatchInlineSnapshot(`
Array [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import * as Rx from 'rxjs';
import { map } from 'rxjs/operators';
import ActualWatchpack from 'watchpack';
import { lastValueFrom } from '@kbn/std';

import { Bundle, ascending } from '../common';
import { watchBundlesForChanges$ } from '../optimizer/watch_bundles_for_changes';
Expand Down Expand Up @@ -78,8 +79,8 @@ afterEach(async () => {
it('notifies of changes and completes once all bundles have changed', async () => {
expect.assertions(18);

const promise = watchBundlesForChanges$(bundleCacheEvent$, Date.now())
.pipe(
const promise = lastValueFrom(
watchBundlesForChanges$(bundleCacheEvent$, Date.now()).pipe(
map((event, i) => {
// each time we trigger a change event we get a 'changed detected' event
if (i === 0 || i === 2 || i === 4 || i === 6) {
Expand Down Expand Up @@ -116,7 +117,7 @@ it('notifies of changes and completes once all bundles have changed', async () =
}
})
)
.toPromise();
);

expect(MockWatchPack.mock.instances).toHaveLength(1);
const [watcher] = (MockWatchPack.mock.instances as any) as Array<jest.Mocked<ActualWatchpack>>;
Expand Down
Loading