-
Couldn't load subscription status.
- Fork 17
feat(dt-utils): refactor the dt-utils #105
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
Open
jin-sir
wants to merge
38
commits into
DTStack:master
Choose a base branch
from
jin-sir:feat_utils
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
50d84ab
chore: upgrade package & replace packaging tools
jin-sir 9131eef
refactor: remove old functions
jin-sir 82a4a25
refactor: rename browserCheck to checkBrowserSupport
jin-sir 9963d6a
refactor: improve copy
jin-sir 5f4b0bd
refactor: rename downLoadData to downloadFile
jin-sir 58a28e5
refactor: rename convertBytes to formatBytes
jin-sir 49bc19b
refactor: rename dateTime to formatDateTime
jin-sir 492de08
feat: add formatSecond
jin-sir 7157840
feat: add formatBase64
jin-sir bc628b4
refactor: rename generateFullUrlPath to generateUrlWithQuery
jin-sir ee4e777
refactor: merge generateAKey and getRandomStr into getKey
jin-sir 8530435
refactor: improve getQueryParameters
jin-sir 5cff92d
feat: add getTypeOfValue
jin-sir 56ba7cc
refactor: improve isMacOS
jin-sir 1705964
refactor: improve isMobile
jin-sir 8df8b0e
refactor: improve isWindows
jin-sir d8eb0bd
refactor: localDB.set support batch set, localDB.clear support except…
jin-sir d35ca34
refactor: improve LocalIndexedDB
jin-sir 1744d5b
feat: add sessionDB
jin-sir 3166fec
feat: add shouldRender
jin-sir c20cf9d
refactor: merge getBase64 and base64Encode into toBase64
jin-sir a2774db
refactor: rename percent to toPercent
jin-sir 4dea50d
refactor: rename exchangeOrder to toSortOrder
jin-sir 20c180a
refactor: rename getThousandth to toThousand
jin-sir fb7148e
feat: add unit testing
jin-sir 3f9cee2
chore: add entry file
jin-sir f5e2bf1
chore: remove docs
jin-sir 44587fe
refactor: build site with VitePress and generate documentation using …
jin-sir 2e8f0a5
chore: add docs:deploy script
jin-sir b766ac0
chore: replace yarn with pnpm
jin-sir 2aa1db3
refactor: rename LocalIndexedDB to IndexedDB
jin-sir 853ef05
refactor: improve code
jin-sir 00c11ed
chore: translate English comments to Chinese
jin-sir c80bde9
chore: update project configuration and set TypeScript package version
jin-sir 698f9d0
chore: update docs
jin-sir 77ce923
Merge remote-tracking branch 'origin/master' into feat_utils
jin-sir 94aba92
chore: replace yarn with pnpm
jin-sir fb73a55
refactor: rename getKey to generateUniqueId
jin-sir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| /** | ||
| * LocalIndexedDB | ||
| * | ||
| * @deprecated | ||
| * @category Storage | ||
| * @description | ||
| * This class provides a wrapper for IndexedDB, a low-level API for client-side storage of significant amounts of structured data, including files/blobs. | ||
| * It allows you to store and retrieve data in a structured format, and to query and manipulate that data using a variety of methods. | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * import { LocalIndexedDB } from 'dt-utils'; | ||
| * | ||
| * // Initialize database | ||
| * const db = new LocalIndexedDB('userDB', 1, 'users'); | ||
| * await db.open(); | ||
| * | ||
| * // Add data | ||
| * await db.add('user1', { name: 'John', age: 30 }); | ||
| * | ||
| * // Get data | ||
| * const user = await db.get('user1'); | ||
| * | ||
| * // Update data | ||
| * await db.set('user1', { name: 'John', age: 31 }); | ||
| * | ||
| * // Delete data | ||
| * await db.delete('user1'); | ||
| * | ||
| * // Clear all data | ||
| * await db.clear(); | ||
| * ``` | ||
| * | ||
| * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB|MDN IndexedDB Usage Guide} | ||
| * @see {@link https://caniuse.com/#feat=indexeddb|Browser Compatibility} | ||
| */ | ||
| class LocalIndexedDB { | ||
| private _db!: IDBDatabase; | ||
| private _version!: number; | ||
| private _database!: string; | ||
| private _storeName!: string; | ||
| private _openLog!: boolean; | ||
|
|
||
| /** | ||
| * Constructor for a new indexedDB object | ||
| * @param database Database name | ||
| * @param version Database version | ||
| * @param storeName Store object name | ||
| * @param openLog Whether to log indexedDB changes | ||
| */ | ||
| constructor(database: string, version: number, storeName: string, openLog = false) { | ||
| if (!this.checkBrowserSupport()) { | ||
| console.error("This browser doesn't support IndexedDB"); | ||
| return; | ||
| } | ||
| this._storeName = storeName; | ||
| this._version = version; | ||
| this._database = database; | ||
| this._openLog = openLog; | ||
| } | ||
|
|
||
| private checkBrowserSupport(): boolean { | ||
| return 'indexedDB' in window; | ||
| } | ||
|
|
||
| /** | ||
| * Open the database indicated in the constructor function. | ||
| * This method returns a Promise that resolves to the db instance. | ||
| */ | ||
| public async open(): Promise<IDBDatabase> { | ||
| if (this._db) { | ||
| return this._db; | ||
| } | ||
|
|
||
| try { | ||
| this._db = await this.createConnection(); | ||
| return this._db; | ||
| } catch (error) { | ||
| this.log('Failed to open database:', error); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| private createConnection(): Promise<IDBDatabase> { | ||
| return new Promise<IDBDatabase>((resolve, reject) => { | ||
| /** | ||
| * If exist the same version database, there need upgrade an new version database, | ||
| * because of the same version can't trigger onupgradeneeded event which will occur | ||
| * object stores was not found exception. | ||
| */ | ||
| const request = indexedDB.open(this._database, this._version); | ||
|
|
||
| request.onsuccess = () => { | ||
| this._db = request.result; | ||
| this.setupVersionChangeHandler(); | ||
| this.log('Open indexedDB success!'); | ||
| resolve(this._db); | ||
| }; | ||
|
|
||
| request.onupgradeneeded = (event) => { | ||
| this._db = request.result; | ||
| this.handleUpgrade(event, request, resolve); | ||
| }; | ||
|
|
||
| request.onblocked = (event) => { | ||
| this.log('Database open blocked', event); | ||
| reject(new Error('Database open blocked')); | ||
| }; | ||
|
|
||
| request.onerror = (event) => { | ||
| this.log('Maybe you not allow my web app to use IndexedDB!', event); | ||
| reject(event); | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| private handleUpgrade( | ||
| event: IDBVersionChangeEvent, | ||
| request: IDBOpenDBRequest, | ||
| resolve: (value: IDBDatabase) => void | ||
| ): void { | ||
| if (!this._db.objectStoreNames.contains(this._storeName)) { | ||
| const objectStore = this._db.createObjectStore(this._storeName); | ||
| objectStore.transaction.oncomplete = () => { | ||
| resolve(request.result); | ||
| }; | ||
| } | ||
| this.log('Database Upgraded', event); | ||
| } | ||
|
|
||
| private setupVersionChangeHandler(): void { | ||
| this._db.onversionchange = (event) => { | ||
| this.log('Database version changed', event); | ||
| // Close database connection to avoid blocking upgrade operations in other tabs | ||
| this._db.close(); | ||
| this._db = null as any; | ||
| }; | ||
| } | ||
|
|
||
| private createTransactionStore(storeName: string, mode: IDBTransactionMode): IDBObjectStore { | ||
| if (!this._db) { | ||
| throw new Error('Database not connected, please call open() method first'); | ||
| } | ||
| const transaction = this._db.transaction([storeName], mode); | ||
|
|
||
| return transaction.objectStore(storeName); | ||
| } | ||
|
|
||
| public async add<T>(key: string, value: T): Promise<IDBRequest> { | ||
| await this.ensureConnection(); | ||
| return this.executeStoreOperation((store) => store.add(value, key)); | ||
| } | ||
|
|
||
| public async set<T>(key: string, value: T): Promise<IDBRequest> { | ||
| await this.ensureConnection(); | ||
| this.log('IndexedDB set', key, value); | ||
| return this.executeStoreOperation((store) => store.put(value, key)); | ||
| } | ||
|
|
||
| public async get<T>(key: string): Promise<T> { | ||
| await this.ensureConnection(); | ||
| this.log('IndexedDB get', key); | ||
| return this.executeStoreOperation<T>((store) => store.get(key)); | ||
| } | ||
|
|
||
| public async delete(key: string): Promise<IDBRequest> { | ||
| await this.ensureConnection(); | ||
| return this.executeStoreOperation((store) => store.delete(key)); | ||
| } | ||
|
|
||
| public async clear(): Promise<IDBRequest> { | ||
| await this.ensureConnection(); | ||
| return this.executeStoreOperation((store) => store.clear()); | ||
| } | ||
|
|
||
| private async ensureConnection(): Promise<void> { | ||
| if (!this._db) { | ||
| await this.open(); | ||
| } | ||
| } | ||
|
|
||
| private executeStoreOperation<T>(operate: (store: IDBObjectStore) => IDBRequest): Promise<T> { | ||
| return new Promise((resolve, reject) => { | ||
| try { | ||
| const store = this.createTransactionStore(this._storeName, 'readwrite'); | ||
| const request = operate(store); | ||
|
|
||
| request.onsuccess = () => resolve(request.result); | ||
| request.onerror = (event) => { | ||
| this.log('Store operation error:', event); | ||
| reject(event); | ||
| }; | ||
| } catch (error) { | ||
| this.log('Error during store operation:', error); | ||
| reject(error); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private log(...args: any[]): void { | ||
| if (this._openLog) { | ||
| console.log(`[IndexedDB ${this._database}]`, ...args); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export default LocalIndexedDB; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为啥废弃,是因为想要直接用第三方包吗,会影响文档