-
Notifications
You must be signed in to change notification settings - Fork 151
/
mutator.ts
154 lines (147 loc) · 6.98 KB
/
mutator.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* Copyright 2018 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { NodeContext } from './nodes';
import { StringContext } from './strings';
import { WorkerContext } from './worker';
import { OffscreenCanvasProcessor } from './commands/offscreen-canvas';
import { TransferrableMutationType, ReadableMutationType, isUserVisibleMutation } from '../transfer/TransferrableMutation';
import { EventSubscriptionProcessor } from './commands/event-subscription';
import { BoundingClientRectProcessor } from './commands/bounding-client-rect';
import { ChildListProcessor } from './commands/child-list';
import { AttributeProcessor } from './commands/attribute';
import { CharacterDataProcessor } from './commands/character-data';
import { PropertyProcessor } from './commands/property';
import { LongTaskExecutor } from './commands/long-task';
import { CommandExecutor } from './commands/interface';
import { WorkerDOMConfiguration, MutationPumpFunction } from './configuration';
import { Phase } from '../transfer/Phase';
import { ObjectMutationProcessor } from './commands/object-mutation';
import { ObjectCreationProcessor } from './commands/object-creation';
import { ObjectContext } from './object-context';
import { ImageBitmapProcessor } from './commands/image-bitmap';
import { StorageProcessor } from './commands/storage';
import { FunctionProcessor } from './commands/function';
import { ScrollIntoViewProcessor } from './commands/scroll-into-view';
export class MutatorProcessor {
private stringContext: StringContext;
private nodeContext: NodeContext;
private mutationQueue: Array<Uint16Array> = [];
private pendingMutations: boolean = false;
private mutationPumpFunction: MutationPumpFunction;
private sanitizer: Sanitizer | undefined;
private executors: {
[key: number]: CommandExecutor;
};
/**
* @param stringContext
* @param nodeContext
* @param workerContext
* @param sanitizer Sanitizer to apply to content if needed.
*/
constructor(
stringContext: StringContext,
nodeContext: NodeContext,
workerContext: WorkerContext,
config: WorkerDOMConfiguration,
objectContext: ObjectContext,
) {
this.stringContext = stringContext;
this.nodeContext = nodeContext;
this.sanitizer = config.sanitizer;
this.mutationPumpFunction = config.mutationPump;
const args: [StringContext, NodeContext, WorkerContext, ObjectContext, WorkerDOMConfiguration] = [
stringContext,
nodeContext,
workerContext,
objectContext,
config,
];
const sharedLongTaskProcessor = LongTaskExecutor.apply(null, args);
this.executors = {
[TransferrableMutationType.CHILD_LIST]: ChildListProcessor.apply(null, args),
[TransferrableMutationType.ATTRIBUTES]: AttributeProcessor.apply(null, args),
[TransferrableMutationType.CHARACTER_DATA]: CharacterDataProcessor.apply(null, args),
[TransferrableMutationType.PROPERTIES]: PropertyProcessor.apply(null, args),
[TransferrableMutationType.EVENT_SUBSCRIPTION]: EventSubscriptionProcessor.apply(null, args),
[TransferrableMutationType.GET_BOUNDING_CLIENT_RECT]: BoundingClientRectProcessor.apply(null, args),
[TransferrableMutationType.LONG_TASK_START]: sharedLongTaskProcessor,
[TransferrableMutationType.LONG_TASK_END]: sharedLongTaskProcessor,
[TransferrableMutationType.OFFSCREEN_CANVAS_INSTANCE]: OffscreenCanvasProcessor.apply(null, args),
[TransferrableMutationType.OBJECT_MUTATION]: ObjectMutationProcessor.apply(null, args),
[TransferrableMutationType.OBJECT_CREATION]: ObjectCreationProcessor.apply(null, args),
[TransferrableMutationType.IMAGE_BITMAP_INSTANCE]: ImageBitmapProcessor.apply(null, args),
[TransferrableMutationType.STORAGE]: StorageProcessor.apply(null, args),
[TransferrableMutationType.FUNCTION_CALL]: FunctionProcessor.apply(null, args),
[TransferrableMutationType.SCROLL_INTO_VIEW]: ScrollIntoViewProcessor.apply(null, args),
};
}
/**
* Process MutationRecords from worker thread applying changes to the existing DOM.
* @param phase Current Phase Worker Thread exists in.
* @param nodes New nodes to add in the main thread with the incoming mutations.
* @param stringValues Additional string values to use in decoding messages.
* @param mutations Changes to apply in both graph shape and content of Elements.
*/
public mutate(phase: Phase, nodes: ArrayBuffer, stringValues: Array<string>, mutations: Uint16Array): void {
this.stringContext.storeValues(stringValues);
this.nodeContext.createNodes(nodes, this.sanitizer);
this.mutationQueue = this.mutationQueue.concat(mutations);
if (!this.pendingMutations) {
this.pendingMutations = true;
this.mutationPumpFunction(this.syncFlush, phase);
}
}
/**
* Apply all stored mutations syncronously. This method works well, but can cause jank if there are too many
* mutations to apply in a single frame.
*
* Investigations in using asyncFlush to resolve are worth considering.
*
* @param allowVisibleMutations
* @return Array of mutation types that were disallowed.
*/
private syncFlush = (allowVisibleMutations: boolean = true): TransferrableMutationType[] => {
if (WORKER_DOM_DEBUG) {
console.group('Mutations');
}
const disallowedMutations: TransferrableMutationType[] = [];
this.mutationQueue.forEach((mutationArray) => {
const length: number = mutationArray.length;
let operationStart: number = 0;
while (operationStart < length) {
// TransferrableMutationType is always at position 0.
const mutationType = mutationArray[operationStart];
// TODO(worker-dom): Hoist `allow` up to entry point (index.amp.ts) to avoid bundling `isUserVisibleMutation`.
const allow = allowVisibleMutations || !isUserVisibleMutation(mutationType);
if (!allow) {
// TODO(worker-dom): Consider returning the strings from executor.print() for better error messaging.
disallowedMutations.push(mutationType);
}
const executor = this.executors[mutationType];
if (WORKER_DOM_DEBUG) {
console.log(allow ? '' : '[disallowed]', ReadableMutationType[mutationType], executor.print(mutationArray, operationStart));
}
operationStart = executor.execute(mutationArray, operationStart, allow);
}
});
if (WORKER_DOM_DEBUG) {
console.groupEnd();
}
this.mutationQueue = [];
this.pendingMutations = false;
return disallowedMutations;
};
}