Skip to content

Commit

Permalink
feat(activity): add activity module
Browse files Browse the repository at this point in the history
  • Loading branch information
Garrett Downs committed Mar 26, 2022
1 parent f8fe89a commit 9d99ec5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Expand Up @@ -9,6 +9,19 @@ A standard library to interact with KaiOS 2.x and 3.x\* APIs.

## Examples

### Activity

```js
// Create the service
const service = new KaiOS.Activity({
name: 'toolbox/qr-to-text',
data: {},
});

// Send the request and await the result
const result = await service.start();
```

### Alarm

```js
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
@@ -1,3 +1,4 @@
import { Activity } from './modules/activity';
import { Alarm } from './modules/alarm';
import { App } from './modules/app';
import { Battery } from './modules/battery';
Expand All @@ -7,6 +8,7 @@ import { LocalStorage } from './modules/localStorage';
import { Network } from './modules/network';
import { Volume } from './modules/volume';

export * from './modules/activity';
export * from './modules/alarm';
export * from './modules/app';
export * from './modules/battery';
Expand All @@ -17,6 +19,7 @@ export * from './modules/network';
export * from './modules/volume';

export default class KaiOS {
static Activity = Activity;
static Alarm = Alarm;
static App = App;
static Battery = Battery;
Expand Down
3 changes: 3 additions & 0 deletions src/models/MozWindow.ts
@@ -0,0 +1,3 @@
export type MozWindow = Window & {
MozActivity: any;
} & any;
1 change: 1 addition & 0 deletions src/models/index.ts
Expand Up @@ -9,5 +9,6 @@ export * from './Manifest';
export * from './MozBattery';
export * from './MozDeviceStorage';
export * from './MozNavigator';
export * from './MozWindow';
export * from './NetworkStatus';
export * from './Request';
28 changes: 28 additions & 0 deletions src/modules/activity.ts
@@ -0,0 +1,28 @@
import { MozWindow } from '../models';

type Data = {
name: string;
data: { [key: string]: any };
};

export class Activity<TResult = any> {
data: Data;

constructor(data: Data) {
this.data = data;
}

start(): Promise<TResult> {
return new Promise((resolve, reject) => {
const activity = new (window as MozWindow).MozActivity(this.data);

activity.onsuccess = function () {
resolve(this.result);
};

activity.onerror = function () {
reject(this.error);
};
});
}
}

0 comments on commit 9d99ec5

Please sign in to comment.