Skip to content

Commit 35a1d68

Browse files
committed
Add docs for methods
1 parent db1198a commit 35a1d68

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

static/js/passed-state.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ class PassedState {
55
_key = 'python-type-challenges';
66
_state = null;
77

8+
/**
9+
* Initializing when there is no state in the local storage. If there is no state in the local storage, the initial state is required.
10+
* this function will check the new state and the old state whether is undefined or not and updated the old state based on new state.
11+
*
12+
* @param {object} initialState - the initial state of the challenges which grouped by the level.
13+
* @returns void
14+
*/
815
init(initialState) {
916
const currentState = localStorage.getItem(this._key);
1017
// initialize the state when there is no state in the local storage.
@@ -19,10 +26,10 @@ class PassedState {
1926
const state = this._checkAndMerge(JSON.parse(currentState), rawState);
2027
this._save(state);
2128
this._state = state;
22-
return
2329
}
2430
/**
2531
* prepare the state for initialization.
32+
*
2633
* @param {object} rawState
2734
* @returns state - the state contains the challenge name and whether the challenge is passed.
2835
*/
@@ -54,12 +61,17 @@ class PassedState {
5461
return this._state;
5562
}
5663

64+
/**
65+
* Save the state to the local storage with JSON format.
66+
* @param {object} state - the state contains the challenge name and whether the challenge is passed.
67+
*/
5768
_save(state) {
5869
localStorage.setItem(this._key, JSON.stringify(state));
5970
}
6071

6172
/**
62-
* Set the challenge as passed in the state.
73+
* Set the target challenge as passed in the state.
74+
*
6375
* @param {'basic' | 'intermediate' | 'advanced' | 'extreme'} level - the level of the challenge.
6476
* @param {string} challengeName - the name of the challenge.
6577
* @returns void
@@ -81,10 +93,14 @@ class PassedState {
8193
}
8294

8395
/**
84-
* Merge the new state to the current state. this function will compare the new state with the current state and finally overwrite the current state based on the new state:
85-
* - If the old key in the current state is not in the new state, the old key will be removed from the current state.
86-
* - If the new key in the new state is not in the current state, the new key will be added to the current state.
87-
* @param {object} newState
96+
* Merge the new state and the current state.
97+
* this function will compare the new state with the current state and finally overwrite the current state based on the new state:
98+
* - If the old key in the current state isn't in the new state, the old key will be removed from the current state.
99+
* - If the new key in the new state isn't in the current state, the new key will be added to the current state.
100+
*
101+
* @param {object} oldState - the current state stored in the local storage.
102+
* @param {object} newState - the latest state from the server.
103+
* @returns mergedState - the merged state.
88104
*/
89105
_checkAndMerge(oldState, newState) {
90106
if (!newState && !oldState) {

0 commit comments

Comments
 (0)