Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions packages/unigraph-dev-common/src/api/unigraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export default function unigraph(url: string, browserId: string): Unigraph<WebSo
const subscriptions: Record<string, Function> = {};
const subResults: Record<string, any> = {};
const subFakeUpdates: Record<string, any[]> = {};
const subBlockingRoutine: Record<string, any> = {};
const states: Record<string, AppState> = {};
const caches: Record<string, any> = {
namespaceMap: isJsonString(window.localStorage.getItem('caches/namespaceMap'))
Expand Down Expand Up @@ -203,13 +204,23 @@ export default function unigraph(url: string, browserId: string): Unigraph<WebSo
cacheCallbacks[parsed.name]?.forEach((el) => el(parsed.result));
}
if (parsed.type === 'subscription' && parsed.id && subscriptions[parsed.id] && parsed.result) {
if (subBlockingRoutine[parsed.id] && !parsed.ofUpdate) {
return ev;
}
if (
Array.isArray(subFakeUpdates[parsed.id]) &&
(subFakeUpdates[parsed.id].includes(parsed.ofUpdate) || subFakeUpdates[parsed.id].length > 0) &&
subFakeUpdates[parsed.id].lastIndexOf(parsed.ofUpdate) < subFakeUpdates[parsed.id].length - 1
) {
return ev;
}
// Reconciled subscription with client, blocking polling/routine updates for 5 seconds
subBlockingRoutine[parsed.id] = true;
setTimeout(() => {
subBlockingRoutine[parsed.id] = false;
}, 5000);

// Now we can safely update the state
subFakeUpdates[parsed.id] = [];
subResults[parsed.id] = JSON.parse(JSON.stringify(parsed.result));
if (parsed.supplementary) {
Expand Down Expand Up @@ -423,16 +434,16 @@ export default function unigraph(url: string, browserId: string): Unigraph<WebSo
api.sendFakeUpdate?.(subId, newObject, id);
}
}
if (thisEventId && (!Array.isArray(subIds) || subIds.length === 1)) {
if (!Array.isArray(subIds) || subIds.length === 1) {
const subId = Array.isArray(subIds) ? subIds[0] : subIds;
if (subFakeUpdates[subId] === undefined) subFakeUpdates[subId] = [thisEventId];
else subFakeUpdates[subId].push(thisEventId);
if (subFakeUpdates[subId] === undefined) subFakeUpdates[subId] = [id];
else subFakeUpdates[subId].push(id);
}
sendEvent(connection, 'update_object', { uid, newObject, upsert, pad, id, subIds, origin, usedUids });
}),
sendFakeUpdate: (subId, updater, eventId, fullObject?) => {
// Merge updater object with existing one
// console.log('subId0', JSON.parse(JSON.stringify(subResults[subId])));
// console.log('subId0', JSON.parse(JSON.stringify(subResults[subId], getCircularReplacer())));
const id = eventId;
let newObj: any;
if (fullObject) {
Expand All @@ -442,6 +453,7 @@ export default function unigraph(url: string, browserId: string): Unigraph<WebSo
}

subResults[subId] = newObj;
// console.log(newObj);

// Record state changes
if (id) {
Expand Down
2 changes: 1 addition & 1 deletion packages/unigraph-dev-common/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export function assignUids(object: any, totalLeases: string[], usedLeases: strin
} else {
const keys = Object.keys(object);
if (keys.includes('unigraph.id')) return;
if (!keys.includes('uid')) {
if (!keys.includes('uid') || object.uid === undefined || object.uid === null) {
// Assign a new uid to the object
const newUid = totalLeases.shift();
if (!newUid) throw new RangeError('No more uids available');
Expand Down
8 changes: 6 additions & 2 deletions packages/unigraph-dev-explorer/src/Workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,10 @@ export function WorkSpace(this: any) {
delete config.undefine;
delete config.id;
}
const uid = await window.unigraph.addObject(
const uid = (window.unigraph as any).leaseUid();
window.unigraph.addObject(
{
uid,
name: node.getName(),
env: 'react-explorer',
view: node.getComponent(),
Expand All @@ -584,11 +586,13 @@ export function WorkSpace(this: any) {
},
},
'$/schema/view',
undefined,
[],
);
await window.unigraph.runExecutable(
'$/executable/add-item-to-list',
{
item: uid[0],
item: uid,
where: '$/entity/favorite_bar',
},
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const MAX_DEPTH = 8;
const MAX_DEPTH = 16;
const getQuery: (depth: number) => string = (depth: number) => {
if (depth >= MAX_DEPTH) return '{ uid _hide type {uid <unigraph.id>} }';
return `{
Expand Down