-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
578b3bd
commit ecc7760
Showing
8 changed files
with
112 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { val, drv, sub, act } from "@r-val/core" | ||
import {toJS, assignVals, keepAlive} from "@r-val/utils" | ||
import { delay } from "q"; | ||
|
||
test("toJS", () => { | ||
expect(toJS(val(3))).toBe(3) | ||
|
||
expect(toJS(drv(() => 3))).toBe(3) | ||
|
||
expect(toJS(val({x: 3 }))).toEqual({x: 3}) | ||
|
||
expect(toJS(val({x: 3, toJS() { return 2 } }))).toEqual(2) | ||
|
||
expect(toJS(val([3]))).toEqual([3]) | ||
|
||
expect(toJS(val({x: val(3) }))).toEqual({x: 3}) | ||
|
||
{ | ||
const x = {x: 3} | ||
expect(toJS(x)).not.toBe(x) | ||
} | ||
}) | ||
|
||
test("assignVals", () => { | ||
const x = { | ||
a: val(3), | ||
b: 2, | ||
c: drv(() => x.b * 2, v => { x.b = v}), | ||
} | ||
|
||
assignVals(x, { | ||
a: 2, | ||
}, { | ||
a: 4, | ||
c: 5 | ||
}) | ||
|
||
expect(toJS(x)).toEqual({ | ||
a: 4, | ||
b: 5, | ||
c: 10 | ||
}) | ||
}) | ||
|
||
test("keepAlive", async () => { | ||
let calc = 0 | ||
const x = val(1) | ||
const y = drv(() => { | ||
calc++ | ||
return x() | ||
}) | ||
|
||
await delay(10) | ||
const d = keepAlive(y) | ||
|
||
expect(calc).toBe(0) | ||
expect(y()).toBe(1) | ||
expect(calc).toBe(1) | ||
|
||
x(2) | ||
await delay(10) | ||
x(3) | ||
expect(calc).toBe(1) | ||
expect(y()).toBe(3) | ||
expect(calc).toBe(2) | ||
expect(y()).toBe(3) | ||
expect(calc).toBe(2) | ||
|
||
d() | ||
await delay(10) | ||
expect(y()).toBe(3) | ||
expect(calc).toBe(3) | ||
|
||
await delay(10) | ||
expect(y()).toBe(3) | ||
expect(calc).toBe(4) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,40 @@ | ||
import { isVal, run, isDrv, Drv, Disposer, rval, _once } from "@r-val/core"; | ||
|
||
// type SnapshotType<T> = { | ||
// [K in keyof T]?: T[K] extends Val<infer X, infer S> | ||
// ? X | S | ||
// : T[K] extends Drv<any> | ||
// ? never | ||
// : T[K] extends Function | ||
// ? never | ||
// : T[K] extends (string | number | boolean) | ||
// ? T[K] | ||
// : SnapshotType<T[K]> | ||
// } | ||
import { isVal, run, isDrv, Drv, Disposer, rval, _once, _isPlainObject, Val } from '@r-val/core' | ||
|
||
// TODO: add typings! | ||
export function toJS(thing) { | ||
if (!thing) return thing; | ||
if (typeof thing.toJS === "function") return thing.toJS() | ||
if (!thing) return thing | ||
if (typeof thing.toJS === 'function') return thing.toJS() | ||
if (isVal(thing) || isDrv(thing)) return toJS(thing()) | ||
if (Array.isArray(thing)) return thing.map(toJS) | ||
if (typeof thing === "object" && (Object.getPrototypeOf(thing) === Object || Object.getPrototypeOf(thing) === null)) { | ||
if (_isPlainObject(thing)) { | ||
const res = {} | ||
for(const key in thing) res[key] = toJS(thing[key]) | ||
for (const key in thing) res[key] = toJS(thing[key]) | ||
return res | ||
} | ||
return thing | ||
} | ||
|
||
type AssignVals<T> = { | ||
[K in keyof T]?: T[K] extends Val<infer T, infer S> ? T | S : T[K] extends Drv<infer T> ? T : never | ||
} | ||
|
||
export function assignVals<T>(target: T, vals: AssignVals<T>, ...moreVals: AssignVals<T>[]) | ||
export function assignVals(target, vals, ...moreVals) { | ||
if (moreVals.length) | ||
vals = Object.assign(vals, ...moreVals) | ||
if (moreVals.length) vals = Object.assign(vals, ...moreVals) | ||
run(() => { | ||
for (const key in vals) { | ||
if (isVal(target[key]) || isDrv(target[key])) | ||
target[key](vals[key]) | ||
throw new Error(`[assignVals] value at key "${key}" is not a 'val' or 'drv'`) | ||
if (isVal(target[key]) || isDrv(target[key])) target[key](vals[key]) | ||
else throw new Error(`[assignVals] value at key "${key}" is not a 'val' or 'drv'`) | ||
} | ||
}) | ||
return target | ||
} | ||
|
||
export function keepAlive(target: Drv<any>): Disposer { | ||
return rval(target).effect(target, _once((didChange, pull) => { | ||
didChange() // we never have to pull, we only detect for changes once, so that the target becomes hot | ||
})) | ||
} | ||
return rval(target).effect( | ||
target, | ||
_once((didChange, pull) => { | ||
didChange() // we never have to pull, we only detect for changes once, so that the target becomes hot | ||
}) | ||
) | ||
} |