UNPKG

1.36 kBPlain TextView Raw
1/**
2 * Copyright 2018 The Incremental DOM Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS-IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import { notifications } from "./notifications";
18
19/**
20 * A context object keeps track of the state of a patch.
21 */
22class Context {
23 private created: Array<Node> = [];
24 private deleted: Array<Node> = [];
25
26 public markCreated(node: Node) {
27 this.created.push(node);
28 }
29
30 public markDeleted(node: Node) {
31 this.deleted.push(node);
32 }
33
34 /**
35 * Notifies about nodes that were created during the patch operation.
36 */
37 public notifyChanges() {
38 if (notifications.nodesCreated && this.created.length > 0) {
39 notifications.nodesCreated(this.created);
40 }
41
42 if (notifications.nodesDeleted && this.deleted.length > 0) {
43 notifications.nodesDeleted(this.deleted);
44 }
45 }
46}
47
48export { Context };