forked from microsoft/FluidFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfluidHandle.ts
82 lines (71 loc) · 2.92 KB
/
fluidHandle.ts
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
75
76
77
78
79
80
81
82
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
import {
IFluidHandle,
IFluidHandleContext,
FluidObject,
} from "@fluidframework/core-interfaces";
import { generateHandleContextPath } from "@fluidframework/runtime-utils";
export class FluidObjectHandle<T extends FluidObject = FluidObject> implements IFluidHandle {
private readonly pendingHandlesToMakeVisible: Set<IFluidHandle> = new Set();
public readonly absolutePath: string;
public get IFluidHandle(): IFluidHandle { return this; }
public get isAttached(): boolean {
return this.routeContext.isAttached;
}
/**
* Tells whether the object of this handle is visible in the container locally or globally.
*/
private get visible(): boolean {
/**
* If the object of this handle is attached, it is visible in the container. Ideally, checking local visibility
* should be enough for a handle. However, there are scenarios where the object becomes locally visible but the
* handle does not know this - This will happen is attachGraph is never called on the handle. Couple of examples
* where this can happen:
* 1. Handles to DDS other than the default handle won't know if the DDS becomes visible after the handle was
* created.
* 2. Handles to root data stores will never know that it was visible because the handle will not be stores in
* another DDS and so, attachGraph will never be called on it.
*/
return this.isAttached || this.locallyVisible;
}
// Tracks whether this handle is locally visible in the container.
private locallyVisible: boolean = false;
/**
* Creates a new FluidObjectHandle.
* @param value - The FluidObject 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.visible) {
return;
}
this.locallyVisible = true;
this.pendingHandlesToMakeVisible.forEach((handle) => {
handle.attachGraph();
});
this.pendingHandlesToMakeVisible.clear();
this.routeContext.attachGraph();
}
public bind(handle: IFluidHandle) {
// If this handle is visible, attach the graph of the incoming handle as well.
if (this.visible) {
handle.attachGraph();
return;
}
this.pendingHandlesToMakeVisible.add(handle);
}
}