forked from microsoft/FluidFramework
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.ts
More file actions
105 lines (95 loc) · 3.22 KB
/
app.ts
File metadata and controls
105 lines (95 loc) · 3.22 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
import {
createExampleDriver,
getSpecifiedServiceFromWebpack,
} from "@fluid-example/example-driver";
import type {
ICodeDetailsLoader,
IContainer,
IFluidCodeDetails,
IFluidModuleWithDetails,
} from "@fluidframework/container-definitions/legacy";
import {
createDetachedContainer,
loadExistingContainer,
} from "@fluidframework/container-loader/legacy";
import { createElement } from "react";
// eslint-disable-next-line import-x/no-internal-modules
import { createRoot } from "react-dom/client";
import {
BlobCollectionContainerRuntimeFactory,
type IBlobCollection,
} from "./container/index.js";
import { BlobCollectionView, DebugView } from "./view.js";
const service = getSpecifiedServiceFromWebpack();
const {
urlResolver,
documentServiceFactory,
createCreateNewRequest,
createLoadExistingRequest,
} = await createExampleDriver(service);
const codeLoader: ICodeDetailsLoader = {
load: async (details: IFluidCodeDetails): Promise<IFluidModuleWithDetails> => {
return {
module: { fluidExport: new BlobCollectionContainerRuntimeFactory() },
details,
};
},
};
const doAttach = async (): Promise<void> => {
// Some services support or require specifying the container id at attach time (local, odsp). For
// services that do not (t9s), the passed id will be ignored.
let id = Date.now().toString();
const createNewRequest = createCreateNewRequest(id);
await container.attach(createNewRequest);
// For most services, the id on the resolvedUrl is the authoritative source for the container id
// (regardless of whether the id passed in createCreateNewRequest is respected or not). However,
// for odsp the id is a hashed combination of drive and container ID which we can't use. Instead,
// we retain the id we generated above.
if (service !== "odsp") {
if (container.resolvedUrl === undefined) {
throw new Error("Resolved Url unexpectedly missing!");
}
id = container.resolvedUrl.id;
}
// Update url and tab title
location.hash = id;
document.title = id;
};
let container: IContainer;
let attach: (() => void) | undefined;
if (location.hash.length === 0) {
container = await createDetachedContainer({
codeDetails: { package: "1.0" },
urlResolver,
documentServiceFactory,
codeLoader,
});
// This function is synchronous since it is intended to be called in response to an event - we need to be
// locally responsible for the handling of any async errors.
attach = () => {
doAttach().catch(console.error);
};
} else {
const id = location.hash.slice(1);
container = await loadExistingContainer({
request: await createLoadExistingRequest(id),
urlResolver,
documentServiceFactory,
codeLoader,
});
// Update url and tab title
location.hash = id;
document.title = id;
}
const blobCollection = (await container.getEntryPoint()) as IBlobCollection;
// Render view
const debugDiv = document.querySelector("#debug") as HTMLDivElement;
const debugRoot = createRoot(debugDiv);
debugRoot.render(createElement(DebugView, { attach }));
const appDiv = document.querySelector("#app") as HTMLDivElement;
const appRoot = createRoot(appDiv);
appRoot.render(createElement(BlobCollectionView, { blobCollection }));