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

Typed api.storeResult #8732

Merged
merged 2 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
16 changes: 8 additions & 8 deletions packages/core/core/src/RequestTracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ type Request<TInput, TResult> = {|
id: string,
+type: string,
input: TInput,
run: ({|input: TInput, ...StaticRunOpts|}) => Async<TResult>,
run: ({|input: TInput, ...StaticRunOpts<TResult>|}) => Async<TResult>,
|};

type StoredRequest = {|
Expand All @@ -132,7 +132,7 @@ type RequestGraphNode =
| EnvNode
| OptionNode;

export type RunAPI = {|
export type RunAPI<TResult> = {|
invalidateOnFileCreate: InternalFileCreateInvalidation => void,
invalidateOnFileDelete: ProjectPath => void,
invalidateOnFileUpdate: ProjectPath => void,
Expand All @@ -141,7 +141,7 @@ export type RunAPI = {|
invalidateOnEnvChange: string => void,
invalidateOnOptionChange: string => void,
getInvalidations(): Array<RequestInvalidation>,
storeResult: (result: mixed, cacheKey?: string) => void,
storeResult(result: TResult, cacheKey?: string): void,
getRequestResult<T>(contentKey: ContentKey): Async<?T>,
getPreviousResult<T>(ifMatch?: string): Async<?T>,
getSubRequests(): Array<StoredRequest>,
Expand All @@ -157,10 +157,10 @@ type RunRequestOpts = {|
force: boolean,
|};

export type StaticRunOpts = {|
export type StaticRunOpts<TResult> = {|
farm: WorkerFarm,
options: ParcelOptions,
api: RunAPI,
api: RunAPI<TResult>,
invalidateReason: InvalidateReason,
|};

Expand Down Expand Up @@ -1021,12 +1021,12 @@ export default class RequestTracker {
}
}

createAPI(
createAPI<TResult>(
requestId: NodeId,
previousInvalidations: Array<RequestInvalidation>,
): {|api: RunAPI, subRequestContentKeys: Set<ContentKey>|} {
): {|api: RunAPI<TResult>, subRequestContentKeys: Set<ContentKey>|} {
let subRequestContentKeys = new Set<ContentKey>();
let api: RunAPI = {
let api: RunAPI<TResult> = {
invalidateOnFileCreate: input =>
this.graph.invalidateOnFileCreate(requestId, input),
invalidateOnFileDelete: filePath =>
Expand Down
8 changes: 4 additions & 4 deletions packages/core/core/src/applyRuntimes.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type RuntimeConnection = {|
isEntry: ?boolean,
|};

export default async function applyRuntimes({
export default async function applyRuntimes<TResult>({
bundleGraph,
config,
options,
Expand All @@ -55,7 +55,7 @@ export default async function applyRuntimes({
options: ParcelOptions,
optionsRef: SharedReference,
pluginOptions: PluginOptions,
api: RunAPI,
api: RunAPI<TResult>,
previousDevDeps: Map<string, string>,
devDepRequests: Map<string, DevDepRequest>,
configs: Map<string, Config>,
Expand Down Expand Up @@ -247,8 +247,8 @@ export default async function applyRuntimes({
return changedAssets;
}

function reconcileNewRuntimes(
api: RunAPI,
function reconcileNewRuntimes<TResult>(
api: RunAPI<TResult>,
connections: Array<RuntimeConnection>,
optionsRef: SharedReference,
) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/core/src/requests/AssetGraphRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type AssetGraphRequestResult = {|

type RunInput = {|
input: AssetGraphRequestInput,
...StaticRunOpts,
...StaticRunOpts<AssetGraphRequestResult>,
|};

type AssetGraphRequest = {|
Expand Down Expand Up @@ -108,7 +108,7 @@ export class AssetGraphBuilder {
changedAssets: Map<string, Asset> = new Map();
optionsRef: SharedReference;
options: ParcelOptions;
api: RunAPI;
api: RunAPI<AssetGraphRequestResult>;
name: string;
cacheKey: string;
shouldBuildLazily: boolean;
Expand Down
8 changes: 4 additions & 4 deletions packages/core/core/src/requests/AssetRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import {runConfigRequest} from './ConfigRequest';
import {fromProjectPath, fromProjectPathRelative} from '../projectPath';
import {report} from '../ReporterRunner';

type RunInput = {|
type RunInput<TResult> = {|
input: AssetRequestInput,
...StaticRunOpts,
...StaticRunOpts<TResult>,
|};

export type AssetRequest = {|
id: ContentKey,
+type: 'asset_request',
run: RunInput => Async<AssetRequestResult>,
run: (RunInput<AssetRequestResult>) => Async<AssetRequestResult>,
input: AssetRequestInput,
|};

Expand Down Expand Up @@ -63,7 +63,7 @@ function getId(input: AssetRequestInput) {
);
}

async function run({input, api, farm, invalidateReason, options}: RunInput) {
async function run({input, api, farm, invalidateReason, options}) {
report({
type: 'buildProgress',
phase: 'transforming',
Expand Down
6 changes: 3 additions & 3 deletions packages/core/core/src/requests/BundleGraphRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ type BundleGraphRequestResult = {|

type RunInput = {|
input: BundleGraphRequestInput,
...StaticRunOpts,
...StaticRunOpts<BundleGraphResult>,
|};

type BundleGraphResult = {|
export type BundleGraphResult = {|
bundleGraph: InternalBundleGraph,
changedAssets: Map<string, Asset>,
assetRequests: Array<AssetGroup>,
Expand Down Expand Up @@ -162,7 +162,7 @@ class BundlerRunner {
optionsRef: SharedReference;
config: ParcelConfig;
pluginOptions: PluginOptions;
api: RunAPI;
api: RunAPI<BundleGraphResult>;
previousDevDeps: Map<string, string>;
devDepRequests: Map<string, DevDepRequest>;
configs: Map<string, Config>;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/core/src/requests/ConfigRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ export async function loadPluginConfig<T: PluginWithLoadConfig>(
}
}

export async function runConfigRequest(
api: RunAPI,
export async function runConfigRequest<TResult>(
api: RunAPI<TResult>,
configRequest: ConfigRequest,
) {
let {
Expand Down
23 changes: 18 additions & 5 deletions packages/core/core/src/requests/DevDepRequest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow
import type {DependencySpecifier} from '@parcel/types';
import type {DependencySpecifier, SemverRange} from '@parcel/types';
import type ParcelConfig from '../ParcelConfig';
import type {
DevDepRequest,
Expand Down Expand Up @@ -100,7 +100,9 @@ type DevDepRequests = {|
invalidDevDeps: Array<DevDepSpecifier>,
|};

export async function getDevDepRequests(api: RunAPI): Promise<DevDepRequests> {
export async function getDevDepRequests<TResult>(
api: RunAPI<TResult>,
): Promise<DevDepRequests> {
let previousDevDepRequests = new Map(
await Promise.all(
api
Expand Down Expand Up @@ -163,11 +165,22 @@ export function invalidateDevDeps(
}
}

export async function runDevDepRequest(
api: RunAPI,
type DevDepRequestResult = {|
specifier: DependencySpecifier,
resolveFrom: ProjectPath,
hash: string,
additionalInvalidations: void | Array<{|
range?: ?SemverRange,
resolveFrom: ProjectPath,
specifier: DependencySpecifier,
|}>,
|};

export async function runDevDepRequest<TResult>(
api: RunAPI<TResult>,
devDepRequest: DevDepRequest,
) {
await api.runRequest<null, void>({
await api.runRequest<null, DevDepRequestResult | void>({
id: 'dev_dep_request:' + devDepRequest.specifier + ':' + devDepRequest.hash,
type: 'dev_dep_request',
run: ({api}) => {
mischnic marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
8 changes: 4 additions & 4 deletions packages/core/core/src/requests/EntryRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ import {
toProjectPath,
} from '../projectPath';

type RunOpts = {|
type RunOpts<TResult> = {|
input: ProjectPath,
...StaticRunOpts,
...StaticRunOpts<TResult>,
|};

export type EntryRequest = {|
id: string,
+type: 'entry_request',
run: RunOpts => Async<EntryResult>,
run: (RunOpts<EntryResult>) => Async<EntryResult>,
input: ProjectPath,
|};

Expand All @@ -53,7 +53,7 @@ export default function createEntryRequest(input: ProjectPath): EntryRequest {
};
}

async function run({input, api, options}: RunOpts): Promise<EntryResult> {
async function run({input, api, options}): Promise<EntryResult> {
let entryResolver = new EntryResolver(options);
let filePath = fromProjectPath(options.projectRoot, input);
let result = await entryResolver.resolveEntry(filePath);
Expand Down
8 changes: 4 additions & 4 deletions packages/core/core/src/requests/PackageRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ type PackageRequestInput = {|
useMainThread?: boolean,
|};

type RunInput = {|
type RunInput<TResult> = {|
input: PackageRequestInput,
...StaticRunOpts,
...StaticRunOpts<TResult>,
|};

export type PackageRequest = {|
id: ContentKey,
+type: 'package_request',
run: RunInput => Async<BundleInfo>,
run: (RunInput<BundleInfo>) => Async<BundleInfo>,
input: PackageRequestInput,
|};

Expand All @@ -46,7 +46,7 @@ export function createPackageRequest(
};
}

async function run({input, api, farm}: RunInput) {
async function run({input, api, farm}) {
let {bundleGraphReference, optionsRef, bundle, useMainThread} = input;
let runPackage = farm.createHandle('runPackage', useMainThread);

Expand Down
20 changes: 11 additions & 9 deletions packages/core/core/src/requests/ParcelBuildRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import type {StaticRunOpts} from '../RequestTracker';
import type {Asset, AssetGroup, PackagedBundleInfo} from '../types';
import type BundleGraph from '../BundleGraph';

import createBundleGraphRequest from './BundleGraphRequest';
import createBundleGraphRequest, {
type BundleGraphResult,
} from './BundleGraphRequest';
import createWriteBundlesRequest from './WriteBundlesRequest';
import {assertSignalNotAborted} from '../utils';
import dumpGraphToGraphViz from '../dumpGraphToGraphViz';
Expand All @@ -32,15 +34,15 @@ type ParcelBuildRequestResult = {|
assetRequests: Array<AssetGroup>,
|};

type RunInput = {|
type RunInput<TResult> = {|
input: ParcelBuildRequestInput,
...StaticRunOpts,
...StaticRunOpts<TResult>,
|};

export type ParcelBuildRequest = {|
id: ContentKey,
+type: 'parcel_build_request',
run: RunInput => Async<ParcelBuildRequestResult>,
run: (RunInput<ParcelBuildRequestResult>) => Async<ParcelBuildRequestResult>,
input: ParcelBuildRequestInput,
|};

Expand All @@ -55,7 +57,7 @@ export default function createParcelBuildRequest(
};
}

async function run({input, api, options}: RunInput) {
async function run({input, api, options}) {
let {optionsRef, requestedAssetIds, signal} = input;

let bundleGraphRequest = createBundleGraphRequest({
Expand All @@ -64,10 +66,10 @@ async function run({input, api, options}: RunInput) {
signal,
});

let {bundleGraph, changedAssets, assetRequests} = await api.runRequest(
bundleGraphRequest,
{force: options.shouldBuildLazily && requestedAssetIds.size > 0},
);
let {bundleGraph, changedAssets, assetRequests}: BundleGraphResult =
await api.runRequest(bundleGraphRequest, {
force: options.shouldBuildLazily && requestedAssetIds.size > 0,
});

// $FlowFixMe Added in Flow 0.121.0 upgrade in #4381 (Windows only)
dumpGraphToGraphViz(bundleGraph._graph, 'BundleGraph', bundleGraphEdgeTypes);
Expand Down
8 changes: 4 additions & 4 deletions packages/core/core/src/requests/ParcelConfigRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ export type ConfigAndCachePath = {|
cachePath: string,
|};

type RunOpts = {|
type RunOpts<TResult> = {|
input: null,
...StaticRunOpts,
...StaticRunOpts<TResult>,
|};

export type ParcelConfigRequest = {|
id: string,
type: string,
input: null,
run: RunOpts => Async<ConfigAndCachePath>,
run: (RunOpts<ConfigAndCachePath>) => Async<ConfigAndCachePath>,
|};

type ParcelConfigChain = {|
Expand All @@ -68,7 +68,7 @@ export default function createParcelConfigRequest(): ParcelConfigRequest {
return {
id: type,
type,
async run({api, options}: RunOpts): Promise<ConfigAndCachePath> {
async run({api, options}) {
let {
config,
extendedFiles,
Expand Down
8 changes: 4 additions & 4 deletions packages/core/core/src/requests/PathRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {Priority} from '../types';
export type PathRequest = {|
id: string,
+type: 'path_request',
run: RunOpts => Async<?AssetGroup>,
run: (RunOpts<?AssetGroup>) => Async<?AssetGroup>,
input: PathRequestInput,
|};

Expand All @@ -37,9 +37,9 @@ export type PathRequestInput = {|
name: string,
|};

type RunOpts = {|
type RunOpts<TResult> = {|
input: PathRequestInput,
...StaticRunOpts,
...StaticRunOpts<TResult>,
|};

const type = 'path_request';
Expand All @@ -56,7 +56,7 @@ export default function createPathRequest(
};
}

async function run({input, api, options}: RunOpts) {
async function run({input, api, options}) {
let configResult = nullthrows(
await api.runRequest<null, ConfigAndCachePath>(createParcelConfigRequest()),
);
Expand Down
Loading