ctx-core 5.15.0 → 5.16.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.
@@ -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_, rmemo__off, rmemo__on, rmemo__subscribe, sig_ } from './index.js'
7
+ import { lock_memosig_, memo_, type memo_T, memosig_, rmemo__off, rmemo__on, rmemo__add, sig_ } from './index.js'
8
8
  test('memo_|static value', ()=>{
9
9
  let count = 0
10
10
  const memo = memo_(()=>{
@@ -171,23 +171,29 @@ test('sig_|undefined', ()=>{
171
171
  equal(sig(), undefined)
172
172
  equal(memo(), undefined)
173
173
  })
174
- test('rmemo|subscriber|has strong refernce to the return value', ()=>{
174
+ test('rmemo|subscriber|has strong reference to the return value', ()=>{
175
+ const add_arg_aa:[memo_T<number|undefined>, number|undefined][] = []
175
176
  const num$ = sig_<number|undefined>(
176
- undefined,
177
- ()=>99)
178
- equal(num$.b, undefined)
177
+ undefined
178
+ ).add<number>((sig, old_val)=>{
179
+ add_arg_aa.push([sig, old_val])
180
+ return 99
181
+ })
182
+ equal(num$.a, undefined)
183
+ equal(add_arg_aa, [])
179
184
  equal(num$(), undefined)
180
- equal(num$.b![0][0], 99)
185
+ equal(num$.a![0][1], 99)
186
+ equal(add_arg_aa, [[num$, undefined]])
181
187
  })
182
188
  test('sig_|subscriber|notified if sig is set before read', ()=>{
183
189
  let count = 0
184
190
  let subscriber__num:number|undefined = undefined
185
191
  const num$ = sig_<number|undefined>(
186
- undefined,
187
- num$=>{
188
- count++
189
- subscriber__num = num$()
190
- })
192
+ undefined
193
+ ).add(num$=>{
194
+ count++
195
+ subscriber__num = num$()
196
+ })
191
197
  equal(count, 0)
192
198
  equal(subscriber__num, undefined)
193
199
  num$._ = 1
@@ -201,11 +207,11 @@ test('sig_|subscriber|sets sig', ()=>{
201
207
  const base$ = sig_(0)
202
208
  let count = 0
203
209
  const num$ = sig_(
204
- 0,
205
- async num$=>{
206
- count++
207
- num$._ = base$() + 1
208
- })
210
+ 0
211
+ ).add(async num$=>{
212
+ count++
213
+ num$._ = base$() + 1
214
+ })
209
215
  equal(count, 0)
210
216
  equal(num$(), 1)
211
217
  equal(count, 1)
@@ -220,13 +226,13 @@ test('sig_|async subsubscriber|case 1', async ()=>{
220
226
  const id$ = sig_('id-0')
221
227
  let count = 0
222
228
  const user$ = sig_<{ id:string }|null>(
223
- null,
224
- async (_user$)=>{
225
- count++
226
- id$()
227
- const user:{ id:string } = await new Promise(_resolve=>resolve = _resolve)
228
- _user$._ = user
229
- })
229
+ null
230
+ ).add(async (_user$)=>{
231
+ count++
232
+ id$()
233
+ const user:{ id:string } = await new Promise(_resolve=>resolve = _resolve)
234
+ _user$._ = user
235
+ })
230
236
  equal(count, 0)
231
237
  equal(user$(), null)
232
238
  equal(count, 1)
@@ -247,14 +253,15 @@ test('sig_|async subsubscriber|case 2', async ()=>{
247
253
  const b$ = sig_(2)
248
254
  const sleepCycles = 5
249
255
  const taskArgumentsCalls:number[][] = []
250
- const sum$ = sig_<null|number>(null,
251
- async sum$=>{
252
- taskArgumentsCalls.push([a$(), b$()])
253
- for (let i = 0; i < sleepCycles; i++) {
254
- await Promise.resolve()
255
- }
256
- sum$._ = a$() + b$()
257
- })
256
+ const sum$ = sig_<null|number>(
257
+ null
258
+ ).add(async sum$=>{
259
+ taskArgumentsCalls.push([a$(), b$()])
260
+ for (let i = 0; i < sleepCycles; i++) {
261
+ await Promise.resolve()
262
+ }
263
+ sum$._ = a$() + b$()
264
+ })
258
265
  equal(sum$(), null)
259
266
  deepStrictEqual(taskArgumentsCalls, [[1, 2]])
260
267
  a$._ = 10
@@ -303,10 +310,10 @@ test('prevents diamond dependency problem 1', ()=>{
303
310
  const b$ = memo_(()=>a$().replace('a', 'b'))
304
311
  const c$ = memo_(()=>a$().replace('a', 'c'))
305
312
  const d$ = memo_(()=>a$().replace('a', 'd'))
306
- memo_(()=>`${b$()}${c$()}${d$()}`,
307
- combined$=>
308
- values.push(combined$())
309
- )()
313
+ memo_(
314
+ ()=>`${b$()}${c$()}${d$()}`)
315
+ .add(combined$=>values.push(combined$()))
316
+ ()
310
317
  deepStrictEqual(values, ['b0c0d0'])
311
318
  store$._ = 1
312
319
  store$._ = 2
@@ -321,9 +328,9 @@ test('prevents diamond dependency problem 2', ()=>{
321
328
  const d$ = memo_(()=>c$().replace('c', 'd'))
322
329
  const e$ = memo_(()=>d$().replace('d', 'e'))
323
330
  memo_<string>(
324
- ()=>[a$(), e$()].join(''),
325
- $=>values.push($())
326
- )()
331
+ ()=>[a$(), e$()].join(''))
332
+ .add($=>values.push($()))
333
+ ()
327
334
  deepStrictEqual(values, ['a0e0'])
328
335
  store$._ = 1
329
336
  deepStrictEqual(values, ['a0e0', 'a1e1'])
@@ -336,9 +343,9 @@ test('prevents diamond dependency problem 3', ()=>{
336
343
  const c$ = memo_(()=>b$().replace('b', 'c'))
337
344
  const d$ = memo_(()=>c$().replace('c', 'd'))
338
345
  memo_<string>(
339
- ()=>`${a$()}${b$()}${c$()}${d$()}`,
340
- combined$=>values.push(combined$())
341
- )()
346
+ ()=>`${a$()}${b$()}${c$()}${d$()}`)
347
+ .add(combined$=>values.push(combined$()))
348
+ ()
342
349
  deepStrictEqual(values, ['a0b0c0d0'])
343
350
  store$._ = 1
344
351
  deepStrictEqual(values, ['a0b0c0d0', 'a1b1c1d1'])
@@ -355,13 +362,13 @@ test('autosubscribe: prevents diamond dependency problem 4 (complex)', ()=>{
355
362
  const f$ = memo_(()=>`f${e$()}`)
356
363
  const g$ = memo_(()=>`g${f$()}`)
357
364
  memo_(
358
- ()=>e$(),
359
- combined1$=>values.push(combined1$())
360
- )()
365
+ ()=>e$())
366
+ .add(combined1$=>values.push(combined1$()))
367
+ ()
361
368
  memo_(
362
- ()=>[e$(), g$()].join(''),
363
- combined2$=>values.push(combined2$())
364
- )()
369
+ ()=>[e$(), g$()].join(''))
370
+ .add(combined2$=>values.push(combined2$()))
371
+ ()
365
372
  deepStrictEqual(values, ['eca0b0da0', 'eca0b0da0gfeca0b0da0'])
366
373
  store1$._ = 1
367
374
  store2$._ = 2
@@ -408,9 +415,9 @@ test('prevents diamond dependency problem 6', ()=>{
408
415
  const b$ = memo_(()=>`b${store2$()}`)
409
416
  const c$ = memo_(()=>b$().replace('b', 'c'))
410
417
  memo_(
411
- ()=>`${a$()}${c$()}`,
412
- combined$=>values.push(combined$())
413
- )()
418
+ ()=>`${a$()}${c$()}`)
419
+ .add(combined$=>values.push(combined$()))
420
+ ()
414
421
  deepStrictEqual(values, ['a0c0'])
415
422
  store1$._ = 1
416
423
  deepStrictEqual(values, ['a0c0', 'a1c0'])
@@ -421,9 +428,9 @@ test('prevents dependency listeners from being out of order', ()=>{
421
428
  return `${base$()}a`
422
429
  })
423
430
  const values:string[] = []
424
- const b$ = memo_(()=>{
425
- return `${a$()}b`
426
- }, b$=>values.push(b$()))
431
+ const b$ = memo_(
432
+ ()=>`${a$()}b`)
433
+ .add(b$=>values.push(b$()))
427
434
  equal(b$(), '0ab')
428
435
  deepStrictEqual(values, ['0ab'])
429
436
  equal(a$(), '0a')
@@ -462,14 +469,17 @@ test('.rmemo__on + .rmemo__off', ()=>{
462
469
  equal(memo$(), 14)
463
470
  equal(count, 5)
464
471
  })
465
- test('rmemo__subscribe', ()=>{
472
+ test('rmemo__add', ()=>{
466
473
  const base$ = sig_(1)
467
474
  let count = 0
468
475
  const subscriber_base_a:number[] = []
469
- const off = rmemo__subscribe(base$, ()=>{
476
+ const off = rmemo__add(base$, ()=>{
470
477
  count++
471
478
  subscriber_base_a.push(base$())
472
479
  })
480
+ equal(subscriber_base_a, [])
481
+ equal(count, 0)
482
+ base$()
473
483
  equal(subscriber_base_a, [1])
474
484
  equal(count, 1)
475
485
  base$._ = 2