Commit 1b347555 authored by Florent Coriat's avatar Florent Coriat
Browse files

Merge branch '26-pass-a-handler-when-opening-the-crdt' into 'dev'

Resolve "Pass a handler when opening the crdt"

See merge request !25
parents e28875f7 3a1f1b0e
Pipeline #272909 canceled with stages
......@@ -5,17 +5,25 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- [Unreleased]
- Added
- Changed
- Deprecated
- Removed
- Fixed
- Security
- [1.0.1] - 2021-05-17
- Changed
- Update documentation
- Fixed
- Fix concurrency between edits and remote updates
## [Unreleased]
### Added
- Provide a service worker file in public repository
- Add a callback to receive notifications when a crdt is updated
### Changed
- Group the MVMap updates during a reset/reconnection
- Remove frequent pooling but keep a backup call when it has not received a new version for a while
### Deprecated
### Removed
### Fixed
### Security
## [1.0.1] - 2021-05-17
### Changed
- Update documentation
### Fixed
- Fix concurrency between edits and remote updates
build
\ No newline at end of file
build
public/c-service-worker.js
......@@ -7,3 +7,5 @@ package-lock.json
# production
/build
public/c-service-worker.js
This diff is collapsed.
......@@ -6,21 +6,21 @@
"license": "MIT",
"homepage": "./",
"dependencies": {
"@concordant/c-client": "~1.2.0",
"@testing-library/jest-dom": "^5.12.0",
"@concordant/c-client": "~1.3.0",
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^11.2.7",
"@testing-library/user-event": "^13.1.9",
"@types/jest": "^26.0.23",
"@types/node": "^14.14.45",
"@types/react": "^16.9.53",
"@types/react-dom": "^16.9.8",
"@types/react-helmet": "^6.1.1",
"@testing-library/user-event": "^13.2.0",
"@types/jest": "^26.0.24",
"@types/node": "^14.17.5",
"@types/react": "^16.14.11",
"@types/react-dom": "^16.9.14",
"@types/react-helmet": "^6.1.2",
"assert": "^2.0.0",
"react": "^16.14.0",
"react-dom": "^16.14.0",
"react-helmet": "^6.1.0",
"react-scripts": "^4.0.3",
"typescript": "^4.2.4",
"typescript": "^4.3.5",
"web-vitals": "^1.1.2"
},
"scripts": {
......@@ -45,12 +45,12 @@
]
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.23.0",
"@typescript-eslint/parser": "^4.23.0",
"@typescript-eslint/eslint-plugin": "^4.28.4",
"@typescript-eslint/parser": "^4.28.4",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.5",
"eslint": "^7.26.0",
"eslint-plugin-react": "^7.23.2",
"eslint": "^7.31.0",
"eslint-plugin-react": "^7.24.0",
"husky": "^6.0.0",
"lint-staged": "^10.5.4",
"prettier": "2.2.1"
......
This diff is collapsed.
......@@ -31,6 +31,8 @@ import Submit1Input from "./Submit1Input";
import CONFIG from "../config.json";
const TIMEOUTGET = 60000;
const session = client.Session.Companion.connect(
CONFIG.dbName,
CONFIG.serviceUrl,
......@@ -53,7 +55,7 @@ interface IGameState {
* This class represent the Game that glues all components together.
*/
class Game extends React.Component<Record<string, unknown>, IGameState> {
timerID!: NodeJS.Timeout;
timeoutGet!: NodeJS.Timeout;
modifiedCells: string[];
constructor(props: Record<string, unknown>) {
......@@ -67,9 +69,7 @@ class Game extends React.Component<Record<string, unknown>, IGameState> {
"grid" + gridNum,
"MVMap",
false,
function () {
return;
}
this.handler.bind(this)
);
this.state = {
gridNum: gridNum,
......@@ -79,23 +79,13 @@ class Game extends React.Component<Record<string, unknown>, IGameState> {
};
}
/**
* Every 3 seconds, retrieves remote changes
*/
private setSyncTimer() {
this.timerID = setTimeout(() => {
this.pullGrid();
this.setSyncTimer();
}, 3000);
}
/**
* Called after the component is rendered.
* It set a timer to refresh cells values.
*/
componentDidMount(): void {
this.initFrom(generateStaticGrid(this.state.gridNum));
this.setSyncTimer();
this.setGetTimeout();
}
/**
......@@ -103,7 +93,24 @@ class Game extends React.Component<Record<string, unknown>, IGameState> {
* It remove the timer set in componentDidMount().
*/
componentWillUnmount(): void {
clearInterval(this.timerID);
clearInterval(this.timeoutGet);
}
/**
* Get remote changes
*/
private setGetTimeout() {
this.timeoutGet = setTimeout(() => {
collection.forceGet(this.state.mvmap);
this.setGetTimeout();
}, TIMEOUTGET);
}
/**
* Handles update
*/
private handler() {
this.pullGrid();
}
/**
......@@ -111,9 +118,9 @@ class Game extends React.Component<Record<string, unknown>, IGameState> {
*/
pullGrid(): void {
if (!this.state.isConnected) {
console.error("updateGrid() called while not connected.");
return;
}
clearTimeout(this.timeoutGet);
const cells = this.state.cells;
collection.pull(client.utils.ConsistencyLevel.None);
for (let index = 0; index < 81; index++) {
......@@ -129,29 +136,31 @@ class Game extends React.Component<Record<string, unknown>, IGameState> {
}
});
this.setState({ cells: cells });
this.setGetTimeout();
}
/**
* This function is used to simulate the offline mode.
*/
switchConnection(): void {
if (this.state.isConnected) {
this.modifiedCells = new Array(81).fill(null);
clearInterval(this.timerID);
} else {
for (let index = 0; index < 81; index++) {
if (
this.state.cells[index].modifiable &&
this.modifiedCells[index] !== null
) {
session.transaction(client.utils.ConsistencyLevel.None, () => {
this.state.mvmap.setString(index, this.modifiedCells[index]);
});
}
this.setState({ isConnected: !this.state.isConnected }, () => {
if (this.state.isConnected) {
session.transaction(client.utils.ConsistencyLevel.None, () => {
for (let index = 0; index < 81; index++) {
if (
this.state.cells[index].modifiable &&
this.modifiedCells[index] !== null
) {
this.state.mvmap.setString(index, this.modifiedCells[index]);
}
}
});
this.pullGrid();
} else {
clearInterval(this.timeoutGet);
this.modifiedCells = new Array(81).fill(null);
}
this.setSyncTimer();
}
this.setState({ isConnected: !this.state.isConnected });
});
}
/**
......@@ -173,18 +182,18 @@ class Game extends React.Component<Record<string, unknown>, IGameState> {
*/
reset(): void {
const cells = this.state.cells;
for (let index = 0; index < 81; index++) {
if (cells[index].modifiable) {
cells[index].value = "";
if (this.state.isConnected) {
session.transaction(client.utils.ConsistencyLevel.None, () => {
session.transaction(client.utils.ConsistencyLevel.None, () => {
for (let index = 0; index < 81; index++) {
if (cells[index].modifiable) {
cells[index].value = "";
if (this.state.isConnected) {
this.state.mvmap.setString(index, cells[index].value);
});
} else {
this.modifiedCells[index] = "";
} else {
this.modifiedCells[index] = "";
}
}
}
}
});
this.setState({ cells: cells });
}
......@@ -231,9 +240,7 @@ class Game extends React.Component<Record<string, unknown>, IGameState> {
"grid" + gridNum,
"MVMap",
false,
function () {
return;
}
this.handler.bind(this)
);
this.setState({ gridNum: gridNum, mvmap: mvmap });
this.initFrom(generateStaticGrid(gridNum));
......
This diff is collapsed.
......@@ -6,20 +6,20 @@
"license": "MIT",
"homepage": "./",
"dependencies": {
"@testing-library/jest-dom": "^5.12.0",
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^11.2.6",
"@testing-library/user-event": "^13.1.8",
"@types/jest": "^26.0.23",
"@types/node": "^14.14.44",
"@types/react": "^16.9.53",
"@types/react-dom": "^16.9.8",
"@types/react-helmet": "^6.1.1",
"@testing-library/user-event": "^13.2.0",
"@types/jest": "^26.0.24",
"@types/node": "^14.17.5",
"@types/react": "^16.14.11",
"@types/react-dom": "^16.9.14",
"@types/react-helmet": "^6.1.2",
"assert": "^2.0.0",
"react": "^16.14.0",
"react-dom": "^16.14.0",
"react-helmet": "^6.1.0",
"react-scripts": "^4.0.3",
"typescript": "^4.2.4",
"typescript": "^4.3.5",
"web-vitals": "^1.1.2"
},
"scripts": {
......@@ -44,12 +44,12 @@
]
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.22.1",
"@typescript-eslint/parser": "^4.22.1",
"@typescript-eslint/eslint-plugin": "^4.28.4",
"@typescript-eslint/parser": "^4.28.4",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.5",
"eslint": "^7.25.0",
"eslint-plugin-react": "^7.23.2",
"eslint": "^7.31.0",
"eslint-plugin-react": "^7.24.0",
"husky": "^6.0.0",
"lint-staged": "^10.5.4",
"prettier": "2.2.1"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment