<commit_before>import EventTarget from '../../core/EventTarget';
import { ServiceWorkerGlobalScope } from '../../traits';
import validateTrait from '../../core/validateTrait';
import getRegistration from './getRegistration';

const onMessageHandler = function(event) {
    if (event.data.type === 'ServiceWorkerEventTarget') {
        this.emit(event.data.event, event.data.data);
    } else {
        console.log(`[ServiceWorker${navigator ? 'Client' : ''}] Ignoring event`, event);
    }
};

const ServiceWorkerEventTarget = {

    emit(event, data) {
        const message = {
            type: 'ServiceWorkerEventTarget', event, data,
        };

        if (self && validateTrait(self, ServiceWorkerGlobalScope)) {
            self.postMessage(message);
        } else {
            getRegistration().then(worker => worker.postMessage(message));
        }

        super.emit(event, data);
    },

    constructor(...args) {
        super.constructor(...args);

        const messageTarget = navigator ? getRegistration() : Promise.resolve(self);


        messageTarget.then(target => {
            target.onmessage = onMessageHandler.bind(this);
        });

        return this;
    },

    __proto__: EventTarget,
};

export default ServiceWorkerEventTarget;<commit_msg>⚙️  messages can't be posted on the worker it self #29<commit_after>import EventTarget from '../../core/EventTarget';
import { ServiceWorkerGlobalScope } from '../../traits';
import validateTrait from '../../core/validateTrait';
import getRegistration from './getRegistration';

const onMessageHandler = function(event) {
    if (event.data.type === 'ServiceWorkerEventTarget') {
        this.emit(event.data.event, event.data.data);
    } else {
        console.log(`[ServiceWorker${navigator ? 'Client' : ''}] Ignoring event`, event);
    }
};

const ServiceWorkerEventTarget = {

    emit(event, data) {
        const message = {
            type: 'ServiceWorkerEventTarget', event, data,
        };

        if (self && validateTrait(self, ServiceWorkerGlobalScope)) {
            self.clients.matchAll().then(clients => clients.forEach(client => client.postMessage(message)));
        } else {
            getRegistration().then(worker => worker.postMessage(message));
        }

        super.emit(event, data);
    },

    constructor(...args) {
        super.constructor(...args);

        const messageTarget = navigator ? getRegistration() : Promise.resolve(self);


        messageTarget.then(target => {
            target.onmessage = onMessageHandler.bind(this);
        });

        return this;
    },

    __proto__: EventTarget,
};

export default ServiceWorkerEventTarget;
{'fraction_non_alphanumeric': 0.14012738853503184, 'fraction_numerical': 0.0007493443237167478, 'mean_word_length': 2.781869688385269, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5891017', 'n_tokens_mistral': 720, 'n_tokens_neox': 704, 'n_words': 145}