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:
EYHN
2024-01-30 06:31:11 +00:00
parent 588b3bcf33
commit c9f8e49f75
6 changed files with 597 additions and 0 deletions
+2
View File
@@ -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"