Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/snippets.js

Issue 29856564: Issue 6862 - Make Snippets module emit events (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Avoid duplicate notifications Created Aug. 15, 2018, 2:52 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/snippets.js
===================================================================
--- a/lib/snippets.js
+++ b/lib/snippets.js
@@ -16,53 +16,69 @@
*/
"use strict";
/**
* @fileOverview Snippets implementation.
*/
+const {EventEmitter} = require("./events");
const {Filter} = require("./filterClasses");
const singleCharacterEscapes = new Map([
["n", "\n"], ["r", "\r"], ["t", "\t"]
]);
let filters = new Set();
/**
* Container for snippet filters
* @class
*/
-let Snippets = {
+let Snippets = Object.assign(new EventEmitter(), {
/**
* Removes all known filters
*/
clear()
{
+ if (filters.size == 0)
+ return;
+
filters.clear();
+
+ this.emit("snippets.filtersCleared");
},
/**
* Add a new snippet filter
* @param {SnippetFilter} filter
*/
add(filter)
{
+ let {size} = filters;
+
filters.add(filter.text);
+
+ if (size != filters.size)
+ this.emit("snippets.filterAdded", filter);
},
/**
* Removes a snippet filter
* @param {SnippetFilter} filter
*/
remove(filter)
{
+ let {size} = filters;
+
filters.delete(filter.text);
+
+ if (size != filters.size)
+ this.emit("snippets.filterRemoved", filter);
},
/**
* Returns a list of all scripts active on a particular domain
* @param {string} domain
* @return {string[]}
*/
getScriptsForDomain(domain)
@@ -71,17 +87,17 @@
for (let text of filters)
{
let filter = Filter.fromText(text);
if (filter.isActiveOnDomain(domain))
result.push(filter.script);
}
return result;
}
-};
+});
exports.Snippets = Snippets;
/**
* Parses a script and returns a list of all its commands and their arguments
* @param {string} script
* @return {Array.<string[]>}
*/
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld