sonic-widget 2.2.3 → 2.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +114 -4
  2. package/dist/index.js +1246 -1246
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -16,7 +16,7 @@
16
16
  Use this script tag to get access to the widget:
17
17
 
18
18
  ```html
19
- <script src="https://cdn.jsdelivr.net/npm/sonic-widget@2.2.3/dist/index.min.js"></script>
19
+ <script src="https://cdn.jsdelivr.net/npm/sonic-widget@2.2.5/dist/index.min.js"></script>
20
20
  ```
21
21
 
22
22
  or
@@ -43,7 +43,7 @@ and adding path in `angular.json`
43
43
  2. Use this script tag to get access to the widget at initial html page:
44
44
 
45
45
  ```html
46
- <script src="https://cdn.jsdelivr.net/npm/sonic-widget@2.2.3/dist/index.min.js"></script>
46
+ <script src="https://cdn.jsdelivr.net/npm/sonic-widget@2.2.5/dist/index.min.js"></script>
47
47
  ```
48
48
 
49
49
  or
@@ -107,6 +107,117 @@ ngOnInit(): void {
107
107
  }
108
108
  ```
109
109
 
110
+ 7. Add service worker for widget to protect the multiple calls for tensorflow. So, it will have cache to reuse it. Here, I'm adding `service-worker.js` file at `src/assets` folder. So, below steps will be based on this file path.
111
+
112
+ ```javascript
113
+ const CACHE_NAME = 'demo-app-ai-tensorflow-models-cache-v1';
114
+ const urlsToCache = [
115
+ // coco ssd
116
+ 'https://storage.googleapis.com/tfjs-models/savedmodel/ssdlite_mobilenet_v2/model.json',
117
+
118
+ // document model
119
+ "https://teachablemachine.withgoogle.com/models/ikFAYzyyu/model.json",
120
+ "https://teachablemachine.withgoogle.com/models/ikFAYzyyu/metadata.json",
121
+
122
+ // mask model
123
+ "https://teachablemachine.withgoogle.com/models/_08Yck2dy/model.json",
124
+ "https://teachablemachine.withgoogle.com/models/_08Yck2dy/metadata.json",
125
+
126
+ // blazeface
127
+ 'https://tfhub.dev/tensorflow/tfjs-model/blazeface/1/default/1/model.json?tfjs-format=file',
128
+
129
+ // handpose model
130
+ "https://tfhub.dev/mediapipe/tfjs-model/handpose_3d/detector/full/1/model.json?tfjs-format=file",
131
+ "https://tfhub.dev/mediapipe/tfjs-model/handpose_3d/landmark/full/1/model.json?tfjs-format=file"
132
+ // Add more model files if necessary
133
+ ]
134
+
135
+ self.addEventListener('install', function(event) {
136
+ // Perform install steps
137
+ event.waitUntil(
138
+ caches.open(CACHE_NAME)
139
+ .then(function(cache) {
140
+ return cache.addAll(urlsToCache);
141
+ })
142
+ );
143
+ });
144
+
145
+ self.addEventListener('fetch', function(event) {
146
+ const request = event.request;
147
+
148
+ // Skip caching for requests from browser extensions
149
+ if (request.url.startsWith('chrome-extension://')) {
150
+ return;
151
+ }
152
+
153
+ event.respondWith(
154
+ caches.match(request)
155
+ .then(function(response) {
156
+ // Cache hit - return response
157
+ if (response) {
158
+ return response;
159
+ }
160
+
161
+ // Clone the request
162
+ const fetchRequest = request.clone();
163
+
164
+ return fetch(fetchRequest).then(
165
+ function(response) {
166
+ // Check if we received a valid response
167
+ if(!response || response.status !== 200 || response.type !== 'basic') {
168
+ return response;
169
+ }
170
+
171
+ // Clone the response
172
+ const responseToCache = response.clone();
173
+ // Save the response for future matching
174
+ caches.open(CACHE_NAME)
175
+ .then(function(cache) {
176
+ cache.put(request, responseToCache);
177
+ });
178
+
179
+ return response;
180
+ }
181
+ );
182
+ })
183
+ );
184
+ });
185
+ ```
186
+ 8. Use this script tag to get access to the service worker at initial html page:
187
+
188
+ ```html
189
+ <script src="src/assets/service-worker.js"></script>
190
+ ```
191
+
192
+ or
193
+
194
+ it can be added in `angular.json` by installing the npm package
195
+
196
+ ```json
197
+ "scripts": [ ..., "src/assets/service-worker.js" ]
198
+ ```
199
+
200
+ 9. Register the service worker at your required place:
201
+ ```ts
202
+ ngAfterViewInit(): void {
203
+ if ('serviceWorker' in navigator) {
204
+ window.addEventListener('load', () => {
205
+ navigator.serviceWorker.register('/demo-app/assets/service-worker.js').then(
206
+ (registration) => {
207
+ console.log(
208
+ 'ServiceWorker registered with scope: ',
209
+ registration.scope
210
+ );
211
+ },
212
+ (err) => {
213
+ console.error('ServiceWorker registration failed: ', err);
214
+ }
215
+ );
216
+ });
217
+ }
218
+ }
219
+ ```
220
+
110
221
  &nbsp;
111
222
 
112
223
  ## Implementation Details for Vanilla JavaScript:
@@ -130,7 +241,7 @@ ngOnInit(): void {
130
241
  4. Add a Script tag to the head tag or body tag. And use the latest version.
131
242
 
132
243
  ```html
133
- <script src="https://cdn.jsdelivr.net/npm/sonic-widget@2.2.3/dist/index.min.js"></script>
244
+ <script src="https://cdn.jsdelivr.net/npm/sonic-widget@2.2.5/dist/index.min.js"></script>
134
245
  ```
135
246
 
136
247
  5. Next, add the script tag at a necessary place. But add below the widget script.
@@ -169,7 +280,6 @@ ngOnInit(): void {
169
280
  _Here, the config variable is used to pass the data. And the container variable is to get the dom element of the id **`xxxx`** button and add it to the event listener. It checks if the button is clicked and sends the config object data to the **`sonic`** module.
170
281
  **`sonic.SonicWidget(config)`**_
171
282
 
172
-
173
283
  _You can get the logs with **`onMessage`** for all actions and stages.
174
284
  Check with **`data.code`**_
175
285