saumitrabhave / qr-zbar-ane

ANE for QR Code Reader
MIT License
84 stars 23 forks source link

Is Scanner a static, singleton or does it have a permanent reference? #39

Closed napcat closed 9 years ago

napcat commented 10 years ago

I found a strange behavior...

I have two different classes. Witch class has her own instance of a scanner object with a function listening for a ScannerEvent.Scan event.

The problem is:

If I create an instance of ClassA and run a scan the function executed on a ScannerEvent.Scan is from ClassA.

If I start create and instance of ClassB after I created and instance of ClassA, and run a scan the function executed on the ScannerEvent.Scan if from Class A and not Class B.

This strange beahviour also happens if a swap the order (first ClassB and then ClassA).

ClassA { private function init() { try { scanner = new Scanner(); scanner.setTargetArea(512, "0xFF333333", "0xFFFFFFFF"); scanner.setConfig(Symbology.QRCODE, Config.ENABLE, 1); scanner.setConfig(Symbology.CODE128, Config.ENABLE, 1); scanner.setConfig(Symbology.EAN13, Config.ENABLE, 1);

            scanner.addEventListener(ScannerEvent.SCAN, this.onScannerCode);
        }
        catch (error:Error) { "error qrcode : " + error }

}

private function onScannerCode(e:ScannerEvent):void {

trace("CLASS A"); } }

ClassB { private function init() { try { scanner = new Scanner(); scanner.setTargetArea(512, "0xFF333333", "0xFFFFFFFF"); scanner.setConfig(Symbology.QRCODE, Config.ENABLE, 1); scanner.setConfig(Symbology.CODE128, Config.ENABLE, 1); scanner.setConfig(Symbology.EAN13, Config.ENABLE, 1);

            scanner.addEventListener(ScannerEvent.SCAN, this.onScannerCode);
        }
        catch (error:Error) { "error qrcode : " + error }

}

private function onScannerCode(e:ScannerEvent):void { trace("CLASS B"); } }

saumitrabhave commented 10 years ago

Hi napcat:

Thanks for pointing out, its a design flaw in current version.

Actually Scanner is neither static or singleton, but the underlying bridge ( extensionContenxt which calls methods and receives event from native code) is singleton and binds the events on the first instance of the Scanner only.

In next update I will make the Scanner class a singleton.

EDIT: I think it would be ugly for developer to use scanner as a singleton, reason being developer needs to call dispose() after the usage of scanner is done. Hence following design seems better.

  1. Create Scanner instance, use it.
  2. call dispose when usage is done.
  3. Create another Scanner whenever needed. (If there is an instance which is not disposed yet, throw exception mentioning the same.)
saumitrabhave commented 9 years ago

Fixed in New Design, You can try the current ANE (Android Only for now)