Skip to content

Commit

Permalink
util methods
Browse files Browse the repository at this point in the history
  • Loading branch information
zach-herridge committed Nov 21, 2023
1 parent b88c23a commit c74cf8a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/echo-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { EchoContext } from './echo-context'
import { EchoPluginService } from './echo-plugin-service'
import { ECHO_CONTEXT_SERVICE, EchoContextService } from './echo-context-service'
import { SageValuationService } from './sage-valuation-service'
import { validResults } from './smart-cache-hooks'

export {
PoeStashService,
Expand All @@ -22,6 +23,7 @@ export {
PoeLogService,
EchoRouter,
EchoDirService,
validResults,
EchoContextService,
ECHO_CONTEXT_SERVICE
}
Expand Down
12 changes: 10 additions & 2 deletions src/echo-common/src/smart-cache-hooks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { useCallback, useEffect, useState } from 'react'
import { SmartCache, SmartCacheEvent, SmartCacheLoadConfig, SmartCacheStore } from './smart-cache'
import { SmartCache, SmartCacheEvent, SmartCacheLoadConfig, SmartCacheResultEvent, SmartCacheStore } from './smart-cache'
import { filterNullish } from 'ts-ratchet'
import { Observable, map, of, tap } from 'rxjs'
import { Observable, UnaryFunction, map, filter, pipe, OperatorFunction, tap } from 'rxjs'

export function validResults<T>(): UnaryFunction<Observable<SmartCacheEvent<T>>, Observable<T>> {
return pipe(
filter((x) => x.type === "result") as OperatorFunction<SmartCacheEvent<T>, SmartCacheResultEvent<T>>,
map((e) => e.result),
filterNullish()
)
}

export type SmartCacheHookType<T> = SmartCacheStore<T> & {
value: T | null | undefined
Expand Down
12 changes: 3 additions & 9 deletions src/echo-plugin-examples/stash-plugin/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from 'react'
import { context } from './entry'
import { delay, filter, last, scan, tap, toArray } from 'rxjs'
import { EchoPoeItem } from 'echo-common'
import { EchoPoeItem, validResults } from 'echo-common'

const App = () => {
const league = 'Ancestor'
Expand Down Expand Up @@ -34,19 +34,13 @@ const App = () => {
style={{ backgroundColor: `#${partialTab.metadata.colour}` }}
className="flex-shrink-0 cursor-pointer py-2 px-4 shadow-md no-underline rounded-full text-white text-sm hover:text-white hover:bg-blue-light focus:outline-none active:shadow-none mr-2"
onClick={() =>

context().poeStash.snapshot(
league,
[partialTab.id!!]
).pipe(
tap(((e) => setStatus(e.type))),
scan((a: EchoPoeItem[], e) => {
if (e.type === "result" && e.result) {
a.push(e.result)
}
return a
}, []),
last(),
validResults(),
toArray()
).subscribe((items) => {
setStatus(`loaded ${items.length}`)
console.log("final items", items)
Expand Down
2 changes: 1 addition & 1 deletion src/ts-ratchet/src/rx-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { filter, Observable, OperatorFunction, pipe, UnaryFunction } from 'rxjs'

export function filterNullish<T>(): UnaryFunction<Observable<T | null | undefined>, Observable<T>> {
return pipe(filter((x) => x != null) as OperatorFunction<T | null | undefined, T>)
return pipe(filter((x) => x != null && x != undefined) as OperatorFunction<T | null | undefined, T>)
}

0 comments on commit c74cf8a

Please sign in to comment.