Skip to content

Instantly share code, notes, and snippets.

@ypcode
Created August 19, 2019 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ypcode/98c42d635baf6c34e7e28ac1aa6c0d5d to your computer and use it in GitHub Desktop.
Save ypcode/98c42d635baf6c34e7e28ac1aa6c0d5d to your computer and use it in GitHub Desktop.
import { BaseComponentContext } from "@microsoft/sp-component-base";
import { ServiceKey, ServiceScope } from "@microsoft/sp-core-library";
import { ISpContextServiceLabWebPartProps } from "../webparts/spContextServiceLab/SpContextServiceLabWebPart";
export interface IComponentContextService {
configure(spfxComponentContext: BaseComponentContext, properties: ISpContextServiceLabWebPartProps,force?:boolean): void;
instanceId: string;
properties: ISpContextServiceLabWebPartProps;
// TODO Expose any needed component specific property
// NOTE Avoid simply exposing the whole WebPart/Component context
// Exposing only the needed information in that service allows to have better control
// and better understanding of what's really component specific or not
// It also mitigates risk of unexpected behavior in OTB API
}
export class ComponentContextService implements IComponentContextService {
private _instanceId: string = null;
private _properties: ISpContextServiceLabWebPartProps = null;
private _configured: boolean = false;
constructor(private serviceScope: ServiceScope) { }
public configure(spfxComponentContext: BaseComponentContext, properties: ISpContextServiceLabWebPartProps, force:boolean=false): void {
if (this._configured && !force) {
throw new Error("The ComponentContext Service has already been configured. Please review the configure() call");
}
this._instanceId = (spfxComponentContext && spfxComponentContext.instanceId) || null;
this._properties = properties;
this._configured = (this._instanceId && this._properties && true) || false;
}
public get instanceId(): string {
if (!this._configured) {
throw new Error("The Component Context Service has not been properly configured.");
}
return this._instanceId;
}
public get properties(): ISpContextServiceLabWebPartProps {
if (!this._configured) {
throw new Error("The Component Context Service has not been properly initialized.");
}
return this._properties;
}
}
export const ComponentContextServiceKey = ServiceKey.create<IComponentContextService>("ypcode::ComponentContextService", ComponentContextService);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment