Skip to content

Commit

Permalink
Merge pull request #39 from gravity-ui/fix-hook-state
Browse files Browse the repository at this point in the history
Fix hook state + public ensureRunning
  • Loading branch information
vanilla-wave authored Oct 5, 2023
2 parents 978f5f2 + d08025a commit 4039b6a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 26 deletions.
52 changes: 26 additions & 26 deletions src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,31 @@ export class Controller<HintParams, Presets extends string, Steps extends string
this.logger.debug('Progress reset finished', presetArg);
};

async ensureRunning() {
if (this.status === 'active') {
return;
}

if (!this.progressLoadingPromise) {
this.progressLoadingPromise = this.options.getProgressState();
}

this.logger.debug('Loading onboarding progress data');
try {
const newProgressState = await this.progressLoadingPromise;
this.state.progress = {
...defaultProgress,
...newProgressState,
};
this.status = 'active';
this.emitChange();

this.logger.debug('Onboarding progress data loaded');
} catch (e) {
this.logger.error('progress data loading error');
}
}

private resolvePresetSlug = (presetSlug: Presets) => {
const preset = this.options.config.presets[presetSlug];

Expand Down Expand Up @@ -606,31 +631,6 @@ export class Controller<HintParams, Presets extends string, Steps extends string
await this.saveBaseState();
}

private async ensureRunning() {
if (this.status === 'active') {
return;
}

if (!this.progressLoadingPromise) {
this.progressLoadingPromise = this.options.getProgressState();
}

this.logger.debug('Loading onboarding progress data');
try {
const newProgressState = await this.progressLoadingPromise;
this.state.progress = {
...defaultProgress,
...newProgressState,
};
this.status = 'active';
this.emitChange();

this.logger.debug('Onboarding progress data loaded');
} catch (e) {
this.logger.error('progress data loading error');
}
}

private ensurePresetExists(preset: string): asserts preset is Presets {
// @ts-ignore
if (!this.options.config.presets[preset]) {
Expand Down Expand Up @@ -683,7 +683,7 @@ export class Controller<HintParams, Presets extends string, Steps extends string
}

private emitChange = () => {
this.state = {...this.state};
this.state = JSON.parse(JSON.stringify(this.state));

for (const listener of this.stateListeners) {
listener();
Expand Down
35 changes: 35 additions & 0 deletions src/tests/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,24 @@ describe('store api', function () {
expect(Object.is(snapshot1, snapshot2)).toBe(false);
});

it('change state -> deep field updates', async function () {
const options = getOptions();

const controller = new Controller(options);

const snapshot1 = controller.getSnapshot();
await controller.addPreset('createQueue');
const snapshot2 = controller.getSnapshot();

expect(Object.is(snapshot1.base.activePresets, snapshot2.base.activePresets)).toBe(false);
expect(Object.is(snapshot1.base.availablePresets, snapshot2.base.availablePresets)).toBe(
false,
);
expect(Object.is(snapshot1.base.suggestedPresets, snapshot2.base.suggestedPresets)).toBe(
false,
);
});

it('change progress -> trigger callback', async function () {
const options = getOptions();

Expand All @@ -190,6 +208,23 @@ describe('store api', function () {
expect(Object.is(snapshot1, snapshot2)).toBe(false);
});

it('change progress -> deep field updates', async function () {
const options = getOptions();

const controller = new Controller(options);

const snapshot1 = controller.getSnapshot();
await controller.passStep('createSprint');
const snapshot2 = controller.getSnapshot();

expect(
Object.is(snapshot1.progress?.presetPassedSteps, snapshot2.progress?.presetPassedSteps),
).toBe(false);
expect(
Object.is(snapshot1.progress?.finishedPresets, snapshot2.progress?.finishedPresets),
).toBe(false);
});

it('unsubscribe -> no cb', async function () {
const options = getOptions();

Expand Down

0 comments on commit 4039b6a

Please sign in to comment.