ember-polling-push-updates 0.0.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.
@@ -0,0 +1,13 @@
1
+ steps:
2
+ install:
3
+ image: madnificent/ember:5.12.0
4
+ commands:
5
+ - npm ci
6
+ release:
7
+ image: plugins/npm
8
+ settings:
9
+ token:
10
+ from_secret: npm_access_token
11
+ when:
12
+ - event: tag
13
+ ref: refs/tags/*
package/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # ember-polling-push-updates
2
+
3
+ [Short description of the addon.]
4
+
5
+ ## Compatibility
6
+
7
+ - Ember.js v4.12 or above
8
+ - Ember CLI v4.12 or above
9
+ - Node.js v18 or above
10
+
11
+ ## Installation
12
+
13
+ ```
14
+ ember install ember-polling-push-updates
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ [Longer description of how to use the addon in apps.]
20
+
21
+ ## Contributing
22
+
23
+ See the [Contributing](CONTRIBUTING.md) guide for details.
24
+
25
+ ## License
26
+
27
+ This project is licensed under the [MIT License](LICENSE.md).
@@ -0,0 +1,165 @@
1
+ import { tracked } from '@glimmer/tracking';
2
+ import Service from '@ember/service';
3
+
4
+ export default class PushUpdatesService extends Service {
5
+ @tracked tabUri;
6
+ @tracked tabUriP;
7
+
8
+ handlers = {};
9
+
10
+ constructor() {
11
+ super(...arguments);
12
+
13
+ this.startQueryLoop();
14
+ }
15
+
16
+ addHandler( kind, functor ) {
17
+ this.handlers[kind] ||= [];
18
+ this.handlers[kind].push( functor );
19
+ }
20
+
21
+ removeHandler( kind, functor ) {
22
+ this.handlers[kind] =
23
+ (this.handlers[kind] || [])
24
+ .filter( (element) => element !== functor );
25
+ }
26
+
27
+ async ensureTabUri() {
28
+ if( ! this.tabUriP ) {
29
+ this.tabUriP = new Promise( async (acc, rej) => {
30
+ try {
31
+ const body = await fetch(`/polling/tabUri`);
32
+ const resp = await body.json();
33
+ const tabUri = resp.data.attributes.tabUri;
34
+ this.tabUri = tabUri;
35
+ acc(tabUri);
36
+ } catch (e) {
37
+ console.error(`Could not get tab URI ${e}`);
38
+ rej();
39
+ }
40
+ });
41
+ }
42
+ return await this.tabUriP;
43
+ }
44
+
45
+ async startQueryLoop() {
46
+ let tabUri = await this.ensureTabUri(); // will be updated if we need to refresh the tab uri
47
+ while (true) {
48
+ try {
49
+ const body = (await (await fetch(`/polling/messages?tab=${encodeURIComponent(tabUri)}`)).json());
50
+ const messages = body.data;
51
+
52
+ if( body.data ) {
53
+ // call processors if they exist
54
+ messages
55
+ .forEach(
56
+ (message) =>
57
+ this
58
+ .handlers[message.attributes.channel]
59
+ ?.forEach(
60
+ (handler) => { try {
61
+ handler.call(message.attributes.content)
62
+ } catch (e) {
63
+ console.warn(`Handler did not respond to message`, { handler, message, e })
64
+ } }));
65
+ } else {
66
+ // we are in the reror state and should fetch again
67
+ console.warn(`Lost tabId, asking for a new one ${tabUri}`);
68
+ // TODO: inform our registered consumers our tab has changed so they can setup their monitoring requests again
69
+ this.tabUriP = null;
70
+ tabUri = await this.ensureTabUri();
71
+ }
72
+ } catch (e) {
73
+ console.warn(`Failed to poll messages ${e}.`);
74
+ }
75
+ await timeout( 2000 );
76
+ }
77
+ }
78
+
79
+ // SUPPORT FOR SPECIFIC TYPES OF MONITOR
80
+ async monitorCache({ path, callback, initial=true }) {
81
+ const tabUri = await this.ensureTabUri();
82
+ const queryParams = new URLSearchParams({ tab: tabUri, path });
83
+ await fetch(`/cache-monitor/monitor?${queryParams}`,
84
+ {
85
+ method: 'POST',
86
+ headers: { 'accept': 'application/vnd.api+json' }
87
+ });
88
+ this.addHandler(
89
+ "http://services.semantic.works/cache-monitor",
90
+ callback);
91
+ if( initial )
92
+ await callback();
93
+ }
94
+
95
+ async unMonitorCache({ path, callback }) {
96
+ const tabUri = await this.ensureTabUri();
97
+ const queryParams = new URLSearchParams({ tab: tabUri, path });
98
+ await fetch(`/cache-monitor/monitor?${queryParams}`,
99
+ {
100
+ method: 'DELETE',
101
+ headers: { 'accept': 'application/vnd.api+json' }
102
+ });
103
+ this.removeHandler(
104
+ "http://services.semantic.works/cache-monitor",
105
+ callback
106
+ )
107
+ }
108
+
109
+ async monitorResource({ uri, callback }) {
110
+ const tabUri = await this.ensureTabUri();
111
+ const queryParams = new URLSearchParams({ subject: uri });
112
+ await fetch(`/resource-monitor/monitor?${queryParams}`,
113
+ {
114
+ method: "POST",
115
+ headers: {
116
+ 'accept': "application/vnd.api+json",
117
+ "MU-TAB-ID": tabUri
118
+ }
119
+ });
120
+ this.addHandler(
121
+ "http://services.semantic.works/resource-monitor",
122
+ callback);
123
+ }
124
+
125
+ async unMonitorResource({ uri: uri, callback }) {
126
+ const tabUri = await this.ensureTabUri();
127
+ const queryParams = new URLSearchParams({ subject: uri });
128
+ await fetch(`/resource-monitor/monitor?${queryParams}`,
129
+ {
130
+ method: "DELETE",
131
+ headers: {
132
+ 'accept': "application/vnd.api+json",
133
+ "MU-TAB-ID": tabUri
134
+ }
135
+ })
136
+ this.removeHandler("http://services.semantic.works/resource-monitor", callback);
137
+ }
138
+
139
+ modelReloadCallbacks = {};
140
+
141
+ async monitorModel(model) {
142
+ const availableModel = await model;
143
+ if( availableModel.uri ) {
144
+ const callback = () => availableModel.reload();
145
+ this.modelReloadCallbacks[model] ||= [];
146
+ this.modelReloadCallbacks[model].push( callback );
147
+ await this.monitorResource( { uri: availableModel.uri, callback });
148
+ } else {
149
+ throw `Could not monitor model because no URI was found ${availableModel}`;
150
+ }
151
+ }
152
+
153
+ async unMonitorModel(model) {
154
+ const availableModel = await model;
155
+ if ( availableModel.uri && this.modelReloadCallbacks[model] ) {
156
+ const [callback, restCallbacks] = this.modelReloadCallbacks[model];
157
+ this.modelReloadCallbacks[model] = restCallbacks;
158
+ await this.unMonitorResource({ uri: availableModel.uri, callback });
159
+ }
160
+ }
161
+ }
162
+
163
+ function timeout() {
164
+ return true;
165
+ }
@@ -0,0 +1,5 @@
1
+ export default function timeout(ms) {
2
+ return new Promise((acc) =>
3
+ setTimeout(() => acc(), ms)
4
+ );
5
+ }
@@ -0,0 +1 @@
1
+ export { default } from 'ember-polling-push-updates/services/push-updates';
@@ -0,0 +1 @@
1
+ export { default } from 'ember-polling-push-updates/utils/timeout';
package/index.js ADDED
@@ -0,0 +1,5 @@
1
+ 'use strict';
2
+
3
+ module.exports = {
4
+ name: require('./package').name,
5
+ };
package/package.json ADDED
@@ -0,0 +1,87 @@
1
+ {
2
+ "name": "ember-polling-push-updates",
3
+ "version": "0.0.0",
4
+ "description": "The default blueprint for ember-cli addons.",
5
+ "keywords": [
6
+ "ember-addon"
7
+ ],
8
+ "repository": "",
9
+ "license": "MIT",
10
+ "author": "",
11
+ "directories": {
12
+ "doc": "doc",
13
+ "test": "tests"
14
+ },
15
+ "scripts": {
16
+ "build": "ember build --environment=production",
17
+ "lint": "concurrently \"npm:lint:*(!fix)\" --names \"lint:\"",
18
+ "lint:css": "stylelint \"**/*.css\"",
19
+ "lint:css:fix": "concurrently \"npm:lint:css -- --fix\"",
20
+ "lint:fix": "concurrently \"npm:lint:*:fix\" --names \"fix:\"",
21
+ "lint:hbs": "ember-template-lint .",
22
+ "lint:hbs:fix": "ember-template-lint . --fix",
23
+ "lint:js": "eslint . --cache",
24
+ "lint:js:fix": "eslint . --fix",
25
+ "start": "ember serve",
26
+ "test": "concurrently \"npm:lint\" \"npm:test:*\" --names \"lint,test:\"",
27
+ "test:ember": "ember test",
28
+ "test:ember-compatibility": "ember try:each"
29
+ },
30
+ "dependencies": {
31
+ "@babel/core": "^7.25.2",
32
+ "ember-cli-babel": "^8.2.0",
33
+ "ember-cli-htmlbars": "^6.3.0"
34
+ },
35
+ "devDependencies": {
36
+ "@babel/eslint-parser": "^7.25.1",
37
+ "@babel/plugin-proposal-decorators": "^7.24.7",
38
+ "@ember/optional-features": "^2.1.0",
39
+ "@ember/test-helpers": "^3.3.1",
40
+ "@embroider/test-setup": "^4.0.0",
41
+ "@glimmer/component": "^1.1.2",
42
+ "@glimmer/tracking": "^1.1.2",
43
+ "broccoli-asset-rev": "^3.0.0",
44
+ "concurrently": "^8.2.2",
45
+ "ember-auto-import": "^2.8.1",
46
+ "ember-cli": "~5.12.0",
47
+ "ember-cli-clean-css": "^3.0.0",
48
+ "ember-cli-dependency-checker": "^3.3.2",
49
+ "ember-cli-inject-live-reload": "^2.1.0",
50
+ "ember-cli-sri": "^2.1.1",
51
+ "ember-cli-terser": "^4.0.2",
52
+ "ember-load-initializers": "^2.1.2",
53
+ "ember-page-title": "^8.2.3",
54
+ "ember-qunit": "^8.1.0",
55
+ "ember-resolver": "^12.0.1",
56
+ "ember-source": "~5.12.0",
57
+ "ember-source-channel-url": "^3.0.0",
58
+ "ember-template-lint": "^6.0.0",
59
+ "ember-try": "^3.0.0",
60
+ "eslint": "^8.57.1",
61
+ "eslint-config-prettier": "^9.1.0",
62
+ "eslint-plugin-ember": "^12.2.1",
63
+ "eslint-plugin-n": "^16.6.2",
64
+ "eslint-plugin-prettier": "^5.2.1",
65
+ "eslint-plugin-qunit": "^8.1.2",
66
+ "loader.js": "^4.7.0",
67
+ "prettier": "^3.3.3",
68
+ "qunit": "^2.22.0",
69
+ "qunit-dom": "^3.2.1",
70
+ "stylelint": "^15.11.0",
71
+ "stylelint-config-standard": "^34.0.0",
72
+ "stylelint-prettier": "^4.1.0",
73
+ "webpack": "^5.95.0"
74
+ },
75
+ "peerDependencies": {
76
+ "ember-source": ">= 4.0.0"
77
+ },
78
+ "engines": {
79
+ "node": ">= 18"
80
+ },
81
+ "ember": {
82
+ "edition": "octane"
83
+ },
84
+ "ember-addon": {
85
+ "configPath": "tests/dummy/config"
86
+ }
87
+ }