@tingping/web 0.0.0-stage → 0.1.0
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.
- package/LICENSE +202 -0
- package/NOTICE +4 -0
- package/README.md +73 -2
- package/dist/cjs/device-id.d.ts +13 -0
- package/dist/cjs/device-id.d.ts.map +1 -0
- package/dist/cjs/device-id.js +40 -0
- package/dist/cjs/device-id.js.map +1 -0
- package/dist/cjs/index.d.ts +6 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +27 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/provider.d.ts +55 -0
- package/dist/cjs/provider.d.ts.map +1 -0
- package/dist/cjs/provider.js +178 -0
- package/dist/cjs/provider.js.map +1 -0
- package/dist/cjs/push.d.ts +26 -0
- package/dist/cjs/push.d.ts.map +1 -0
- package/dist/cjs/push.js +105 -0
- package/dist/cjs/push.js.map +1 -0
- package/dist/cjs/sw-handlers.d.ts +53 -0
- package/dist/cjs/sw-handlers.d.ts.map +1 -0
- package/dist/cjs/sw-handlers.js +113 -0
- package/dist/cjs/sw-handlers.js.map +1 -0
- package/dist/esm/device-id.d.ts +13 -0
- package/dist/esm/device-id.d.ts.map +1 -0
- package/dist/esm/device-id.js +36 -0
- package/dist/esm/device-id.js.map +1 -0
- package/dist/esm/index.d.ts +6 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +6 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/provider.d.ts +55 -0
- package/dist/esm/provider.d.ts.map +1 -0
- package/dist/esm/provider.js +174 -0
- package/dist/esm/provider.js.map +1 -0
- package/dist/esm/push.d.ts +26 -0
- package/dist/esm/push.d.ts.map +1 -0
- package/dist/esm/push.js +97 -0
- package/dist/esm/push.js.map +1 -0
- package/dist/esm/sw-handlers.d.ts +53 -0
- package/dist/esm/sw-handlers.d.ts.map +1 -0
- package/dist/esm/sw-handlers.js +105 -0
- package/dist/esm/sw-handlers.js.map +1 -0
- package/dist/sw.js +97 -0
- package/package.json +69 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 InnvoSols Technologies
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,74 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @tingping/web
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
tingping for React web apps. It delivers notifications in real time over a WebSocket while the app is open, and through Web Push (VAPID) when it is closed. It needs no Firebase project and no third-party push account: your own `@tingping/core` or `tingping-core` server does the sending.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @tingping/web
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## 1. Serve the service worker
|
|
10
|
+
|
|
11
|
+
The package ships a ready-made service worker. Copy it to your site root so it controls the whole origin:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
cp node_modules/@tingping/web/dist/sw.js public/sw.js
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Already have a service worker? Copy the file under another name instead, and load it from yours with `importScripts('/tingping-sw.js')`. Optional settings go on `self.TINGPING_SW_OPTIONS = { fallbackTitle, badge }` before the import. Then pass `serviceWorkerUrl` to the provider.
|
|
18
|
+
|
|
19
|
+
## 2. Wrap your app
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
import { TingPingProvider } from '@tingping/web';
|
|
23
|
+
|
|
24
|
+
export function App() {
|
|
25
|
+
const { user, accessToken } = useAuth(); // your auth
|
|
26
|
+
return (
|
|
27
|
+
<TingPingProvider
|
|
28
|
+
serverUrl="https://api.example.com/tingping"
|
|
29
|
+
userId={user?.id} // nothing connects while logged out
|
|
30
|
+
tenantId={user?.orgId}
|
|
31
|
+
authToken={accessToken}
|
|
32
|
+
topics={['payments']}
|
|
33
|
+
onNotificationTap={(frame) => navigate(frame.notification?.click_action ?? '/')}
|
|
34
|
+
>
|
|
35
|
+
<Routes />
|
|
36
|
+
</TingPingProvider>
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## 3. Use it
|
|
42
|
+
|
|
43
|
+
```tsx
|
|
44
|
+
import { useTingPing } from '@tingping/web';
|
|
45
|
+
|
|
46
|
+
function Bell() {
|
|
47
|
+
const { isConnected, notifications, pushPermission, requestPushPermission, clearNotifications } = useTingPing();
|
|
48
|
+
return (
|
|
49
|
+
<div>
|
|
50
|
+
<span title={isConnected ? 'live' : 'offline'}>🔔 {notifications.length}</span>
|
|
51
|
+
{pushPermission === 'default' && <button onClick={requestPushPermission}>Enable notifications</button>}
|
|
52
|
+
<button onClick={clearNotifications}>Clear</button>
|
|
53
|
+
</div>
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Ask for permission from a click, as above. Browsers block or silently ignore permission prompts that appear without a user gesture, which is why `autoRequestPermission` defaults to false.
|
|
59
|
+
|
|
60
|
+
The context also provides `status`, `lastNotification`, `error`, `deviceId`, `pushEnabled` and `unregister()`. Call `unregister()` on logout to remove the device and its push subscription.
|
|
61
|
+
|
|
62
|
+
## How delivery works
|
|
63
|
+
|
|
64
|
+
- **App open:** frames arrive over the WebSocket, are ACKed and de-duplicated, and are added to `notifications`. `onNotification` fires for each one.
|
|
65
|
+
- **App closed or in the background:**
|
|
66
|
+
- The server sends a Web Push message.
|
|
67
|
+
- The service worker shows it as a system notification. It also forwards it to any open tabs, so they stay in sync.
|
|
68
|
+
- Data-only events are passed silently to visible tabs. When no tab is on screen, the worker shows a quiet notification instead, because browsers require one for every push.
|
|
69
|
+
- **Notification clicked:** an open tab is focused and gets `onNotificationTap`, with no reload. If no tab is open, the worker opens `click_action`.
|
|
70
|
+
- **Device offline:** the server buffers frames and replays them on reconnect.
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
Apache License 2.0. Copyright 2026 InnvoSols Technologies. The `LICENSE` and `NOTICE` files ship with the package.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Minimal synchronous key/value storage (`localStorage` fits). */
|
|
2
|
+
export interface KeyValueStorage {
|
|
3
|
+
getItem(key: string): string | null;
|
|
4
|
+
setItem(key: string, value: string): void;
|
|
5
|
+
}
|
|
6
|
+
export declare const DEVICE_ID_STORAGE_KEY = "tingping:device_id";
|
|
7
|
+
/**
|
|
8
|
+
* Returns this browser's stable device id, creating and persisting one on first
|
|
9
|
+
* use. The id is the device `token` registered with tingping and the WebSocket
|
|
10
|
+
* `device_id`; every tab of the browser shares it.
|
|
11
|
+
*/
|
|
12
|
+
export declare function getOrCreateDeviceId(storage?: KeyValueStorage | null, key?: string): string;
|
|
13
|
+
//# sourceMappingURL=device-id.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"device-id.d.ts","sourceRoot":"","sources":["../../src/device-id.ts"],"names":[],"mappings":"AAEA,mEAAmE;AACnE,MAAM,WAAW,eAAe;IAC9B,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IACpC,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CAC3C;AAED,eAAO,MAAM,qBAAqB,uBAAuB,CAAC;AAY1D;;;;GAIG;AACH,wBAAgB,mBAAmB,CACjC,OAAO,GAAE,eAAe,GAAG,IAAuB,EAClD,GAAG,GAAE,MAA8B,GAClC,MAAM,CAcR"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEVICE_ID_STORAGE_KEY = void 0;
|
|
4
|
+
exports.getOrCreateDeviceId = getOrCreateDeviceId;
|
|
5
|
+
const client_core_1 = require("@tingping/client-core");
|
|
6
|
+
exports.DEVICE_ID_STORAGE_KEY = 'tingping:device_id';
|
|
7
|
+
function defaultStorage() {
|
|
8
|
+
try {
|
|
9
|
+
const storage = globalThis.localStorage;
|
|
10
|
+
return storage ?? null;
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
// Accessing localStorage throws in some privacy modes.
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Returns this browser's stable device id, creating and persisting one on first
|
|
19
|
+
* use. The id is the device `token` registered with tingping and the WebSocket
|
|
20
|
+
* `device_id`; every tab of the browser shares it.
|
|
21
|
+
*/
|
|
22
|
+
function getOrCreateDeviceId(storage = defaultStorage(), key = exports.DEVICE_ID_STORAGE_KEY) {
|
|
23
|
+
try {
|
|
24
|
+
const existing = storage?.getItem(key);
|
|
25
|
+
if (existing)
|
|
26
|
+
return existing;
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
// Fall through and mint a new id.
|
|
30
|
+
}
|
|
31
|
+
const id = (0, client_core_1.randomDeviceId)('web');
|
|
32
|
+
try {
|
|
33
|
+
storage?.setItem(key, id);
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
// Quota or privacy mode: the id still works for this page's lifetime.
|
|
37
|
+
}
|
|
38
|
+
return id;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=device-id.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"device-id.js","sourceRoot":"","sources":["../../src/device-id.ts"],"names":[],"mappings":";;;;AAAA,uDAAuD;AAQ1C,QAAA,qBAAqB,GAAG,oBAAoB,CAAC;AAE1D,SAAS,cAAc;IACrB,IAAI,CAAC;QACH,MAAM,OAAO,GAAI,UAAiD,CAAC,YAAY,CAAC;QAChF,OAAO,OAAO,IAAI,IAAI,CAAC;IACzB,CAAC;IAAC,MAAM,CAAC;QACP,uDAAuD;QACvD,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,6BACE,OAAO,GAA2B,cAAc,EAAE,EAClD,GAAG,GAAW,QAAA,qBAAqB;IAEnC,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC;QACvC,IAAI,QAAQ;YAAE,OAAO,QAAQ,CAAC;IAChC,CAAC;IAAC,MAAM,CAAC;QACP,kCAAkC;IACpC,CAAC;IACD,MAAM,EAAE,GAAG,IAAA,4BAAc,EAAC,KAAK,CAAC,CAAC;IACjC,IAAI,CAAC;QACH,OAAO,EAAE,OAAO,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;IAC5B,CAAC;IAAC,MAAM,CAAC;QACP,sEAAsE;IACxE,CAAC;IACD,OAAO,EAAE,CAAC;AACZ,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { TingPingProvider, useTingPing, type TingPingContextValue, type TingPingProviderProps } from './provider.js';
|
|
2
|
+
export { currentPushPermission, fetchVapidPublicKey, isPushSupported, subscribeToPush, unsubscribeFromPush, urlBase64ToUint8Array, type PushPermission, type SubscribeOptions, } from './push.js';
|
|
3
|
+
export { DEVICE_ID_STORAGE_KEY, getOrCreateDeviceId, type KeyValueStorage } from './device-id.js';
|
|
4
|
+
export { handleNotificationClick, handlePush, notificationOptions, parsePushPayload, resolveClickUrl, SW_MESSAGE, type SwClientLike, type SwOptions, type SwScopeLike, } from './sw-handlers.js';
|
|
5
|
+
export { TingPingClient, TingPingClientError, type ConnectionStatus, type NotificationFrame, type NotificationPayload, type TingPingClientOptions, type WebPushSubscriptionJSON, } from '@tingping/client-core';
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,KAAK,oBAAoB,EAAE,KAAK,qBAAqB,EAAE,MAAM,eAAe,CAAC;AACrH,OAAO,EACL,qBAAqB,EACrB,mBAAmB,EACnB,eAAe,EACf,eAAe,EACf,mBAAmB,EACnB,qBAAqB,EACrB,KAAK,cAAc,EACnB,KAAK,gBAAgB,GACtB,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,qBAAqB,EAAE,mBAAmB,EAAE,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAClG,OAAO,EACL,uBAAuB,EACvB,UAAU,EACV,mBAAmB,EACnB,gBAAgB,EAChB,eAAe,EACf,UAAU,EACV,KAAK,YAAY,EACjB,KAAK,SAAS,EACd,KAAK,WAAW,GACjB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACL,cAAc,EACd,mBAAmB,EACnB,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,EACtB,KAAK,mBAAmB,EACxB,KAAK,qBAAqB,EAC1B,KAAK,uBAAuB,GAC7B,MAAM,uBAAuB,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TingPingClientError = exports.TingPingClient = exports.SW_MESSAGE = exports.resolveClickUrl = exports.parsePushPayload = exports.notificationOptions = exports.handlePush = exports.handleNotificationClick = exports.getOrCreateDeviceId = exports.DEVICE_ID_STORAGE_KEY = exports.urlBase64ToUint8Array = exports.unsubscribeFromPush = exports.subscribeToPush = exports.isPushSupported = exports.fetchVapidPublicKey = exports.currentPushPermission = exports.useTingPing = exports.TingPingProvider = void 0;
|
|
4
|
+
var provider_js_1 = require("./provider.js");
|
|
5
|
+
Object.defineProperty(exports, "TingPingProvider", { enumerable: true, get: function () { return provider_js_1.TingPingProvider; } });
|
|
6
|
+
Object.defineProperty(exports, "useTingPing", { enumerable: true, get: function () { return provider_js_1.useTingPing; } });
|
|
7
|
+
var push_js_1 = require("./push.js");
|
|
8
|
+
Object.defineProperty(exports, "currentPushPermission", { enumerable: true, get: function () { return push_js_1.currentPushPermission; } });
|
|
9
|
+
Object.defineProperty(exports, "fetchVapidPublicKey", { enumerable: true, get: function () { return push_js_1.fetchVapidPublicKey; } });
|
|
10
|
+
Object.defineProperty(exports, "isPushSupported", { enumerable: true, get: function () { return push_js_1.isPushSupported; } });
|
|
11
|
+
Object.defineProperty(exports, "subscribeToPush", { enumerable: true, get: function () { return push_js_1.subscribeToPush; } });
|
|
12
|
+
Object.defineProperty(exports, "unsubscribeFromPush", { enumerable: true, get: function () { return push_js_1.unsubscribeFromPush; } });
|
|
13
|
+
Object.defineProperty(exports, "urlBase64ToUint8Array", { enumerable: true, get: function () { return push_js_1.urlBase64ToUint8Array; } });
|
|
14
|
+
var device_id_js_1 = require("./device-id.js");
|
|
15
|
+
Object.defineProperty(exports, "DEVICE_ID_STORAGE_KEY", { enumerable: true, get: function () { return device_id_js_1.DEVICE_ID_STORAGE_KEY; } });
|
|
16
|
+
Object.defineProperty(exports, "getOrCreateDeviceId", { enumerable: true, get: function () { return device_id_js_1.getOrCreateDeviceId; } });
|
|
17
|
+
var sw_handlers_js_1 = require("./sw-handlers.js");
|
|
18
|
+
Object.defineProperty(exports, "handleNotificationClick", { enumerable: true, get: function () { return sw_handlers_js_1.handleNotificationClick; } });
|
|
19
|
+
Object.defineProperty(exports, "handlePush", { enumerable: true, get: function () { return sw_handlers_js_1.handlePush; } });
|
|
20
|
+
Object.defineProperty(exports, "notificationOptions", { enumerable: true, get: function () { return sw_handlers_js_1.notificationOptions; } });
|
|
21
|
+
Object.defineProperty(exports, "parsePushPayload", { enumerable: true, get: function () { return sw_handlers_js_1.parsePushPayload; } });
|
|
22
|
+
Object.defineProperty(exports, "resolveClickUrl", { enumerable: true, get: function () { return sw_handlers_js_1.resolveClickUrl; } });
|
|
23
|
+
Object.defineProperty(exports, "SW_MESSAGE", { enumerable: true, get: function () { return sw_handlers_js_1.SW_MESSAGE; } });
|
|
24
|
+
var client_core_1 = require("@tingping/client-core");
|
|
25
|
+
Object.defineProperty(exports, "TingPingClient", { enumerable: true, get: function () { return client_core_1.TingPingClient; } });
|
|
26
|
+
Object.defineProperty(exports, "TingPingClientError", { enumerable: true, get: function () { return client_core_1.TingPingClientError; } });
|
|
27
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,6CAAqH;AAA5G,+GAAA,gBAAgB,OAAA;AAAE,0GAAA,WAAW,OAAA;AACtC,qCASmB;AARjB,gHAAA,qBAAqB,OAAA;AACrB,8GAAA,mBAAmB,OAAA;AACnB,0GAAA,eAAe,OAAA;AACf,0GAAA,eAAe,OAAA;AACf,8GAAA,mBAAmB,OAAA;AACnB,gHAAA,qBAAqB,OAAA;AAIvB,+CAAkG;AAAzF,qHAAA,qBAAqB,OAAA;AAAE,mHAAA,mBAAmB,OAAA;AACnD,mDAU0B;AATxB,yHAAA,uBAAuB,OAAA;AACvB,4GAAA,UAAU,OAAA;AACV,qHAAA,mBAAmB,OAAA;AACnB,kHAAA,gBAAgB,OAAA;AAChB,iHAAA,eAAe,OAAA;AACf,4GAAA,UAAU,OAAA;AAKZ,qDAQ+B;AAP7B,6GAAA,cAAc,OAAA;AACd,kHAAA,mBAAmB,OAAA"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
import { type AuthTokenSource, type ConnectionStatus, type NotificationFrame, type TingPingClientOptions } from '@tingping/client-core';
|
|
3
|
+
import { type PushPermission } from './push.js';
|
|
4
|
+
export interface TingPingProviderProps {
|
|
5
|
+
/** Base URL of the tingping endpoints, e.g. `https://api.example.com/tingping`. */
|
|
6
|
+
serverUrl: string;
|
|
7
|
+
/** The signed-in user. Nothing connects while it is empty (logged out). */
|
|
8
|
+
userId: string | null | undefined;
|
|
9
|
+
/** Your bearer token (or a function returning a fresh one). */
|
|
10
|
+
authToken?: AuthTokenSource;
|
|
11
|
+
tenantId?: string | null;
|
|
12
|
+
topics?: string[];
|
|
13
|
+
deviceMeta?: Record<string, unknown>;
|
|
14
|
+
/** Subscribe to Web Push when permission is granted. Default true. */
|
|
15
|
+
enablePush?: boolean;
|
|
16
|
+
/** VAPID public key; fetched from the server when omitted. */
|
|
17
|
+
vapidPublicKey?: string;
|
|
18
|
+
/** Where the tingping service worker is served. Default `/sw.js`. */
|
|
19
|
+
serviceWorkerUrl?: string;
|
|
20
|
+
/** Ask for notification permission on mount. Default false: call `requestPushPermission()` from a click. */
|
|
21
|
+
autoRequestPermission?: boolean;
|
|
22
|
+
/** Every new notification received over the WebSocket. */
|
|
23
|
+
onNotification?: (frame: NotificationFrame) => void;
|
|
24
|
+
/** The user tapped a system notification while the app was open. */
|
|
25
|
+
onNotificationTap?: (frame: NotificationFrame) => void;
|
|
26
|
+
/** How many recent notifications to keep in context. Default 50. */
|
|
27
|
+
maxNotifications?: number;
|
|
28
|
+
/** Low-level overrides (custom WebSocket/fetch, backoff), mainly for tests. */
|
|
29
|
+
clientOptions?: Partial<Pick<TingPingClientOptions, 'createWebSocket' | 'fetch' | 'backoff' | 'heartbeatIntervalMs' | 'pongTimeoutMs' | 'random'>>;
|
|
30
|
+
children?: ReactNode;
|
|
31
|
+
}
|
|
32
|
+
export interface TingPingContextValue {
|
|
33
|
+
status: ConnectionStatus;
|
|
34
|
+
isConnected: boolean;
|
|
35
|
+
lastNotification: NotificationFrame | null;
|
|
36
|
+
notifications: NotificationFrame[];
|
|
37
|
+
error: Error | null;
|
|
38
|
+
deviceId: string | null;
|
|
39
|
+
pushPermission: PushPermission;
|
|
40
|
+
/** Whether a Web Push subscription is registered for this device. */
|
|
41
|
+
pushEnabled: boolean;
|
|
42
|
+
/** Asks for notification permission (call from a user gesture) and subscribes on success. */
|
|
43
|
+
requestPushPermission(): Promise<PushPermission>;
|
|
44
|
+
/** Unregisters this device (e.g. on logout), disconnects and drops the push subscription. */
|
|
45
|
+
unregister(): Promise<void>;
|
|
46
|
+
clearNotifications(): void;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Declarative tingping for React web apps: registers the browser, keeps a live
|
|
50
|
+
* WebSocket, wires Web Push and exposes everything through `useTingPing()`.
|
|
51
|
+
*/
|
|
52
|
+
export declare function TingPingProvider(props: TingPingProviderProps): ReactNode;
|
|
53
|
+
/** Live tingping state for the nearest `<TingPingProvider>`. */
|
|
54
|
+
export declare function useTingPing(): TingPingContextValue;
|
|
55
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../src/provider.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAgF,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AACrH,OAAO,EAEL,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,EACtB,KAAK,qBAAqB,EAC3B,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAML,KAAK,cAAc,EACpB,MAAM,WAAW,CAAC;AAGnB,MAAM,WAAW,qBAAqB;IACpC,mFAAmF;IACnF,SAAS,EAAE,MAAM,CAAC;IAClB,2EAA2E;IAC3E,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;IAClC,+DAA+D;IAC/D,SAAS,CAAC,EAAE,eAAe,CAAC;IAC5B,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACrC,sEAAsE;IACtE,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,8DAA8D;IAC9D,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,qEAAqE;IACrE,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,4GAA4G;IAC5G,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,0DAA0D;IAC1D,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,iBAAiB,KAAK,IAAI,CAAC;IACpD,oEAAoE;IACpE,iBAAiB,CAAC,EAAE,CAAC,KAAK,EAAE,iBAAiB,KAAK,IAAI,CAAC;IACvD,oEAAoE;IACpE,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,+EAA+E;IAC/E,aAAa,CAAC,EAAE,OAAO,CACrB,IAAI,CAAC,qBAAqB,EAAE,iBAAiB,GAAG,OAAO,GAAG,SAAS,GAAG,qBAAqB,GAAG,eAAe,GAAG,QAAQ,CAAC,CAC1H,CAAC;IACF,QAAQ,CAAC,EAAE,SAAS,CAAC;CACtB;AAED,MAAM,WAAW,oBAAoB;IACnC,MAAM,EAAE,gBAAgB,CAAC;IACzB,WAAW,EAAE,OAAO,CAAC;IACrB,gBAAgB,EAAE,iBAAiB,GAAG,IAAI,CAAC;IAC3C,aAAa,EAAE,iBAAiB,EAAE,CAAC;IACnC,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IACpB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,cAAc,EAAE,cAAc,CAAC;IAC/B,qEAAqE;IACrE,WAAW,EAAE,OAAO,CAAC;IACrB,6FAA6F;IAC7F,qBAAqB,IAAI,OAAO,CAAC,cAAc,CAAC,CAAC;IACjD,6FAA6F;IAC7F,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5B,kBAAkB,IAAI,IAAI,CAAC;CAC5B;AAQD;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,qBAAqB,GAAG,SAAS,CAiKxE;AAED,gEAAgE;AAChE,wBAAgB,WAAW,IAAI,oBAAoB,CAIlD"}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TingPingProvider = TingPingProvider;
|
|
4
|
+
exports.useTingPing = useTingPing;
|
|
5
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
6
|
+
const react_1 = require("react");
|
|
7
|
+
const client_core_1 = require("@tingping/client-core");
|
|
8
|
+
const device_id_js_1 = require("./device-id.js");
|
|
9
|
+
const push_js_1 = require("./push.js");
|
|
10
|
+
const sw_handlers_js_1 = require("./sw-handlers.js");
|
|
11
|
+
const TingPingContext = (0, react_1.createContext)(null);
|
|
12
|
+
function stableKey(value) {
|
|
13
|
+
return JSON.stringify(value ?? null);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Declarative tingping for React web apps: registers the browser, keeps a live
|
|
17
|
+
* WebSocket, wires Web Push and exposes everything through `useTingPing()`.
|
|
18
|
+
*/
|
|
19
|
+
function TingPingProvider(props) {
|
|
20
|
+
const { serverUrl, userId, tenantId = null, enablePush = true, vapidPublicKey, serviceWorkerUrl = '/sw.js', autoRequestPermission = false, children, } = props;
|
|
21
|
+
const [status, setStatus] = (0, react_1.useState)('idle');
|
|
22
|
+
const [notifications, setNotifications] = (0, react_1.useState)([]);
|
|
23
|
+
const [error, setError] = (0, react_1.useState)(null);
|
|
24
|
+
const [deviceId, setDeviceId] = (0, react_1.useState)(null);
|
|
25
|
+
const [pushPermission, setPushPermission] = (0, react_1.useState)(() => (0, push_js_1.currentPushPermission)());
|
|
26
|
+
const [pushEnabled, setPushEnabled] = (0, react_1.useState)(false);
|
|
27
|
+
// Callbacks and object props live in refs so that inline values do not reconnect.
|
|
28
|
+
const latest = (0, react_1.useRef)(props);
|
|
29
|
+
latest.current = props;
|
|
30
|
+
const clientRef = (0, react_1.useRef)(null);
|
|
31
|
+
const topicsKey = stableKey(props.topics);
|
|
32
|
+
const metaKey = stableKey(props.deviceMeta);
|
|
33
|
+
const optionsKey = stableKey(Object.keys(props.clientOptions ?? {}));
|
|
34
|
+
const subscribe = (0, react_1.useCallback)(async (requestPermission) => {
|
|
35
|
+
if (!enablePush || !(0, push_js_1.isPushSupported)())
|
|
36
|
+
return false;
|
|
37
|
+
const fetchImpl = latest.current.clientOptions?.fetch ?? fetch;
|
|
38
|
+
const key = vapidPublicKey ?? (await (0, push_js_1.fetchVapidPublicKey)(serverUrl, fetchImpl));
|
|
39
|
+
if (key === null)
|
|
40
|
+
return false;
|
|
41
|
+
const subscription = await (0, push_js_1.subscribeToPush)({ vapidPublicKey: key, serviceWorkerUrl, requestPermission });
|
|
42
|
+
setPushPermission((0, push_js_1.currentPushPermission)());
|
|
43
|
+
if (subscription === null)
|
|
44
|
+
return false;
|
|
45
|
+
clientRef.current?.setSubscription(subscription);
|
|
46
|
+
setPushEnabled(true);
|
|
47
|
+
return true;
|
|
48
|
+
}, [enablePush, vapidPublicKey, serverUrl, serviceWorkerUrl]);
|
|
49
|
+
(0, react_1.useEffect)(() => {
|
|
50
|
+
if (!userId) {
|
|
51
|
+
setStatus('idle');
|
|
52
|
+
setDeviceId(null);
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
let cancelled = false;
|
|
56
|
+
const id = (0, device_id_js_1.getOrCreateDeviceId)();
|
|
57
|
+
setDeviceId(id);
|
|
58
|
+
setError(null);
|
|
59
|
+
const current = latest.current;
|
|
60
|
+
const client = new client_core_1.TingPingClient({
|
|
61
|
+
serverUrl,
|
|
62
|
+
userId,
|
|
63
|
+
tenantId,
|
|
64
|
+
deviceId: id,
|
|
65
|
+
platform: 'web',
|
|
66
|
+
authToken: async () => {
|
|
67
|
+
const source = latest.current.authToken;
|
|
68
|
+
return typeof source === 'function' ? source() : source;
|
|
69
|
+
},
|
|
70
|
+
topics: current.topics ?? [],
|
|
71
|
+
deviceMeta: current.deviceMeta ?? {},
|
|
72
|
+
...current.clientOptions,
|
|
73
|
+
});
|
|
74
|
+
clientRef.current = client;
|
|
75
|
+
const offs = [
|
|
76
|
+
client.on('status', (s) => {
|
|
77
|
+
if (!cancelled)
|
|
78
|
+
setStatus(s);
|
|
79
|
+
}),
|
|
80
|
+
client.on('notification', (frame) => {
|
|
81
|
+
if (cancelled)
|
|
82
|
+
return;
|
|
83
|
+
setNotifications((list) => [frame, ...list].slice(0, Math.max(1, latest.current.maxNotifications ?? 50)));
|
|
84
|
+
latest.current.onNotification?.(frame);
|
|
85
|
+
}),
|
|
86
|
+
client.on('error', (e) => {
|
|
87
|
+
if (!cancelled)
|
|
88
|
+
setError(e);
|
|
89
|
+
}),
|
|
90
|
+
];
|
|
91
|
+
void (async () => {
|
|
92
|
+
try {
|
|
93
|
+
if ((0, push_js_1.currentPushPermission)() === 'granted' || autoRequestPermission)
|
|
94
|
+
await subscribe(autoRequestPermission);
|
|
95
|
+
}
|
|
96
|
+
catch (e) {
|
|
97
|
+
if (!cancelled)
|
|
98
|
+
setError(e instanceof Error ? e : new Error(String(e)));
|
|
99
|
+
}
|
|
100
|
+
if (!cancelled)
|
|
101
|
+
await client.start();
|
|
102
|
+
})();
|
|
103
|
+
const reconnect = () => client.reconnectNow();
|
|
104
|
+
const onVisibility = () => {
|
|
105
|
+
if (document.visibilityState === 'visible')
|
|
106
|
+
client.reconnectNow();
|
|
107
|
+
};
|
|
108
|
+
globalThis.addEventListener?.('online', reconnect);
|
|
109
|
+
globalThis.document?.addEventListener('visibilitychange', onVisibility);
|
|
110
|
+
return () => {
|
|
111
|
+
cancelled = true;
|
|
112
|
+
for (const off of offs)
|
|
113
|
+
off();
|
|
114
|
+
client.disconnect();
|
|
115
|
+
if (clientRef.current === client)
|
|
116
|
+
clientRef.current = null;
|
|
117
|
+
globalThis.removeEventListener?.('online', reconnect);
|
|
118
|
+
globalThis.document?.removeEventListener('visibilitychange', onVisibility);
|
|
119
|
+
};
|
|
120
|
+
// Object props are compared by value (topicsKey, metaKey, optionsKey); callbacks are read from `latest`.
|
|
121
|
+
}, [serverUrl, userId, tenantId, topicsKey, metaKey, optionsKey, subscribe, autoRequestPermission]);
|
|
122
|
+
(0, react_1.useEffect)(() => {
|
|
123
|
+
const container = globalThis.navigator?.serviceWorker;
|
|
124
|
+
if (container === undefined)
|
|
125
|
+
return undefined;
|
|
126
|
+
const onMessage = (event) => {
|
|
127
|
+
const data = event.data;
|
|
128
|
+
if (data?.type === sw_handlers_js_1.SW_MESSAGE.CLICK && data.frame)
|
|
129
|
+
latest.current.onNotificationTap?.(data.frame);
|
|
130
|
+
};
|
|
131
|
+
container.addEventListener('message', onMessage);
|
|
132
|
+
return () => container.removeEventListener('message', onMessage);
|
|
133
|
+
}, []);
|
|
134
|
+
const requestPushPermission = (0, react_1.useCallback)(async () => {
|
|
135
|
+
try {
|
|
136
|
+
if (await subscribe(true))
|
|
137
|
+
await clientRef.current?.register();
|
|
138
|
+
}
|
|
139
|
+
catch (e) {
|
|
140
|
+
setError(e instanceof Error ? e : new Error(String(e)));
|
|
141
|
+
}
|
|
142
|
+
const permission = (0, push_js_1.currentPushPermission)();
|
|
143
|
+
setPushPermission(permission);
|
|
144
|
+
return permission;
|
|
145
|
+
}, [subscribe]);
|
|
146
|
+
const unregister = (0, react_1.useCallback)(async () => {
|
|
147
|
+
await clientRef.current?.unregister();
|
|
148
|
+
try {
|
|
149
|
+
await (0, push_js_1.unsubscribeFromPush)(serviceWorkerUrl);
|
|
150
|
+
}
|
|
151
|
+
finally {
|
|
152
|
+
setPushEnabled(false);
|
|
153
|
+
}
|
|
154
|
+
}, [serviceWorkerUrl]);
|
|
155
|
+
const clearNotifications = (0, react_1.useCallback)(() => setNotifications([]), []);
|
|
156
|
+
const value = (0, react_1.useMemo)(() => ({
|
|
157
|
+
status,
|
|
158
|
+
isConnected: status === 'open',
|
|
159
|
+
lastNotification: notifications[0] ?? null,
|
|
160
|
+
notifications,
|
|
161
|
+
error,
|
|
162
|
+
deviceId,
|
|
163
|
+
pushPermission,
|
|
164
|
+
pushEnabled,
|
|
165
|
+
requestPushPermission,
|
|
166
|
+
unregister,
|
|
167
|
+
clearNotifications,
|
|
168
|
+
}), [status, notifications, error, deviceId, pushPermission, pushEnabled, requestPushPermission, unregister, clearNotifications]);
|
|
169
|
+
return (0, jsx_runtime_1.jsx)(TingPingContext.Provider, { value: value, children: children });
|
|
170
|
+
}
|
|
171
|
+
/** Live tingping state for the nearest `<TingPingProvider>`. */
|
|
172
|
+
function useTingPing() {
|
|
173
|
+
const value = (0, react_1.useContext)(TingPingContext);
|
|
174
|
+
if (value === null)
|
|
175
|
+
throw new Error('useTingPing() must be used inside <TingPingProvider>');
|
|
176
|
+
return value;
|
|
177
|
+
}
|
|
178
|
+
//# sourceMappingURL=provider.js.map
|