Skip to content

Commit

Permalink
fix(api): add ability to couple update event with fakeupdate
Browse files Browse the repository at this point in the history
  • Loading branch information
thesophiaxu committed Feb 18, 2022
1 parent 079662c commit a847ae9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/unigraph-dev-common/src/api/unigraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,9 @@ export default function unigraph(url: string, browserId: string): Unigraph<WebSo
// TODO: This is very useless, should be removed once we get something better
sendEvent(connection, 'update_spo', { objects, isDelete, subIds });
},
updateObject: (uid, newObject, upsert = true, pad = true, subIds, origin, eagarlyUpdate) =>
updateObject: (uid, newObject, upsert = true, pad = true, subIds, origin, eagarlyUpdate, thisEventId: any) =>
new Promise((resolve, reject) => {
const id = getRandomInt();
const id = thisEventId || getRandomInt();
callbacks[id] = (response: any) => {
if (response.success) resolve(id);
else reject(response);
Expand All @@ -400,11 +400,17 @@ export default function unigraph(url: string, browserId: string): Unigraph<WebSo
api.sendFakeUpdate?.(subId, newObject, id);
}
}
if (thisEventId && (!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);
}
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])));
const id = eventId;
let newObj: any;
if (fullObject) {
newObj = updater;
Expand All @@ -419,9 +425,9 @@ export default function unigraph(url: string, browserId: string): Unigraph<WebSo
}, 0);

// Record state changes
if (eventId) {
if (subFakeUpdates[subId] === undefined) subFakeUpdates[subId] = [eventId];
else subFakeUpdates[subId].push(eventId);
if (id) {
if (subFakeUpdates[subId] === undefined) subFakeUpdates[subId] = [id];
else subFakeUpdates[subId].push(id);
}
},
deleteRelation: (uid, relation) => {
Expand Down
1 change: 1 addition & 0 deletions packages/unigraph-dev-common/src/types/unigraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ export interface Unigraph<TT = WebSocket | false> {
subIds?: any[] | any,
origin?: any[],
eagarlyUpdate?: boolean,
thisEventId?: any,
): any;
/**
* Deletes relationships by supplying the origin UID and JSONs to delete.
Expand Down

0 comments on commit a847ae9

Please sign in to comment.