Skip to content

Migrate to xstate as app skeleton #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 2, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor storage into editor
  • Loading branch information
ShMcK committed Jun 2, 2019
commit 9bcf64cc75b38a7c46df17f18d1d44fd5fd6afb9
2 changes: 1 addition & 1 deletion src/editor/commands/loadSolution.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as CR from 'typings'
import * as storage from '../storage'
import * as storage from '../../services/storage'
import { gitLoadCommits, gitClear } from '../../services/git'

export default async function loadSolution(): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion src/editor/commands/runTest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getOutputChannel } from '../channel'
import { exec } from '../../services/node'
import * as storage from '../storage'
import * as storage from '../../services/storage'
import * as testResult from '../../services/testResult'

// ensure only latest run_test action is taken
Expand Down
2 changes: 1 addition & 1 deletion src/editor/commands/tutorialLoad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as CR from 'typings'
import api from '../../services/api'
import tutorialSetup from '../../services/tutorialSetup'
import { loadProgressPosition } from '../../services/position'
import * as storage from '../storage'
import * as storage from '../../services/storage'
import rootSetup from '../../services/rootSetup'
import { isEmptyWorkspace, openReadme } from '../workspace'
import * as git from '../../services/git'
Expand Down
74 changes: 4 additions & 70 deletions src/editor/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,77 +8,11 @@ export function setStorage(workspaceState: vscode.Memento): void {
storage = workspaceState
}

// TUTORIAL
const STORE_TUTORIAL = 'coderoad:tutorial'

export async function getTutorial(): Promise<CR.Tutorial | undefined> {
return storage.get(STORE_TUTORIAL)
}

export async function setTutorial(tutorial: CR.Tutorial): Promise<void> {
await storage.update(STORE_TUTORIAL, tutorial)
export function get<T>(key: string): T | undefined {
return storage.get(key)
}

// POSITION
const STORE_POSITION = 'coderoad:position'

const defaultPosition = { levelId: '', stageId: '', stepId: '' }

export async function getPosition(): Promise<CR.Position> {
const position: CR.Position | undefined = storage.get(STORE_POSITION)
return position || defaultPosition
export function update<T>(key: string, value: string | Object): Thenable<void> {
return storage.update(key, value)
}

export async function setPosition(position: CR.Position): Promise<void> {
await storage.update(STORE_POSITION, position)
}

// PROGRESS
const STORE_PROGRESS = 'coderoad:progress'

const defaultProgress = { levels: {}, stages: {}, steps: {}, hints: {}, complete: false }

export async function getProgress(): Promise<CR.Progress> {
const progress: CR.Progress | undefined = await storage.get(STORE_PROGRESS)
return progress || defaultProgress
}

export async function resetProgress(): Promise<void> {
await storage.update(STORE_PROGRESS, defaultProgress)
}

interface ProgressUpdate {
levels?: {
[levelId: string]: boolean
}
stages?: {
[stageid: string]: boolean
}
steps?: {
[stepId: string]: boolean
}
}

export async function updateProgress(record: ProgressUpdate): Promise<void> {
const progress = await getProgress()
if (record.levels) {
progress.levels = {
...progress.levels,
...record.levels,
}
}
if (record.stages) {
progress.stages = {
...progress.stages,
...record.stages,
}
}
if (record.steps) {
progress.steps = {
...progress.steps,
...record.steps,
}
}

await storage.update(STORE_PROGRESS, progress)
}
2 changes: 1 addition & 1 deletion src/services/position.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as CR from 'typings'
import * as storage from '../editor/storage'
import * as storage from './storage'

export async function getInitial(tutorial: CR.Tutorial): Promise<CR.Position> {
const { data } = tutorial
Expand Down
77 changes: 77 additions & 0 deletions src/services/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import * as CR from 'typings'
import * as storage from '../editor/storage'

// TUTORIAL
const STORE_TUTORIAL = 'coderoad:tutorial'

export async function getTutorial(): Promise<CR.Tutorial | undefined> {
return storage.get<CR.Tutorial>(STORE_TUTORIAL)
}

export async function setTutorial(tutorial: CR.Tutorial): Promise<void> {
await storage.update<CR.Tutorial>(STORE_TUTORIAL, tutorial)
}

// POSITION
const STORE_POSITION = 'coderoad:position'

const defaultPosition = { levelId: '', stageId: '', stepId: '' }

export async function getPosition(): Promise<CR.Position> {
const position: CR.Position | undefined = storage.get<CR.Position>(STORE_POSITION)
return position || defaultPosition
}

export async function setPosition(position: CR.Position): Promise<void> {
await storage.update<CR.Position>(STORE_POSITION, position)
}

// PROGRESS
const STORE_PROGRESS = 'coderoad:progress'

const defaultProgress = { levels: {}, stages: {}, steps: {}, hints: {}, complete: false }

export async function getProgress(): Promise<CR.Progress> {
const progress: CR.Progress | undefined = await storage.get<CR.Progress>(STORE_PROGRESS)
return progress || defaultProgress
}

export async function resetProgress(): Promise<void> {
await storage.update<CR.Progress>(STORE_PROGRESS, defaultProgress)
}

interface ProgressUpdate {
levels?: {
[levelId: string]: boolean
}
stages?: {
[stageid: string]: boolean
}
steps?: {
[stepId: string]: boolean
}
}

export async function updateProgress(record: ProgressUpdate): Promise<void> {
const progress = await getProgress()
if (record.levels) {
progress.levels = {
...progress.levels,
...record.levels,
}
}
if (record.stages) {
progress.stages = {
...progress.stages,
...record.stages,
}
}
if (record.steps) {
progress.steps = {
...progress.steps,
...record.steps,
}
}

await storage.update<CR.Progress>(STORE_PROGRESS, progress)
}
2 changes: 1 addition & 1 deletion src/services/testResult.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as CR from 'typings'
import * as vscode from 'vscode'
import * as storage from '../editor/storage'
import * as storage from './storage'


export async function onSuccess(position: CR.Position) {
Expand Down
2 changes: 1 addition & 1 deletion src/services/tutorialSetup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as CR from 'typings'
import * as position from '../services/position'
import * as storage from '../editor/storage'
import * as storage from '../services/storage'
import { isEmptyWorkspace } from '../editor/workspace'
import { gitLoadCommits, gitInitIfNotExists, gitSetupRemote } from '../services/git'

Expand Down
2 changes: 1 addition & 1 deletion src/state/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assign, send } from 'xstate'
import * as CR from 'typings'
import * as storage from '../../editor/storage'
import * as storage from '../../services/storage'
import * as git from '../../services/git'

let initialTutorial: CR.Tutorial | undefined
Expand Down