-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
117 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<script lang="ts"> | ||
import { onMount } from 'svelte'; | ||
import { client } from '../../../../../src/'; | ||
import type { Client } from './+server'; | ||
const rpc = client<Client>({ endpoint: '/serverCacheTest' }); | ||
let res1, res2 | ||
onMount(async () => { | ||
res1 = await rpc.Service.hello('name') | ||
res2 = await rpc.Service.hello2(); | ||
}) | ||
// let res1: Promise<Returned<typeof rpc.Service.hello>> = | ||
// rpc.Service.hello.cache({ | ||
// expiry: 1000 * 30, | ||
// mode: 'update', | ||
// onInvalidate: (res) => (res1 = res) | ||
// })('world'); | ||
</script> | ||
|
||
<div> | ||
<p> | ||
{#if res1} | ||
hello: {res1} | ||
{/if} | ||
</p> | ||
<p> | ||
{#if res2} | ||
hello2: {res2} | ||
{/if} | ||
</p> | ||
</div> | ||
|
||
<style> | ||
div { | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {json} from '@sveltejs/kit' | ||
|
||
import { Composer, toRPC, rpc } from '../../../../../src/'; | ||
import { cacheMiddleware, sveltekitMiddleware } from '../../../../../src/middlewares'; | ||
|
||
class Service { | ||
@rpc() | ||
async hello(name: string) { | ||
await new Promise(r => setTimeout(r, 5000)); | ||
const msg = `Hello ${name} ${new Date()}` | ||
return msg | ||
} | ||
|
||
@rpc() | ||
async hello2() { | ||
await new Promise(r => setTimeout(r, 2000)); | ||
return 'hello from hello2' | ||
} | ||
} | ||
|
||
const composer = Composer.init({ | ||
Service: new Service() | ||
}) | ||
|
||
export type Client = typeof composer.clientType | ||
|
||
let cache = {} | ||
const cacheInterface = { | ||
async get(key) { | ||
console.log('cache get', key) | ||
return cache[key] | ||
}, | ||
async set(key, val) { | ||
console.log('cache set', key, val) | ||
cache[key] = val | ||
} | ||
} | ||
|
||
composer.use(sveltekitMiddleware()) | ||
composer.use(cacheMiddleware(cacheInterface)) | ||
|
||
export async function POST(event) { | ||
return json(await composer.exec(event)) | ||
} |