Skip to content

Instantly share code, notes, and snippets.

class DemoUserService {
private getUserProfilePromise: Promise<EGClientUserEntity>;
public getUserProfile(forUserID: string): Promise<EGClientUserEntity> {
if (!this.getUserProfilePromise) {
this.getUserProfilePromise = ...
// See here for details:
}
return this.getUserProfilePromise;
class DemoUserService {
private getUserProfilePromise: Promise<EGClientUserEntity>;
public getUserProfile(forUserID: string): Promise<EGClientUserEntity> {
if (!this.getUserProfilePromise) {
this.getUserProfilePromise = new Promise<EGClientUserEntity>((resolve, reject) => {
@pagalvin
pagalvin / retPromise.ts
Last active May 31, 2018 10:44
VS Code snippet for to return a new promise
return new Promise<SomeModel> ( (resolve, reject) => {
try {
// logic goes here
}
catch (exception) {
const errMsg = {
msg: '[ServiceName]: [methodName]: ERROR! Failed to do the thing.',
errDetails: exception
};
console.error(errMsg);
@pagalvin
pagalvin / invokeService.ts
Created May 29, 2018 14:06
Invoking methods on services
private async handleConfirmAdvanceWorkflow() {
this.setState(
{
isAdvancingWorkflow: true,
didAdvanceWorkflow: false,
wasErrorAdvancingWorkflow: false
} as DraftResearchApproveOMaticState);
@pagalvin
pagalvin / consumeService.ts
Last active May 29, 2018 13:56
Consume singleton service in SPFx
public onInit(): Promise<void> {
return new Promise<void>( (resolve, reject) => {
DocumentsService.initializeInstance(this.context);
WorkflowService.initializeInstance(this.context);
resolve();
});
}
@pagalvin
pagalvin / workflowservice.ts
Last active February 1, 2019 19:51
Singleton Service in SPFx
import SPHttpClient from '@microsoft/sp-http/lib/spHttpClient/SPHttpClient';
import WebPartContext from '@microsoft/sp-webpart-base/lib/core/WebPartContext';
import { ConfigService } from '../../framework/services/ConfigService/ConfigService';
export class WorkflowService {
private static instance: EGWorkflowService;
private constructor(private context: WebPartContext) {
this.initialize();
class SearchableCat implements IBinarySearchable {
// placeholders
public IsEqualTo(value): boolean { return true; };
public IsLessThan(value): boolean { return true; };
public IsGreaterThan(value): boolean { return true; };
public CatName: string;
public PreferredFood: "Wet" | "Dry";
public Age: number;
class SearchableCar implements IBinarySearchable {
// placeholders
public IsEqualTo(value): boolean { return true; };
public IsLessThan(value): boolean { return true; };
public IsGreaterThan(value): boolean { return true; };
public CarName: string;
public CarType: "Convertible" | "Four Door" | "Classic";
interface IBinarySearchable {
IsEqualTo(value): boolean;
IsLessThan(value): boolean;
IsGreaterThan(value): boolean;
}
class BinarySearcher {
static Search<T extends IBinarySearchable>(searchIn: T[], searchFor: any){
#
// Starting with ClientContext, the constructor requires a URL to the
// server running SharePoint.
ClientContext context = new ClientContext("http://SiteUrl");
// Assume that the web has a list named "Announcements".
List announcementsList = context.Web.Lists.GetByTitle("NewsTicker");
// We are just creating a regular list item, so we don't need to
// set any properties. If we wanted to create a new folder, for