mirror of
https://github.com/tutao/tutanota.git
synced 2025-12-08 06:09:50 +00:00
Reformat code
This commit is contained in:
parent
c378bc4e02
commit
f349798e75
182 changed files with 4364 additions and 3197 deletions
|
|
@ -21,6 +21,7 @@ import EC from "../../common/EntityConstants"
|
|||
import {SessionTypeRef} from "../../entities/sys/Session"
|
||||
import {StatisticLogEntryTypeRef} from "../../entities/tutanota/StatisticLogEntry"
|
||||
import {BucketPermissionTypeRef} from "../../entities/sys/BucketPermission"
|
||||
|
||||
const ValueType = EC.ValueType
|
||||
|
||||
assertWorkerOrNode()
|
||||
|
|
@ -52,7 +53,7 @@ export class EntityRestCache {
|
|||
/**
|
||||
* stores all contents that would be stored on the server, otherwise
|
||||
*/
|
||||
_entities: {[key: string] : {[key: Id] : Object}};
|
||||
_entities: {[key: string]: {[key: Id]: Object}};
|
||||
// Example:
|
||||
// _entities = {
|
||||
// 'path': { // element type
|
||||
|
|
@ -62,7 +63,7 @@ export class EntityRestCache {
|
|||
// },
|
||||
// // and so on
|
||||
// }
|
||||
_listEntities: {[key: string] : {[key: Id] : {allRange: Id[], lowerRangeId: Id, upperRangeId: Id, elements: {[key: Id] : Object}}}};
|
||||
_listEntities: {[key: string]: {[key: Id]: {allRange: Id[], lowerRangeId: Id, upperRangeId: Id, elements: {[key: Id]: Object}}}};
|
||||
// Example:
|
||||
// _listEntities {
|
||||
// 'path': { // list element type
|
||||
|
|
@ -84,7 +85,10 @@ export class EntityRestCache {
|
|||
this._entityRestClient = entityRestClient
|
||||
this._entities = {}
|
||||
this._listEntities = {}
|
||||
this._ignoredTypes = [EntityEventBatchTypeRef, PermissionTypeRef, BucketPermissionTypeRef, SessionTypeRef, StatisticLogEntryTypeRef]
|
||||
this._ignoredTypes = [
|
||||
EntityEventBatchTypeRef, PermissionTypeRef, BucketPermissionTypeRef, SessionTypeRef,
|
||||
StatisticLogEntryTypeRef
|
||||
]
|
||||
}
|
||||
|
||||
entityRequest<T>(typeRef: TypeRef<T>, method: HttpMethodEnum, listId: ?Id, id: ?Id, entity: ?T, queryParameter: ?Params): Promise<any> {
|
||||
|
|
@ -95,15 +99,19 @@ export class EntityRestCache {
|
|||
} else if (!id && queryParameter && queryParameter["ids"]) {
|
||||
// load multiple entities
|
||||
// TODO: load multiple is not used yet. implement providing from cache when used
|
||||
return this._entityRestClient.entityRequest(typeRef, method, listId, id, entity, queryParameter).each(entity => {
|
||||
this._putIntoCache(entity)
|
||||
})
|
||||
} else if (listId && !id && queryParameter && queryParameter["start"] !== null && queryParameter["start"] !== undefined && queryParameter["count"] !== null && queryParameter["count"] !== undefined && queryParameter["reverse"]) { // check for null and undefined because "" and 0 are als falsy
|
||||
return this._entityRestClient.entityRequest(typeRef, method, listId, id, entity, queryParameter)
|
||||
.each(entity => {
|
||||
this._putIntoCache(entity)
|
||||
})
|
||||
} else if (listId && !id && queryParameter && queryParameter["start"] !== null && queryParameter["start"]
|
||||
!== undefined && queryParameter["count"] !== null && queryParameter["count"] !== undefined
|
||||
&& queryParameter["reverse"]) { // check for null and undefined because "" and 0 are als falsy
|
||||
// load range
|
||||
return resolveTypeReference(typeRef).then(typeModel => {
|
||||
if (typeModel.values["_id"].type === ValueType.GeneratedId) {
|
||||
let params = neverNull(queryParameter)
|
||||
return this._loadRange(typeRef, neverNull(listId), params["start"], Number(params["count"]), params["reverse"] === "true")
|
||||
return this._loadRange(typeRef, neverNull(listId), params["start"], Number(params["count"]), params["reverse"]
|
||||
=== "true")
|
||||
} else {
|
||||
// we currently only store ranges for generated ids
|
||||
return this._entityRestClient.entityRequest(typeRef, method, listId, id, entity, queryParameter)
|
||||
|
|
@ -114,13 +122,15 @@ export class EntityRestCache {
|
|||
if (this._isInCache(typeRef, listId, id)) {
|
||||
return Promise.resolve(this._getFromCache(typeRef, listId, id))
|
||||
} else {
|
||||
return this._entityRestClient.entityRequest(typeRef, method, listId, id, entity, queryParameter).then(entity => {
|
||||
this._putIntoCache(entity)
|
||||
return entity
|
||||
})
|
||||
return this._entityRestClient.entityRequest(typeRef, method, listId, id, entity, queryParameter)
|
||||
.then(entity => {
|
||||
this._putIntoCache(entity)
|
||||
return entity
|
||||
})
|
||||
}
|
||||
} else {
|
||||
throw new Error("invalid request params: " + String(listId) + ", " + String(id) + ", " + JSON.stringify(queryParameter))
|
||||
throw new Error("invalid request params: " + String(listId) + ", " + String(id) + ", "
|
||||
+ JSON.stringify(queryParameter))
|
||||
}
|
||||
} else {
|
||||
return this._entityRestClient.entityRequest(typeRef, method, listId, id, entity, queryParameter)
|
||||
|
|
@ -129,9 +139,11 @@ export class EntityRestCache {
|
|||
|
||||
_loadRange<T>(typeRef: TypeRef<T>, listId: Id, start: Id, count: number, reverse: boolean): Promise<T[]> {
|
||||
let path = typeRefToPath(typeRef)
|
||||
let listCache = (this._listEntities[path] && this._listEntities[path][listId]) ? this._listEntities[path][listId] : null
|
||||
let listCache = (this._listEntities[path]
|
||||
&& this._listEntities[path][listId]) ? this._listEntities[path][listId] : null
|
||||
// check which range must be loaded from server
|
||||
if (!listCache || (start === GENERATED_MAX_ID && reverse && listCache.upperRangeId !== GENERATED_MAX_ID) || (start === GENERATED_MIN_ID && !reverse && listCache.lowerRangeId !== GENERATED_MIN_ID)) {
|
||||
if (!listCache || (start === GENERATED_MAX_ID && reverse && listCache.upperRangeId !== GENERATED_MAX_ID)
|
||||
|| (start === GENERATED_MIN_ID && !reverse && listCache.lowerRangeId !== GENERATED_MIN_ID)) {
|
||||
// this is the first request for this list or
|
||||
// our upper range id is not MAX_ID and we now read the range starting with MAX_ID. we just replace the complete existing range with the new one because we do not want to handle multiple ranges or
|
||||
// our lower range id is not MIN_ID and we now read the range starting with MIN_ID. we just replace the complete existing range with the new one because we do not want to handle multiple ranges
|
||||
|
|
@ -141,7 +153,7 @@ export class EntityRestCache {
|
|||
count: String(count),
|
||||
reverse: String(reverse)
|
||||
}).then(result => {
|
||||
let entities = ((result:any):T[])
|
||||
let entities = ((result: any): T[])
|
||||
// create the list data path in the cache if not existing
|
||||
if (!listCache) {
|
||||
if (!this._listEntities[path]) {
|
||||
|
|
@ -156,7 +168,8 @@ export class EntityRestCache {
|
|||
}
|
||||
return this._handleElementRangeResult(listCache, start, count, reverse, entities, count)
|
||||
})
|
||||
} else if (!firstBiggerThanSecond(start, listCache.upperRangeId) && !firstBiggerThanSecond(listCache.lowerRangeId, start)) { // check if the requested start element is located in the range
|
||||
} else if (!firstBiggerThanSecond(start, listCache.upperRangeId)
|
||||
&& !firstBiggerThanSecond(listCache.lowerRangeId, start)) { // check if the requested start element is located in the range
|
||||
// count the numbers of elements that are already in allRange to determine the number of elements to read
|
||||
let newRequestParams = this._getNumberOfElementsToRead(listCache, start, count, reverse)
|
||||
if (newRequestParams.newCount > 0) {
|
||||
|
|
@ -165,13 +178,14 @@ export class EntityRestCache {
|
|||
count: String(newRequestParams.newCount),
|
||||
reverse: String(reverse)
|
||||
}).then(entities => {
|
||||
return this._handleElementRangeResult(neverNull(listCache), start, count, reverse, ((entities:any):T[]), newRequestParams.newCount)
|
||||
return this._handleElementRangeResult(neverNull(listCache), start, count, reverse, ((entities: any): T[]), newRequestParams.newCount)
|
||||
})
|
||||
} else {
|
||||
// all elements are located in the cache.
|
||||
return Promise.resolve(this._provideFromCache(listCache, start, count, reverse))
|
||||
}
|
||||
} else if ((firstBiggerThanSecond(start, listCache.upperRangeId) && !reverse) || (firstBiggerThanSecond(listCache.lowerRangeId, start) && reverse)) {
|
||||
} else if ((firstBiggerThanSecond(start, listCache.upperRangeId) && !reverse)
|
||||
|| (firstBiggerThanSecond(listCache.lowerRangeId, start) && reverse)) {
|
||||
let loadStartId
|
||||
if (firstBiggerThanSecond(start, listCache.upperRangeId) && !reverse) {
|
||||
// start is higher than range. load from upper range id with same count. then, if all available elements have been loaded or the requested number is in cache, return from cache. otherwise load again the same way.
|
||||
|
|
@ -186,10 +200,10 @@ export class EntityRestCache {
|
|||
reverse: String(reverse)
|
||||
}).then(entities => {
|
||||
// put the new elements into the cache
|
||||
this._handleElementRangeResult(neverNull(listCache), loadStartId, count, reverse, ((entities:any):T[]), count)
|
||||
this._handleElementRangeResult(neverNull(listCache), loadStartId, count, reverse, ((entities: any): T[]), count)
|
||||
// provide from cache with the actual start id
|
||||
let resultElements = this._provideFromCache(neverNull(listCache), start, count, reverse)
|
||||
if (((entities:any):T[]).length < count || resultElements.length === count) {
|
||||
if (((entities: any): T[]).length < count || resultElements.length === count) {
|
||||
// either all available elements have been loaded from target or the requested number of elements could be provided from cache
|
||||
return resultElements
|
||||
} else {
|
||||
|
|
@ -202,12 +216,14 @@ export class EntityRestCache {
|
|||
}
|
||||
})
|
||||
} else {
|
||||
let msg = "invalid range request. path: " + path + " list: " + listId + " start: " + start + " count: " + count + " reverse: " + String(reverse) + " lower: " + listCache.lowerRangeId + " upper: " + listCache.upperRangeId
|
||||
let msg = "invalid range request. path: " + path + " list: " + listId + " start: " + start + " count: "
|
||||
+ count + " reverse: " + String(reverse) + " lower: " + listCache.lowerRangeId + " upper: "
|
||||
+ listCache.upperRangeId
|
||||
return Promise.reject(new Error(msg))
|
||||
}
|
||||
}
|
||||
|
||||
_handleElementRangeResult<T>(listCache: {allRange: Id[], lowerRangeId: Id, upperRangeId: Id, elements: {[key: Id] : Object}}, start: Id, count: number, reverse: boolean, elements: T[], targetCount: number): T[] {
|
||||
_handleElementRangeResult<T>(listCache: {allRange: Id[], lowerRangeId: Id, upperRangeId: Id, elements: {[key: Id]: Object}}, start: Id, count: number, reverse: boolean, elements: T[], targetCount: number): T[] {
|
||||
let elementsToAdd = elements
|
||||
if (elements.length > 0) {
|
||||
// Ensure that elements are cached in ascending (not reverse) order
|
||||
|
|
@ -247,13 +263,14 @@ export class EntityRestCache {
|
|||
* order to read no duplicate values.
|
||||
* @return returns the new start and count value.
|
||||
*/
|
||||
_getNumberOfElementsToRead<T>(listCache: {allRange: Id[], lowerRangeId: Id, upperRangeId: Id, elements: {[key: Id] : Object}}, start: Id, count: number, reverse: boolean): {newStart:string, newCount:number} {
|
||||
_getNumberOfElementsToRead<T>(listCache: {allRange: Id[], lowerRangeId: Id, upperRangeId: Id, elements: {[key: Id]: Object}}, start: Id, count: number, reverse: boolean): {newStart: string, newCount: number} {
|
||||
let allRangeList = listCache['allRange']
|
||||
let elementsToRead = count
|
||||
let startElementId = start
|
||||
|
||||
let indexOfStart = allRangeList.indexOf(start)
|
||||
if ((!reverse && listCache.upperRangeId === GENERATED_MAX_ID) || (reverse && listCache.lowerRangeId === GENERATED_MIN_ID)) {
|
||||
if ((!reverse && listCache.upperRangeId === GENERATED_MAX_ID) || (reverse && listCache.lowerRangeId
|
||||
=== GENERATED_MIN_ID)) {
|
||||
// we have already loaded the complete range in the desired direction, so we do not have to load from server
|
||||
elementsToRead = 0
|
||||
} else if (allRangeList.length === 0) { // Element range is empty, so read all elements
|
||||
|
|
@ -266,13 +283,16 @@ export class EntityRestCache {
|
|||
elementsToRead = count - (allRangeList.length - 1 - indexOfStart)
|
||||
startElementId = allRangeList[allRangeList.length - 1] // use the highest id in allRange as start element
|
||||
}
|
||||
} else if (listCache["lowerRangeId"] === start || (firstBiggerThanSecond(start, listCache["lowerRangeId"]) && (firstBiggerThanSecond(allRangeList[0], start)))) { // Start element is not in allRange but has been used has start element for a range request, eg. EntityRestInterface.GENERATED_MIN_ID, or start is between lower range id and lowest element in range
|
||||
} else if (listCache["lowerRangeId"] === start || (firstBiggerThanSecond(start, listCache["lowerRangeId"])
|
||||
&& (firstBiggerThanSecond(allRangeList[0], start)))) { // Start element is not in allRange but has been used has start element for a range request, eg. EntityRestInterface.GENERATED_MIN_ID, or start is between lower range id and lowest element in range
|
||||
if (!reverse) { // if not reverse read only elements that are not in allRange
|
||||
startElementId = allRangeList[allRangeList.length - 1] // use the highest id in allRange as start element
|
||||
elementsToRead = count - allRangeList.length
|
||||
}
|
||||
// if reverse read all elements
|
||||
} else if (listCache["upperRangeId"] === start || (firstBiggerThanSecond(start, allRangeList[allRangeList.length - 1]) && (firstBiggerThanSecond(listCache["upperRangeId"], start)))) { // Start element is not in allRange but has been used has start element for a range request, eg. EntityRestInterface.GENERATED_MAX_ID, or start is between upper range id and highest element in range
|
||||
} else if (listCache["upperRangeId"] === start
|
||||
|| (firstBiggerThanSecond(start, allRangeList[allRangeList.length - 1])
|
||||
&& (firstBiggerThanSecond(listCache["upperRangeId"], start)))) { // Start element is not in allRange but has been used has start element for a range request, eg. EntityRestInterface.GENERATED_MAX_ID, or start is between upper range id and highest element in range
|
||||
if (reverse) { // if not reverse read only elements that are not in allRange
|
||||
startElementId = allRangeList[0] // use the highest id in allRange as start element
|
||||
elementsToRead = count - allRangeList.length
|
||||
|
|
@ -282,7 +302,7 @@ export class EntityRestCache {
|
|||
return {newStart: startElementId, newCount: elementsToRead}
|
||||
}
|
||||
|
||||
_provideFromCache<T>(listCache: {allRange: Id[], lowerRangeId: Id, upperRangeId: Id, elements: {[key: Id] : Object}}, start: Id, count: number, reverse: boolean): T[] {
|
||||
_provideFromCache<T>(listCache: {allRange: Id[], lowerRangeId: Id, upperRangeId: Id, elements: {[key: Id]: Object}}, start: Id, count: number, reverse: boolean): T[] {
|
||||
let range = listCache.allRange
|
||||
let ids: Id[] = []
|
||||
if (reverse) {
|
||||
|
|
@ -313,7 +333,7 @@ export class EntityRestCache {
|
|||
}
|
||||
let result: T[] = []
|
||||
for (let a = 0; a < ids.length; a++) {
|
||||
result.push(clone((listCache.elements[ids[a]]:any)))
|
||||
result.push(clone((listCache.elements[ids[a]]: any)))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
|
@ -328,17 +348,19 @@ export class EntityRestCache {
|
|||
let typeRef = new TypeRef(data.application, data.type)
|
||||
if (data.operation === OperationType.UPDATE) {
|
||||
if (this._isInCache(typeRef, data.instanceListId, data.instanceId)) {
|
||||
return this._entityRestClient.entityRequest(typeRef, HttpMethod.GET, data.instanceListId, data.instanceId).then(entity => {
|
||||
this._putIntoCache(entity)
|
||||
})
|
||||
return this._entityRestClient.entityRequest(typeRef, HttpMethod.GET, data.instanceListId, data.instanceId)
|
||||
.then(entity => {
|
||||
this._putIntoCache(entity)
|
||||
})
|
||||
}
|
||||
} else if (data.operation === OperationType.DELETE) {
|
||||
this._tryRemoveFromCache(typeRef, data.instanceListId, data.instanceId)
|
||||
} else if (data.operation === OperationType.CREATE) {
|
||||
if (data.instanceListId && this._isInCacheRange(typeRef, data.instanceListId, data.instanceId)) {
|
||||
return this._entityRestClient.entityRequest(typeRef, HttpMethod.GET, data.instanceListId, data.instanceId).then(entity => {
|
||||
this._putIntoCache(entity)
|
||||
})
|
||||
return this._entityRestClient.entityRequest(typeRef, HttpMethod.GET, data.instanceListId, data.instanceId)
|
||||
.then(entity => {
|
||||
this._putIntoCache(entity)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -348,7 +370,8 @@ export class EntityRestCache {
|
|||
_isInCache(typeRef: TypeRef<any>, listId: ?Id, id: Id): boolean {
|
||||
let path = typeRefToPath(typeRef)
|
||||
if (listId) {
|
||||
return (this._listEntities[path] != null && this._listEntities[path][listId] != null && this._listEntities[path][listId].elements[id] != null)
|
||||
return (this._listEntities[path] != null && this._listEntities[path][listId] != null
|
||||
&& this._listEntities[path][listId].elements[id] != null)
|
||||
} else {
|
||||
return (this._entities[path] != null && this._entities[path][id] != null)
|
||||
}
|
||||
|
|
@ -365,12 +388,14 @@ export class EntityRestCache {
|
|||
|
||||
_isInCacheRange(typeRef: TypeRef<any>, listId: Id, id: Id): boolean {
|
||||
let path = typeRefToPath(typeRef)
|
||||
return (this._listEntities[path] != null && this._listEntities[path][listId] != null && firstBiggerThanSecond(this._listEntities[path][listId].upperRangeId, id) && firstBiggerThanSecond(id, this._listEntities[path][listId].lowerRangeId))
|
||||
return (this._listEntities[path] != null && this._listEntities[path][listId] != null
|
||||
&& firstBiggerThanSecond(this._listEntities[path][listId].upperRangeId, id)
|
||||
&& firstBiggerThanSecond(id, this._listEntities[path][listId].lowerRangeId))
|
||||
}
|
||||
|
||||
_putIntoCache(originalEntity: any): void {
|
||||
let entity = clone(originalEntity)
|
||||
let path = typeRefToPath((entity:any)._type)
|
||||
let path = typeRefToPath((entity: any)._type)
|
||||
if (entity._id instanceof Array) {
|
||||
if (!this._listEntities[path]) {
|
||||
this._listEntities[path] = {}
|
||||
|
|
@ -385,7 +410,8 @@ export class EntityRestCache {
|
|||
// if the element already exists in the cache, overwrite it
|
||||
// add new element to existing list if necessary
|
||||
this._listEntities[path][listId].elements[id] = entity
|
||||
if (!firstBiggerThanSecond(id, this._listEntities[path][listId].upperRangeId) && !firstBiggerThanSecond(this._listEntities[path][listId].lowerRangeId, id)) {
|
||||
if (!firstBiggerThanSecond(id, this._listEntities[path][listId].upperRangeId)
|
||||
&& !firstBiggerThanSecond(this._listEntities[path][listId].lowerRangeId, id)) {
|
||||
this._insertIntoRange(this._listEntities[path][listId].allRange, id)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue