Skip to content

Commit

Permalink
feat: Improve CozyLink typing and add reset() method
Browse files Browse the repository at this point in the history
In the Flagship app, we want to be able to reset the local PouchDB
files in order to prevent beta testers to be blocked by an erroneous
Pouch replication

To make this possible we want to expose a public method that resets all
the cozy-client's links
  • Loading branch information
Ldoppea committed Sep 19, 2024
1 parent d1aee07 commit 2921150
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 9 deletions.
28 changes: 26 additions & 2 deletions packages/cozy-client/src/CozyLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,37 @@ export default class CozyLink {
}
}

request(operation, result, forward) {
/**
* Request the given operation from the link
*
* @param {any} operation - The operation to request
* @param {any} result - The result from the previous request of the chain
* @param {any} forward - The next request of the chain
* @returns {Promise<any>}
*/
async request(operation, result, forward) {
throw new Error('request is not implemented')
}

persistCozyData(data, forward) {
/**
* Persist the given data into the links storage
*
* @param {any} data - The document to persist
* @param {any} forward - The next persistCozyData of the chain
* @returns {Promise<any>}
*/
async persistCozyData(data, forward) {
throw new Error('persistCozyData is not implemented')
}

/**
* Reset the link data
*
* @returns {Promise<any>}
*/
async reset() {
throw new Error('reset is not implemented')
}
}

const toLink = handler =>
Expand Down
2 changes: 1 addition & 1 deletion packages/cozy-client/src/FlagshipLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class FlagshipLink extends CozyLink {
// does nothing, we don't need any client for this kind of link
}

reset() {
async reset() {
// does nothing, we don't need any client for this kind of link
}

Expand Down
2 changes: 1 addition & 1 deletion packages/cozy-client/src/StackLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default class StackLink extends CozyLink {
this.stackClient = client.stackClient || client.client
}

reset() {
async reset() {
this.stackClient = null
}

Expand Down
25 changes: 23 additions & 2 deletions packages/cozy-client/types/CozyLink.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
export default class CozyLink {
constructor(requestHandler: any, persistHandler: any);
request(operation: any, result: any, forward: any): void;
persistCozyData(data: any, forward: any): void;
/**
* Request the given operation from the link
*
* @param {any} operation - The operation to request
* @param {any} result - The result from the previous request of the chain
* @param {any} forward - The next request of the chain
* @returns {Promise<any>}
*/
request(operation: any, result: any, forward: any): Promise<any>;
/**
* Persist the given data into the links storage
*
* @param {any} data - The document to persist
* @param {any} forward - The next persistCozyData of the chain
* @returns {Promise<any>}
*/
persistCozyData(data: any, forward: any): Promise<any>;
/**
* Reset the link data
*
* @returns {Promise<any>}
*/
reset(): Promise<any>;
}
export function chain(links: any): any;
1 change: 0 additions & 1 deletion packages/cozy-client/types/FlagshipLink.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ export default class FlagshipLink extends CozyLink {
});
webviewIntent: import("cozy-intent").WebviewService;
registerClient(client: any): void;
reset(): void;
}
import CozyLink from "./CozyLink";
1 change: 0 additions & 1 deletion packages/cozy-client/types/StackLink.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export default class StackLink extends CozyLink {
stackClient: any;
isOnline: any;
registerClient(client: any): void;
reset(): void;
/**
*
* @param {QueryDefinition} query - Query to execute
Expand Down
1 change: 0 additions & 1 deletion packages/cozy-pouch-link/types/CozyPouchLink.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ declare class PouchLink extends CozyLink {
}): Promise<void>;
onLogin(): Promise<void>;
pouches: PouchManager;
reset(): Promise<void>;
/**
* Receives PouchDB updates (documents grouped by doctype).
* Normalizes the data (.id -> ._id, .rev -> _rev).
Expand Down

0 comments on commit 2921150

Please sign in to comment.