Skip to content

Commit 1c8f64a

Browse files
Removed static class JsInteropClass, made static functions non-static, added helperclass to be able to reference the BarcodeReaderInterop instance. This makes it thread safe
1 parent 3c67bb8 commit 1c8f64a

File tree

4 files changed

+66
-70
lines changed

4 files changed

+66
-70
lines changed

BlazorBarcodeScanner.ZXing.JS/BarcodeReader.razor.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public partial class BarcodeReader : ComponentBase, IDisposable, IAsyncDisposabl
7979
public EventCallback<DecodingChangedArgs> OnDecodingChanged { get; set; }
8080

8181
private bool _isDecoding = false;
82+
private DotNetObjectReference<BarcodeReaderInterop>? dotNetHelper;
8283
public bool IsDecoding
8384
{
8485
get => _isDecoding;
@@ -135,17 +136,19 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
135136
{
136137
if (firstRender) {
137138
_backend = new BarcodeReaderInterop(JSRuntime);
139+
dotNetHelper = DotNetObjectReference.Create(_backend);
140+
await JSRuntime.InvokeVoidAsync("Helpers.setDotNetHelper", dotNetHelper);
138141
try
139142
{
140143
_DecodedPictureCapture = DecodedPictureCapture;
141144
await _backend.SetLastDecodedPictureFormat(DecodedPictureCapture ? "image/jpeg" : null);
142145

143146
await GetVideoInputDevicesAsync();
144147

145-
BarcodeReaderInterop.BarcodeReceived += ReceivedBarcodeText;
146-
BarcodeReaderInterop.ErrorReceived += ReceivedErrorMessage;
147-
BarcodeReaderInterop.DecodingStarted += DecodingStarted;
148-
BarcodeReaderInterop.DecodingStopped += DecodingStopped;
148+
_backend.BarcodeReceived += ReceivedBarcodeText;
149+
_backend.ErrorReceived += ReceivedErrorMessage;
150+
_backend.DecodingStarted += DecodingStarted;
151+
_backend.DecodingStopped += DecodingStopped;
149152

150153
if (StartCameraAutomatically && _videoInputDevices.Count > 0)
151154
{
@@ -179,11 +182,11 @@ public async ValueTask DisposeAsync()
179182
try
180183
{
181184
await StopDecoding();
182-
183-
BarcodeReaderInterop.BarcodeReceived -= ReceivedBarcodeText;
184-
BarcodeReaderInterop.ErrorReceived -= ReceivedErrorMessage;
185-
BarcodeReaderInterop.DecodingStarted -= DecodingStarted;
186-
BarcodeReaderInterop.DecodingStopped -= DecodingStopped;
185+
186+
_backend.BarcodeReceived -= ReceivedBarcodeText;
187+
_backend.ErrorReceived -= ReceivedErrorMessage;
188+
_backend.DecodingStarted -= DecodingStarted;
189+
_backend.DecodingStopped -= DecodingStopped;
187190
}
188191
catch (Exception ex)
189192
{
@@ -238,7 +241,7 @@ public async Task<string> CaptureLastDecodedPicture()
238241

239242
public async Task StopDecoding()
240243
{
241-
BarcodeReaderInterop.OnBarcodeReceived(string.Empty);
244+
_backend.OnBarcodeReceived(string.Empty);
242245
await _backend.StopDecoding();
243246
StateHasChanged();
244247
}

BlazorBarcodeScanner.ZXing.JS/BarcodeReaderInterop.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,9 @@ private async Task<string> PictureGetMarshalled(string source)
133133
return await jSRuntime.InvokeAsync<string>("BlazorBarcodeScanner.pictureGetBase64", source);
134134
}
135135

136-
private static string lastCode = string.Empty;
137-
public static void OnBarcodeReceived(string barcodeText)
136+
private string lastCode = string.Empty;
137+
[JSInvokable]
138+
public void OnBarcodeReceived(string barcodeText)
138139
{
139140
if (string.IsNullOrEmpty(barcodeText))
140141
{
@@ -154,7 +155,8 @@ public static void OnBarcodeReceived(string barcodeText)
154155

155156
BarcodeReceived?.Invoke(args);
156157
}
157-
public static void OnErrorReceived(Exception exception)
158+
[JSInvokable]
159+
public void OnErrorReceived(Exception exception)
158160
{
159161
if (string.IsNullOrEmpty(exception.Message))
160162
{
@@ -168,16 +170,17 @@ public static void OnErrorReceived(Exception exception)
168170

169171
ErrorReceived?.Invoke(args);
170172
}
171-
172-
public static void OnNotFoundReceived()
173+
[JSInvokable]
174+
public void OnNotFoundReceived()
173175
{
174176
if (!string.IsNullOrEmpty(lastCode))
175177
{
176178
lastCode = string.Empty;
177179
BarcodeNotFound?.Invoke();
178180
}
179181
}
180-
public static void OnDecodingStarted(string deviceId)
182+
[JSInvokable]
183+
public void OnDecodingStarted(string deviceId)
181184
{
182185
if (string.IsNullOrEmpty(deviceId))
183186
{
@@ -191,8 +194,8 @@ public static void OnDecodingStarted(string deviceId)
191194

192195
DecodingStarted?.Invoke(args);
193196
}
194-
195-
public static void OnDecodingStopped(string deviceId)
197+
[JSInvokable]
198+
public void OnDecodingStopped(string deviceId)
196199
{
197200
if (string.IsNullOrEmpty(deviceId))
198201
{
@@ -207,13 +210,13 @@ public static void OnDecodingStopped(string deviceId)
207210
DecodingStopped?.Invoke(args);
208211
}
209212

210-
public static event BarcodeReceivedEventHandler BarcodeReceived;
211-
public static event ErrorReceivedEventHandler ErrorReceived;
213+
public event BarcodeReceivedEventHandler BarcodeReceived;
214+
public event ErrorReceivedEventHandler ErrorReceived;
212215

213-
public static event DecodingStartedEventHandler DecodingStarted;
214-
public static event DecodingStoppedEventHandler DecodingStopped;
216+
public event DecodingStartedEventHandler DecodingStarted;
217+
public event DecodingStoppedEventHandler DecodingStopped;
215218

216-
public static event Action BarcodeNotFound;
219+
public event Action BarcodeNotFound;
217220
}
218221
public class ErrorReceivedEventArgs : EventArgs {
219222
public string Message { get; set; }

BlazorBarcodeScanner.ZXing.JS/JsInteropClass.cs

Lines changed: 0 additions & 41 deletions
This file was deleted.

BlazorBarcodeScanner.ZXing.JS/wwwroot/BlazorBarcodeScanner.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,35 @@
11
console.log("Init BlazorBarcodeScanner");
2+
3+
class Helpers {
4+
static dotNetHelper;
5+
6+
static setDotNetHelper(value) {
7+
Helpers.dotNetHelper = value;
8+
}
9+
10+
static async receiveBarcode(text) {
11+
await Helpers.dotNetHelper.invokeMethodAsync('OnBarcodeReceived', text);
12+
}
13+
14+
static async receiveError(err) {
15+
await Helpers.dotNetHelper.invokeMethodAsync('OnErrorReceived', err);
16+
}
17+
18+
static async receiveNotFound() {
19+
await Helpers.dotNetHelper.invokeMethodAsync('OnNotFoundReceived');
20+
}
21+
22+
static async decodingStarted(deviceId) {
23+
await Helpers.dotNetHelper.invokeMethodAsync('OnDecodingStarted', deviceId);
24+
}
25+
26+
static async decodingStopped(deviceId) {
27+
await Helpers.dotNetHelper.invokeMethodAsync('OnDecodingStopped', deviceId);
28+
}
29+
}
30+
31+
window.Helpers = Helpers;
32+
233
async function mediaStreamSetTorch(track, onOff) {
334
await track.applyConstraints({
435
advanced: [{
@@ -99,20 +130,20 @@ window.BlazorBarcodeScanner = {
99130
if (this.lastPictureDecodedFormat) {
100131
this.lastPictureDecoded = this.codeReader.captureCanvas.toDataURL(this.lastPictureDecodedFormat);
101132
}
102-
DotNet.invokeMethodAsync('BlazorBarcodeScanner.ZXing.JS', 'ReceiveBarcode', result.text)
133+
Helpers.receiveBarcode(result.text)
103134
.then(message => {
104135
console.log(message);
105136
});
106137
}
107138
if (err && !(err instanceof ZXing.NotFoundException)) {
108-
DotNet.invokeMethodAsync('BlazorBarcodeScanner.ZXing.JS', 'ReceiveError', err)
139+
Helpers.receiveError(err)
109140
.then(message => {
110141
console.log(message);
111142
});
112143
}
113144
if (err && (err instanceof ZXing.NotFoundException)) {
114145
this.lastPictureDecoded = undefined;
115-
DotNet.invokeMethodAsync('BlazorBarcodeScanner.ZXing.JS', 'ReceiveNotFound');
146+
Helpers.receiveNotFound();
116147
}
117148
});
118149

@@ -123,15 +154,15 @@ window.BlazorBarcodeScanner = {
123154
advanced: [{ torch: true }] // or false to turn off the torch
124155
}); */
125156
console.log(`Started continous decode from camera with id ${this.selectedDeviceId}`);
126-
DotNet.invokeMethodAsync('BlazorBarcodeScanner.ZXing.JS', 'DecodingStarted', this.selectedDeviceId)
157+
Helpers.decodingStarted(this.selectedDeviceId)
127158
},
128159
stopDecoding: function () {
129160
this.codeReader.reset();
130-
DotNet.invokeMethodAsync('BlazorBarcodeScanner.ZXing.JS', 'ReceiveBarcode', '')
161+
Helpers.receiveBarcode('')
131162
.then(message => {
132163
console.log(message);
133164
});
134-
DotNet.invokeMethodAsync('BlazorBarcodeScanner.ZXing.JS', 'DecodingStopped', this.selectedDeviceId)
165+
Helpers.decodingStopped(this.selectedDeviceId)
135166
console.log('Reset camera stream.');
136167
},
137168
setTorchOn: function () {

0 commit comments

Comments
 (0)