Skip to content

Commit db50631

Browse files
committed
WIP
1 parent c424340 commit db50631

File tree

2 files changed

+57
-23
lines changed

2 files changed

+57
-23
lines changed

packages/database/src/core/Repo.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,13 @@ import {
4848
statsManagerGetOrCreateReporter
4949
} from './stats/StatsManager';
5050
import { StatsReporter, statsReporterIncludeStat } from './stats/StatsReporter';
51+
import { syncPointGetView, syncPointViewExistsForQuery, syncPointViewForQuery } from './SyncPoint';
5152
import {
53+
createNewTag,
5254
SyncTree,
5355
syncTreeAckUserWrite,
5456
syncTreeAddEventRegistration,
57+
syncTreeAddToPath,
5558
syncTreeApplyServerMerge,
5659
syncTreeApplyServerOverwrite,
5760
syncTreeApplyTaggedQueryMerge,
@@ -60,7 +63,9 @@ import {
6063
syncTreeApplyUserOverwrite,
6164
syncTreeCalcCompleteEventCache,
6265
syncTreeGetServerValue,
63-
syncTreeRemoveEventRegistration
66+
syncTreeMakeQueryKey_,
67+
syncTreeRemoveEventRegistration,
68+
syncTreeTagForQuery_
6469
} from './SyncTree';
6570
import { Indexable } from './util/misc';
6671
import {
@@ -466,15 +471,30 @@ export function repoGetValue(repo: Repo, query: QueryContext): Promise<Node> {
466471
}
467472
return repo.server_.get(query).then(
468473
payload => {
469-
const node = nodeFromJSON(payload as string).withIndex(
474+
const node = nodeFromJSON(payload).withIndex(
470475
query._queryParams.getIndex()
471476
);
472-
const events = syncTreeApplyServerOverwrite(
473-
repo.serverSyncTree_,
474-
query._path,
475-
node
476-
);
477-
eventQueueRaiseEventsAtPath(repo.eventQueue_, query._path, events);
477+
// if this is not a filtered query, then overwrite at path
478+
if(query._queryParams.loadsAllData()) {
479+
const events = syncTreeApplyServerOverwrite(
480+
repo.serverSyncTree_,
481+
query._path,
482+
node
483+
);
484+
eventQueueRaiseEventsAtPath(repo.eventQueue_, query._path, events);
485+
} else {
486+
// Otherwise, only overwrite for query
487+
console.log('query')
488+
syncTreeAddToPath(query, repo.serverSyncTree_);
489+
const tag = syncTreeTagForQuery_(repo.serverSyncTree_, query);
490+
const events = syncTreeApplyTaggedQueryOverwrite(
491+
repo.serverSyncTree_,
492+
query._path,
493+
node,
494+
tag
495+
);
496+
eventQueueRaiseEventsAtPath(repo.eventQueue_, query._path, events);
497+
}
478498
return Promise.resolve(node);
479499
},
480500
err => {

packages/database/src/core/SyncTree.ts

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -482,16 +482,14 @@ export function syncTreeApplyTaggedQueryMerge(
482482
}
483483
}
484484

485-
/**
486-
* Add an event callback for the specified query.
487-
*
488-
* @returns Events to raise.
489-
*/
490-
export function syncTreeAddEventRegistration(
491-
syncTree: SyncTree,
492-
query: QueryContext,
493-
eventRegistration: EventRegistration
494-
): Event[] {
485+
export function createNewTag(syncTree: SyncTree, queryKey: string) {
486+
const tag = syncTreeGetNextQueryTag_();
487+
syncTree.queryToTagMap.set(queryKey, tag);
488+
syncTree.tagToQueryMap.set(tag, queryKey);
489+
return tag;
490+
}
491+
492+
export function syncTreeAddToPath(query: QueryContext, syncTree: SyncTree) {
495493
const path = query._path;
496494

497495
let serverCache: Node | null = null;
@@ -537,6 +535,7 @@ export function syncTreeAddEventRegistration(
537535
});
538536
}
539537

538+
540539
const viewAlreadyExists = syncPointViewExistsForQuery(syncPoint, query);
541540
if (!viewAlreadyExists && !query._queryParams.loadsAllData()) {
542541
// We need to track a tag for this query
@@ -545,11 +544,26 @@ export function syncTreeAddEventRegistration(
545544
!syncTree.queryToTagMap.has(queryKey),
546545
'View does not exist, but we have a tag'
547546
);
548-
const tag = syncTreeGetNextQueryTag_();
549-
syncTree.queryToTagMap.set(queryKey, tag);
550-
syncTree.tagToQueryMap.set(tag, queryKey);
547+
createNewTag(syncTree, queryKey);
551548
}
552549
const writesCache = writeTreeChildWrites(syncTree.pendingWriteTree_, path);
550+
// TODO: break this down so you do the minimal amount
551+
return { syncPoint, writesCache, serverCache, serverCacheComplete, foundAncestorDefaultView, viewAlreadyExists };
552+
}
553+
554+
/**
555+
* Add an event callback for the specified query.
556+
*
557+
* @returns Events to raise.
558+
*/
559+
export function syncTreeAddEventRegistration(
560+
syncTree: SyncTree,
561+
query: QueryContext,
562+
eventRegistration: EventRegistration
563+
): Event[] {
564+
565+
const { syncPoint, serverCache, writesCache, serverCacheComplete, viewAlreadyExists, foundAncestorDefaultView } = syncTreeAddToPath(query, syncTree);
566+
553567
let events = syncPointAddEventRegistration(
554568
syncPoint,
555569
query,
@@ -803,7 +817,7 @@ function syncTreeCreateListenerForView_(
803817
/**
804818
* Return the tag associated with the given query.
805819
*/
806-
function syncTreeTagForQuery_(
820+
export function syncTreeTagForQuery_(
807821
syncTree: SyncTree,
808822
query: QueryContext
809823
): number | null {
@@ -814,7 +828,7 @@ function syncTreeTagForQuery_(
814828
/**
815829
* Given a query, computes a "queryKey" suitable for use in our queryToTagMap_.
816830
*/
817-
function syncTreeMakeQueryKey_(query: QueryContext): string {
831+
export function syncTreeMakeQueryKey_(query: QueryContext): string {
818832
return query._path.toString() + '$' + query._queryIdentifier;
819833
}
820834

0 commit comments

Comments
 (0)