UNPKG

3.32 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.EventContext = EventContext;
7
8/*
9Copyright 2015, 2016 OpenMarket Ltd
10Copyright 2019 The Matrix.org Foundation C.I.C.
11
12Licensed under the Apache License, Version 2.0 (the "License");
13you may not use this file except in compliance with the License.
14You may obtain a copy of the License at
15
16 http://www.apache.org/licenses/LICENSE-2.0
17
18Unless required by applicable law or agreed to in writing, software
19distributed under the License is distributed on an "AS IS" BASIS,
20WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21See the License for the specific language governing permissions and
22limitations under the License.
23*/
24
25/**
26 * @module models/event-context
27 */
28
29/**
30 * Construct a new EventContext
31 *
32 * An eventcontext is used for circumstances such as search results, when we
33 * have a particular event of interest, and a bunch of events before and after
34 * it.
35 *
36 * It also stores pagination tokens for going backwards and forwards in the
37 * timeline.
38 *
39 * @param {MatrixEvent} ourEvent the event at the centre of this context
40 *
41 * @constructor
42 */
43function EventContext(ourEvent) {
44 this._timeline = [ourEvent];
45 this._ourEventIndex = 0;
46 this._paginateTokens = {
47 b: null,
48 f: null
49 }; // this is used by MatrixClient to keep track of active requests
50
51 this._paginateRequests = {
52 b: null,
53 f: null
54 };
55}
56/**
57 * Get the main event of interest
58 *
59 * This is a convenience function for getTimeline()[getOurEventIndex()].
60 *
61 * @return {MatrixEvent} The event at the centre of this context.
62 */
63
64
65EventContext.prototype.getEvent = function () {
66 return this._timeline[this._ourEventIndex];
67};
68/**
69 * Get the list of events in this context
70 *
71 * @return {Array} An array of MatrixEvents
72 */
73
74
75EventContext.prototype.getTimeline = function () {
76 return this._timeline;
77};
78/**
79 * Get the index in the timeline of our event
80 *
81 * @return {Number}
82 */
83
84
85EventContext.prototype.getOurEventIndex = function () {
86 return this._ourEventIndex;
87};
88/**
89 * Get a pagination token.
90 *
91 * @param {boolean} backwards true to get the pagination token for going
92 * backwards in time
93 * @return {string}
94 */
95
96
97EventContext.prototype.getPaginateToken = function (backwards) {
98 return this._paginateTokens[backwards ? 'b' : 'f'];
99};
100/**
101 * Set a pagination token.
102 *
103 * Generally this will be used only by the matrix js sdk.
104 *
105 * @param {string} token pagination token
106 * @param {boolean} backwards true to set the pagination token for going
107 * backwards in time
108 */
109
110
111EventContext.prototype.setPaginateToken = function (token, backwards) {
112 this._paginateTokens[backwards ? 'b' : 'f'] = token;
113};
114/**
115 * Add more events to the timeline
116 *
117 * @param {Array} events new events, in timeline order
118 * @param {boolean} atStart true to insert new events at the start
119 */
120
121
122EventContext.prototype.addEvents = function (events, atStart) {
123 // TODO: should we share logic with Room.addEventsToTimeline?
124 // Should Room even use EventContext?
125 if (atStart) {
126 this._timeline = events.concat(this._timeline);
127 this._ourEventIndex += events.length;
128 } else {
129 this._timeline = this._timeline.concat(events);
130 }
131};
\No newline at end of file