forked from microsoft/FluidFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfluidHandle.ts
More file actions
74 lines (64 loc) · 2.48 KB
/
fluidHandle.ts
File metadata and controls
74 lines (64 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*!
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import {
IFluidObject,
IFluidHandle,
IFluidHandleContext,
} from "@fluidframework/core-interfaces";
import { AttachState } from "@fluidframework/container-definitions";
import { generateHandleContextPath } from "@fluidframework/runtime-utils";
export class FluidObjectHandle<T extends IFluidObject = IFluidObject> implements IFluidHandle {
// This is used to break the recursion while attaching the graph. Also tells the attach state of the graph.
private graphAttachState: AttachState = AttachState.Detached;
private bound: Set<IFluidHandle> | undefined;
public readonly absolutePath: string;
public get IFluidHandle(): IFluidHandle { return this; }
public get isAttached(): boolean {
return this.routeContext.isAttached;
}
/**
* Creates a new FluidObjectHandle.
* @param value - The IFluidObject object this handle is for.
* @param path - The path to this handle relative to the routeContext.
* @param routeContext - The parent IFluidHandleContext that has a route to this handle.
*/
constructor(
protected readonly value: T,
public readonly path: string,
public readonly routeContext: IFluidHandleContext,
) {
this.absolutePath = generateHandleContextPath(path, this.routeContext);
}
public async get(): Promise<any> {
return this.value;
}
public attachGraph(): void {
// If this handle is already in attaching state in the graph or attached, no need to attach again.
if (this.graphAttachState !== AttachState.Detached) {
return;
}
this.graphAttachState = AttachState.Attaching;
if (this.bound !== undefined) {
for (const handle of this.bound) {
handle.attachGraph();
}
this.bound = undefined;
}
this.routeContext.attachGraph();
this.graphAttachState = AttachState.Attached;
}
public bind(handle: IFluidHandle) {
// If the dds is already attached or its graph is already in attaching or attached state,
// then attach the incoming handle too.
if (this.isAttached || this.graphAttachState !== AttachState.Detached) {
handle.attachGraph();
return;
}
if (this.bound === undefined) {
this.bound = new Set<IFluidHandle>();
}
this.bound.add(handle);
}
}