2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2017-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-07-07 00:48:14 +00:00
|
|
|
/* eslint-disable */
|
|
|
|
|
|
|
|
// This file was taken from Backbone and then modified. It does not conform to this
|
|
|
|
// project's standards.
|
|
|
|
|
2020-11-18 15:15:42 +00:00
|
|
|
(function () {
|
2018-07-07 00:48:14 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-07-19 18:59:08 +00:00
|
|
|
// Note: this is all the code required to customize Backbone's trigger() method to make
|
|
|
|
// it resilient to exceptions thrown by event handlers. Indentation and code styles
|
|
|
|
// were kept inline with the Backbone implementation for easier diffs.
|
|
|
|
|
|
|
|
// The changes are:
|
2018-07-07 00:48:14 +00:00
|
|
|
// 1. added 'name' parameter to triggerEvents to give it access to the
|
|
|
|
// current event name
|
|
|
|
// 2. added try/catch handlers to triggerEvents with error logging inside
|
|
|
|
// every while loop
|
2017-07-19 18:59:08 +00:00
|
|
|
|
|
|
|
// And of course, we update the protoypes of Backbone.Model/Backbone.View as well as
|
|
|
|
// Backbone.Events itself
|
2017-07-19 20:47:05 +00:00
|
|
|
|
2018-07-07 00:48:14 +00:00
|
|
|
const arr = [];
|
2017-07-19 20:47:05 +00:00
|
|
|
|
2018-07-07 00:48:14 +00:00
|
|
|
const slice = arr.slice;
|
2017-07-19 20:47:05 +00:00
|
|
|
|
|
|
|
// Regular expression used to split event strings.
|
2018-07-07 00:48:14 +00:00
|
|
|
const eventSplitter = /\s+/;
|
2017-07-19 20:47:05 +00:00
|
|
|
|
|
|
|
// Implement fancy features of the Events API such as multiple event
|
|
|
|
// names `"change blur"` and jQuery-style event maps `{change: action}`
|
|
|
|
// in terms of the existing API.
|
2020-11-18 15:15:42 +00:00
|
|
|
const eventsApi = function (obj, action, name, rest) {
|
2017-07-19 20:47:05 +00:00
|
|
|
if (!name) return true;
|
|
|
|
|
|
|
|
// Handle event maps.
|
|
|
|
if (typeof name === 'object') {
|
2018-07-07 00:48:14 +00:00
|
|
|
for (const key in name) {
|
|
|
|
obj[action](...[key, name[key]].concat(rest));
|
2017-07-19 20:47:05 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle space separated event names.
|
|
|
|
if (eventSplitter.test(name)) {
|
2018-07-07 00:48:14 +00:00
|
|
|
const names = name.split(eventSplitter);
|
|
|
|
for (let i = 0, l = names.length; i < l; i++) {
|
|
|
|
obj[action](...[names[i]].concat(rest));
|
2017-07-19 20:47:05 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A difficult-to-believe, but optimized internal dispatch function for
|
|
|
|
// triggering events. Tries to keep the usual cases speedy (most internal
|
|
|
|
// Backbone events have 3 arguments).
|
2020-11-18 15:15:42 +00:00
|
|
|
const triggerEvents = function (events, name, args) {
|
2018-07-07 00:48:14 +00:00
|
|
|
let ev,
|
2018-04-27 21:25:04 +00:00
|
|
|
i = -1,
|
|
|
|
l = events.length,
|
|
|
|
a1 = args[0],
|
|
|
|
a2 = args[1],
|
|
|
|
a3 = args[2];
|
2020-11-18 15:15:42 +00:00
|
|
|
const logError = function (error) {
|
2021-10-07 23:28:47 +00:00
|
|
|
window.SignalContext.log.error(
|
2018-04-27 21:25:04 +00:00
|
|
|
'Model caught error triggering',
|
|
|
|
name,
|
|
|
|
'event:',
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
2017-07-19 18:59:08 +00:00
|
|
|
};
|
2017-07-19 20:47:05 +00:00
|
|
|
switch (args.length) {
|
2017-07-19 18:59:08 +00:00
|
|
|
case 0:
|
|
|
|
while (++i < l) {
|
|
|
|
try {
|
|
|
|
(ev = events[i]).callback.call(ev.ctx);
|
2018-04-27 21:25:04 +00:00
|
|
|
} catch (error) {
|
2017-07-19 18:59:08 +00:00
|
|
|
logError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case 1:
|
|
|
|
while (++i < l) {
|
|
|
|
try {
|
|
|
|
(ev = events[i]).callback.call(ev.ctx, a1);
|
2018-04-27 21:25:04 +00:00
|
|
|
} catch (error) {
|
2017-07-19 18:59:08 +00:00
|
|
|
logError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case 2:
|
|
|
|
while (++i < l) {
|
|
|
|
try {
|
|
|
|
(ev = events[i]).callback.call(ev.ctx, a1, a2);
|
2018-04-27 21:25:04 +00:00
|
|
|
} catch (error) {
|
2017-07-19 18:59:08 +00:00
|
|
|
logError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case 3:
|
|
|
|
while (++i < l) {
|
|
|
|
try {
|
|
|
|
(ev = events[i]).callback.call(ev.ctx, a1, a2, a3);
|
2018-04-27 21:25:04 +00:00
|
|
|
} catch (error) {
|
2017-07-19 18:59:08 +00:00
|
|
|
logError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
while (++i < l) {
|
|
|
|
try {
|
|
|
|
(ev = events[i]).callback.apply(ev.ctx, args);
|
2018-04-27 21:25:04 +00:00
|
|
|
} catch (error) {
|
2017-07-19 18:59:08 +00:00
|
|
|
logError(error);
|
|
|
|
}
|
|
|
|
}
|
2017-07-19 20:47:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Trigger one or many events, firing all bound callbacks. Callbacks are
|
|
|
|
// passed the same arguments as `trigger` is, apart from the event name
|
|
|
|
// (unless you're listening on `"all"`, which will cause your callback to
|
|
|
|
// receive the true name of the event as the first argument).
|
2017-07-21 18:48:54 +00:00
|
|
|
function trigger(name) {
|
2017-07-19 20:47:05 +00:00
|
|
|
if (!this._events) return this;
|
2018-07-07 00:48:14 +00:00
|
|
|
const args = slice.call(arguments, 1);
|
2017-07-19 20:47:05 +00:00
|
|
|
if (!eventsApi(this, 'trigger', name, args)) return this;
|
2018-07-07 00:48:14 +00:00
|
|
|
const events = this._events[name];
|
|
|
|
const allEvents = this._events.all;
|
2017-07-19 18:59:08 +00:00
|
|
|
if (events) triggerEvents(events, name, args);
|
|
|
|
if (allEvents) triggerEvents(allEvents, name, arguments);
|
2017-07-19 20:47:05 +00:00
|
|
|
return this;
|
2017-07-21 18:48:54 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
Backbone.Model.prototype.trigger =
|
|
|
|
Backbone.View.prototype.trigger =
|
|
|
|
Backbone.Collection.prototype.trigger =
|
|
|
|
Backbone.Events.trigger =
|
|
|
|
trigger;
|
2017-07-19 20:47:05 +00:00
|
|
|
})();
|