From 3d46ed02dacc20cf271feac7a782ef5b6abc396e Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 21 Nov 2023 14:41:19 +0100 Subject: [PATCH] @uppy/store-default: refactor to typescript (#4785) * @uppy/store-default: refactor to typescript * fixup! @uppy/store-default: refactor to typescript * use geniric --- packages/@uppy/store-default/src/index.js | 40 --------------- .../src/{index.test.js => index.test.ts} | 9 ++-- packages/@uppy/store-default/src/index.ts | 49 +++++++++++++++++++ .../@uppy/store-default/tsconfig.build.json | 13 +++++ packages/@uppy/store-default/tsconfig.json | 9 ++++ 5 files changed, 76 insertions(+), 44 deletions(-) delete mode 100644 packages/@uppy/store-default/src/index.js rename packages/@uppy/store-default/src/{index.test.js => index.test.ts} (75%) create mode 100644 packages/@uppy/store-default/src/index.ts create mode 100644 packages/@uppy/store-default/tsconfig.build.json create mode 100644 packages/@uppy/store-default/tsconfig.json diff --git a/packages/@uppy/store-default/src/index.js b/packages/@uppy/store-default/src/index.js deleted file mode 100644 index ff1b123c0d..0000000000 --- a/packages/@uppy/store-default/src/index.js +++ /dev/null @@ -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 diff --git a/packages/@uppy/store-default/src/index.test.js b/packages/@uppy/store-default/src/index.test.ts similarity index 75% rename from packages/@uppy/store-default/src/index.test.js rename to packages/@uppy/store-default/src/index.test.ts index d943c4f041..05ae9e5d82 100644 --- a/packages/@uppy/store-default/src/index.test.js +++ b/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>): void { calls++ - expect([prevState, nextState, patch]).toEqual(expected) + expect(args).toEqual(expected) } const store = new DefaultStore() diff --git a/packages/@uppy/store-default/src/index.ts b/packages/@uppy/store-default/src/index.ts new file mode 100644 index 0000000000..adc766045e --- /dev/null +++ b/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 + +export type Listener = ( + prevState: T, + nextState: T, + patch: Partial, +) => void + +/** + * Default store that keeps state in a simple object. + */ +class DefaultStore { + static VERSION = packageJson.version + + public state: T = {} as T + + #callbacks = new Set>() + + getState(): T { + return this.state + } + + setState(patch: Partial): void { + const prevState = { ...this.state } + const nextState = { ...this.state, ...patch } + + this.state = nextState + this.#publish(prevState, nextState, patch) + } + + subscribe(listener: Listener): () => void { + this.#callbacks.add(listener) + return () => { + this.#callbacks.delete(listener) + } + } + + #publish(...args: Parameters>): void { + this.#callbacks.forEach((listener) => { + listener(...args) + }) + } +} + +export default DefaultStore diff --git a/packages/@uppy/store-default/tsconfig.build.json b/packages/@uppy/store-default/tsconfig.build.json new file mode 100644 index 0000000000..cfb3e6f0d9 --- /dev/null +++ b/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": [] +} diff --git a/packages/@uppy/store-default/tsconfig.json b/packages/@uppy/store-default/tsconfig.json new file mode 100644 index 0000000000..687288fd88 --- /dev/null +++ b/packages/@uppy/store-default/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../../../tsconfig.shared", + "compilerOptions": { + "emitDeclarationOnly": false, + "noEmit": true + }, + "include": ["./package.json", "./src/**/*.*"], + "references": [] +}