mirror of
https://github.com/gristlabs/grist-core.git
synced 2026-03-02 04:09:24 +00:00
(core) move client code to core
Summary: This moves all client code to core, and makes minimal fix-ups to get grist and grist-core to compile correctly. The client works in core, but I'm leaving clean-up around the build and bundles to follow-up. Test Plan: existing tests pass; server-dev bundle looks sane Reviewers: dsagal Reviewed By: dsagal Differential Revision: https://phab.getgrist.com/D2627
This commit is contained in:
42
app/client/lib/koArrayWrap.ts
Normal file
42
app/client/lib/koArrayWrap.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import {KoArray} from 'app/client/lib/koArray';
|
||||
import {IDisposableOwnerT, MutableObsArray, ObsArray, setDisposeOwner} from 'grainjs';
|
||||
|
||||
/**
|
||||
* Returns a grainjs ObsArray that reflects the given koArray, mapping small changes using
|
||||
* similarly efficient events.
|
||||
*
|
||||
* (Note that for both ObsArray and koArray, the main purpose in life is to be more efficient than
|
||||
* an array-valued observable by handling small changes more efficiently.)
|
||||
*/
|
||||
export function createObsArray<T>(
|
||||
owner: IDisposableOwnerT<ObsArray<T>> | null,
|
||||
koArray: KoArray<T>,
|
||||
): ObsArray<T> {
|
||||
return setDisposeOwner(owner, new KoWrapObsArray(koArray));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* An Observable that wraps a Knockout observable, created via fromKo(). It keeps minimal overhead
|
||||
* when unused by only subscribing to the wrapped observable while it itself has subscriptions.
|
||||
*
|
||||
* This way, when unused, the only reference is from the wrapper to the wrapped object. KoWrapObs
|
||||
* should not be disposed; its lifetime is tied to that of the wrapped object.
|
||||
*/
|
||||
class KoWrapObsArray<T> extends MutableObsArray<T> {
|
||||
private _koSub: any = null;
|
||||
|
||||
constructor(_koArray: KoArray<T>) {
|
||||
super(Array.from(_koArray.peek()));
|
||||
|
||||
this._koSub = _koArray.subscribe((splice: any) => {
|
||||
const newValues = splice.array.slice(splice.start, splice.start + splice.added);
|
||||
this.splice(splice.start, splice.deleted.length, ...newValues);
|
||||
}, null, 'spliceChange');
|
||||
}
|
||||
|
||||
public dispose(): void {
|
||||
this._koSub.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user