@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.
Files changed (62) hide show
  1. package/.eslintrc.js +19 -0
  2. package/LICENSE +21 -0
  3. package/README.md +56 -0
  4. package/api-extractor.json +4 -0
  5. package/dist/FluidCache.d.ts +39 -0
  6. package/dist/FluidCache.d.ts.map +1 -0
  7. package/dist/FluidCache.js +159 -0
  8. package/dist/FluidCache.js.map +1 -0
  9. package/dist/FluidCacheIndexedDb.d.ts +65 -0
  10. package/dist/FluidCacheIndexedDb.d.ts.map +1 -0
  11. package/dist/FluidCacheIndexedDb.js +65 -0
  12. package/dist/FluidCacheIndexedDb.js.map +1 -0
  13. package/dist/fluidCacheTelemetry.d.ts +19 -0
  14. package/dist/fluidCacheTelemetry.d.ts.map +1 -0
  15. package/dist/fluidCacheTelemetry.js +7 -0
  16. package/dist/fluidCacheTelemetry.js.map +1 -0
  17. package/dist/index.d.ts +7 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +21 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/packageVersion.d.ts +9 -0
  22. package/dist/packageVersion.d.ts.map +1 -0
  23. package/dist/packageVersion.js +12 -0
  24. package/dist/packageVersion.js.map +1 -0
  25. package/dist/scheduleIdleTask.d.ts +22 -0
  26. package/dist/scheduleIdleTask.d.ts.map +1 -0
  27. package/dist/scheduleIdleTask.js +80 -0
  28. package/dist/scheduleIdleTask.js.map +1 -0
  29. package/jest.config.js +12 -0
  30. package/lib/FluidCache.d.ts +39 -0
  31. package/lib/FluidCache.d.ts.map +1 -0
  32. package/lib/FluidCache.js +155 -0
  33. package/lib/FluidCache.js.map +1 -0
  34. package/lib/FluidCacheIndexedDb.d.ts +65 -0
  35. package/lib/FluidCacheIndexedDb.d.ts.map +1 -0
  36. package/lib/FluidCacheIndexedDb.js +59 -0
  37. package/lib/FluidCacheIndexedDb.js.map +1 -0
  38. package/lib/fluidCacheTelemetry.d.ts +19 -0
  39. package/lib/fluidCacheTelemetry.d.ts.map +1 -0
  40. package/lib/fluidCacheTelemetry.js +6 -0
  41. package/lib/fluidCacheTelemetry.js.map +1 -0
  42. package/lib/index.d.ts +7 -0
  43. package/lib/index.d.ts.map +1 -0
  44. package/lib/index.js +7 -0
  45. package/lib/index.js.map +1 -0
  46. package/lib/packageVersion.d.ts +9 -0
  47. package/lib/packageVersion.d.ts.map +1 -0
  48. package/lib/packageVersion.js +9 -0
  49. package/lib/packageVersion.js.map +1 -0
  50. package/lib/scheduleIdleTask.d.ts +22 -0
  51. package/lib/scheduleIdleTask.d.ts.map +1 -0
  52. package/lib/scheduleIdleTask.js +76 -0
  53. package/lib/scheduleIdleTask.js.map +1 -0
  54. package/package.json +67 -0
  55. package/src/FluidCache.ts +276 -0
  56. package/src/FluidCacheIndexedDb.ts +155 -0
  57. package/src/fluidCacheTelemetry.ts +21 -0
  58. package/src/index.ts +7 -0
  59. package/src/packageVersion.ts +9 -0
  60. package/src/scheduleIdleTask.ts +119 -0
  61. package/tsconfig.esnext.json +7 -0
  62. 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
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./lib",
5
+ "module": "esnext"
6
+ },
7
+ }
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
+ }