mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-09 21:25:45 +08:00
feat(infra): livedata (#5562)
LiveData is a reactive data type.
## basic usage
@example
```ts
const livedata = new LiveData(0); // create livedata with initial value
livedata.next(1); // update value
console.log(livedata.value); // get current value
livedata.subscribe(v => { // subscribe to value changes
console.log(v); // 1
});
```
## observable
LiveData is a rxjs observable, you can use rxjs operators.
@example
```ts
new LiveData(0).pipe(
map(v => v + 1),
filter(v => v > 1),
...
)
```
NOTICE: different from normal observable, LiveData will always emit the latest value when you subscribe to it.
## from observable
LiveData can be created from observable or from other livedata.
@example
```ts
const A = LiveData.from(
of(1, 2, 3, 4), // from observable
0 // initial value
);
const B = LiveData.from(
A.pipe(map(v => 'from a ' + v)), // from other livedata
'' // initial value
);
```
NOTICE: LiveData.from will not complete when the observable completes, you can use `spreadComplete` option to change
this behavior.
## Why is it called LiveData
This API is very similar to LiveData in Android, as both are based on Observable, so I named it LiveData.
This commit is contained in:
@@ -13049,7 +13049,9 @@ __metadata:
|
||||
"@blocksuite/lit": "npm:0.12.0-nightly-202401290223-b6302df"
|
||||
"@blocksuite/presets": "npm:0.12.0-nightly-202401290223-b6302df"
|
||||
"@blocksuite/store": "npm:0.12.0-nightly-202401290223-b6302df"
|
||||
"@testing-library/react": "npm:^14.0.0"
|
||||
async-call-rpc: "npm:^6.3.1"
|
||||
foxact: "npm:^0.2.20"
|
||||
jotai: "npm:^2.5.1"
|
||||
jotai-effect: "npm:^0.2.3"
|
||||
nanoid: "npm:^5.0.3"
|
||||
|
||||
Reference in New Issue
Block a user