mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-03 02:20:19 +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:
@@ -7,6 +7,7 @@
|
||||
"./command": "./src/command/index.ts",
|
||||
"./atom": "./src/atom/index.ts",
|
||||
"./app-config-storage": "./src/app-config-storage.ts",
|
||||
"./livedata": "./src/livedata/index.ts",
|
||||
".": "./src/index.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
@@ -16,9 +17,11 @@
|
||||
"@blocksuite/blocks": "0.12.0-nightly-202401290223-b6302df",
|
||||
"@blocksuite/global": "0.12.0-nightly-202401290223-b6302df",
|
||||
"@blocksuite/store": "0.12.0-nightly-202401290223-b6302df",
|
||||
"foxact": "^0.2.20",
|
||||
"jotai": "^2.5.1",
|
||||
"jotai-effect": "^0.2.3",
|
||||
"nanoid": "^5.0.3",
|
||||
"react": "18.2.0",
|
||||
"tinykeys": "^2.1.0",
|
||||
"yjs": "^13.6.10",
|
||||
"zod": "^3.22.4"
|
||||
@@ -28,6 +31,7 @@
|
||||
"@affine/templates": "workspace:*",
|
||||
"@blocksuite/lit": "0.12.0-nightly-202401290223-b6302df",
|
||||
"@blocksuite/presets": "0.12.0-nightly-202401290223-b6302df",
|
||||
"@testing-library/react": "^14.0.0",
|
||||
"async-call-rpc": "^6.3.1",
|
||||
"react": "^18.2.0",
|
||||
"rxjs": "^7.8.1",
|
||||
|
||||
Reference in New Issue
Block a user