Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: rm isAttached type on containers #303

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions crates/loro-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,7 @@ impl LoroText {
/// Whether the container is attached to a docuemnt.
///
/// If it's detached, the operations on the container will not be persisted.
#[wasm_bindgen(js_name = "isAttached", skip_typescript)]
#[wasm_bindgen(js_name = "isAttached")]
pub fn is_attached(&self) -> bool {
self.handler.is_attached()
}
Expand Down Expand Up @@ -1702,7 +1702,7 @@ impl LoroMap {
/// Whether the container is attached to a docuemnt.
///
/// If it's detached, the operations on the container will not be persisted.
#[wasm_bindgen(js_name = "isAttached", skip_typescript)]
#[wasm_bindgen(js_name = "isAttached")]
pub fn is_attached(&self) -> bool {
self.handler.is_attached()
}
Expand Down Expand Up @@ -1970,7 +1970,7 @@ impl LoroList {
/// Whether the container is attached to a docuemnt.
///
/// If it's detached, the operations on the container will not be persisted.
#[wasm_bindgen(js_name = "isAttached", skip_typescript)]
#[wasm_bindgen(js_name = "isAttached")]
pub fn is_attached(&self) -> bool {
self.handler.is_attached()
}
Expand Down Expand Up @@ -2372,7 +2372,7 @@ impl LoroTree {
/// Whether the container is attached to a docuemnt.
///
/// If it's detached, the operations on the container will not be persisted.
#[wasm_bindgen(js_name = "isAttached", skip_typescript)]
#[wasm_bindgen(js_name = "isAttached")]
pub fn is_attached(&self) -> bool {
self.handler.is_attached()
}
Expand Down
50 changes: 16 additions & 34 deletions loro-js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,98 +199,80 @@ declare module "loro-wasm" {
interface Loro<T extends Record<string, any> = Record<string, any>> {
getTypedMap<Key extends keyof T & string>(
name: Key,
): T[Key] extends LoroMap ? SetAttached<T[Key], true> : never;
): T[Key] extends LoroMap ? T[Key] : never;
getTypedList<Key extends keyof T & string>(
name: Key,
): T[Key] extends LoroList ? SetAttached<T[Key], true> : never;
getMap(key: string | ContainerID): LoroMap<T[string], true>;
getList(key: string | ContainerID): LoroList<T[string], true>;
getTree(key: string | ContainerID): LoroTree<T[string], true>;
getText(key: string | ContainerID): LoroText<true>;
): T[Key] extends LoroList ? T[Key] : never;
getMap(key: string | ContainerID): LoroMap<T[string]>;
getList(key: string | ContainerID): LoroList<T[string]>;
getTree(key: string | ContainerID): LoroTree<T[string]>;
getText(key: string | ContainerID): LoroText;
}

interface LoroList<
T extends any[] = any[],
Attached extends boolean = false,
> {
new (): LoroList<T, false>;
insertContainer<C extends Container>(
pos: number,
child: C,
): SetAttached<C, Attached>;
get(index: number): undefined | Value | SetAttached<Container, Attached>;
): C;
get(index: number): undefined | Value | Container;
getTyped<Key extends keyof T & number>(loro: Loro, index: Key): T[Key];
insertTyped<Key extends keyof T & number>(pos: Key, value: T[Key]): void;
insert(pos: number, value: Value): void;
delete(pos: number, len: number): void;
subscribe(txn: Loro, listener: Listener): number;
getAttached(): undefined | LoroList<T, true>;
isAttached(): Attached;
}

interface LoroMap<
T extends Record<string, any> = Record<string, any>,
Attached extends boolean = false,
> {
new (): LoroMap<T, false>;
getOrCreateContainer<C extends Container>(
key: string,
child: C,
): SetAttached<C, Attached>;
): C;
setContainer<C extends Container>(
key: string,
child: C,
): SetAttached<C, Attached>;
get(key: string): undefined | Value | SetAttached<Container, Attached>;
): C;
get(key: string): undefined | Value | Container;
getTyped<Key extends keyof T & string>(txn: Loro, key: Key): T[Key];
set(key: string, value: Value): void;
setTyped<Key extends keyof T & string>(key: Key, value: T[Key]): void;
delete(key: string): void;
subscribe(txn: Loro, listener: Listener): number;
getAttached(): undefined | LoroMap<T, true>;
isAttached(): Attached;
}

interface LoroText<Attached extends boolean = false> {
new (): LoroText<false>;
insert(pos: number, text: string): void;
delete(pos: number, len: number): void;
subscribe(txn: Loro, listener: Listener): number;
getAttached(): undefined | LoroText<true>;
isAttached(): Attached;
}

interface LoroTree<
T extends Record<string, any> = Record<string, any>,
Attached extends boolean = false,
> {
new (): LoroTree<T, false>;
createNode(parent: TreeID | undefined): LoroTreeNode<T, Attached>;
createNode(parent: TreeID | undefined): LoroTreeNode<T>;
move(target: TreeID, parent: TreeID | undefined): void;
delete(target: TreeID): void;
has(target: TreeID): boolean;
getNodeByID(target: TreeID): LoroTreeNode;
subscribe(txn: Loro, listener: Listener): number;
getAttached(): undefined | LoroTree<T, true>;
isAttached(): Attached;
}

interface LoroTreeNode<
T extends Record<string, any> = Record<string, any>,
Attached extends boolean = false,
> {
readonly data: LoroMap<T, Attached>;
createNode(): LoroTreeNode<T, Attached>;
readonly data: LoroMap<T>;
createNode(): LoroTreeNode<T>;
setAsRoot(): void;
moveTo(parent: LoroTreeNode<T, Attached>): void;
moveTo(parent: LoroTreeNode<T>): void;
parent(): LoroTreeNode | undefined;
children(): Array<LoroTreeNode<T, Attached>>;
children(): Array<LoroTreeNode<T>>;
}
}

type SetAttached<T extends Container, Attached extends boolean> = T extends
LoroMap<infer U> ? LoroMap<U, Attached>
: T extends LoroList<infer U> ? LoroList<U, Attached>
: T extends LoroTree<infer U> ? LoroTree<U, Attached>
: T extends LoroText ? LoroText<Attached>
: never;
22 changes: 0 additions & 22 deletions loro-js/tests/type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,3 @@ test("You shuold not insert a container by using `insert` function", () => {
const list = new LoroList();
expectTypeOf(list).not.toMatchTypeOf<Value>();
});

test("Container attached state", () => {
const list = new LoroList();
expect(list.isAttached()).toBe(false);
expectTypeOf(list.isAttached()).toMatchTypeOf<false>();
const doc = new Loro();
{
const map = doc.getMap("map");
expectTypeOf(map.isAttached()).toMatchTypeOf<true>();
expectTypeOf(map).toMatchTypeOf<LoroMap<any, true>>();
}
{
const map = new LoroMap();
expectTypeOf(map.isAttached()).toMatchTypeOf<false>();
expectTypeOf(map).toMatchTypeOf<LoroMap<any, false>>();
}
{
const map = list.insertContainer(0, new LoroMap());
expectTypeOf(map.isAttached()).toMatchTypeOf<false>();
expectTypeOf(map).toMatchTypeOf<LoroMap<any, false>>();
}
});
Loading