Felienne Hermans
Felienne Hermans
December 19th, 2021

Code Reading Club Reading Countdown - 19

Article cover photo

Countdown - Snippet 19

Today, a snippet in typescript.

Snippet

CRCRC-19

Snippet in code

setGlobalState(state: Record<string, any> = {}) {
  if (state === globalState) {
    console.warn('state has not changed!');
    return false;
  }

  const changeKeys: string[] = [];
  const prevGlobalState = cloneDeep(globalState);
  globalState = cloneDeep(
    Object.keys(state).reduce((_globalState, changeKey) => {
      if (isMaster || _globalState.hasOwnProperty(changeKey)) {
        changeKeys.push(changeKey);
        return Object.assign(_globalState, { [changeKey]: state[changeKey] });
      }
      console.warn(`'${changeKey}' not declared when init state!`);
      return _globalState;
    }, globalState),
  );
  if (changeKeys.length === 0) {
    console.warn('state has not changed!');
    return false;
  }
  emitGlobal(globalState, prevGlobalState);
  return true;
},

Question

In what situations is the global state not changed by this code?

Submission

Submit your answer in our form.