Convert "reliable trigger" to TypeScript
This commit is contained in:
parent
fc99882f05
commit
d4bba46b2c
7 changed files with 185 additions and 189 deletions
|
@ -1,4 +1,4 @@
|
||||||
<!-- Copyright 2014-2021 Signal Messenger, LLC -->
|
<!-- Copyright 2014-2022 Signal Messenger, LLC -->
|
||||||
<!-- SPDX-License-Identifier: AGPL-3.0-only -->
|
<!-- SPDX-License-Identifier: AGPL-3.0-only -->
|
||||||
|
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
|
@ -158,8 +158,11 @@
|
||||||
type="text/javascript"
|
type="text/javascript"
|
||||||
src="ts/manage_full_screen_class.js"
|
src="ts/manage_full_screen_class.js"
|
||||||
></script>
|
></script>
|
||||||
<script type="text/javascript" src="ts/backboneJquery.js"></script>
|
<script type="text/javascript" src="ts/backbone/backboneJquery.js"></script>
|
||||||
<script type="text/javascript" src="js/reliable_trigger.js"></script>
|
<script
|
||||||
|
type="text/javascript"
|
||||||
|
src="ts/backbone/reliable_trigger.js"
|
||||||
|
></script>
|
||||||
|
|
||||||
<script
|
<script
|
||||||
type="text/javascript"
|
type="text/javascript"
|
||||||
|
|
|
@ -1,144 +0,0 @@
|
||||||
// Copyright 2017-2020 Signal Messenger, LLC
|
|
||||||
// SPDX-License-Identifier: AGPL-3.0-only
|
|
||||||
|
|
||||||
/* eslint-disable */
|
|
||||||
|
|
||||||
// This file was taken from Backbone and then modified. It does not conform to this
|
|
||||||
// project's standards.
|
|
||||||
|
|
||||||
(function () {
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
// 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:
|
|
||||||
// 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
|
|
||||||
|
|
||||||
// And of course, we update the protoypes of Backbone.Model/Backbone.View as well as
|
|
||||||
// Backbone.Events itself
|
|
||||||
|
|
||||||
const arr = [];
|
|
||||||
|
|
||||||
const slice = arr.slice;
|
|
||||||
|
|
||||||
// Regular expression used to split event strings.
|
|
||||||
const eventSplitter = /\s+/;
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
const eventsApi = function (obj, action, name, rest) {
|
|
||||||
if (!name) return true;
|
|
||||||
|
|
||||||
// Handle event maps.
|
|
||||||
if (typeof name === 'object') {
|
|
||||||
for (const key in name) {
|
|
||||||
obj[action](...[key, name[key]].concat(rest));
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle space separated event names.
|
|
||||||
if (eventSplitter.test(name)) {
|
|
||||||
const names = name.split(eventSplitter);
|
|
||||||
for (let i = 0, l = names.length; i < l; i++) {
|
|
||||||
obj[action](...[names[i]].concat(rest));
|
|
||||||
}
|
|
||||||
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).
|
|
||||||
const triggerEvents = function (events, name, args) {
|
|
||||||
let ev,
|
|
||||||
i = -1,
|
|
||||||
l = events.length,
|
|
||||||
a1 = args[0],
|
|
||||||
a2 = args[1],
|
|
||||||
a3 = args[2];
|
|
||||||
const logError = function (error) {
|
|
||||||
window.SignalContext.log.error(
|
|
||||||
'Model caught error triggering',
|
|
||||||
name,
|
|
||||||
'event:',
|
|
||||||
error && error.stack ? error.stack : error
|
|
||||||
);
|
|
||||||
};
|
|
||||||
switch (args.length) {
|
|
||||||
case 0:
|
|
||||||
while (++i < l) {
|
|
||||||
try {
|
|
||||||
(ev = events[i]).callback.call(ev.ctx);
|
|
||||||
} catch (error) {
|
|
||||||
logError(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
case 1:
|
|
||||||
while (++i < l) {
|
|
||||||
try {
|
|
||||||
(ev = events[i]).callback.call(ev.ctx, a1);
|
|
||||||
} catch (error) {
|
|
||||||
logError(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
case 2:
|
|
||||||
while (++i < l) {
|
|
||||||
try {
|
|
||||||
(ev = events[i]).callback.call(ev.ctx, a1, a2);
|
|
||||||
} catch (error) {
|
|
||||||
logError(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
case 3:
|
|
||||||
while (++i < l) {
|
|
||||||
try {
|
|
||||||
(ev = events[i]).callback.call(ev.ctx, a1, a2, a3);
|
|
||||||
} catch (error) {
|
|
||||||
logError(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
default:
|
|
||||||
while (++i < l) {
|
|
||||||
try {
|
|
||||||
(ev = events[i]).callback.apply(ev.ctx, args);
|
|
||||||
} catch (error) {
|
|
||||||
logError(error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// 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).
|
|
||||||
function trigger(name) {
|
|
||||||
if (!this._events) return this;
|
|
||||||
const args = slice.call(arguments, 1);
|
|
||||||
if (!eventsApi(this, 'trigger', name, args)) return this;
|
|
||||||
const events = this._events[name];
|
|
||||||
const allEvents = this._events.all;
|
|
||||||
if (events) triggerEvents(events, name, args);
|
|
||||||
if (allEvents) triggerEvents(allEvents, name, arguments);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
Backbone.Model.prototype.trigger =
|
|
||||||
Backbone.View.prototype.trigger =
|
|
||||||
Backbone.Collection.prototype.trigger =
|
|
||||||
Backbone.Events.trigger =
|
|
||||||
trigger;
|
|
||||||
})();
|
|
|
@ -1,4 +1,4 @@
|
||||||
<!-- Copyright 2019-2021 Signal Messenger, LLC -->
|
<!-- Copyright 2019-2022 Signal Messenger, LLC -->
|
||||||
<!-- SPDX-License-Identifier: AGPL-3.0-only -->
|
<!-- SPDX-License-Identifier: AGPL-3.0-only -->
|
||||||
|
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
|
@ -9,7 +9,10 @@
|
||||||
<body>
|
<body>
|
||||||
<div id="root"></div>
|
<div id="root"></div>
|
||||||
<script type="text/javascript" src="../../js/components.js"></script>
|
<script type="text/javascript" src="../../js/components.js"></script>
|
||||||
<script type="text/javascript" src="../../ts/backbonejQuery.js"></script>
|
<script
|
||||||
|
type="text/javascript"
|
||||||
|
src="../../ts/backbone/backbonejQuery.js"
|
||||||
|
></script>
|
||||||
|
|
||||||
<script
|
<script
|
||||||
type="text/javascript"
|
type="text/javascript"
|
||||||
|
|
|
@ -66,11 +66,13 @@
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/javascript" src="../js/components.js"></script>
|
<script type="text/javascript" src="../js/components.js"></script>
|
||||||
<script type="text/javascript" src="../ts/backboneJquery.js"></script>
|
|
||||||
<script
|
<script
|
||||||
type="text/javascript"
|
type="text/javascript"
|
||||||
src="../js/reliable_trigger.js"
|
src="../ts/backbone/backboneJquery.js"
|
||||||
data-cover
|
></script>
|
||||||
|
<script
|
||||||
|
type="text/javascript"
|
||||||
|
src="../ts/backbone/reliable_trigger.js"
|
||||||
></script>
|
></script>
|
||||||
<script
|
<script
|
||||||
type="text/javascript"
|
type="text/javascript"
|
||||||
|
@ -107,8 +109,6 @@
|
||||||
|
|
||||||
<script type="text/javascript" src="views/whisper_view_test.js"></script>
|
<script type="text/javascript" src="views/whisper_view_test.js"></script>
|
||||||
|
|
||||||
<script type="text/javascript" src="reliable_trigger_test.js"></script>
|
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
window.Signal.conversationControllerStart();
|
window.Signal.conversationControllerStart();
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
// Copyright 2020 Signal Messenger, LLC
|
// Copyright 2020-2022 Signal Messenger, LLC
|
||||||
// SPDX-License-Identifier: AGPL-3.0-only
|
// SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
// we are requiring backbone in preload.js, and we need to tell backbone where
|
// we are requiring backbone in preload.js, and we need to tell backbone where
|
157
ts/backbone/reliable_trigger.ts
Normal file
157
ts/backbone/reliable_trigger.ts
Normal file
|
@ -0,0 +1,157 @@
|
||||||
|
// Copyright 2017-2022 Signal Messenger, LLC
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
import type * as Backbone from 'backbone';
|
||||||
|
|
||||||
|
type InternalBackboneEvent = {
|
||||||
|
callback: (...args: Array<unknown>) => unknown;
|
||||||
|
ctx: unknown;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* eslint-disable */
|
||||||
|
|
||||||
|
// This file was taken from Backbone and then modified. It does not conform to this
|
||||||
|
// project's standards.
|
||||||
|
|
||||||
|
// 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:
|
||||||
|
// 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
|
||||||
|
|
||||||
|
// And of course, we update the protoypes of Backbone.Model/Backbone.View as well as
|
||||||
|
// Backbone.Events itself
|
||||||
|
|
||||||
|
// Regular expression used to split event strings.
|
||||||
|
const eventSplitter = /\s+/;
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
const eventsApi = function (
|
||||||
|
obj: Backbone.Events,
|
||||||
|
name: string | Record<string, unknown>,
|
||||||
|
rest: ReadonlyArray<unknown>
|
||||||
|
) {
|
||||||
|
if (!name) return true;
|
||||||
|
|
||||||
|
// Handle event maps.
|
||||||
|
if (typeof name === 'object') {
|
||||||
|
for (const key in name) {
|
||||||
|
obj.trigger(key, name[key], ...rest);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle space separated event names.
|
||||||
|
if (eventSplitter.test(name)) {
|
||||||
|
const names = name.split(eventSplitter);
|
||||||
|
for (let i = 0, l = names.length; i < l; i++) {
|
||||||
|
obj.trigger(names[i], ...rest);
|
||||||
|
}
|
||||||
|
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).
|
||||||
|
const triggerEvents = function (
|
||||||
|
events: ReadonlyArray<InternalBackboneEvent>,
|
||||||
|
name: string,
|
||||||
|
args: Array<unknown>
|
||||||
|
) {
|
||||||
|
let ev,
|
||||||
|
i = -1,
|
||||||
|
l = events.length,
|
||||||
|
a1 = args[0],
|
||||||
|
a2 = args[1],
|
||||||
|
a3 = args[2];
|
||||||
|
const logError = function (error: unknown) {
|
||||||
|
window.SignalContext.log.error(
|
||||||
|
'Model caught error triggering',
|
||||||
|
name,
|
||||||
|
'event:',
|
||||||
|
error && error instanceof Error && error.stack ? error.stack : error
|
||||||
|
);
|
||||||
|
};
|
||||||
|
switch (args.length) {
|
||||||
|
case 0:
|
||||||
|
while (++i < l) {
|
||||||
|
try {
|
||||||
|
(ev = events[i]).callback.call(ev.ctx);
|
||||||
|
} catch (error) {
|
||||||
|
logError(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
case 1:
|
||||||
|
while (++i < l) {
|
||||||
|
try {
|
||||||
|
(ev = events[i]).callback.call(ev.ctx, a1);
|
||||||
|
} catch (error) {
|
||||||
|
logError(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
case 2:
|
||||||
|
while (++i < l) {
|
||||||
|
try {
|
||||||
|
(ev = events[i]).callback.call(ev.ctx, a1, a2);
|
||||||
|
} catch (error) {
|
||||||
|
logError(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
case 3:
|
||||||
|
while (++i < l) {
|
||||||
|
try {
|
||||||
|
(ev = events[i]).callback.call(ev.ctx, a1, a2, a3);
|
||||||
|
} catch (error) {
|
||||||
|
logError(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
default:
|
||||||
|
while (++i < l) {
|
||||||
|
try {
|
||||||
|
(ev = events[i]).callback.apply(ev.ctx, args);
|
||||||
|
} catch (error) {
|
||||||
|
logError(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 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).
|
||||||
|
function trigger<
|
||||||
|
T extends Backbone.Events & {
|
||||||
|
_events: undefined | Record<string, ReadonlyArray<InternalBackboneEvent>>;
|
||||||
|
}
|
||||||
|
>(this: T, name: string, ...args: Array<unknown>): T {
|
||||||
|
if (!this._events) return this;
|
||||||
|
if (!eventsApi(this, name, args)) return this;
|
||||||
|
const events = this._events[name];
|
||||||
|
const allEvents = this._events.all;
|
||||||
|
if (events) triggerEvents(events, name, args);
|
||||||
|
if (allEvents) triggerEvents(allEvents, name, [...arguments]);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
[
|
||||||
|
window.Backbone.Model.prototype,
|
||||||
|
window.Backbone.View.prototype,
|
||||||
|
window.Backbone.Collection.prototype,
|
||||||
|
window.Backbone.Events,
|
||||||
|
].forEach(proto => {
|
||||||
|
Object.assign(proto, { trigger });
|
||||||
|
});
|
|
@ -1,46 +1,23 @@
|
||||||
// Copyright 2017-2020 Signal Messenger, LLC
|
// Copyright 2017-2022 Signal Messenger, LLC
|
||||||
// SPDX-License-Identifier: AGPL-3.0-only
|
// SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
/* global Backbone */
|
import { assert } from 'chai';
|
||||||
|
import { Model } from 'backbone';
|
||||||
|
|
||||||
'use strict';
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
|
|
||||||
describe('ReliableTrigger', () => {
|
describe('reliable trigger', () => {
|
||||||
describe('trigger', () => {
|
describe('trigger', () => {
|
||||||
let Model;
|
let model: Model;
|
||||||
let model;
|
|
||||||
|
|
||||||
before(() => {
|
|
||||||
({ Model } = Backbone);
|
|
||||||
});
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
model = new Model();
|
model = new Model();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns successfully if this._events is falsey', () => {
|
it('returns successfully if this._events is falsey', () => {
|
||||||
model._events = null;
|
(model as any)._events = null;
|
||||||
model.trigger('click');
|
model.trigger('click');
|
||||||
});
|
});
|
||||||
it('handles map of events to trigger', () => {
|
|
||||||
let a = 0;
|
|
||||||
let b = 0;
|
|
||||||
|
|
||||||
model.on('a', arg => {
|
|
||||||
a = arg;
|
|
||||||
});
|
|
||||||
model.on('b', arg => {
|
|
||||||
b = arg;
|
|
||||||
});
|
|
||||||
|
|
||||||
model.trigger({
|
|
||||||
a: 1,
|
|
||||||
b: 2,
|
|
||||||
});
|
|
||||||
|
|
||||||
assert.strictEqual(a, 1);
|
|
||||||
assert.strictEqual(b, 2);
|
|
||||||
});
|
|
||||||
it('handles space-separated list of events to trigger', () => {
|
it('handles space-separated list of events to trigger', () => {
|
||||||
let a = false;
|
let a = false;
|
||||||
let b = false;
|
let b = false;
|
||||||
|
@ -112,7 +89,7 @@ describe('ReliableTrigger', () => {
|
||||||
assert.strictEqual(called, true);
|
assert.strictEqual(called, true);
|
||||||
});
|
});
|
||||||
it('calls clients with 1 arg', () => {
|
it('calls clients with 1 arg', () => {
|
||||||
let args;
|
let args: Array<unknown> = [];
|
||||||
model.on('event', (...eventArgs) => {
|
model.on('event', (...eventArgs) => {
|
||||||
args = eventArgs;
|
args = eventArgs;
|
||||||
});
|
});
|
||||||
|
@ -122,7 +99,7 @@ describe('ReliableTrigger', () => {
|
||||||
assert.strictEqual(args[0], 1);
|
assert.strictEqual(args[0], 1);
|
||||||
});
|
});
|
||||||
it('calls clients with 2 args', () => {
|
it('calls clients with 2 args', () => {
|
||||||
let args;
|
let args: Array<unknown> = [];
|
||||||
model.on('event', (...eventArgs) => {
|
model.on('event', (...eventArgs) => {
|
||||||
args = eventArgs;
|
args = eventArgs;
|
||||||
});
|
});
|
||||||
|
@ -133,7 +110,7 @@ describe('ReliableTrigger', () => {
|
||||||
assert.strictEqual(args[1], 2);
|
assert.strictEqual(args[1], 2);
|
||||||
});
|
});
|
||||||
it('calls clients with 3 args', () => {
|
it('calls clients with 3 args', () => {
|
||||||
let args;
|
let args: Array<unknown> = [];
|
||||||
model.on('event', (...eventArgs) => {
|
model.on('event', (...eventArgs) => {
|
||||||
args = eventArgs;
|
args = eventArgs;
|
||||||
});
|
});
|
||||||
|
@ -145,7 +122,7 @@ describe('ReliableTrigger', () => {
|
||||||
assert.strictEqual(args[2], 3);
|
assert.strictEqual(args[2], 3);
|
||||||
});
|
});
|
||||||
it('calls clients with 4+ args', () => {
|
it('calls clients with 4+ args', () => {
|
||||||
let args;
|
let args: Array<unknown> = [];
|
||||||
model.on('event', (...eventArgs) => {
|
model.on('event', (...eventArgs) => {
|
||||||
args = eventArgs;
|
args = eventArgs;
|
||||||
});
|
});
|
Loading…
Reference in a new issue