Skip to content

Commit

Permalink
add: Callback push message added limit
Browse files Browse the repository at this point in the history
  • Loading branch information
nianyi778 committed Apr 8, 2024
1 parent 894a656 commit 631ccc4
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"editor.formatOnSave": true
},
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"editor.defaultFormatter": "esbenp.prettier-vscode",
"javascript.format.enable": false,
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"name": "@lidakai/local-cache",
"version": "0.1.3",
"version": "0.1.4",
"jsnext:source": "./src/index.ts",
"types": "./dist/types/index.d.ts",
"main": "./dist/lib/index.js",
"module": "./dist/es/index.js",
"files": [
"dist"
],
"scripts": {
"prepare": "modern build && husky install",
"dev": "modern dev",
Expand Down Expand Up @@ -34,6 +37,7 @@
"dist/"
],
"dependencies": {
"@types/eventemitter3": "^2.0.2",
"@types/lodash-es": "^4.17.8",
"eventemitter3": "^5.0.1",
"idb-keyval": "^6.2.1",
Expand Down
16 changes: 13 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions src/constants/defaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,20 @@ export interface DeaultConfig {
}

export const deaultConfig: DeaultConfig = {
open: true,
open: true, // 开关
fetchFreeze: false, // 是否冻结window.fetch 防止被覆盖,默认false
expiration: {
maxEntries: 200, // 最多缓存X条数据
overrideRetain: true, // 超限制后处理策略,true覆盖 false停止工作
},
db: {
dbName: defaultDbName,
dbVersion: defaultDbVersion,
storeName: defaultStoreName,
storeVersion: defaultStoreVersion,
dbName: defaultDbName, // 本地数据库名字
dbVersion: defaultDbVersion, // 本地数据库版本
storeName: defaultStoreName, // 表名
storeVersion: defaultStoreVersion, // 表版本
},
rules: {
// 自定义规则
statuses: [0, 200], // 缓存的状态码
method: 'GET',
filters: [/\/api\//],
Expand Down
19 changes: 15 additions & 4 deletions src/modules/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,32 @@
* @description 事件
* */
import EventEmitter from 'eventemitter3';
import { FetchOptions } from './fetch';
// 创建一个事件对象
const emitter = new EventEmitter();
const eventName = 'local-cache';
/**
* @description 发送消息
* */
export function sendMessage(data: unknown) {
export function sendMessage(data: DataType) {
// 发送消息
emitter.emit(eventName, data);
}

export function onMessage(callback: (data: unknown) => void) {
export function onMessage(callback: (data: DataType) => void, url: string) {
// 接收消息
callback &&
emitter.on(eventName, (data: unknown) => {
callback?.(data);
emitter.on(eventName, (data: DataType) => {
if (url && url === data.url) {
callback?.(data);
} else if (!url) {
callback?.(data);
}
});
}

interface DataType {
url: string;
options: FetchOptions;
result: unknown;
}
4 changes: 2 additions & 2 deletions src/modules/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type FetchHandler = (

type BodyInit = ArrayBuffer | Blob | string;

interface FetchOptions extends RequestInit {
export interface FetchOptions extends RequestInit {
body?: BodyInit;
}

Expand Down Expand Up @@ -68,7 +68,7 @@ function createFetchProxy(): FetchHandler {
/**
* @description main
* */
export function watchFetch(config: Partial<DeaultConfig> = {}) {
export function watchFetch(config: Partial<DeaultConfig> = {}): void {
const c = mergeConfig(config);
updateConfig(c); // config 更新
setCustomStore(c); // 初始化并冻结config
Expand Down

0 comments on commit 631ccc4

Please sign in to comment.