Skip to content

Commit

Permalink
multiple yields in update
Browse files Browse the repository at this point in the history
  • Loading branch information
hazae41 committed Sep 29, 2022
1 parent c6e8daf commit 09294c2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 44 deletions.
1 change: 0 additions & 1 deletion src/libs/generator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export async function nextOf<T, R>(generator: AsyncGenerator<T, R>) {
const next = await generator.next()
if (!next.done) return next.value
throw new Error("Generator returned")
}

export async function returnOf<T, R>(generator: AsyncGenerator<T, R>) {
Expand Down
10 changes: 3 additions & 7 deletions src/mods/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,12 @@ export class Core extends Ortho<string, State | undefined> {
return current

const next: State<D, E, K> = {
time: Date.now(),
data: current?.data,
error: current?.error,
cooldown: current?.cooldown,
expiration: current?.expiration,
aborter: current?.aborter,
optimistic: current?.optimistic,
...current,
...state
}

if (next.time === undefined)
next.time = Date.now()
next.data = await this.normalize(false, next, params)

const {
Expand Down
68 changes: 32 additions & 36 deletions src/mods/single/helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { nextOf, returnOf } from "libs/generator";
import { returnOf } from "libs/generator";
import { getTimeFromDelay } from "libs/time";
import { Core } from "mods/core";
import { AbortError } from "mods/errors";
Expand Down Expand Up @@ -139,9 +139,7 @@ export class SingleHelper {
try {
const generator = updater(current, { signal })

{
const { data, error } = await nextOf(generator)

for await (const { data, error } of generator) {
const optimistic: State<D, E, K> = {}

if (data !== undefined)
Expand All @@ -153,46 +151,44 @@ export class SingleHelper {
params)
}

{
let result = await returnOf(generator)

if (result === undefined) {
if (fetcher === undefined)
throw new Error("Updater returned nothing and undefined fetcher")
result = await fetcher(key, { signal, cache: "reload" })
}

const {
data,
error,
time = Date.now(),
cooldown = getTimeFromDelay(dcooldown),
expiration = getTimeFromDelay(dexpiration)
} = result
let result = await returnOf(generator)

if (signal.aborted)
throw new AbortError(signal)

current = await this.core.get(skey, params)
if (result === undefined) {
if (fetcher === undefined)
throw new Error("Updater returned nothing and undefined fetcher")
result = await fetcher(key, { signal, cache: "reload" })
}

if (error !== undefined) {
if (current?.aborter !== aborter)
return current
return await this.core.mutate(skey, current,
c => ({ time: c?.time, cooldown, expiration, aborter: undefined, optimistic: false, data: c?.data, error }),
params)
}
const {
data,
error,
time = Date.now(),
cooldown = getTimeFromDelay(dcooldown),
expiration = getTimeFromDelay(dexpiration)
} = result

const state: State<D, E, K> = {}
if (signal.aborted)
throw new AbortError(signal)

if (data !== undefined)
state.data = data
state.error = error
current = await this.core.get(skey, params)

if (error !== undefined) {
if (current?.aborter !== aborter)
return current
return await this.core.mutate(skey, current,
() => ({ time, cooldown, expiration, aborter: undefined, optimistic: false, ...state }),
c => ({ time: c?.time, cooldown, expiration, aborter: undefined, optimistic: false, data: c?.data, error }),
params)
}

const state: State<D, E, K> = {}

if (data !== undefined)
state.data = data
state.error = error

return await this.core.mutate(skey, current,
() => ({ time, cooldown, expiration, aborter: undefined, optimistic: false, ...state }),
params)
} catch (error: any) {
current = await this.core.get(skey, params)

Expand Down

1 comment on commit 09294c2

@vercel
Copy link

@vercel vercel bot commented on 09294c2 Sep 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

xswr-test – ./

xswr-test-git-master-hazae41.vercel.app
test.xswr.hazae41.me
xswr-test-hazae41.vercel.app

Please sign in to comment.