@sprucelabs/spruce-heartwood-utils 31.1.4 → 31.1.6

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,13 +3,15 @@ import { AudioController, CachedValue, Device, TheaterSettingValueTypes, Theatre
3
3
  import { Orientation } from '../types/heartwood.types';
4
4
  export declare class HeartwoodDevice extends EventEmitter implements Device {
5
5
  static Class?: new (storage: Storage) => Device;
6
+ static setTimeout(callback: () => void, delay: number): NodeJS.Timeout;
7
+ static clearTimeout(timeoutId: ReturnType<typeof setTimeout>): void;
6
8
  private storage;
7
9
  private orientation;
8
10
  private isGettingTheatreSetting;
9
11
  protected constructor(storage: Storage);
10
12
  turnTorchOn(brightness?: number): void;
11
13
  turnTorchOff(): void;
12
- AudioController(_mp3Url: string): AudioController;
14
+ AudioController(mp3Url: string): Promise<AudioController>;
13
15
  openUrl(url: string): void;
14
16
  static Device(storage: Storage): HeartwoodDevice;
15
17
  setCachedValue(key: string, value: CachedValue): void;
@@ -23,4 +25,27 @@ export declare class HeartwoodDevice extends EventEmitter implements Device {
23
25
  sendCommand(command: string, payload?: Record<string, any>): void;
24
26
  setTheatreSetting<N extends TheatreSettingName>(name: N, value: TheaterSettingValueTypes[N]): void;
25
27
  getTheatreSetting<N extends TheatreSettingName>(name: N): Promise<TheaterSettingValueTypes[N] | null>;
28
+ onMessage<T>(name: string, timeoutMs?: number): Promise<T | null>;
29
+ }
30
+ export type DeviceOnMessageCallback = (callback: (json: string) => void) => void;
31
+ export type DevicePostMessageCallback = (json: string) => void;
32
+ declare global {
33
+ interface Window {
34
+ api?: {
35
+ postMessage: DevicePostMessageCallback;
36
+ onMessage?: DeviceOnMessageCallback;
37
+ };
38
+ webkit?: {
39
+ messageHandlers?: {
40
+ messages?: {
41
+ postMessage?: (json: string) => void;
42
+ onMessage?: DeviceOnMessageCallback;
43
+ };
44
+ };
45
+ };
46
+ AndroidInterface?: {
47
+ postMessage?: (json: string) => void;
48
+ onMessage?: DeviceOnMessageCallback;
49
+ };
50
+ }
26
51
  }
@@ -8,6 +8,12 @@ const events_1 = __importDefault(require("events"));
8
8
  const schema_1 = require("@sprucelabs/schema");
9
9
  const HeartwoodAudioController_1 = __importDefault(require("./HeartwoodAudioController"));
10
10
  class HeartwoodDevice extends events_1.default {
11
+ static setTimeout(callback, delay) {
12
+ return setTimeout(callback, delay);
13
+ }
14
+ static clearTimeout(timeoutId) {
15
+ clearTimeout(timeoutId);
16
+ }
11
17
  constructor(storage) {
12
18
  super();
13
19
  this.orientation = 'unknown-orientation';
@@ -22,8 +28,10 @@ class HeartwoodDevice extends events_1.default {
22
28
  turnTorchOff() {
23
29
  this.sendCommand('turnTorchOff');
24
30
  }
25
- AudioController(_mp3Url) {
26
- return new HeartwoodAudioController_1.default();
31
+ async AudioController(mp3Url) {
32
+ const audio = new HeartwoodAudioController_1.default(this);
33
+ await audio.load(mp3Url);
34
+ return audio;
27
35
  }
28
36
  openUrl(url) {
29
37
  void this.sendCommand('openUrl', {
@@ -70,12 +78,9 @@ class HeartwoodDevice extends events_1.default {
70
78
  };
71
79
  const message = JSON.stringify(options);
72
80
  if (!this.isGettingTheatreSetting) {
73
- //@ts-ignore
74
81
  window.webkit?.messageHandlers?.messages?.postMessage?.(message);
75
- // @ts-ignore
76
- window.AndroidInterface?.postMessage(message);
82
+ window.AndroidInterface?.postMessage?.(message);
77
83
  }
78
- //@ts-ignore
79
84
  window.api?.postMessage?.(message);
80
85
  this.emit('command', command, payload);
81
86
  }
@@ -86,7 +91,6 @@ class HeartwoodDevice extends events_1.default {
86
91
  });
87
92
  }
88
93
  async getTheatreSetting(name) {
89
- //@ts-ignore
90
94
  if (window.api) {
91
95
  this.isGettingTheatreSetting = true;
92
96
  this.sendCommand('getTheatreSetting', {
@@ -97,16 +101,22 @@ class HeartwoodDevice extends events_1.default {
97
101
  else {
98
102
  return null;
99
103
  }
104
+ const setting = await this.onMessage(name);
105
+ return setting;
106
+ }
107
+ async onMessage(name, timeoutMs = 250) {
100
108
  return new Promise((resolve) => {
101
- let timeout = setTimeout(() => resolve(null), 250);
102
- //@ts-ignore
103
- window.api?.onMessage?.((json) => {
109
+ let timeout = HeartwoodDevice.setTimeout(() => resolve(null), timeoutMs);
110
+ const cb = (json) => {
104
111
  const { value, name: returnedName } = JSON.parse(json);
105
112
  if (name === returnedName) {
106
- clearTimeout(timeout);
113
+ HeartwoodDevice.clearTimeout(timeout);
107
114
  resolve(value);
108
115
  }
109
- });
116
+ };
117
+ window.api?.onMessage?.(cb);
118
+ window.webkit?.messageHandlers?.messages?.onMessage?.(cb);
119
+ window.AndroidInterface?.onMessage?.(cb);
110
120
  });
111
121
  }
112
122
  }
@@ -34,6 +34,9 @@ class SpruceError extends error_1.default {
34
34
  case 'WEB_HOST_NOT_SET':
35
35
  message = `I could not generate a URL because process.env.PUBLIC_URL is not set! Make sure to set that and then reboot the Heartwood skill.`;
36
36
  break;
37
+ case 'DEVICE_ERROR':
38
+ message = 'A Device error just happened!';
39
+ break;
37
40
  default:
38
41
  message = super.friendlyMessage();
39
42
  }
@@ -3,13 +3,15 @@ import { AudioController, CachedValue, Device, TheaterSettingValueTypes, Theatre
3
3
  import { Orientation } from '../types/heartwood.types';
4
4
  export declare class HeartwoodDevice extends EventEmitter implements Device {
5
5
  static Class?: new (storage: Storage) => Device;
6
+ static setTimeout(callback: () => void, delay: number): NodeJS.Timeout;
7
+ static clearTimeout(timeoutId: ReturnType<typeof setTimeout>): void;
6
8
  private storage;
7
9
  private orientation;
8
10
  private isGettingTheatreSetting;
9
11
  protected constructor(storage: Storage);
10
12
  turnTorchOn(brightness?: number): void;
11
13
  turnTorchOff(): void;
12
- AudioController(_mp3Url: string): AudioController;
14
+ AudioController(mp3Url: string): Promise<AudioController>;
13
15
  openUrl(url: string): void;
14
16
  static Device(storage: Storage): HeartwoodDevice;
15
17
  setCachedValue(key: string, value: CachedValue): void;
@@ -23,4 +25,27 @@ export declare class HeartwoodDevice extends EventEmitter implements Device {
23
25
  sendCommand(command: string, payload?: Record<string, any>): void;
24
26
  setTheatreSetting<N extends TheatreSettingName>(name: N, value: TheaterSettingValueTypes[N]): void;
25
27
  getTheatreSetting<N extends TheatreSettingName>(name: N): Promise<TheaterSettingValueTypes[N] | null>;
28
+ onMessage<T>(name: string, timeoutMs?: number): Promise<T | null>;
29
+ }
30
+ export type DeviceOnMessageCallback = (callback: (json: string) => void) => void;
31
+ export type DevicePostMessageCallback = (json: string) => void;
32
+ declare global {
33
+ interface Window {
34
+ api?: {
35
+ postMessage: DevicePostMessageCallback;
36
+ onMessage?: DeviceOnMessageCallback;
37
+ };
38
+ webkit?: {
39
+ messageHandlers?: {
40
+ messages?: {
41
+ postMessage?: (json: string) => void;
42
+ onMessage?: DeviceOnMessageCallback;
43
+ };
44
+ };
45
+ };
46
+ AndroidInterface?: {
47
+ postMessage?: (json: string) => void;
48
+ onMessage?: DeviceOnMessageCallback;
49
+ };
50
+ }
26
51
  }
@@ -11,6 +11,12 @@ import EventEmitter from 'events';
11
11
  import { assertOptions } from '@sprucelabs/schema';
12
12
  import HeartwoodAudioController from './HeartwoodAudioController.js';
13
13
  export class HeartwoodDevice extends EventEmitter {
14
+ static setTimeout(callback, delay) {
15
+ return setTimeout(callback, delay);
16
+ }
17
+ static clearTimeout(timeoutId) {
18
+ clearTimeout(timeoutId);
19
+ }
14
20
  constructor(storage) {
15
21
  super();
16
22
  this.orientation = 'unknown-orientation';
@@ -25,8 +31,12 @@ export class HeartwoodDevice extends EventEmitter {
25
31
  turnTorchOff() {
26
32
  this.sendCommand('turnTorchOff');
27
33
  }
28
- AudioController(_mp3Url) {
29
- return new HeartwoodAudioController();
34
+ AudioController(mp3Url) {
35
+ return __awaiter(this, void 0, void 0, function* () {
36
+ const audio = new HeartwoodAudioController(this);
37
+ yield audio.load(mp3Url);
38
+ return audio;
39
+ });
30
40
  }
31
41
  openUrl(url) {
32
42
  void this.sendCommand('openUrl', {
@@ -68,20 +78,17 @@ export class HeartwoodDevice extends EventEmitter {
68
78
  this.sendCommand('skillViewLoaded');
69
79
  }
70
80
  sendCommand(command, payload) {
71
- var _a, _b, _c, _d, _e, _f, _g;
81
+ var _a, _b, _c, _d, _e, _f, _g, _h;
72
82
  const options = {
73
83
  command,
74
84
  payload,
75
85
  };
76
86
  const message = JSON.stringify(options);
77
87
  if (!this.isGettingTheatreSetting) {
78
- //@ts-ignore
79
88
  (_d = (_c = (_b = (_a = window.webkit) === null || _a === void 0 ? void 0 : _a.messageHandlers) === null || _b === void 0 ? void 0 : _b.messages) === null || _c === void 0 ? void 0 : _c.postMessage) === null || _d === void 0 ? void 0 : _d.call(_c, message);
80
- // @ts-ignore
81
- (_e = window.AndroidInterface) === null || _e === void 0 ? void 0 : _e.postMessage(message);
89
+ (_f = (_e = window.AndroidInterface) === null || _e === void 0 ? void 0 : _e.postMessage) === null || _f === void 0 ? void 0 : _f.call(_e, message);
82
90
  }
83
- //@ts-ignore
84
- (_g = (_f = window.api) === null || _f === void 0 ? void 0 : _f.postMessage) === null || _g === void 0 ? void 0 : _g.call(_f, message);
91
+ (_h = (_g = window.api) === null || _g === void 0 ? void 0 : _g.postMessage) === null || _h === void 0 ? void 0 : _h.call(_g, message);
85
92
  this.emit('command', command, payload);
86
93
  }
87
94
  setTheatreSetting(name, value) {
@@ -92,7 +99,6 @@ export class HeartwoodDevice extends EventEmitter {
92
99
  }
93
100
  getTheatreSetting(name) {
94
101
  return __awaiter(this, void 0, void 0, function* () {
95
- //@ts-ignore
96
102
  if (window.api) {
97
103
  this.isGettingTheatreSetting = true;
98
104
  this.sendCommand('getTheatreSetting', {
@@ -103,17 +109,25 @@ export class HeartwoodDevice extends EventEmitter {
103
109
  else {
104
110
  return null;
105
111
  }
112
+ const setting = yield this.onMessage(name);
113
+ return setting;
114
+ });
115
+ }
116
+ onMessage(name_1) {
117
+ return __awaiter(this, arguments, void 0, function* (name, timeoutMs = 250) {
106
118
  return new Promise((resolve) => {
107
- var _a, _b;
108
- let timeout = setTimeout(() => resolve(null), 250);
109
- //@ts-ignore
110
- (_b = (_a = window.api) === null || _a === void 0 ? void 0 : _a.onMessage) === null || _b === void 0 ? void 0 : _b.call(_a, (json) => {
119
+ var _a, _b, _c, _d, _e, _f, _g, _h;
120
+ let timeout = HeartwoodDevice.setTimeout(() => resolve(null), timeoutMs);
121
+ const cb = (json) => {
111
122
  const { value, name: returnedName } = JSON.parse(json);
112
123
  if (name === returnedName) {
113
- clearTimeout(timeout);
124
+ HeartwoodDevice.clearTimeout(timeout);
114
125
  resolve(value);
115
126
  }
116
- });
127
+ };
128
+ (_b = (_a = window.api) === null || _a === void 0 ? void 0 : _a.onMessage) === null || _b === void 0 ? void 0 : _b.call(_a, cb);
129
+ (_f = (_e = (_d = (_c = window.webkit) === null || _c === void 0 ? void 0 : _c.messageHandlers) === null || _d === void 0 ? void 0 : _d.messages) === null || _e === void 0 ? void 0 : _e.onMessage) === null || _f === void 0 ? void 0 : _f.call(_e, cb);
130
+ (_h = (_g = window.AndroidInterface) === null || _g === void 0 ? void 0 : _g.onMessage) === null || _h === void 0 ? void 0 : _h.call(_g, cb);
117
131
  });
118
132
  });
119
133
  }
@@ -29,6 +29,9 @@ export default class SpruceError extends AbstractSpruceError {
29
29
  case 'WEB_HOST_NOT_SET':
30
30
  message = `I could not generate a URL because process.env.PUBLIC_URL is not set! Make sure to set that and then reboot the Heartwood skill.`;
31
31
  break;
32
+ case 'DEVICE_ERROR':
33
+ message = 'A Device error just happened!';
34
+ break;
32
35
  default:
33
36
  message = super.friendlyMessage();
34
37
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sprucelabs/spruce-heartwood-utils",
3
3
  "description": "Heartwood Utilities",
4
- "version": "31.1.4",
4
+ "version": "31.1.6",
5
5
  "skill": {
6
6
  "namespace": "heartwood"
7
7
  },