소스 검색

@uppy/store-default: refactor to typescript (#4785)

* @uppy/store-default: refactor to typescript

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

* use geniric
Antoine du Hamel 1 년 전
부모
커밋
3d46ed02da

+ 0 - 40
packages/@uppy/store-default/src/index.js

@@ -1,40 +0,0 @@
-import packageJson from '../package.json'
-/**
- * Default store that keeps state in a simple object.
- */
-class DefaultStore {
-  static VERSION = packageJson.version
-
-  #callbacks = new Set()
-
-  constructor () {
-    this.state = {}
-  }
-
-  getState () {
-    return this.state
-  }
-
-  setState (patch) {
-    const prevState = { ...this.state }
-    const nextState = { ...this.state, ...patch }
-
-    this.state = nextState
-    this.#publish(prevState, nextState, patch)
-  }
-
-  subscribe (listener) {
-    this.#callbacks.add(listener)
-    return () => {
-      this.#callbacks.delete(listener)
-    }
-  }
-
-  #publish (...args) {
-    this.#callbacks.forEach((listener) => {
-      listener(...args)
-    })
-  }
-}
-
-export default DefaultStore

+ 5 - 4
packages/@uppy/store-default/src/index.test.js → packages/@uppy/store-default/src/index.test.ts

@@ -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/)
   })
 
@@ -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()

+ 49 - 0
packages/@uppy/store-default/src/index.ts

@@ -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 - 0
packages/@uppy/store-default/tsconfig.build.json

@@ -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 - 0
packages/@uppy/store-default/tsconfig.json

@@ -0,0 +1,9 @@
+{
+  "extends": "../../../tsconfig.shared",
+  "compilerOptions": {
+    "emitDeclarationOnly": false,
+    "noEmit": true
+  },
+  "include": ["./package.json", "./src/**/*.*"],
+  "references": []
+}