Skip to content

Commit

Permalink
Exposed package "path" on IComponentContext. (#1220)
Browse files Browse the repository at this point in the history
  • Loading branch information
agarwal-navin authored Feb 14, 2020
1 parent 22da6d5 commit 29d9a9f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class ExternalComponentLoader extends PrimedComponent
componentRuntime = await this.context.hostRuntime.createComponent(
uuid(),
[
WaterParkLoaderName,
...this.context.packagePath,
"url",
url,
pkgReg.IComponentDefaultFactoryName.getDefaultFactoryName(),
Expand All @@ -133,7 +133,7 @@ export class ExternalComponentLoader extends PrimedComponent
componentRuntime = await this.context.hostRuntime.createComponent(
uuid(),
[
WaterParkLoaderName,
...this.context.packagePath,
"url",
url,
]);
Expand Down
39 changes: 24 additions & 15 deletions packages/runtime/container-runtime/src/componentContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export interface IComponentAttributes {
}

interface ISnapshotDetails {
pkg: string[];
pkg: readonly string[];
snapshot: ISnapshotTree;
}

Expand All @@ -67,6 +67,12 @@ export abstract class ComponentContext extends EventEmitter implements IComponen
return this._hostRuntime.id;
}

public get packagePath(): readonly string[] {
// The component must be loaded before the path is accessed.
assert(this.loaded);
return this.pkg;
}

public get parentBranch(): string {
return this._hostRuntime.parentBranch;
}
Expand Down Expand Up @@ -146,6 +152,7 @@ export abstract class ComponentContext extends EventEmitter implements IComponen
public readonly storage: IDocumentStorageService,
public readonly scope: IComponent,
public readonly attach: (componentRuntime: IComponentRuntime) => void,
protected pkg?: readonly string[],
) {
super();
}
Expand Down Expand Up @@ -384,6 +391,10 @@ export abstract class ComponentContext extends EventEmitter implements IComponen
this.loaded = true;
this.componentRuntime = componentRuntime;

// Freeze the package path to ensure that someone doesn't modify it when it is
// returned in packagePath().
Object.freeze(this.pkg);

// And notify the pending promise it is now available
this.componentRuntimeDeferred.resolve(this.componentRuntime);
}
Expand Down Expand Up @@ -426,7 +437,7 @@ export class RemotedComponentContext extends ComponentContext {
runtime: ContainerRuntime,
storage: IDocumentStorageService,
scope: IComponent,
private readonly pkg?: string[],
pkg?: string[],
) {
super(
runtime,
Expand All @@ -436,7 +447,8 @@ export class RemotedComponentContext extends ComponentContext {
scope,
() => {
throw new Error("Already attached");
});
},
pkg);

if (initSnapshotValue && typeof initSnapshotValue !== "string") {
// This will allow the summarizer to avoid calling realize if there
Expand Down Expand Up @@ -464,12 +476,7 @@ export class RemotedComponentContext extends ComponentContext {
tree = this.initSnapshotValue;
}

if (tree === null || tree.blobs[".component"] === undefined) {
this.details = {
pkg: this.pkg,
snapshot: tree,
};
} else {
if (tree !== null && tree.blobs[".component"] !== undefined) {
// Need to rip through snapshot and use that to populate extraBlobs
const { pkg, snapshotFormatVersion } =
await readAndParse<IComponentAttributes>(
Expand All @@ -488,11 +495,13 @@ export class RemotedComponentContext extends ComponentContext {
} else if (snapshotFormatVersion === currentSnapshotFormatVersion) {
pkgFromSnapshot = JSON.parse(pkg) as string[];
}
this.details = {
pkg: pkgFromSnapshot,
snapshot: tree,
};
this.pkg = pkgFromSnapshot;
}

this.details = {
pkg: this.pkg,
snapshot: tree,
};
}

return this.details;
Expand All @@ -502,14 +511,14 @@ export class RemotedComponentContext extends ComponentContext {
export class LocalComponentContext extends ComponentContext {
constructor(
id: string,
private readonly pkg: string[],
pkg: string[],
runtime: ContainerRuntime,
storage: IDocumentStorageService,
scope: IComponent,
attachCb: (componentRuntime: IComponentRuntime) => void,
public readonly createProps?: any,
) {
super(runtime, id, false, storage, scope, attachCb);
super(runtime, id, false, storage, scope, attachCb, pkg);
}

public generateAttachMessage(): IAttachMessage {
Expand Down
4 changes: 4 additions & 0 deletions packages/runtime/runtime-definitions/src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ export interface IComponentRuntime extends EventEmitter, IComponentRouter, Parti
export interface IComponentContext extends EventEmitter {
readonly documentId: string;
readonly id: string;
/**
* The package path of the component as per the package factory.
*/
readonly packagePath: readonly string[];
readonly existing: boolean;
readonly options: any;
readonly clientId: string;
Expand Down

0 comments on commit 29d9a9f

Please sign in to comment.