-
-
Notifications
You must be signed in to change notification settings - Fork 612
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stop cloning events when adding to state #612
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,8 +20,6 @@ limitations under the License. | |
*/ | ||
|
||
const RoomState = require("./room-state"); | ||
const utils = require("../utils"); | ||
const MatrixEvent = require("./event").MatrixEvent; | ||
|
||
/** | ||
* Construct a new EventTimeline | ||
|
@@ -88,19 +86,19 @@ EventTimeline.prototype.initialiseState = function(stateEvents) { | |
throw new Error("Cannot initialise state after events are added"); | ||
} | ||
|
||
// we deep-copy the events here, in case they get changed later - we don't | ||
// want changes to the start state leaking through to the end state. | ||
const oldStateEvents = utils.map( | ||
utils.deepCopy( | ||
stateEvents.map(function(mxEvent) { | ||
return mxEvent.event; | ||
}), | ||
), | ||
function(ev) { | ||
return new MatrixEvent(ev); | ||
}); | ||
|
||
this._startState.setStateEvents(oldStateEvents); | ||
// We previously deep copied events here and used different copies in | ||
// the oldState and state events: this decision seems to date back | ||
// quite a way and was apparently made to fix a bug where modifications | ||
// made to the start state leaked through to the end state. | ||
// This really shouldn't be possible though: the events themselves should | ||
// not change. Duplicating the events uses a lot of extra memory, | ||
// so we now no longer do it. To assert that they really do never change, | ||
// freeze them! Note that we can't do this for events in general: | ||
for (const e of stateEvents) { | ||
Object.freeze(e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need to do some babel foo to make this work on node? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remarkably no: Object.freeze has been supported in node basically forever: http://node.green/#ES2015-misc-Object-static-methods-accept-primitives-Object-freeze |
||
} | ||
|
||
this._startState.setStateEvents(stateEvents); | ||
this._endState.setStateEvents(stateEvents); | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(why not?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have elaborated