diff --git a/.github/workflows/release-alpha.yml b/.github/workflows/release-alpha.yml new file mode 100644 index 0000000..4348710 --- /dev/null +++ b/.github/workflows/release-alpha.yml @@ -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 }} diff --git a/packages/model/package.json b/packages/model/package.json index 375f2e6..6ee6059 100644 --- a/packages/model/package.json +++ b/packages/model/package.json @@ -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", diff --git a/packages/model/src/clients/client-factory.ts b/packages/model/src/clients/client-factory.ts index 0be3a78..5121f28 100644 --- a/packages/model/src/clients/client-factory.ts +++ b/packages/model/src/clients/client-factory.ts @@ -227,13 +227,13 @@ export function clientFactory( const cache = options?.cache || createCache(); - function getDataFromCache(params: Parameters[0]) { - const data = cache.get(`${hash(params.url)}-${hash(params.variables || {})}`); + function getDataFromCache(params: Parameters[0], type: 'error' | 'data' = 'data') { + const data = cache.get(`${hash(params.url)}-${hash(params.variables || {})}-${type}`); return data; } - function setDataToCache(params: Parameters[0], data: T) { - const key = `${hash(params.url)}-${hash(params.variables || {})}`; + function setDataToCache(params: Parameters[0], data: T, type: 'error' | 'data' = 'data') { + const key = `${hash(params.url)}-${hash(params.variables || {})}-${type}`; cache.put(key, data); } @@ -246,10 +246,15 @@ export function clientFactory( // 处于Hydration阶段,一律先从缓存里面拿 if (hydrationStatus.value !== 2) { const data = getDataFromCache(params); + const error = getDataFromCache(params, 'error'); if (data) { subject.next(data); subject.complete(); return subject; + } else if (error) { + subject.error(error); + subject.complete(); + return subject; } } const data = getDataFromCache(params); @@ -264,6 +269,7 @@ export function clientFactory( subject.next(data); subject.complete(); }).catch(e => { + setDataToCache(params, e, 'error'); subject.error(e); subject.complete(); }); @@ -277,7 +283,11 @@ export function clientFactory( 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': @@ -286,6 +296,7 @@ export function clientFactory( subject.next(data); subject.complete(); }).catch(e => { + setDataToCache(params, e, 'error'); subject.error(e); subject.complete(); }); @@ -295,7 +306,9 @@ export function clientFactory( 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; @@ -306,8 +319,9 @@ export function clientFactory( 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}`);