Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement observeQuery on memory cache
- Loading branch information
Showing
6 changed files
with
406 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
import { Listener } from '@orbit/core'; | ||
import { | ||
RecordChange, | ||
QueryExpression, | ||
FindRecord, | ||
FindRecords, | ||
FindRelatedRecord, | ||
FindRelatedRecords, | ||
equalRecordIdentities, | ||
Query, | ||
RecordNotFoundException | ||
} from '@orbit/data'; | ||
import { | ||
QueryResult, | ||
QueryResultData, | ||
SyncRecordCache | ||
} from '@orbit/record-cache'; | ||
|
||
export interface QueryObserverSettings { | ||
cache: SyncRecordCache; | ||
query: Query; | ||
} | ||
|
||
export default class QueryObserver { | ||
cache: SyncRecordCache; | ||
query: Query; | ||
|
||
constructor(settings: QueryObserverSettings) { | ||
this.cache = settings.cache; | ||
this.query = settings.query; | ||
} | ||
|
||
match(change: RecordChange): boolean { | ||
return !!this.query.expressions.find(expression => | ||
this._queryExpressionMatchChange(expression, change) | ||
); | ||
} | ||
|
||
execute(): QueryResult { | ||
try { | ||
return this.cache.query(this.query); | ||
} catch (error) { | ||
if (error instanceof RecordNotFoundException) { | ||
if (this.query.expressions.length === 1) { | ||
return this._emptyQueryResult(this.query.expressions[0]); | ||
} else { | ||
return this.query.expressions.map(expression => | ||
this._emptyQueryResult(expression) | ||
); | ||
} | ||
} | ||
throw error; | ||
} | ||
} | ||
|
||
on(listener: Listener): () => void { | ||
const unsubscribeChange = this.cache.on( | ||
'change', | ||
(change: RecordChange) => { | ||
if (this.match(change)) { | ||
listener(this.execute()); | ||
} | ||
} | ||
); | ||
|
||
const unsubscribeReset = this.cache.on('reset', () => { | ||
listener(this.execute()); | ||
}); | ||
|
||
listener(this.execute()); | ||
|
||
return function unsubscribe() { | ||
unsubscribeChange(); | ||
unsubscribeReset(); | ||
}; | ||
} | ||
|
||
protected _emptyQueryResult(expression: QueryExpression): QueryResultData { | ||
switch (expression.op) { | ||
case 'findRecords': | ||
case 'findRelatedRecords': | ||
return []; | ||
default: | ||
return null; | ||
} | ||
} | ||
|
||
protected _queryExpressionMatchChange( | ||
expression: QueryExpression, | ||
change: RecordChange | ||
): boolean { | ||
switch (expression.op) { | ||
case 'findRecord': | ||
return this._findRecordQueryExpressionMatchChange( | ||
expression as FindRecord, | ||
change | ||
); | ||
case 'findRecords': | ||
return this._findRecordsQueryExpressionMatchChange( | ||
expression as FindRecords, | ||
change | ||
); | ||
case 'findRelatedRecord': | ||
return this._findRelatedRecordQueryExpressionMatchChange( | ||
expression as FindRelatedRecord, | ||
change | ||
); | ||
case 'findRelatedRecords': | ||
return this._findRelatedRecordsQueryExpressionMatchChange( | ||
expression as FindRelatedRecords, | ||
change | ||
); | ||
default: | ||
return true; | ||
} | ||
} | ||
|
||
protected _findRecordQueryExpressionMatchChange( | ||
expression: FindRecord, | ||
change: RecordChange | ||
): boolean { | ||
return equalRecordIdentities(expression.record, change); | ||
} | ||
|
||
protected _findRecordsQueryExpressionMatchChange( | ||
expression: FindRecords, | ||
change: RecordChange | ||
): boolean { | ||
if (expression.type) { | ||
return expression.type === change.type; | ||
} else if (expression.records) { | ||
for (let record of expression.records) { | ||
if (record.type === change.type) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
protected _findRelatedRecordQueryExpressionMatchChange( | ||
expression: FindRelatedRecord, | ||
change: RecordChange | ||
): boolean { | ||
return ( | ||
equalRecordIdentities(expression.record, change) && | ||
(change.relationships.includes(expression.relationship) || change.remove) | ||
); | ||
} | ||
|
||
protected _findRelatedRecordsQueryExpressionMatchChange( | ||
expression: FindRelatedRecords, | ||
change: RecordChange | ||
): boolean { | ||
const { type } = this.cache.schema.getRelationship( | ||
expression.record.type, | ||
expression.relationship | ||
); | ||
|
||
if (Array.isArray(type) && type.find(type => type === change.type)) { | ||
return true; | ||
} else if (type === change.type) { | ||
return true; | ||
} | ||
|
||
return ( | ||
equalRecordIdentities(expression.record, change) && | ||
(change.relationships.includes(expression.relationship) || change.remove) | ||
); | ||
} | ||
} |
Oops, something went wrong.