UNPKG

2.77 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2018 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.SearchBoxDebounce = exports.SearchBoxDebounceOptions = void 0;
19const event_1 = require("../../common/event");
20const disposable_1 = require("../../common/disposable");
21const debounce = require("lodash.debounce");
22var SearchBoxDebounceOptions;
23(function (SearchBoxDebounceOptions) {
24 /**
25 * The default debounce option.
26 */
27 SearchBoxDebounceOptions.DEFAULT = {
28 delay: 200
29 };
30})(SearchBoxDebounceOptions = exports.SearchBoxDebounceOptions || (exports.SearchBoxDebounceOptions = {}));
31/**
32 * It notifies the clients, once if the underlying search term has changed after a given amount of delay.
33 */
34class SearchBoxDebounce {
35 constructor(options) {
36 this.options = options;
37 this.disposables = new disposable_1.DisposableCollection();
38 this.emitter = new event_1.Emitter();
39 this.disposables.push(this.emitter);
40 this.handler = debounce(() => this.fireChanged(this.state), this.options.delay).bind(this);
41 }
42 append(input) {
43 if (input === undefined) {
44 this.reset();
45 return undefined;
46 }
47 if (this.state === undefined) {
48 this.state = input;
49 }
50 else {
51 if (input === '\b') {
52 this.state = this.state.length === 1 ? '' : this.state.substring(0, this.state.length - 1);
53 }
54 else {
55 this.state += input;
56 }
57 }
58 this.handler();
59 return this.state;
60 }
61 get onChanged() {
62 return this.emitter.event;
63 }
64 dispose() {
65 this.disposables.dispose();
66 }
67 fireChanged(value) {
68 this.emitter.fire(value);
69 }
70 reset() {
71 this.state = undefined;
72 this.fireChanged(undefined);
73 }
74}
75exports.SearchBoxDebounce = SearchBoxDebounce;
76//# sourceMappingURL=search-box-debounce.js.map
\No newline at end of file