Skip to content

Commit

Permalink
@uppy/store-default: refactor to typescript (#4785)
Browse files Browse the repository at this point in the history
* @uppy/store-default: refactor to typescript

* fixup! @uppy/store-default: refactor to typescript

* use geniric
  • Loading branch information
aduh95 authored Nov 21, 2023
1 parent 73fdcaf commit 3d46ed0
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 44 deletions.
40 changes: 0 additions & 40 deletions packages/@uppy/store-default/src/index.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { describe, expect, it } from 'vitest'
import assert from 'node:assert'
import DefaultStore from './index.js'
import DefaultStore, { type Listener, type GenericState } from './index.ts'

describe('DefaultStore', () => {
it('cannot be created without new', () => {
// @ts-expect-error TypeScript warns us that the following will throw.
assert.throws(() => DefaultStore(), /TypeError/)
})

Expand All @@ -22,11 +23,11 @@ describe('DefaultStore', () => {
})

it('notifies subscriptions when state changes', () => {
let expected = []
let expected: GenericState[] = []
let calls = 0
function listener (prevState, nextState, patch) {
function listener(...args: Parameters<Listener<GenericState>>): void {
calls++
expect([prevState, nextState, patch]).toEqual(expected)
expect(args).toEqual(expected)
}

const store = new DefaultStore()
Expand Down
49 changes: 49 additions & 0 deletions packages/@uppy/store-default/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore We don't want TS to generate types for the package.json
import packageJson from '../package.json'

export type GenericState = Record<string, unknown>

export type Listener<T> = (
prevState: T,
nextState: T,
patch: Partial<T>,
) => void

/**
* Default store that keeps state in a simple object.
*/
class DefaultStore<T extends GenericState = GenericState> {
static VERSION = packageJson.version

public state: T = {} as T

#callbacks = new Set<Listener<T>>()

getState(): T {
return this.state
}

setState(patch: Partial<T>): void {
const prevState = { ...this.state }
const nextState = { ...this.state, ...patch }

this.state = nextState
this.#publish(prevState, nextState, patch)
}

subscribe(listener: Listener<T>): () => void {
this.#callbacks.add(listener)
return () => {
this.#callbacks.delete(listener)
}
}

#publish(...args: Parameters<Listener<T>>): void {
this.#callbacks.forEach((listener) => {
listener(...args)
})
}
}

export default DefaultStore
13 changes: 13 additions & 0 deletions packages/@uppy/store-default/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../../../tsconfig.shared",
"compilerOptions": {
"outDir": "./lib",
"rootDir": "./src",
"resolveJsonModule": false,
"noImplicitAny": false,
"skipLibCheck": true
},
"include": ["./src/**/*.*"],
"exclude": ["./src/**/*.test.ts"],
"references": []
}
9 changes: 9 additions & 0 deletions packages/@uppy/store-default/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../../tsconfig.shared",
"compilerOptions": {
"emitDeclarationOnly": false,
"noEmit": true
},
"include": ["./package.json", "./src/**/*.*"],
"references": []
}

0 comments on commit 3d46ed0

Please sign in to comment.