Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Receive broadcast data from external barcode scanner #67

Closed
ghost opened this issue Apr 6, 2022 · 7 comments
Closed

Receive broadcast data from external barcode scanner #67

ghost opened this issue Apr 6, 2022 · 7 comments
Assignees
Labels
bug wait for feedback made actions and wait for feedback from issuer

Comments

@ghost
Copy link

ghost commented Apr 6, 2022

Hi, is it possible to retrieve the data globally (because i have many input from the pages) when a hardware scanner captured the barcode? I have tested the sample code from the documentation but it does not work.

@bsorrentino
Copy link
Owner

Hi, is it possible to retrieve the data globally (because i have many input from the pages) when a hardware scanner captured the barcode? I have tested the sample code from the documentation but it does not work.

Hi @JustonJ99

I need more details.

  • is it possible to retrieve the data globally?

What does it mean ? The data is sent in an event handler

  • I have tested the sample code from the documentation but it does not work.

do u mean not work handling of barcode ?

@bsorrentino bsorrentino added the wait for feedback made actions and wait for feedback from issuer label Apr 6, 2022
@kmitdebus
Copy link

I think I get the same error ("its not working with external reader")

My Code:

window.broadcaster.addEventListener( 'de.someExternalBroadcast.SCAN_DATA', true, function( e ) { console.log( e ); });

broadcaster.js:84 Uncaught TypeError: Cannot set property type of #<Event> which has only a getter at Broadcaster.fireEvent (broadcaster.js:84:30) at <anonymous>:1:20

Broadcasted data is: {data: "25C6AE3E", secondaryData: null, type: "#41", typeExt: "HiTag1/S"}

@bsorrentino
Copy link
Owner

Hi @kmitdebus thanks for sharing infos, I'll investigate as soon as possible

@lgl017
Copy link

lgl017 commented May 10, 2024

I think I get the same error ("its not working with external reader")

My Code:

window.broadcaster.addEventListener( 'de.someExternalBroadcast.SCAN_DATA', true, function( e ) { console.log( e ); });

broadcaster.js:84 Uncaught TypeError: Cannot set property type of #<Event> which has only a getter at Broadcaster.fireEvent (broadcaster.js:84:30) at <anonymous>:1:20

Broadcasted data is: {data: "25C6AE3E", secondaryData: null, type: "#41", typeExt: "HiTag1/S"}

I get the same error too, i find the issue in js code:

Broadcaster.prototype.fireEvent = function (type, data) {
       if (!this._channelExists(type))
           return;
       var event = document.createEvent('Event');
       event.initEvent(type, false, false);
       if (data) {
           for (var i in data) {
               if (data.hasOwnProperty(i)) {
                   event[i] = data[i];
                 //^^^ will error when key is "type", "type" in event object is readonly
               }
           }
       }
       this._channelFire(event);
   };

to fix that, special handling key "type" to another

@bsorrentino
Copy link
Owner

Hi @lgl017 thanks for valuable feedback

Now there is another issue .... what's the best way to handle such problem:

  1. catch error and ignore it ( simplest )
  2. rename data.type property in event.dataType
  3. throw a more meaningful exception

I prefer the n. 2 what do you think ?

@bsorrentino bsorrentino self-assigned this May 10, 2024
bsorrentino added a commit that referenced this issue May 10, 2024
for backward compatibility copy the data's properties to Event skipping the ones already present in Event itself

solve #67
@bsorrentino
Copy link
Owner

bsorrentino commented May 10, 2024

I think I get the same error ("its not working with external reader")
My Code:
window.broadcaster.addEventListener( 'de.someExternalBroadcast.SCAN_DATA', true, function( e ) { console.log( e ); });
broadcaster.js:84 Uncaught TypeError: Cannot set property type of #<Event> which has only a getter at Broadcaster.fireEvent (broadcaster.js:84:30) at <anonymous>:1:20
Broadcasted data is: {data: "25C6AE3E", secondaryData: null, type: "#41", typeExt: "HiTag1/S"}

I get the same error too, i find the issue in js code:

Broadcaster.prototype.fireEvent = function (type, data) {
       if (!this._channelExists(type))
           return;
       var event = document.createEvent('Event');
       event.initEvent(type, false, false);
       if (data) {
           for (var i in data) {
               if (data.hasOwnProperty(i)) {
                   event[i] = data[i];
                 //^^^ will error when key is "type", "type" in event object is readonly
               }
           }
       }
       this._channelFire(event);
   };

to fix that, special handling key "type" to another

I've updated the fireEvent code

Broadcaster.prototype.fireEvent = function (type, data) {
        if (!this._channelExists(type))
            return;
        var event = new Event(type, { bubbles: false, cancelable: false });
        if (data) {
            event['data$'] = data; // fix issue #67
            // for backward compatibility
            for (var i in data) {
                if (data.hasOwnProperty(i) && event[i] === undefined) {
                    event[i] = data[i];
                }
            }
        }
        this._channelFire(event);
}

The solution is to store the given data object in a new Event.data$ property. For backward compatibility the data properties will be added to Event object skipping the ones already present in Event itself.
So in the case of type clashing to retrieve type value we have to access the data property:

// in the listener 
    var listener = function( e ) {
      console.log( "type:  ", e.data$.type  );
    }

bsorrentino added a commit that referenced this issue May 10, 2024
enforce property uniqueness

update #67
@bsorrentino
Copy link
Owner

bsorrentino commented May 11, 2024

fix released in 5.2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug wait for feedback made actions and wait for feedback from issuer
Projects
None yet
Development

No branches or pull requests

3 participants