Skip to content

Commit

Permalink
revert keepAlive removal
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed May 24, 2024
1 parent 03cfbb8 commit b1cb351
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
7 changes: 7 additions & 0 deletions .changeset/breezy-waves-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@effect-rx/rx-react": minor
"@effect-rx/rx": minor
"@effect-rx/rx-vue": minor
---

revert keepAlive removal
3 changes: 2 additions & 1 deletion packages/rx-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ function scheduleTask(f: () => void): void {
* @category context
*/
export const RegistryContext = React.createContext<Registry.Registry>(Registry.make({
scheduleTask
scheduleTask,
defaultIdleTTL: 150
}))

interface RxStore<A> {
Expand Down
1 change: 1 addition & 0 deletions packages/rx/src/Registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ export const make: (
readonly initialValues?: Iterable<readonly [Rx.Rx<any>, any]> | undefined
readonly scheduleTask?: ((f: () => void) => void) | undefined
readonly timeoutResolution?: number | undefined
readonly defaultIdleTTL?: number | undefined
} | undefined
) => Registry = internal.make
14 changes: 9 additions & 5 deletions packages/rx/src/internal/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,22 @@ export const make = (options?: {
readonly initialValues?: Iterable<readonly [Rx.Rx<any>, any]> | undefined
readonly scheduleTask?: ((f: () => void) => void) | undefined
readonly timeoutResolution?: number | undefined
readonly defaultIdleTTL?: number | undefined
}): Registry.Registry =>
new RegistryImpl(
options?.initialValues,
options?.scheduleTask,
options?.timeoutResolution
options?.timeoutResolution,
options?.defaultIdleTTL
)

class RegistryImpl implements Registry.Registry {
readonly [TypeId]: Registry.TypeId
constructor(
initialValues?: Iterable<readonly [Rx.Rx<any>, any]>,
readonly scheduleTask = queueMicrotask,
readonly timeoutResolution = 5000
readonly timeoutResolution = 5000,
readonly defaultIdleTTL?: number
) {
this[TypeId] = TypeId
if (initialValues !== undefined) {
Expand Down Expand Up @@ -89,7 +92,7 @@ class RegistryImpl implements Registry.Registry {
if (node === undefined) {
node = this.createNode(rx)
this.nodes.set(rx, node)
} else if (!rx.keepAlive && rx.idleTTL) {
} else if (!rx.keepAlive && (rx.idleTTL || this.defaultIdleTTL)) {
this.removeNodeTimeout(node)
}
return node
Expand Down Expand Up @@ -128,7 +131,7 @@ class RegistryImpl implements Registry.Registry {
}

removeNode(node: Node<any>): void {
if (node.rx.idleTTL) {
if (!node.rx.keepAlive && (node.rx.idleTTL || this.defaultIdleTTL)) {
this.setNodeTimeout(node)
} else {
this.nodes.delete(node.rx)
Expand All @@ -141,7 +144,8 @@ class RegistryImpl implements Registry.Registry {
return
}

const ttl = Math.ceil(node.rx.idleTTL! / this.timeoutResolution) * this.timeoutResolution
const idleTTL = node.rx.idleTTL ?? this.defaultIdleTTL!
const ttl = Math.ceil(idleTTL! / this.timeoutResolution) * this.timeoutResolution
const timestamp = Date.now() + ttl
const bucket = timestamp - (timestamp % this.timeoutResolution) + this.timeoutResolution

Expand Down
6 changes: 2 additions & 4 deletions packages/rx/test/Rx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,16 +630,14 @@ describe("Rx", () => {
})

it("idleTTL", async () => {
const state = Rx.make(0).pipe(
Rx.setIdleTTL(2000)
)
const state = Rx.make(0)
const state2 = Rx.make(0).pipe(
Rx.setIdleTTL(10000)
)
const state3 = Rx.make(0).pipe(
Rx.setIdleTTL(3000)
)
const r = Registry.make()
const r = Registry.make({ defaultIdleTTL: 2000 })
r.set(state, 10)
r.set(state2, 10)
r.set(state3, 10)
Expand Down

0 comments on commit b1cb351

Please sign in to comment.