This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 831
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
157 additions
and
29 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,32 @@ | ||
/* | ||
Copyright 2022 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { ClientEvent, ClientEventHandlerMap } from "matrix-js-sdk/src/matrix"; | ||
import { SyncState } from "matrix-js-sdk/src/sync"; | ||
|
||
/** | ||
* Creates a MatrixClient event listener function that can be used to get notified about reconnects. | ||
* @param callback The callback to be called on reconnect | ||
*/ | ||
export const createReconnectedListener = (callback: () => void): ClientEventHandlerMap[ClientEvent.Sync] => { | ||
return (syncState: SyncState, prevState: SyncState) => { | ||
if (syncState !== SyncState.Error && prevState !== syncState) { | ||
// Consider the client reconnected if there is no error with syncing. | ||
// This means the state could be RECONNECTING, SYNCING, PREPARED or CATCHUP. | ||
callback(); | ||
} | ||
}; | ||
}; |
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,52 @@ | ||
/* | ||
Copyright 2022 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { ClientEvent, ClientEventHandlerMap } from "matrix-js-sdk/src/matrix"; | ||
import { SyncState } from "matrix-js-sdk/src/sync"; | ||
|
||
import { createReconnectedListener } from "../../src/utils/connection"; | ||
|
||
describe("createReconnectedListener", () => { | ||
let reconnectedListener: ClientEventHandlerMap[ClientEvent.Sync]; | ||
let onReconnect: jest.Mock; | ||
|
||
beforeEach(() => { | ||
onReconnect = jest.fn(); | ||
reconnectedListener = createReconnectedListener(onReconnect); | ||
}); | ||
|
||
[ | ||
[SyncState.Prepared, SyncState.Syncing], | ||
[SyncState.Syncing, SyncState.Reconnecting], | ||
[SyncState.Reconnecting, SyncState.Syncing], | ||
].forEach(([from, to]) => { | ||
it(`should invoke the callback on a transition from ${from} to ${to}`, () => { | ||
reconnectedListener(to, from); | ||
expect(onReconnect).toBeCalled(); | ||
}); | ||
}); | ||
|
||
[ | ||
[SyncState.Syncing, SyncState.Syncing], | ||
[SyncState.Catchup, SyncState.Error], | ||
[SyncState.Reconnecting, SyncState.Error], | ||
].forEach(([from, to]) => { | ||
it(`should not invoke the callback on a transition from ${from} to ${to}`, () => { | ||
reconnectedListener(to, from); | ||
expect(onReconnect).not.toBeCalled(); | ||
}); | ||
}); | ||
}); |