@netless/slide 1.4.59-alpha.4 → 1.4.59

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.
@@ -0,0 +1,206 @@
1
+ function assertSyncEventQueuePolicy(policy) {
2
+ if (policy !== "fifo" && policy !== "latest-pending-render") {
3
+ throw new Error("Invalid sync event queue policy: " + policy);
4
+ }
5
+ }
6
+ export function resolveSyncEventQueuePolicy(mode, policy) {
7
+ if (policy === void 0) { policy = "fifo"; }
8
+ assertSyncEventQueuePolicy(policy);
9
+ return mode === "interactive" ? policy : "fifo";
10
+ }
11
+ function isPageScopedEvent(event) {
12
+ switch (event.type) {
13
+ case "renderSlide":
14
+ case "nextStep":
15
+ case "prevStep":
16
+ case "interactiveAnim":
17
+ case "mediaPlay":
18
+ case "mediaPause":
19
+ case "mediaSeek":
20
+ case "mediaFullscreen":
21
+ return true;
22
+ default:
23
+ return false;
24
+ }
25
+ }
26
+ var SyncTaskManager = /** @class */ (function () {
27
+ function SyncTaskManager(fn, options) {
28
+ var _this = this;
29
+ if (options === void 0) { options = {}; }
30
+ this.tasks = [];
31
+ this.isDestroy = false;
32
+ this.isScheduling = false;
33
+ this.isTaskRunning = false;
34
+ this.isCatchingUp = false;
35
+ this.compactedTasks = new WeakMap();
36
+ this.finishTask = function () {
37
+ _this.isTaskRunning = false;
38
+ _this.runningTask = undefined;
39
+ if (_this.isDestroy) {
40
+ _this.tasks.length = 0;
41
+ _this.isScheduling = false;
42
+ }
43
+ else if (_this.tasks.length > 0) {
44
+ window.requestAnimationFrame(_this.schedule);
45
+ }
46
+ else {
47
+ _this.isScheduling = false;
48
+ _this.setCatchUpState(false);
49
+ }
50
+ };
51
+ this.schedule = function () {
52
+ if (_this.isDestroy) {
53
+ _this.tasks.length = 0;
54
+ _this.isScheduling = false;
55
+ return;
56
+ }
57
+ _this.isScheduling = true;
58
+ var task = _this.tasks.shift();
59
+ if (!task) {
60
+ _this.isScheduling = false;
61
+ return;
62
+ }
63
+ _this.isTaskRunning = true;
64
+ _this.runningTask = task;
65
+ var compactInfo = _this.compactedTasks.get(task);
66
+ try {
67
+ _this.fn(task).then(function () {
68
+ var _a;
69
+ _this.compactedTasks.delete(task);
70
+ if (compactInfo && !_this.isDestroy) {
71
+ try {
72
+ (_a = _this.onCompactedRenderComplete) === null || _a === void 0 ? void 0 : _a.call(_this, compactInfo, Date.now());
73
+ }
74
+ catch ( /* Observability must not interrupt synchronization. */_b) { /* Observability must not interrupt synchronization. */ }
75
+ }
76
+ _this.finishTask();
77
+ }, function () {
78
+ _this.compactedTasks.delete(task);
79
+ _this.finishTask();
80
+ });
81
+ }
82
+ catch (_a) {
83
+ _this.compactedTasks.delete(task);
84
+ _this.finishTask();
85
+ }
86
+ };
87
+ this.fn = fn;
88
+ this.policy = options.policy || "fifo";
89
+ this.onCompact = options.onCompact;
90
+ this.onCompactedRenderComplete = options.onCompactedRenderComplete;
91
+ this.onCatchUpStateChange = options.onCatchUpStateChange;
92
+ this.shouldTrackCatchUpEvent = options.shouldTrackCatchUpEvent;
93
+ assertSyncEventQueuePolicy(this.policy);
94
+ }
95
+ SyncTaskManager.prototype.destroy = function () {
96
+ this.isDestroy = true;
97
+ this.isCatchingUp = false;
98
+ this.runningTask = undefined;
99
+ this.tasks.length = 0;
100
+ };
101
+ SyncTaskManager.prototype.setPolicy = function (policy) {
102
+ assertSyncEventQueuePolicy(policy);
103
+ this.policy = policy;
104
+ };
105
+ SyncTaskManager.prototype.setCatchUpState = function (isCatchingUp) {
106
+ var _a;
107
+ if (this.isCatchingUp === isCatchingUp) {
108
+ return;
109
+ }
110
+ this.isCatchingUp = isCatchingUp;
111
+ try {
112
+ (_a = this.onCatchUpStateChange) === null || _a === void 0 ? void 0 : _a.call(this, isCatchingUp);
113
+ }
114
+ catch ( /* Observability must not interrupt synchronization. */_b) { /* Observability must not interrupt synchronization. */ }
115
+ };
116
+ SyncTaskManager.prototype.isCatchUpEvent = function (event) {
117
+ var _a, _b;
118
+ try {
119
+ return (_b = (_a = this.shouldTrackCatchUpEvent) === null || _a === void 0 ? void 0 : _a.call(this, event)) !== null && _b !== void 0 ? _b : true;
120
+ }
121
+ catch (_c) {
122
+ return true;
123
+ }
124
+ };
125
+ SyncTaskManager.prototype.compactForRender = function (event) {
126
+ var droppedEventTypes = {};
127
+ var droppedEventCount = 0;
128
+ var droppedRenderCount = 0;
129
+ var droppedStepCount = 0;
130
+ var droppedAnimationCount = 0;
131
+ var droppedMediaCount = 0;
132
+ while (this.tasks.length > 0) {
133
+ var pendingEvent = this.tasks[this.tasks.length - 1];
134
+ if (!isPageScopedEvent(pendingEvent)) {
135
+ break;
136
+ }
137
+ this.tasks.pop();
138
+ this.compactedTasks.delete(pendingEvent);
139
+ droppedEventCount += 1;
140
+ droppedEventTypes[pendingEvent.type] = (droppedEventTypes[pendingEvent.type] || 0) + 1;
141
+ if (pendingEvent.type === "renderSlide") {
142
+ droppedRenderCount += 1;
143
+ }
144
+ else if (pendingEvent.type === "nextStep" || pendingEvent.type === "prevStep") {
145
+ droppedStepCount += 1;
146
+ }
147
+ else if (pendingEvent.type === "interactiveAnim") {
148
+ droppedAnimationCount += 1;
149
+ }
150
+ else {
151
+ droppedMediaCount += 1;
152
+ }
153
+ }
154
+ if (droppedEventCount === 0) {
155
+ return undefined;
156
+ }
157
+ var boundaryEvent = this.tasks[this.tasks.length - 1];
158
+ return {
159
+ policy: "latest-pending-render",
160
+ droppedEventCount: droppedEventCount,
161
+ droppedRenderCount: droppedRenderCount,
162
+ droppedStepCount: droppedStepCount,
163
+ droppedAnimationCount: droppedAnimationCount,
164
+ droppedMediaCount: droppedMediaCount,
165
+ droppedOperationCount: droppedEventCount - droppedRenderCount,
166
+ droppedEventTypes: droppedEventTypes,
167
+ retainedSlideIndex: event.index,
168
+ retainedRenderReceivedAt: Date.now(),
169
+ hasRunningTask: this.isTaskRunning,
170
+ hardBoundaryType: boundaryEvent === null || boundaryEvent === void 0 ? void 0 : boundaryEvent.type,
171
+ };
172
+ };
173
+ SyncTaskManager.prototype.addTask = function (event) {
174
+ var _this = this;
175
+ var _a;
176
+ if (this.isDestroy) {
177
+ return undefined;
178
+ }
179
+ var hasPendingWork = this.isTaskRunning || this.tasks.length > 0;
180
+ var isIncomingCatchUpEvent = this.policy === "latest-pending-render" && this.isCatchUpEvent(event);
181
+ var hasCatchUpEvent = this.policy === "latest-pending-render" && (isIncomingCatchUpEvent ||
182
+ (!!this.runningTask && this.isCatchUpEvent(this.runningTask)) ||
183
+ this.tasks.some(function (pendingEvent) { return _this.isCatchUpEvent(pendingEvent); }));
184
+ var compactInfo = this.policy === "latest-pending-render" && event.type === "renderSlide" ?
185
+ this.compactForRender(event) :
186
+ undefined;
187
+ this.tasks.push(event);
188
+ var shouldStartCatchUp = hasCatchUpEvent && (hasPendingWork || (isIncomingCatchUpEvent && event.type === "renderSlide"));
189
+ if (shouldStartCatchUp) {
190
+ this.setCatchUpState(true);
191
+ }
192
+ if (compactInfo) {
193
+ this.compactedTasks.set(event, compactInfo);
194
+ try {
195
+ (_a = this.onCompact) === null || _a === void 0 ? void 0 : _a.call(this, compactInfo);
196
+ }
197
+ catch ( /* Observability must not interrupt synchronization. */_b) { /* Observability must not interrupt synchronization. */ }
198
+ }
199
+ if (!this.isScheduling) {
200
+ this.schedule();
201
+ }
202
+ return compactInfo;
203
+ };
204
+ return SyncTaskManager;
205
+ }());
206
+ export { SyncTaskManager };
package/lib/global.js ADDED
@@ -0,0 +1,118 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ var __generator = (this && this.__generator) || function (thisArg, body) {
11
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
+ function verb(n) { return function (v) { return step([n, v]); }; }
14
+ function step(op) {
15
+ if (f) throw new TypeError("Generator is already executing.");
16
+ while (_) try {
17
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
+ if (y = 0, t) op = [op[0] & 2, t.value];
19
+ switch (op[0]) {
20
+ case 0: case 1: t = op; break;
21
+ case 4: _.label++; return { value: op[1], done: false };
22
+ case 5: _.label++; y = op[1]; op = [0]; continue;
23
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
+ default:
25
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
+ if (t[2]) _.ops.pop();
30
+ _.trys.pop(); continue;
31
+ }
32
+ op = body.call(thisArg, _);
33
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
+ }
36
+ };
37
+ /**
38
+ * 预加载静态资源, 调用后会一次性加载除媒体文件以外的远程资源,并生成必要的纹理缓存
39
+ * 创建 Slide 对象时需要设置 useLocalCache 参数 为 true
40
+ * @param taskId 任务 id
41
+ * @param prefix 任务进度 api 返回的资源前缀
42
+ * @param resourceOnly boolean 类型
43
+ * @param onProgress 加载进度回调函数
44
+ */
45
+ import { Slide } from "./Slide";
46
+ export function preloadResource(taskId, prefix, maxResolutionLevel, onProgress, pages) {
47
+ return __awaiter(this, void 0, void 0, function () {
48
+ var div, slide, slideCount, uniquePages, i, pageNumber, i, e_1;
49
+ return __generator(this, function (_a) {
50
+ switch (_a.label) {
51
+ case 0:
52
+ div = document.createElement("div");
53
+ slide = new Slide({
54
+ anchor: div,
55
+ renderOptions: {
56
+ minFPS: 1,
57
+ maxFPS: 1,
58
+ resolution: 1,
59
+ maxResolutionLevel: maxResolutionLevel,
60
+ forceCanvas: true,
61
+ },
62
+ mode: "local",
63
+ interactive: false,
64
+ useLocalCache: true,
65
+ });
66
+ slide.setResource(taskId, prefix);
67
+ return [4 /*yield*/, slide.getSlideCountAsync()];
68
+ case 1:
69
+ slideCount = _a.sent();
70
+ _a.label = 2;
71
+ case 2:
72
+ _a.trys.push([2, 12, 13, 14]);
73
+ if (!(pages && pages.length > 0)) return [3 /*break*/, 7];
74
+ uniquePages = pages.filter(function (item, index) { return pages.indexOf(item) === index; });
75
+ i = 0;
76
+ _a.label = 3;
77
+ case 3:
78
+ if (!(i < uniquePages.length)) return [3 /*break*/, 6];
79
+ pageNumber = uniquePages[i];
80
+ if (!(pageNumber > 0 && pageNumber <= slideCount)) return [3 /*break*/, 5];
81
+ // @ts-ignore
82
+ return [4 /*yield*/, slide.player.stagePool.preload(pageNumber, true)];
83
+ case 4:
84
+ // @ts-ignore
85
+ _a.sent();
86
+ onProgress(Math.round(((i + 1) / uniquePages.length) * 100) / 100);
87
+ _a.label = 5;
88
+ case 5:
89
+ i++;
90
+ return [3 /*break*/, 3];
91
+ case 6: return [3 /*break*/, 11];
92
+ case 7:
93
+ i = 1;
94
+ _a.label = 8;
95
+ case 8:
96
+ if (!(i <= slideCount)) return [3 /*break*/, 11];
97
+ // @ts-ignore
98
+ return [4 /*yield*/, slide.player.stagePool.preload(i, true)];
99
+ case 9:
100
+ // @ts-ignore
101
+ _a.sent();
102
+ onProgress(Math.round((i / slideCount) * 100) / 100);
103
+ _a.label = 10;
104
+ case 10:
105
+ i++;
106
+ return [3 /*break*/, 8];
107
+ case 11: return [3 /*break*/, 14];
108
+ case 12:
109
+ e_1 = _a.sent();
110
+ throw e_1;
111
+ case 13:
112
+ slide.destroy();
113
+ return [7 /*endfinally*/];
114
+ case 14: return [2 /*return*/];
115
+ }
116
+ });
117
+ });
118
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netless/slide",
3
- "version": "1.4.59-alpha.4",
3
+ "version": "1.4.59",
4
4
  "description": "> TODO: description",
5
5
  "author": "huaguzheng <huaguzheng2007@126.com>",
6
6
  "homepage": "https://github.com/netless-io/netless-slide-demo",
@@ -17,7 +17,7 @@
17
17
  "scripts": {
18
18
  "dev": "tsc",
19
19
  "test": "yarn test:unit",
20
- "test:unit": "node __tests__/SyncTaskManager.test.js && node __tests__/RenderSlideVisibility.test.js && node __tests__/GlobalPreload.test.js && node __tests__/RenderDestroySettlement.test.js && node __tests__/SlideMediaLifecycle.test.js",
20
+ "test:unit": "node __tests__/SyncTaskManager.test.js && node __tests__/RenderSlideVisibility.test.js && node __tests__/PlayerContextReleaseWait.test.js && node __tests__/GlobalPreload.test.js && node __tests__/RenderDestroySettlement.test.js && node __tests__/SlideMediaLifecycle.test.js",
21
21
  "test:package": "yarn build && node __tests__/PackageBundle.test.js",
22
22
  "build": "webpack --config webpack.config.js",
23
23
  "watch": "webpack --config webpack.config.js --watch"
@@ -38,5 +38,6 @@
38
38
  },
39
39
  "publishConfig": {
40
40
  "access": "public"
41
- }
41
+ },
42
+ "gitHead": "01cfefb8e1190b9da3ddfb657ceb5ccc008d7a84"
42
43
  }