File tree Expand file tree Collapse file tree 6 files changed +42
-16
lines changed
Expand file tree Collapse file tree 6 files changed +42
-16
lines changed Original file line number Diff line number Diff line change 5959 "editor.formatOnSave" : true
6060 },
6161 "editor.codeActionsOnSave" : {
62- "source.fixAll.eslint" : true
62+ "source.fixAll.eslint" : " explicit "
6363 },
6464 "editor.defaultFormatter" : " esbenp.prettier-vscode" ,
6565 "javascript.format.enable" : false ,
Original file line number Diff line number Diff line change 11{
22 "name" : " @lidakai/local-cache" ,
3- "version" : " 0.1.3 " ,
3+ "version" : " 0.1.4 " ,
44 "jsnext:source" : " ./src/index.ts" ,
55 "types" : " ./dist/types/index.d.ts" ,
66 "main" : " ./dist/lib/index.js" ,
77 "module" : " ./dist/es/index.js" ,
8+ "files" : [
9+ " dist"
10+ ],
811 "scripts" : {
912 "prepare" : " modern build && husky install" ,
1013 "dev" : " modern dev" ,
3437 " dist/"
3538 ],
3639 "dependencies" : {
40+ "@types/eventemitter3" : " ^2.0.2" ,
3741 "@types/lodash-es" : " ^4.17.8" ,
3842 "eventemitter3" : " ^5.0.1" ,
3943 "idb-keyval" : " ^6.2.1" ,
Original file line number Diff line number Diff line change @@ -37,19 +37,20 @@ export interface DeaultConfig {
3737}
3838
3939export const deaultConfig : DeaultConfig = {
40- open : true ,
40+ open : true , // 开关
4141 fetchFreeze : false , // 是否冻结window.fetch 防止被覆盖,默认false
4242 expiration : {
4343 maxEntries : 200 , // 最多缓存X条数据
4444 overrideRetain : true , // 超限制后处理策略,true覆盖 false停止工作
4545 } ,
4646 db : {
47- dbName : defaultDbName ,
48- dbVersion : defaultDbVersion ,
49- storeName : defaultStoreName ,
50- storeVersion : defaultStoreVersion ,
47+ dbName : defaultDbName , // 本地数据库名字
48+ dbVersion : defaultDbVersion , // 本地数据库版本
49+ storeName : defaultStoreName , // 表名
50+ storeVersion : defaultStoreVersion , // 表版本
5151 } ,
5252 rules : {
53+ // 自定义规则
5354 statuses : [ 0 , 200 ] , // 缓存的状态码
5455 method : 'GET' ,
5556 filters : [ / \/ a p i \/ / ] ,
Original file line number Diff line number Diff line change 22 * @description 事件
33 * */
44import EventEmitter from 'eventemitter3' ;
5+ import { FetchOptions } from './fetch' ;
56// 创建一个事件对象
67const emitter = new EventEmitter ( ) ;
78const eventName = 'local-cache' ;
89/**
910 * @description 发送消息
1011 * */
11- export function sendMessage ( data : unknown ) {
12+ export function sendMessage ( data : DataType ) {
1213 // 发送消息
1314 emitter . emit ( eventName , data ) ;
1415}
1516
16- export function onMessage ( callback : ( data : unknown ) => void ) {
17+ export function onMessage ( callback : ( data : DataType ) => void , url : string ) {
1718 // 接收消息
1819 callback &&
19- emitter . on ( eventName , ( data : unknown ) => {
20- callback ?.( data ) ;
20+ emitter . on ( eventName , ( data : DataType ) => {
21+ if ( url && url === data . url ) {
22+ callback ?.( data ) ;
23+ } else if ( ! url ) {
24+ callback ?.( data ) ;
25+ }
2126 } ) ;
2227}
28+
29+ interface DataType {
30+ url : string ;
31+ options : FetchOptions ;
32+ result : unknown ;
33+ }
Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ type FetchHandler = (
1717
1818type BodyInit = ArrayBuffer | Blob | string ;
1919
20- interface FetchOptions extends RequestInit {
20+ export interface FetchOptions extends RequestInit {
2121 body ?: BodyInit ;
2222}
2323
@@ -68,7 +68,7 @@ function createFetchProxy(): FetchHandler {
6868/**
6969 * @description main
7070 * */
71- export function watchFetch ( config : Partial < DeaultConfig > = { } ) {
71+ export function watchFetch ( config : Partial < DeaultConfig > = { } ) : void {
7272 const c = mergeConfig ( config ) ;
7373 updateConfig ( c ) ; // config 更新
7474 setCustomStore ( c ) ; // 初始化并冻结config
You can’t perform that action at this time.
0 commit comments