@fleetbase/ember-core 0.0.3 → 0.0.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.
@@ -3,6 +3,7 @@ const hostServices = [
3
3
  'session',
4
4
  'current-user',
5
5
  'fetch',
6
+ 'socket',
6
7
  'media',
7
8
  'app-cache',
8
9
  'url-search-params',
@@ -3,6 +3,7 @@ const services = [
3
3
  'session',
4
4
  'current-user',
5
5
  'fetch',
6
+ 'socket',
6
7
  'media',
7
8
  'app-cache',
8
9
  'url-search-params',
@@ -1,7 +1,6 @@
1
1
  import RESTSerializer from '@ember-data/serializer/rest';
2
2
  import { isNone } from '@ember/utils';
3
3
  import { underscore } from '@ember/string';
4
- import { isArray } from '@ember/array';
5
4
  import normalizePolymorphicTypeWithinHash from '../utils/serialize/normalize-polymorphic-type-within-hash';
6
5
 
7
6
  export default class ApplicationSerializer extends RESTSerializer {
@@ -67,20 +66,4 @@ export default class ApplicationSerializer extends RESTSerializer {
67
66
 
68
67
  return super.normalize(model, hash, prop);
69
68
  }
70
-
71
- /**
72
- * We only want to save dirty/changed model attributes
73
- *
74
- * @param {Snapshot} snapshot
75
- * @param {Object} json
76
- * @param {String} key
77
- * @param {Array} attributes
78
- */
79
- serializeAttribute(snapshot, json, key, attributes) {
80
- const excludedKeys = ['name', 'meta', 'options', 'config', 'excluded_addons', 'translations', 'tags'];
81
-
82
- if (snapshot.record?.get('isNew') || snapshot.changedAttributes()[key] || isArray(snapshot.attr(key)) || excludedKeys.includes(key)) {
83
- return super.serializeAttribute(snapshot, json, key, attributes);
84
- }
85
- }
86
69
  }
@@ -257,9 +257,12 @@ export default class FetchService extends Service {
257
257
  */
258
258
  request(path, method = 'GET', data = {}, options = {}) {
259
259
  const headers = assign(this.getHeaders(), options.headers ?? {});
260
+ const host = options.host ?? this.host;
261
+ const namespace = options.namespace ?? this.namespace;
262
+ const url = options.externalRequest === true ? path : [host, namespace, path].filter(Boolean).join('/');
260
263
 
261
264
  return new Promise((resolve, reject) => {
262
- return fetch(options.externalRequest === true ? path : `${options.host || this.host}/${options.namespace || this.namespace}/${path}`, {
265
+ return fetch(url, {
263
266
  method,
264
267
  mode: options.mode || 'cors',
265
268
  credentials: options.credentials || this.credentials,
@@ -268,7 +271,6 @@ export default class FetchService extends Service {
268
271
  })
269
272
  .then(this.parseJSON)
270
273
  .then((response) => {
271
- // console.log('[fetch:response]', response);
272
274
  if (response.ok) {
273
275
  if (options.normalizeToEmberData) {
274
276
  const normalized = this.normalizeModel(response.json, options.normalizeModelType);
@@ -0,0 +1,55 @@
1
+ import Service from '@ember/service';
2
+ import { tracked } from '@glimmer/tracking';
3
+ import { isBlank } from '@ember/utils';
4
+ import toBoolean from '../utils/to-boolean';
5
+ import config from 'ember-get-config';
6
+
7
+ export default class SocketService extends Service {
8
+ @tracked channels = [];
9
+
10
+ constructor() {
11
+ super(...arguments);
12
+ this.socket = this.createSocketClusterClient();
13
+ }
14
+
15
+ instance() {
16
+ return this.socket;
17
+ }
18
+
19
+ createSocketClusterClient() {
20
+ const socketConfig = { ...config.socket };
21
+
22
+ if (isBlank(socketConfig.hostname)) {
23
+ socketConfig.hostname = window.location.hostname;
24
+ }
25
+
26
+ socketConfig.secure = toBoolean(socketConfig.secure);
27
+
28
+ return socketClusterClient.create(socketConfig);
29
+ }
30
+
31
+ async listen(channelId, callback) {
32
+ const channel = this.socket.subscribe(channelId);
33
+
34
+ // track channel
35
+ this.channels.pushObject(channel);
36
+
37
+ // listen to channel for events
38
+ await channel.listener('subscribe').once();
39
+
40
+ // get incoming data and console out
41
+ for await (let output of channel) {
42
+ if (typeof callback === 'function') {
43
+ callback(output);
44
+ }
45
+ }
46
+ }
47
+
48
+ closeChannels() {
49
+ for (let i = 0; i < this.channels.length; i++) {
50
+ const channel = this.channels.objectAt(i);
51
+
52
+ channel.close();
53
+ }
54
+ }
55
+ }
@@ -0,0 +1,19 @@
1
+ export default function toBoolean(value) {
2
+ switch (value) {
3
+ case 'true':
4
+ case '1':
5
+ case 1:
6
+ case true:
7
+ return true;
8
+ case 'false':
9
+ case '0':
10
+ case 0:
11
+ case false:
12
+ case null:
13
+ case undefined:
14
+ case '':
15
+ return false;
16
+ default:
17
+ return false;
18
+ }
19
+ }
@@ -0,0 +1 @@
1
+ export { default } from '@fleetbase/ember-core/services/socket';
@@ -0,0 +1 @@
1
+ export { default } from '@fleetbase/ember-core/utils/to-boolean';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fleetbase/ember-core",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "Provides all the core services, decorators and utilities for building a Fleetbase extension for the Console.",
5
5
  "keywords": [
6
6
  "fleetbase-extension",