Skip to content

Commit

Permalink
perf: tidy up subscription on client side
Browse files Browse the repository at this point in the history
  • Loading branch information
thesophiaxu committed Mar 1, 2022
1 parent 5b61ea0 commit 8d2de57
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
5 changes: 3 additions & 2 deletions packages/unigraph-dev-backend/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export default async function startServer(client: DgraphClient) {
await checkOrCreateDefaultDataModel(client);

// Initialize subscriptions
const pollCallback: MsgCallbackFn = (newdata, sub, ofUpdate) => {
const pollCallback: MsgCallbackFn = (newdata, sub, ofUpdate, supplementary?) => {
if (sub?.callbackType === 'messageid') {
const msgPort = sub.msgPort!;
if (msgPort?.readyState === 1) {
Expand All @@ -118,8 +118,9 @@ export default async function startServer(client: DgraphClient) {
id: sub.id,
result: newdata,
ofUpdate,
supplementary,
},
{ replacer: getCircularReplacer() },
supplementary ? {} : { replacer: getCircularReplacer() },
),
);
}
Expand Down
12 changes: 4 additions & 8 deletions packages/unigraph-dev-backend/src/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function buildPollingQuery(subs: { id: any; query: any }[], states: any)
}, '{')} }`;
}

export type MsgCallbackFn = (updated: any, sub: Subscription, ofUpdate?: number | string) => any;
export type MsgCallbackFn = (updated: any, sub: Subscription, ofUpdate?: number | string, supplementary?: any) => any;

export type MergedSubscription = {
subscriptions: Subscription[];
Expand All @@ -104,13 +104,13 @@ export function mergeSubscriptions(
ids?: any[],
states?: any,
): MergedSubscription[] {
function callbackIfChanged(updated: any, sub: Subscription, ofUpdate: any) {
function callbackIfChanged(updated: any, sub: Subscription, ofUpdate: any, supplementary?: any) {
if (
stringify(updated, { replacer: getCircularReplacer() }) !==
stringify(sub.data, { replacer: getCircularReplacer() })
) {
sub.data = updated;
msgCallback(updated, sub, ofUpdate);
msgCallback(updated, sub, ofUpdate, supplementary);
}
}

Expand Down Expand Up @@ -204,10 +204,6 @@ export function mergeSubscriptions(
subscriptions: subs,
aggregateQuery: query,
resolver: (updated: any, ofUpdate: any) => {
const startTime = new Date().getTime();
buildGraph(updated, true);
const graphTime = new Date().getTime() - startTime;
if (graphTime > 5) console.log(`Build graph took ${graphTime}ms, which is a bit slow`);
subs.forEach((el) => {
const uidResolver = (uu: string) => (uu.startsWith('$/') ? states.namespaceMap[uu].uid : uu);
const allUids = (el.query as QueryObject).uid;
Expand All @@ -220,7 +216,7 @@ export function mergeSubscriptions(
(Array.isArray((el.query as QueryObject).uid) ? (el.query as QueryObject).uid.length : 1)
)
return;
callbackIfChanged(updatedIts, el, ofUpdate);
callbackIfChanged(updatedIts, el, ofUpdate, updated);
});
},
});
Expand Down
7 changes: 7 additions & 0 deletions packages/unigraph-dev-common/src/api/unigraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ export default function unigraph(url: string, browserId: string): Unigraph<WebSo
}
subFakeUpdates[parsed.id] = [];
subResults[parsed.id] = JSON.parse(JSON.stringify(parsed.result));
if (parsed.supplementary) {
buildGraph(
_.uniqBy([...parsed.result, ...parsed.supplementary], 'uid'),
true,
parsed.result.length,
);
}
subscriptions[parsed.id](parsed.result);
}
if (parsed.type === 'open_url' && window) window.open(parsed.url, '_blank');
Expand Down
10 changes: 8 additions & 2 deletions packages/unigraph-dev-common/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,11 @@ export class UnigraphObject extends Object {
*
* @param objects Objects with uid references
*/
export function buildGraph(objects: UnigraphObject[], topLevelOnly?: boolean): UnigraphObject[] {
export function buildGraph(
objects: UnigraphObject[],
topLevelOnly?: boolean,
onlyLookAtFirstN?: number,
): UnigraphObject[] {
const objs: any[] = [...objects].map((el: any) => (Array.isArray(el) ? el : new UnigraphObject(el)));
const dict: any = {};

Expand Down Expand Up @@ -352,7 +356,9 @@ export function buildGraph(objects: UnigraphObject[], topLevelOnly?: boolean): U

// console.log(dict);

objs.forEach((object) => buildGraphRecurse(object));
objs.filter((el, index) => (onlyLookAtFirstN ? index < onlyLookAtFirstN : true)).forEach((object) =>
buildGraphRecurse(object),
);

const graphTime = new Date().getTime() - startTime;
if (graphTime > 15) console.log(`Build graph took ${graphTime}ms, which is a bit slow`);
Expand Down

0 comments on commit 8d2de57

Please sign in to comment.