Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/release-alpha.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Release-alpha

on:
workflow_dispatch:
inputs:
logLevel:
description: 'Log level'
required: true
default: 'warning'
tags:
description: 'Test scenario tags'


jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Use Node.js 16
uses: actions/setup-node@v3
with:
node-version: 16
- run: |
corepack enable
pnpm --version
- run: pnpm install --frozen-lockfile
- run: pnpm --filter ./packages/model run coverage
- run: pnpm --filter ./packages/model run build
- uses: JS-DevTools/npm-publish@v1
with:
package: ./packages/model/package.json
token: ${{ secrets.NPM_TOKEN }}
2 changes: 1 addition & 1 deletion packages/model/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kwai-explore/model",
"version": "0.4.3",
"version": "0.4.3-alpha.0",
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"typings": "dist/index.d.cts",
Expand Down
28 changes: 21 additions & 7 deletions packages/model/src/clients/client-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,13 @@ export function clientFactory<ClientType extends 'GQL'| 'REST'>(

const cache = options?.cache || createCache();

function getDataFromCache<T>(params: Parameters<typeof request>[0]) {
const data = cache.get<T>(`${hash(params.url)}-${hash(params.variables || {})}`);
function getDataFromCache<T>(params: Parameters<typeof request>[0], type: 'error' | 'data' = 'data') {
const data = cache.get<T>(`${hash(params.url)}-${hash(params.variables || {})}-${type}`);
return data;
}

function setDataToCache<T>(params: Parameters<typeof request>[0], data: T) {
const key = `${hash(params.url)}-${hash(params.variables || {})}`;
function setDataToCache<T>(params: Parameters<typeof request>[0], data: T, type: 'error' | 'data' = 'data') {
const key = `${hash(params.url)}-${hash(params.variables || {})}-${type}`;
cache.put(key, data);
}

Expand All @@ -246,10 +246,15 @@ export function clientFactory<ClientType extends 'GQL'| 'REST'>(
// 处于Hydration阶段,一律先从缓存里面拿
if (hydrationStatus.value !== 2) {
const data = getDataFromCache<T>(params);
const error = getDataFromCache<T>(params, 'error');
if (data) {
subject.next(data);
subject.complete();
return subject;
} else if (error) {
subject.error(error);
subject.complete();
return subject;
}
}
const data = getDataFromCache<T>(params);
Expand All @@ -264,6 +269,7 @@ export function clientFactory<ClientType extends 'GQL'| 'REST'>(
subject.next(data);
subject.complete();
}).catch(e => {
setDataToCache(params, e, 'error');
subject.error(e);
subject.complete();
});
Expand All @@ -277,7 +283,11 @@ export function clientFactory<ClientType extends 'GQL'| 'REST'>(
setDataToCache(params, data);
subject.next(data);
subject.complete();
}).catch(e => subject.error(e));
}).catch((e) => {
setDataToCache(params, e, 'error');
subject.error(e);
subject.complete();
});
}
break;
case 'network-first':
Expand All @@ -286,6 +296,7 @@ export function clientFactory<ClientType extends 'GQL'| 'REST'>(
subject.next(data);
subject.complete();
}).catch(e => {
setDataToCache(params, e, 'error');
subject.error(e);
subject.complete();
});
Expand All @@ -295,7 +306,9 @@ export function clientFactory<ClientType extends 'GQL'| 'REST'>(
subject.next(data);
subject.complete();
} else {
subject.error('No data in cache');
const e = 'No data in cache';
setDataToCache(params, e, 'error');
subject.error(e);
subject.complete();
}
break;
Expand All @@ -306,8 +319,9 @@ export function clientFactory<ClientType extends 'GQL'| 'REST'>(
subject.complete();
})
.catch(e => {
subject.complete();
setDataToCache(params, e, 'error');
subject.error(e);
subject.complete();
});
default:
throw new Error(`There is a wrong fetchPolicy: ${fetchPolicy}`);
Expand Down