@fluidframework/driver-web-cache 0.58.2000-58133
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +19 -0
- package/LICENSE +21 -0
- package/README.md +56 -0
- package/api-extractor.json +4 -0
- package/dist/FluidCache.d.ts +39 -0
- package/dist/FluidCache.d.ts.map +1 -0
- package/dist/FluidCache.js +159 -0
- package/dist/FluidCache.js.map +1 -0
- package/dist/FluidCacheIndexedDb.d.ts +65 -0
- package/dist/FluidCacheIndexedDb.d.ts.map +1 -0
- package/dist/FluidCacheIndexedDb.js +65 -0
- package/dist/FluidCacheIndexedDb.js.map +1 -0
- package/dist/fluidCacheTelemetry.d.ts +19 -0
- package/dist/fluidCacheTelemetry.d.ts.map +1 -0
- package/dist/fluidCacheTelemetry.js +7 -0
- package/dist/fluidCacheTelemetry.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/packageVersion.d.ts +9 -0
- package/dist/packageVersion.d.ts.map +1 -0
- package/dist/packageVersion.js +12 -0
- package/dist/packageVersion.js.map +1 -0
- package/dist/scheduleIdleTask.d.ts +22 -0
- package/dist/scheduleIdleTask.d.ts.map +1 -0
- package/dist/scheduleIdleTask.js +80 -0
- package/dist/scheduleIdleTask.js.map +1 -0
- package/jest.config.js +12 -0
- package/lib/FluidCache.d.ts +39 -0
- package/lib/FluidCache.d.ts.map +1 -0
- package/lib/FluidCache.js +155 -0
- package/lib/FluidCache.js.map +1 -0
- package/lib/FluidCacheIndexedDb.d.ts +65 -0
- package/lib/FluidCacheIndexedDb.d.ts.map +1 -0
- package/lib/FluidCacheIndexedDb.js +59 -0
- package/lib/FluidCacheIndexedDb.js.map +1 -0
- package/lib/fluidCacheTelemetry.d.ts +19 -0
- package/lib/fluidCacheTelemetry.d.ts.map +1 -0
- package/lib/fluidCacheTelemetry.js +6 -0
- package/lib/fluidCacheTelemetry.js.map +1 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +7 -0
- package/lib/index.js.map +1 -0
- package/lib/packageVersion.d.ts +9 -0
- package/lib/packageVersion.d.ts.map +1 -0
- package/lib/packageVersion.js +9 -0
- package/lib/packageVersion.js.map +1 -0
- package/lib/scheduleIdleTask.d.ts +22 -0
- package/lib/scheduleIdleTask.d.ts.map +1 -0
- package/lib/scheduleIdleTask.js +76 -0
- package/lib/scheduleIdleTask.js.map +1 -0
- package/package.json +67 -0
- package/src/FluidCache.ts +276 -0
- package/src/FluidCacheIndexedDb.ts +155 -0
- package/src/fluidCacheTelemetry.ts +21 -0
- package/src/index.ts +7 -0
- package/src/packageVersion.ts +9 -0
- package/src/scheduleIdleTask.ts +119 -0
- package/tsconfig.esnext.json +7 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// Older versions of Typescript considered this API experimental and not in Typescript included types.
|
|
7
|
+
// This can be removed when FF updates to Typescript 4.4 or later
|
|
8
|
+
declare global {
|
|
9
|
+
interface Window {
|
|
10
|
+
requestIdleCallback:
|
|
11
|
+
| ((
|
|
12
|
+
callback: (deadline: {
|
|
13
|
+
readonly didTimeout: boolean;
|
|
14
|
+
timeRemaining: () => number;
|
|
15
|
+
}) => void,
|
|
16
|
+
opts?: {
|
|
17
|
+
timeout: number;
|
|
18
|
+
}
|
|
19
|
+
) => number)
|
|
20
|
+
| undefined;
|
|
21
|
+
cancelIdleCallback: ((handle: number) => void) | undefined;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface TaskQueueItem {
|
|
26
|
+
/** The task to run */
|
|
27
|
+
task: () => void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// A set of tasks that still have to be run
|
|
31
|
+
let taskQueue: TaskQueueItem[] = [];
|
|
32
|
+
|
|
33
|
+
// Set to true when we have a pending idle task scheduled
|
|
34
|
+
let idleTaskScheduled = false;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* A function that schedules a non critical task to be run when the browser has cycles available
|
|
38
|
+
* @param task - The task to be executed
|
|
39
|
+
* @param options - Optional configuration for the task execution
|
|
40
|
+
*/
|
|
41
|
+
export function scheduleIdleTask(task: () => void) {
|
|
42
|
+
taskQueue.push({
|
|
43
|
+
task,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
ensureIdleCallback();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Ensures an idle callback has been scheduled for the remaining tasks
|
|
51
|
+
*/
|
|
52
|
+
function ensureIdleCallback() {
|
|
53
|
+
if (!idleTaskScheduled) {
|
|
54
|
+
// Exception added when eslint rule was added, this should be revisited when modifying this code
|
|
55
|
+
if (window.requestIdleCallback) {
|
|
56
|
+
window.requestIdleCallback(idleTaskCallback);
|
|
57
|
+
} else {
|
|
58
|
+
const deadline = Date.now() + 50;
|
|
59
|
+
window.setTimeout(
|
|
60
|
+
() =>
|
|
61
|
+
idleTaskCallback({
|
|
62
|
+
timeRemaining: () => Math.max(deadline - Date.now(), 0),
|
|
63
|
+
didTimeout: false,
|
|
64
|
+
}),
|
|
65
|
+
0,
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
idleTaskScheduled = true;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Runs tasks from the task queue
|
|
74
|
+
* @param filter - An optional function that will be called for each task to see if it should run.
|
|
75
|
+
* Returns false for tasks that should not run. If omitted all tasks run.
|
|
76
|
+
* @param shouldContinueRunning - An optional function that will be called to determine if
|
|
77
|
+
* we have enough time to continue running tasks. If omitted, we don't stop running tasks.
|
|
78
|
+
*/
|
|
79
|
+
function runTasks(
|
|
80
|
+
filter?: (taskQueueItem: TaskQueueItem) => boolean,
|
|
81
|
+
shouldContinueRunning?: () => boolean,
|
|
82
|
+
) {
|
|
83
|
+
// The next value for the task queue
|
|
84
|
+
const newTaskQueue: TaskQueueItem[] = [];
|
|
85
|
+
|
|
86
|
+
for (let index = 0; index < taskQueue.length; index += 1) {
|
|
87
|
+
if (shouldContinueRunning && !shouldContinueRunning()) {
|
|
88
|
+
// Add the tasks we didn't get to to the end of the new task queue
|
|
89
|
+
newTaskQueue.push(...taskQueue.slice(index));
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const taskQueueItem = taskQueue[index];
|
|
94
|
+
|
|
95
|
+
if (filter && !filter(taskQueueItem)) {
|
|
96
|
+
newTaskQueue.push(taskQueueItem);
|
|
97
|
+
} else {
|
|
98
|
+
taskQueueItem.task();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
taskQueue = newTaskQueue;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Runs all the tasks in the task queue
|
|
106
|
+
function idleTaskCallback(deadline: {
|
|
107
|
+
timeRemaining: () => number;
|
|
108
|
+
readonly didTimeout: boolean;
|
|
109
|
+
}) {
|
|
110
|
+
// Minimum time that must be available on deadline to run any more tasks
|
|
111
|
+
const minTaskTime = 10;
|
|
112
|
+
runTasks(undefined, () => deadline.timeRemaining() > minTaskTime);
|
|
113
|
+
idleTaskScheduled = false;
|
|
114
|
+
|
|
115
|
+
// If we didn't run through the entire queue, schedule another idle callback
|
|
116
|
+
if (taskQueue.length > 0) {
|
|
117
|
+
ensureIdleCallback();
|
|
118
|
+
}
|
|
119
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "@fluidframework/build-common/ts-common-config.json",
|
|
3
|
+
"exclude": [
|
|
4
|
+
"src/test/**/*"
|
|
5
|
+
],
|
|
6
|
+
"compilerOptions": {
|
|
7
|
+
"rootDir": "./src",
|
|
8
|
+
"outDir": "./dist",
|
|
9
|
+
"composite": true,
|
|
10
|
+
"skipLibCheck": true
|
|
11
|
+
},
|
|
12
|
+
"include": [
|
|
13
|
+
"src/**/*"
|
|
14
|
+
]
|
|
15
|
+
}
|