Skip to content

Instantly share code, notes, and snippets.

@veeramarni
Created July 1, 2019 11:59
Show Gist options
  • Save veeramarni/0ebbe44cb811170baa67fb49f2cb277a to your computer and use it in GitHub Desktop.
Save veeramarni/0ebbe44cb811170baa67fb49f2cb277a to your computer and use it in GitHub Desktop.
subscrip
import { URI } from '@vscode/monaco-editor/esm/vs/base/common/uri';
import { ITextModelContentProvider } from '@vscode/monaco-editor/esm/vs/editor/common/services/resolverService';
import { ApolloClient } from 'apollo-client';
import { IModelService, IModeService } from '@workbench-stack/core';
import { ILanguageSelection } from '@vscode/monaco-editor/esm/vs/editor/common/services/modeService';
// import { IFileDataSubscription } from '@files-stack/client-state';
import gql from 'graphql-tag';
export const FileContent_WsDocument = gql`
query aQuery {
entry {
value
}
}
`;
export const FileOperationDocument = gql`
subscription aQuery {
type
resource
}
`;
export type FileDataSubscription = any;
export const FileContent_Document = gql`
subscription newValues {
name
}
`;
export type FileContent_WsQuery = {
entry: {
value: string;
},
};
interface SubscriptionData {
name: string;
}
export function createTextFileContentProvider(apolloClient: ApolloClient<any>, modelService: IModelService, modeService: IModeService) {
const subscribeFileChange = apolloClient.subscribe<FileDataSubscription>({
query: FileOperationDocument,
}).subscribe({
next(result) {
console.log('----subscribedFilechange', result);
},
});
function resourceToTextFile(scheme: string, resource: URI): URI {
return resource.with({ scheme, query: JSON.stringify({ scheme: resource.scheme }) });
}
function textFileToResource(resource: URI): URI {
console.log('---uri', resource);
return resource.with({ scheme: JSON.parse(resource.query)['scheme'], query: null });
}
async function resolveEditorModel(resource: URI, createAsNeeded: boolean = true) {
const savedFileResource = textFileToResource(resource);
const content = await apolloClient.query<FileContent_WsQuery>({
query: FileContent_WsDocument,
});
// const contentSub = content.subscribe({
// next(queryResult) {
// // latestResult = queryResult;
// console.log('----queryResult --latest', queryResult);
// },
// });
// content.subscribeToMore<SubscriptionData>({
// document: FileContent_Document,
// updateQuery: (_, { subscriptionData }) => {
// return { entry: { value: subscriptionData.data.name } };
// },
// onError: () => {
// console.log('---errorr');
// },
// });
let codeEditorModel = modelService.getModel(resource);
if (codeEditorModel) {
modelService.updateModel(codeEditorModel, content.data.entry.value);
} else if (createAsNeeded) {
const textFileModel = modelService.getModel(savedFileResource);
let languageSelector: ILanguageSelection;
if (textFileModel) {
languageSelector = modeService.create(textFileModel.getModeId());
} else {
languageSelector = modeService.createByFilepathOrFirstLine(savedFileResource.path);
}
codeEditorModel = modelService.createModel(content.data.entry.value, languageSelector, resource);
}
return codeEditorModel;
}
return {
provideTextContent(resource: URI) {
const savedFileResource = textFileToResource(resource);
// Make sure our text file is resolved up to date
const codeEditorModel = resolveEditorModel(resource);
return codeEditorModel;
},
};
}
import gql from 'graphql-tag';
import { ApolloLink, Operation } from 'apollo-link';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { stripSymbols } from 'apollo-utilities';
import { DocumentNode, OperationDefinitionNode } from 'graphql';
import { ApolloClient } from 'apollo-client';
import { mockSingleLink, mockObservableLink } from '../../__mocks__/mockLinks';
import { FileContent_WsDocument, createTextFileContentProvider, FileDataSubscription } from './text-file-content-provider-mock';
import { ModeService } from '../mode-service';
import { ModelService } from '../model-service';
import { URI } from '@vscode/monaco-editor/esm/vs/base/common/uri';
// import { IFileOperation as FileOperation } from '@files-stack/client-state';
import 'jest';
// https://github.com/apollographql/apollo-client/blob/master/packages/apollo-client/src/__tests__/subscribeToMore.ts
const isSub = (operation: Operation) =>
(operation.query as DocumentNode).definitions
.filter(x => x.kind === 'OperationDefinition')
.some((x: OperationDefinitionNode) => x.operation === 'subscription');
describe('text-file-content-provider-mock', () => {
const result = {
data: {
entry: {
value: '1',
},
},
};
const results = ['a', 'b', 'c'].map(name => ({
result: {
data: {
fileData: [
{
name,
resource: 'file:///test',
},
],
} as FileDataSubscription,
},
delay: 10,
}));
const req1 = { request: { query: FileContent_WsDocument } as Operation, result };
// const operationReq = { request: { query: }}
it('trigger new result', async done => {
const wSLink = mockObservableLink();
const httpLink = mockSingleLink(req1);
const link = ApolloLink.split(isSub, wSLink, httpLink);
const client = new ApolloClient({
cache: new InMemoryCache({ addTypename: false }),
link,
});
const textFileContentProvider = createTextFileContentProvider(client, new ModelService(), new ModeService());
const resourceUri = URI.revive({
file: 'file',
scheme: 'file',
path: '/test',
query: '{"scheme":"file"}',
});
console.log('----res', resourceUri);
const testContent = await textFileContentProvider.provideTextContent(resourceUri);
console.log('--testContent', testContent.uri);
setTimeout(() => {
done();
}, 200);
for (let i = 0; i < 2; i++) {
wSLink.simulateResult(results[i]);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment