-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
158 changed files
with
5,175 additions
and
4,458 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,19 @@ | ||
import { Disposer } from './Disposer' | ||
|
||
test('表现正常', async () => { | ||
const disposer = new Disposer() | ||
|
||
const dispose1 = jest.fn() | ||
const dispose2 = jest.fn() | ||
const dispose3 = jest.fn() | ||
|
||
disposer.add(dispose1) | ||
disposer.add(dispose1) | ||
disposer.add(dispose2, dispose3) | ||
|
||
await disposer.dispose() | ||
|
||
expect(dispose1).toBeCalledTimes(1) | ||
expect(dispose2).toBeCalledTimes(1) | ||
expect(dispose3).toBeCalledTimes(1) | ||
}) |
This file contains 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
This file contains 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,133 @@ | ||
import * as Taro from '@tarojs/taro-h5' | ||
import { AnyFunction } from '../enhanceType' | ||
import { EasyStorage } from './EasyStorage' | ||
import { EasyStorageAdapterMemory } from './EasyStorageAdapterMemory' | ||
import { EasyStorageAdapterWeapp } from './EasyStorageAdapterWeapp' | ||
import { EasyStorageDriverBrowserLocalStorage, EasyStorageDriverBrowserSessionStorage } from './EasyStorageAdapterBrowser' | ||
import { ii } from '../ii' | ||
|
||
ii(function patchWX() { | ||
const originalTaroGetStorage = Taro.getStorage | ||
|
||
Object.defineProperties(Taro, { | ||
getStorage: { | ||
value: (...args: any[]) => { | ||
return (originalTaroGetStorage as any)(...args).catch(() => {}) | ||
}, | ||
}, | ||
clearStorage: { | ||
value: (options: { success: AnyFunction }) => { | ||
localStorage.clear() | ||
options.success && options.success() | ||
}, | ||
}, | ||
}) | ||
|
||
;(global as any).wx = Taro | ||
}) | ||
|
||
type StorageValues = { | ||
str: string, | ||
num: number, | ||
bool: boolean, | ||
obj: object, | ||
arr: any[], | ||
} | ||
|
||
type StorageKey = keyof StorageValues | ||
|
||
const storageKeys: StorageKey[] = ['str', 'num', 'bool', 'obj', 'arr'] | ||
|
||
const adapters = [ | ||
EasyStorageDriverBrowserLocalStorage, | ||
EasyStorageDriverBrowserSessionStorage, | ||
EasyStorageAdapterMemory, | ||
EasyStorageAdapterWeapp, | ||
] | ||
|
||
adapters.forEach(Adapter => { | ||
describe(Adapter.name, () => { | ||
const storage = new EasyStorage<StorageValues>(new Adapter()) | ||
|
||
test('键值不存在时返回 null', async () => { | ||
await Promise.all( | ||
storageKeys.map(async key => { | ||
expect(await storage.get(key)).toBeNull() | ||
expect(storage.getSync(key)).toBeNull() | ||
}), | ||
) | ||
}) | ||
|
||
test('键值不存在且设置了默认值时返回默认值', async () => { | ||
await Promise.all( | ||
storageKeys.map(async key => { | ||
expect(await storage.get(key, 'dv')).toBe('dv') | ||
expect(storage.getSync(key, 'dv')).toBe('dv') | ||
}), | ||
) | ||
}) | ||
|
||
test('键值存在时正确返回其值', async () => { | ||
const storageValues: StorageValues = { | ||
str: 'str', | ||
num: 100, | ||
bool: true, | ||
obj: { x: 1, y: '3' }, | ||
arr: [3, { 2: 4 }, false, 'hello'], | ||
} | ||
await Promise.all( | ||
storageKeys.map(async (key, index) => { | ||
if (index % 2) { | ||
await storage.set(key, storageValues[key]) | ||
} else { | ||
storage.setSync(key, storageValues[key]) | ||
} | ||
}), | ||
) | ||
await Promise.all( | ||
storageKeys.map(async key => { | ||
expect(await storage.get(key)).toEqual(storageValues[key]) | ||
expect(storage.getSync(key)).toEqual(storageValues[key]) | ||
}), | ||
) | ||
}) | ||
|
||
test('可删除存储的值', async () => { | ||
await Promise.all( | ||
storageKeys.map(async (key, index) => { | ||
if (index % 2) { | ||
await storage.remove(key) | ||
} else { | ||
storage.removeSync(key) | ||
} | ||
expect(await storage.get(key)).toBeNull() | ||
}), | ||
) | ||
}) | ||
|
||
test('可清空存储的值', async () => { | ||
await storage.set('str', 'test') | ||
expect(await storage.get('str')).toBe('test') | ||
await storage.clear() | ||
expect(await storage.get('str')).toBeNull() | ||
storage.setSync('str', 'test') | ||
expect(storage.getSync('str')).toBe('test') | ||
storage.clearSync() | ||
expect(storage.getSync('str')).toBeNull() | ||
}) | ||
|
||
test('可记住获取的值', async () => { | ||
expect(await storage.get('str')).toBeNull() | ||
expect(await storage.getRemember('str')).toBeNull() | ||
expect(await storage.getRemember('str', () => 'hello')).toBe('hello') | ||
expect(storage.getSync('str', () => 'hello2')).toBe('hello') | ||
expect(await storage.get('str')).toBe('hello') | ||
storage.removeSync('str') | ||
expect(await storage.get('str')).toBeNull() | ||
expect(storage.getRememberSync('str')).toBeNull() | ||
expect(storage.getRememberSync('str', 'hello')).toBe('hello') | ||
expect(await storage.get('str', 'hello2')).toBe('hello') | ||
expect(storage.getSync('str')).toBe('hello') | ||
}) | ||
}) | ||
}) |
This file contains 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,84 @@ | ||
import { EasyStorageAdapter } from './EasyStorageAdapter' | ||
import { isFunction } from '../is' | ||
|
||
export class EasyStorage< | ||
T extends Record<string, any> = Record<string, any>, | ||
K extends Extract<keyof T, string> = Extract<keyof T, string>, | ||
> { | ||
constructor(private adapter: EasyStorageAdapter) {} | ||
|
||
set(key: K, value: T[K]): Promise<void> { | ||
return this.adapter.set(key, value) | ||
} | ||
|
||
setSync(key: K, value: T[K]): void { | ||
this.adapter.setSync(key, value) | ||
} | ||
|
||
get<DV extends T[K] | (() => (T[K] | Promise<T[K]>))>( // eslint-disable-line | ||
key: K, | ||
defaultValue: DV = null as any, | ||
): Promise<T[K]> { // eslint-disable-line | ||
return new Promise((resolve, reject) => { | ||
this.adapter.get(key) | ||
.then(value => { | ||
if (value != null) { | ||
return value | ||
} | ||
return isFunction(defaultValue) ? defaultValue() : defaultValue | ||
}) | ||
.then(resolve) | ||
.catch(reject) | ||
}) | ||
} | ||
|
||
getSync<DV extends T[K] | (() => T[K])>( // eslint-disable-line | ||
key: K, | ||
defaultValue: DV = null as any, | ||
): T[K] { // eslint-disable-line | ||
let value = this.adapter.getSync(key) | ||
if (value == null) { | ||
value = isFunction(defaultValue) ? defaultValue() : defaultValue | ||
} | ||
return value | ||
} | ||
|
||
getRemember<DV extends T[K] | (() => (T[K] | Promise<T[K]>))>( // eslint-disable-line | ||
key: K, | ||
defaultValue: DV = null as any, | ||
): Promise<T[K]> { // eslint-disable-line | ||
return this.get(key, defaultValue).then(value => { | ||
if (value != null) { | ||
return this.set(key, value).then(() => value) | ||
} | ||
return value | ||
}) | ||
} | ||
|
||
getRememberSync<DV extends T[K] | (() => T[K])>( // eslint-disable-line | ||
key: K, | ||
defaultValue: DV = null as any, | ||
): T[K] { // eslint-disable-line | ||
const value = this.getSync(key, defaultValue) | ||
if (value != null) { | ||
this.setSync(key, value) | ||
} | ||
return value | ||
} | ||
|
||
remove(key: K) { | ||
return this.adapter.remove(key) | ||
} | ||
|
||
removeSync(key: K) { | ||
return this.adapter.removeSync(key) | ||
} | ||
|
||
clear() { | ||
return this.adapter.clear() | ||
} | ||
|
||
clearSync() { | ||
return this.adapter.clearSync() | ||
} | ||
} |
This file contains 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,17 @@ | ||
export abstract class EasyStorageAdapter { | ||
abstract set(k: string, v: any): Promise<void> | ||
|
||
abstract setSync(k: string, v: any): void | ||
|
||
abstract get(k: string): Promise<any> | ||
|
||
abstract getSync(k: string): any | ||
|
||
abstract remove(k: string): Promise<void> | ||
|
||
abstract removeSync(k: string): void | ||
|
||
abstract clear(): Promise<void> | ||
|
||
abstract clearSync(): void | ||
} |
Oops, something went wrong.