ctx-core 5.4.1 → 5.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,11 +8,10 @@ import { waitfor } from '../waitfor/index.js'
8
8
  export async function file_exists_(path) {
9
9
  return (
10
10
  (process_release_name ?? false)
11
- && import('node:fs/promises').then(({ access, constants })=>{
12
- return access(path, constants.F_OK)
11
+ && import('node:fs/promises').then(({ access, constants })=>
12
+ access(path, constants.F_OK)
13
13
  .then(()=>true)
14
- .catch(()=>false)
15
- }))
14
+ .catch(()=>false)))
16
15
  }
17
16
  export {
18
17
  file_exists_ as path__exists_
@@ -17,6 +17,7 @@ export declare function lock_memosig_<val_T>(
17
17
  ):sig_T<val_T>
18
18
  export declare function on(rmemo:rmemo_T<unknown>):void
19
19
  export declare function off(rmemo:rmemo_T<unknown>):void
20
+ export declare function rmemo__subscribe(rmemo:rmemo_T<unknown>, listener:()=>unknown):()=>void
20
21
  export type rmemo_T<val_T> = memo_T<val_T>|sig_T<val_T>|lock_memosig_T<val_T>
21
22
  export type memo_T<val_T> = (()=>val_T)&{
22
23
  readonly _:val_T
@@ -135,3 +135,21 @@ export function off(rmemo) {
135
135
  }
136
136
  }
137
137
  }
138
+ /**
139
+ * Bind reactive listener onto the given memo to prevent GC.
140
+ * The listener can autosubscribe to any rmemo.
141
+ * Returns an "off" function which deactivates the reactive listener & removes the GC binding from the given memo.
142
+ * @param {rmemo_T}memo
143
+ * @param {()=>unknown}listener
144
+ * @returns {()=>void}
145
+ */
146
+ export function rmemo__subscribe(memo, listener) {
147
+ let listener_memo = memo_(()=>listener())
148
+ listener_memo()
149
+ memo.b ??= []
150
+ memo.b.push(listener_memo)
151
+ return ()=>{
152
+ off(listener_memo)
153
+ memo.b.splice(memo.b.indexOf(listener_memo), 1)
154
+ }
155
+ }
@@ -4,7 +4,7 @@ import { deepStrictEqual } from 'node:assert'
4
4
  import { test } from 'uvu'
5
5
  import { equal } from 'uvu/assert'
6
6
  import { sleep } from '../sleep/index.js'
7
- import { lock_memosig_, memo_, type memo_T, memosig_, off, on, sig_ } from './index.js'
7
+ import { lock_memosig_, memo_, type memo_T, memosig_, off, on, rmemo__subscribe, sig_ } from './index.js'
8
8
  test('memo_|static value', ()=>{
9
9
  let count = 0
10
10
  const memo = memo_(()=>{
@@ -454,4 +454,22 @@ test('.on + .off', ()=>{
454
454
  equal(memo$(), 14)
455
455
  equal(count, 5)
456
456
  })
457
+ test('rmemo__subscribe', ()=>{
458
+ const base$ = sig_(1)
459
+ let count = 0
460
+ const subscriber_base_a:number[] = []
461
+ const off = rmemo__subscribe(base$, ()=>{
462
+ count++
463
+ subscriber_base_a.push(base$())
464
+ })
465
+ equal(subscriber_base_a, [1])
466
+ equal(count, 1)
467
+ base$._ = 2
468
+ equal(subscriber_base_a, [1, 2])
469
+ equal(count, 2)
470
+ off()
471
+ base$._ = 3
472
+ equal(subscriber_base_a, [1, 2])
473
+ equal(count, 2)
474
+ })
457
475
  test.run()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ctx-core",
3
- "version": "5.4.1",
3
+ "version": "5.5.0",
4
4
  "description": "ctx-core core library",
5
5
  "keywords": [
6
6
  "ctx-core",
@@ -196,21 +196,21 @@
196
196
  "import": {
197
197
  "./rmemo": "{ sig_, memo_ }"
198
198
  },
199
- "limit": "351 B"
199
+ "limit": "352 B"
200
200
  },
201
201
  {
202
202
  "name": "memo_ sig_ be_ ctx_",
203
203
  "import": {
204
204
  "./rmemo": "{ sig_, memo_, be_, ctx_ }"
205
205
  },
206
- "limit": "456 B"
206
+ "limit": "458 B"
207
207
  },
208
208
  {
209
209
  "name": "memo_ sig_ be_ ctx_ be_memo_pair_ be_sig_triple_",
210
210
  "import": {
211
211
  "./rmemo": "{ sig_, memo_, be_, ctx_, be_memo_pair_, be_sig_triple_ }"
212
212
  },
213
- "limit": "549 B"
213
+ "limit": "552 B"
214
214
  }
215
215
  ],
216
216
  "scripts": {
package/rmemo/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { rmemo__subscribe } from '../rmemo/index.js'
2
+ export { rmemo__subscribe as subscribe }
1
3
  export * from '../all/be_/index.js'
2
4
  export * from '../all/be_lock_memosig_triple/index.js'
3
5
  export * from '../all/be_memo_pair/index.js'
package/rmemo/index.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { rmemo__subscribe } from '../rmemo/index.js'
2
+ export { rmemo__subscribe as subscribe }
1
3
  export * from '../all/be_/index.js'
2
4
  export * from '../all/be_lock_memosig_triple/index.js'
3
5
  export * from '../all/be_memo_pair/index.js'
package/tsconfig.json CHANGED
@@ -5,7 +5,6 @@
5
5
  "moduleResolution": "nodenext",
6
6
  "target": "ESNext",
7
7
  "strict": true,
8
- "skipLibCheck": true,
9
8
  "lib": ["ESNext"],
10
9
  "types": [
11
10
  "node"