|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 Puter Technologies Inc. |
| 3 | + * |
| 4 | + * This file is part of Puter. |
| 5 | + * |
| 6 | + * Puter is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as published |
| 8 | + * by the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +const { FeatureBase } = require("../bases/FeatureBase"); |
| 21 | +const { TDetachable } = require("../traits/traits"); |
| 22 | + |
| 23 | +// NOTE: copied from src/backend/src/util/listenerutil.js, |
| 24 | +// which is now deprecated. |
| 25 | + |
| 26 | +class MultiDetachable extends FeatureBase { |
| 27 | + static FEATURES = [ |
| 28 | + require('../features/TraitsFeature'), |
| 29 | + ]; |
| 30 | + |
| 31 | + constructor() { |
| 32 | + this.delegates = []; |
| 33 | + this.detached_ = false; |
| 34 | + } |
| 35 | + |
| 36 | + add (delegate) { |
| 37 | + if ( this.detached_ ) { |
| 38 | + delegate.detach(); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + this.delegates.push(delegate); |
| 43 | + } |
| 44 | + |
| 45 | + static IMPLEMENTS = { |
| 46 | + [TDetachable]: { |
| 47 | + detach () { |
| 48 | + this.detached_ = true; |
| 49 | + for ( const delegate of this.delegates ) { |
| 50 | + delegate.detach(); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +class AlsoDetachable extends FeatureBase { |
| 58 | + static FEATURES = [ |
| 59 | + require('../features/TraitsFeature'), |
| 60 | + ]; |
| 61 | + |
| 62 | + constructor () { |
| 63 | + super(); |
| 64 | + this.also = () => {}; |
| 65 | + } |
| 66 | + |
| 67 | + also (also) { |
| 68 | + this.also = also; |
| 69 | + return this; |
| 70 | + } |
| 71 | + |
| 72 | + static IMPLEMENTS = { |
| 73 | + [TDetachable]: { |
| 74 | + detach () { |
| 75 | + this.detach_(); |
| 76 | + this.also(); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// TODO: this doesn't work, but I don't know why yet. |
| 83 | +class RemoveFromArrayDetachable extends AlsoDetachable { |
| 84 | + constructor (array, element) { |
| 85 | + super(); |
| 86 | + this.array = new WeakRef(array); |
| 87 | + this.element = element; |
| 88 | + } |
| 89 | + |
| 90 | + detach_ () { |
| 91 | + const array = this.array.deref(); |
| 92 | + if ( ! array ) return; |
| 93 | + const index = array.indexOf(this.element); |
| 94 | + if ( index !== -1 ) { |
| 95 | + array.splice(index, 1); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +module.exports = { |
| 101 | + MultiDetachable, |
| 102 | + RemoveFromArrayDetachable, |
| 103 | +}; |
0 commit comments