<commit_before>package pman.ww;

import tannus.io.Signal;
import pman.ww.WorkerPacket;
import tannus.node.ChildProcess;
import js.html.Worker as NWorker;

import Slambda.fn;

class Boss {
    /* Constructor Function */
    public function new():Void {
        packetEvent = new Signal();
    }

/* === Instance Methods === */

    /**
      * initialize [this]
      */
    public function init():Boss {
        _listen();
        return this;
    }

    public function send(type:String, data:Dynamic, encoding:WorkerPacketEncoding=None):Void {
        return ;
    }

    public function kill():Void {
        return ;
    }

    /**
      * listen for messages
      */
    private function _onMessage(handler : Dynamic -> Void):Void {
        return ;
    }

    /**
      * register a packet event handler
      */
    public inline function onPacket(handler:WorkerPacket->Void, once:Bool=false):Void {
        packetEvent.on(handler, once);
    }

    /**
      * register an event handler for the given type
      */
    public inline function on<T>(type:String, handler:Dynamic->Void):Void {
        packetEvent.when(fn(_.type == type), handler);
    }

    /**
      * start listening for packets
      */
    private function _listen():Void {
        _onMessage(function(o : Dynamic) {
            if (isPacket( o )) {
                //packetEvent.call(cast o);
                var pack:WorkerPacket = cast o;
                pack = pack.decode();
                packetEvent.call( pack );
            }
        });
    }

    /**
      * create a packet
      */
    private function packet(type:String, data:Dynamic, encoding:WorkerPacketEncoding):WorkerPacket {
        var res:WorkerPacket = {
            type: type,
            data: data,
            encoding: None
        };
        res = res.encode( encoding );
        return res;
    }

    /**
      * determine whether the given anonymous object is a packet
      */
    private function isPacket(o : Dynamic):Bool {
        return (
            (o.type != null && (o.type is String)) &&
            (o.encoding != null && WorkerPacketEncoding.isWorkerPacketEncoding( o.encoding ))
        );
    }

/* === Instance Fields === */

    public var packetEvent : Signal<WorkerPacket>;

/* === Static Methods === */
<commit_msg>changed the way that listening for specific packet-types is handled in pman.ww.Boss<commit_after>package pman.ww;

import tannus.io.Signal;
import tannus.io.EventDispatcher;

import pman.ww.WorkerPacket;

import tannus.node.ChildProcess;

import js.html.Worker as NWorker;

import Slambda.fn;

class Boss {
    /* Constructor Function */
    public function new():Void {
        packetEvent = new Signal();
        _pb = new EventDispatcher();
        @:privateAccess _pb.__checkEvents = false;
    }

/* === Instance Methods === */

    /**
      * initialize [this]
      */
    public function init():Boss {
        _listen();
        return this;
    }

    public function send(type:String, data:Dynamic, encoding:WorkerPacketEncoding=None):Void {
        return ;
    }

    public function kill():Void {
        return ;
    }

    /**
      * listen for messages
      */
    private function _onMessage(handler : Dynamic -> Void):Void {
        return ;
    }

    /**
      * register a packet event handler
      */
    public inline function onPacket(handler:WorkerPacket->Void, once:Bool=false):Void {
        packetEvent.on(handler, once);
    }

    /**
      * register an event handler for the given type
      */
    public function on<T>(type:String, handler:T->Void):Void {
        _pb.on(type, handler);
    }
    public function once<T>(type:String, handler:T->Void):Void {
        _pb.once(type, handler);
    }
    public function off<T>(type:String, handler:T->Void):Void {
        _pb.off(type, handler);
    }

    /**
      * start listening for packets
      */
    private function _listen():Void {
        _onMessage(function(o : Dynamic) {
            if (isPacket( o )) {
                //packetEvent.call(cast o);
                var pack:WorkerPacket = cast o;
                pack = pack.decode();
                packetEvent.call( pack );
                _pb.dispatch(pack.type, pack.data);
            }
        });
    }

    /**
      * create a packet
      */
    private function packet(type:String, data:Dynamic, encoding:WorkerPacketEncoding):WorkerPacket {
        var res:WorkerPacket = {
            type: type,
            data: data,
            encoding: None
        };
        res = res.encode( encoding );
        return res;
    }

    /**
      * determine whether the given anonymous object is a packet
      */
    private function isPacket(o : Dynamic):Bool {
        return (
            (o.type != null && (o.type is String)) &&
            (o.encoding != null && WorkerPacketEncoding.isWorkerPacketEncoding( o.encoding ))
        );
    }

/* === Instance Fields === */

    public var packetEvent : Signal<WorkerPacket>;
    // packet broadcaster
    public var _pb : EventDispatcher;

/* === Static Methods === */

{'fraction_non_alphanumeric': 0.12111067349350138, 'fraction_numerical': 0.0, 'mean_word_length': 1.9876470588235293, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, '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': '5353717', 'n_tokens_mistral': 1431, 'n_tokens_neox': 1401, 'n_words': 347}