Skip to content

Commit

Permalink
feat: 重构基本完成
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed May 29, 2019
1 parent ee3e4cf commit 6ac8b34
Show file tree
Hide file tree
Showing 158 changed files with 5,175 additions and 4,458 deletions.
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
"^.+\\.ts$": "ts-jest"
},
"collectCoverageFrom": [
"<rootDir>/src/**/*.ts"
"src/**/*.ts",
"!src/**/index.ts"
],
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
Expand All @@ -60,22 +61,24 @@
},
"devDependencies": {
"@tarojs/taro-h5": "^1.2.26",
"@types/jest": "^23.3.4",
"@types/globby": "^9.1.0",
"@types/jest": "^24.0.13",
"@types/sinon": "^5.0.5",
"bili": "^3.4.1",
"codecov": "^3.1.0",
"eslint": "^5.9.0",
"eslint-config-io": "^0.5.0",
"gh-pages": "^2.0.1",
"jest": "^23.6.0",
"globby": "^9.2.0",
"jest": "^24.8.0",
"moment": "^2.22.2",
"nervjs": "^1.3.13",
"rollup-plugin-typescript2": "^0.18.0",
"standard-version": "^4.4.0",
"ts-jest": "^23.10.4",
"ts-jest": "^24.0.2",
"ts-node": "^8.0.2",
"typed-we-app": "^1.7.0-update.6",
"typedoc": "^0.14.2",
"typescript": "^3.3.3333"
"typescript": "^3.4.5"
}
}
19 changes: 19 additions & 0 deletions src/Disposer.test.ts
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)
})
34 changes: 26 additions & 8 deletions src/Disposer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { isFunction } from './isFunction'
import { isPromiseLike } from './is'

export type DisposerItem = () => void
/**
* 待释放项目。
*/
export type DisposerItem = () => void | Promise<void>

/**
* 资源释放器。
Expand All @@ -16,17 +19,32 @@ export class Disposer {
*
* @param items 待释放项目的序列
*/
public add(...items: DisposerItem[]): void {
this.jar.push(...items)
add(...items: DisposerItem[]) {
items.forEach(item => {
if (this.jar.indexOf(item) === -1) {
this.jar.push(item)
}
})
}

/**
* 释放所有项目。
*/
public dispose() {
this.jar.forEach(
item => isFunction(item) && item(),
dispose() {
return (
Promise
.all(
this.jar.map(item => {
const result = item()
if (isPromiseLike(result)) {
return result
}
return Promise.resolve()
}),
)
.then(() => {
this.jar = []
})
)
this.jar = []
}
}
133 changes: 133 additions & 0 deletions src/EasyStorage/EasyStorage.test.ts
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')
})
})
})
84 changes: 84 additions & 0 deletions src/EasyStorage/EasyStorage.ts
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()
}
}
17 changes: 17 additions & 0 deletions src/EasyStorage/EasyStorageAdapter.ts
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
}
Loading

0 comments on commit 6ac8b34

Please sign in to comment.