-
Notifications
You must be signed in to change notification settings - Fork 334
Execute yjs from insight script #11965
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
base: develop
Are you sure you want to change the base?
Changes from 32 commits
2512bb9
4477b4a
31c97b9
936bf45
d9ce757
50266f9
93ba1dc
7c889c6
2932fac
8d17b9e
814eb91
c8acbb2
557bd00
696d9e3
dc1a883
d0610ba
048bd5c
d0d71be
31729b7
bce8380
ec78a92
2d91f12
dca720d
f383682
877b227
7df699c
e29bfc1
8e21525
1c7e750
6f0d885
b10c9c7
a44375a
efda23a
1ed6d4d
e8c11ab
1ec18f1
b81b815
c8e5342
2be45bf
08c21ae
8a73292
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -202,3 +202,4 @@ storybook-static | |
| ######### | ||
|
|
||
| /.mailmap | ||
| /app/ydoc-server-polyglot/nbproject/private/ | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| import { WebsocketProvider } from 'y-websocket' | ||
| import { deserializeIdMap } from 'ydoc-server' | ||
| import * as Ast from 'ydoc-shared/ast' | ||
| import { splitFileContents } from 'ydoc-shared/ensoFile' | ||
| import { DistributedProject } from 'ydoc-shared/yjsModel' | ||
| import * as Y from 'yjs' | ||
|
|
||
| interface SubdocsEvent { | ||
| loaded: Set<Y.Doc> | ||
| added: Set<Y.Doc> | ||
| removed: Set<Y.Doc> | ||
| } | ||
| declare let insight: any | ||
| console.log(`Initializing Insight: ${JSON.stringify(insight)} in 10s`) | ||
|
|
||
| function initializeLater() { | ||
| console.log(`Initializing Insight now...`) | ||
|
|
||
| const PROJECT_NAME = 'NewProject1' | ||
| const RETURN_VALUE_KEY = 'insight_return_value' | ||
|
|
||
| //const doc = new ModuleDoc(new Y.Doc()) | ||
| const doc = new Y.Doc() | ||
| const syncModule = new Ast.MutableModule(doc) | ||
| let spanMap = null | ||
|
|
||
| function attachProvider(url: string, room: string, doc: Y.Doc) { | ||
| console.log(`attachProvider ${url} ${room}`) | ||
| const provider = new WebsocketProvider(url, room, doc) | ||
| const onSync = () => doc.emit('sync', [true, doc]) | ||
| const onDrop = () => doc.emit('sync', [false, doc]) | ||
|
|
||
| const attachedSubdocs = new Map<Y.Doc, ReturnType<typeof attachProvider>>() | ||
|
|
||
| function onSubdocs(e: SubdocsEvent) { | ||
| e.loaded.forEach((subdoc) => { | ||
| attachedSubdocs.set(subdoc, attachProvider(url, subdoc.guid, subdoc)) | ||
| }) | ||
| e.removed.forEach((subdoc) => { | ||
| const subdocProvider = attachedSubdocs.get(subdoc) | ||
| attachedSubdocs.delete(subdoc) | ||
| if (subdocProvider != null) { | ||
| subdocProvider.dispose() | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| provider.on('sync', onSync) | ||
| provider.on('connection-close', onDrop) | ||
| provider.on('connection-error', onDrop) | ||
| doc.on('subdocs', onSubdocs) | ||
|
|
||
| function dispose() { | ||
| provider.disconnect() | ||
| provider.off('sync', onSync) | ||
| provider.off('connection-close', onDrop) | ||
| provider.off('connection-error', onDrop) | ||
| doc.off('subdocs', onSubdocs) | ||
| attachedSubdocs.forEach((subdocProvider) => { | ||
| subdocProvider.dispose() | ||
| }) | ||
| } | ||
| return { provider, dispose } | ||
| } | ||
|
|
||
| const d = new Y.Doc() | ||
| const project = new DistributedProject(d) | ||
| console.log(`Insight: got project ${project}`) | ||
| let provider = null | ||
| try { | ||
| provider = attachProvider('ws://localhost:5976/project', 'index', d) | ||
| } catch (err) { | ||
| console.error(`Insight: Failed to initialize provider`) | ||
| console.error(err) | ||
| } | ||
| console.log(`Insight: got provider ${provider}`) | ||
|
|
||
| function parseContents(contents: string, syncModule: Ast.MutableModule) { | ||
| const { code, idMapJson, metadataJson } = splitFileContents(contents) | ||
|
Check warning on line 79 in app/ydoc-server-polyglot/src/insight.ts
|
||
| const parsedIdMap = deserializeIdMap(idMapJson) | ||
|
|
||
| const { root, spans } = Ast.parseModuleWithSpans(code, syncModule) | ||
| syncModule.setRoot(root) | ||
| spanMap = spans | ||
|
|
||
| Ast.setExternalIds(syncModule, spans, parsedIdMap) | ||
| console.log(`[parseContents] idMap.entries=${parsedIdMap.entries()}`) | ||
| } | ||
|
|
||
| console.log(`Insight: listen on sources`) | ||
| insight.on('source', function (ctx, frame) { | ||
| console.log(`[source] ctx.uri=${ctx.name}`) | ||
| if (ctx.name.includes(PROJECT_NAME)) { | ||
| console.log(`[source] MATCHED ctx=${JSON.stringify(ctx)}`) | ||
| parseContents(ctx.characters, syncModule) | ||
| } | ||
| }) | ||
|
|
||
| function resultToString(result: any): string { | ||
| return '' + result | ||
| } | ||
|
|
||
| /* | ||
| class IdMap { | ||
| private readonly rangeToExpr: Map<SourceRange, ExternalId> | ||
| } | ||
| interface SpanMap { | ||
| nodes: Map<NodeKey, Ast[]> | ||
| tokens: Map<TokenKey, Token> | ||
| } | ||
| type NodeKey = TokenKey = SourceRangeKey | ||
| */ | ||
| console.log(`Insight: listen on return`) | ||
| insight.on( | ||
| 'return', | ||
| function (ctx, frame) { | ||
| console.log(`[return] ctx='${JSON.stringify(ctx)}'`) | ||
| console.log(`[return] frame='${JSON.stringify(frame)}'`) | ||
| console.log(`[return] ctx.returnValue='${ctx.returnValue(frame)}'`) | ||
|
|
||
| const p = project | ||
|
Check warning on line 121 in app/ydoc-server-polyglot/src/insight.ts
|
||
| const result = ctx.returnValue(frame) | ||
| if (result && spanMap) { | ||
| const key = Ast.nodeKey(ctx.charIndex, ctx.charEndIndex - ctx.charIndex) | ||
| const asts = spanMap.nodes.get(key) | ||
| if (asts) { | ||
| const resultValue = resultToString(result) | ||
| for (const ast of asts) { | ||
| const editAst = syncModule.getVersion(ast) | ||
| if (editAst.widgetMetadata(RETURN_VALUE_KEY) !== resultValue) { | ||
| console.log(`[return] setWidgetMetadata ${ast.code()} : ${resultValue}`) | ||
| editAst.setWidgetMetadata(RETURN_VALUE_KEY, resultValue) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| expressions: true, | ||
| statements: false, | ||
| roots: false, | ||
| rootNameFilter: '.*main', | ||
| }, | ||
| ) | ||
| console.log(`Initialization of Insight is over!`) | ||
| } | ||
|
|
||
| setTimeout(initializeLater, 10000) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
;(ast as any).fields.get('metadata').set('expressionUpdate', update)