This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Add results processor abstraction
- Loading branch information
Showing
6 changed files
with
113 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { IResultProcessor } from "../offline/procesors/IResultProcessor"; | ||
import { OperationQueueEntry } from "../offline/OperationQueueEntry"; | ||
import { FetchResult } from "apollo-link"; | ||
import { isClientGeneratedId } from "./createOptimisticResponse"; | ||
|
||
/** | ||
* Allow updates on items created while offline. | ||
* If item is created while offline and client generated ID is provided | ||
* to optimisticResponse, later mutations on this item will be using this client | ||
* generated ID. Once any create operation is successful, we should | ||
* update entries in queue with ID returned from server. | ||
*/ | ||
export class IDProcessor implements IResultProcessor { | ||
|
||
public execute(queue: OperationQueueEntry[], entry: OperationQueueEntry, result: FetchResult) { | ||
const { operation: { operationName }, optimisticResponse } = entry; | ||
if (!result || | ||
!optimisticResponse || | ||
!optimisticResponse[operationName] || | ||
!isClientGeneratedId(optimisticResponse[operationName].id)) { | ||
return; | ||
} | ||
|
||
const clientId = optimisticResponse && optimisticResponse[operationName].id; | ||
|
||
queue.forEach(({ operation: op }) => { | ||
if (op.variables.id === clientId) { | ||
op.variables.id = result.data && result.data[operationName].id; | ||
} | ||
}); | ||
} | ||
} |
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,36 @@ | ||
import { IResultProcessor } from "../offline/procesors/IResultProcessor"; | ||
import { OperationQueueEntry } from "../offline/OperationQueueEntry"; | ||
import { ObjectState } from "./ObjectState"; | ||
import { FetchResult } from "apollo-link"; | ||
|
||
/** | ||
* Manipulate state of item that is being used for conflict resolution purposes. | ||
* This is required for the queue items so that we do not get a conflict with ourself | ||
* @param entry the operation which returns the result we compare with first queue entry | ||
*/ | ||
export class ConflictProcessor implements IResultProcessor { | ||
constructor(private state: ObjectState) { | ||
} | ||
|
||
public execute(queue: OperationQueueEntry[], | ||
entry: OperationQueueEntry, result: FetchResult): void { | ||
const { operation: { operationName } } = entry; | ||
if (!result || !this.state) { | ||
return; | ||
} | ||
|
||
if (result.data && result.data[operationName]) { | ||
for (const { operation: op } of queue) { | ||
if (op.variables.id === entry.operation.variables.id | ||
&& op.operationName === entry.operation.operationName) { | ||
const opVersion = this.state.currentState(op.variables); | ||
const prevOpVersion = this.state.currentState(entry.operation.variables); | ||
if (opVersion === prevOpVersion) { | ||
op.variables = this.state.nextState(op.variables); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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
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,21 @@ | ||
import { FetchResult } from "apollo-link"; | ||
import { OperationQueueEntry } from "../OperationQueueEntry"; | ||
import { isClientGeneratedId } from "../.."; | ||
|
||
/** | ||
* Interface that can be used to perform operation on result data for offline queue. | ||
* | ||
* @see IDProcessor | ||
* @see ConflictProcessor | ||
*/ | ||
export interface IResultProcessor { | ||
|
||
/** | ||
* Process operation and queue | ||
* | ||
* @param queue | ||
* @param op | ||
* @param result | ||
*/ | ||
execute(queue: OperationQueueEntry[], op: OperationQueueEntry, result: FetchResult<any>): void; | ||
} |