From 1a6d92ee4e06a5c360006bbe37bc5051698201b1 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Wed, 24 Jun 2015 12:02:40 -0700 Subject: [PATCH] [Core] Add topic service Add topic service to support listeners, such as the mutation listener added for WTD-1329. --- platform/core/src/services/Throttle.js | 21 +++++++ platform/core/src/services/Topic.js | 87 ++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 platform/core/src/services/Topic.js diff --git a/platform/core/src/services/Throttle.js b/platform/core/src/services/Throttle.js index 0c86a403c7..c0493a733a 100644 --- a/platform/core/src/services/Throttle.js +++ b/platform/core/src/services/Throttle.js @@ -1,3 +1,24 @@ +/***************************************************************************** + * Open MCT Web, Copyright (c) 2014-2015, United States Government + * as represented by the Administrator of the National Aeronautics and Space + * Administration. All rights reserved. + * + * Open MCT Web is licensed under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * Open MCT Web includes source code licensed under additional open source + * licenses. See the Open Source Licenses file (LICENSES.md) included with + * this source code distribution or the Licensing information page available + * at runtime from the About dialog for additional information. + *****************************************************************************/ /*global define*/ define( diff --git a/platform/core/src/services/Topic.js b/platform/core/src/services/Topic.js new file mode 100644 index 0000000000..894274b71c --- /dev/null +++ b/platform/core/src/services/Topic.js @@ -0,0 +1,87 @@ +/***************************************************************************** + * Open MCT Web, Copyright (c) 2014-2015, United States Government + * as represented by the Administrator of the National Aeronautics and Space + * Administration. All rights reserved. + * + * Open MCT Web is licensed under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * Open MCT Web includes source code licensed under additional open source + * licenses. See the Open Source Licenses file (LICENSES.md) included with + * this source code distribution or the Licensing information page available + * at runtime from the About dialog for additional information. + *****************************************************************************/ +/*global define*/ + +define( + [], + function () { + "use strict"; + + /** + * The `topic` service provides a way to create both named, + * shared listeners and anonymous, private listeners. + * + * Usage: + * + * ``` + * var t = topic('foo'); // Use/create a named topic + * t.listen(function () { ... }); + * t.notify({ some: "message" }); + * ``` + * + * Named topics are shared; multiple calls to `topic` + * with the same argument will return a single object instance. + * Anonymous topics (where `topic`has been called with no + * arguments) are private; each call returns a new instance. + * + * @returns {Function} + */ + function Topic() { + var topics = {}; + + function createTopic() { + var listeners = []; + + return { + listen: function (listener) { + listeners.push(listener); + return function unlisten() { + listeners = listeners.filter(function (l) { + return l !== listener; + }); + }; + }, + notify: function (message) { + listeners.forEach(function (listener) { + listener(message); + }); + } + }; + } + + /** + * Use and (if necessary) create a new topic. + * @param {string} [key] name of the topic to use + */ + return function (key) { + if (arguments.length < 1) { + return createTopic(); + } else { + topics[key] = topics[key] || createTopic(); + return topics[key]; + } + }; + } + + return Topic; + } +);