Skip to content

Commit

Permalink
Merge pull request #33 from SpryRocks/merge-cap2
Browse files Browse the repository at this point in the history
Merge cap2
  • Loading branch information
maximzhemerenko authored Aug 31, 2024
2 parents 0aa210d + 0e4f187 commit c23ea0d
Show file tree
Hide file tree
Showing 13 changed files with 1,067 additions and 419 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/gradle-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ jobs:
steps:
- uses: actions/checkout@v4
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: '20.x'
node-version: '22.x'
registry-url: 'https://registry.npmjs.org'
- run: yarn && yarn build
- run: yarn publish
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20
22
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ socket.onError = function(error) {
|-------|---------|
| error | unknown |

#### onStateChanges
```typescript
socket.onStateChanged = function(state) {
// handle socket state change
};
```

##### Callback function parameters

| Name | Type |
|-------|--------------|
| state | SocketState |

### Connect socket to endpoint

```typescript
Expand Down Expand Up @@ -86,3 +99,15 @@ await socket.write(data);
```typescript
await socket.close();
```

### Get current state

```typescript
const state = socket.state;
```

## Q&A

Q: When I call the open method after being disconnected, I will be prompted that the open method can only be called once. How should I reconnect?

A: To re-connect the socket you should create the new socket and then open it.
1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ ext {
buildscript {
ext {
kotlin_version = rootProject.hasProperty('kotlin_version') ? rootProject.ext.kotlin_version : '1.9.22'
androidToolsBuildGradleVersion = rootProject.hasProperty('androidToolsBuildGradleVersion') ? rootProject.ext.androidToolsBuildGradleVersion : '3.6.4'
}
repositories {
google()
Expand Down
2 changes: 1 addition & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-all.zip
distributionUrl=https://services.gradle.org/distributions/gradle-7.0-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@spryrocks/capacitor-socket-connection-plugin",
"version": "2.1.9",
"version": "2.3.0-alpha.5",
"description": "Capacitor Socket Connection Plugin",
"main": "dist/plugin.cjs.js",
"module": "dist/esm/index.js",
Expand Down Expand Up @@ -36,14 +36,14 @@
"clean": "rimraf ./dist"
},
"dependencies": {
"@spryrocks/logger-plugin": "^0.1.15-alpha.1"
"@spryrocks/logger-plugin": "^0.1.17-alpha.2"
},
"devDependencies": {
"@capacitor/core": "^2.4.7",
"rimraf": "^5.0.1",
"rollup": "^3.28.1",
"typescript": "^5.1.6",
"@spryrocks/eslint-config": "^0.2.8-alpha.0"
"rimraf": "^6.0.1",
"rollup": "^4.21.2",
"typescript": "^5.5.4",
"@spryrocks/eslint-config": "^0.3.0-alpha.2"
},
"peerDependencies": {
"@capacitor/core": "^2.4.7"
Expand All @@ -55,5 +55,6 @@
"android": {
"src": "android"
}
}
},
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
9 changes: 9 additions & 0 deletions src/plugin/core/Plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {Plugins} from '@capacitor/core';

type CapPluginListener = (event: unknown) => void;

export type Plugin = {
addListener: (name: string, listener: CapPluginListener) => void;
};

export {Plugins};
1 change: 1 addition & 0 deletions src/plugin/core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Plugin';
6 changes: 0 additions & 6 deletions src/plugin/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ import {
SendDataResult,
} from './models';

type CapPluginListener = (event: unknown) => void;

export type CapPlugin = {
addListener: (name: string, listener: CapPluginListener) => void;
};

export interface ICapacitorSocketConnectionDefinitions {
openConnection(options: OpenConnectionOptions): Promise<OpenConnectionResult>;
closeConnection(options: CloseConnectionOptions): Promise<CloseConnectionResult>;
Expand Down
8 changes: 3 additions & 5 deletions src/plugin/factory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {CapPlugin, ICapacitorSocketConnectionDefinitions} from './definitions';
import {Plugins} from '@capacitor/core';
import {Plugin, Plugins} from './core/index';
import type {ICapacitorSocketConnectionDefinitions} from './definitions';

const pluginName = 'CapacitorSocketConnectionPlugin';

Expand All @@ -18,8 +18,6 @@ export const createPlugin: CreatePlugin = <TPlugin>(
return Plugins[pluginName] as TPlugin;
};

const plugin = createPlugin<ICapacitorSocketConnectionDefinitions & CapPlugin>(
pluginName,
);
const plugin = createPlugin<ICapacitorSocketConnectionDefinitions & Plugin>(pluginName);

export {plugin as NativePlugin};
19 changes: 13 additions & 6 deletions src/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,38 @@ import {
OnSocketClose,
OnSocketData,
OnSocketError,
OnStateChanged,
SocketData,
SocketOptions,
SocketState as State,
} from './types';
import {createLogger} from './logger';
import {ErrorLevel} from '@spryrocks/logger-plugin';
import {SocketConnectionError} from './error';

type State = 'initial' | 'opening' | 'opened' | 'closing' | 'closed' | 'error';

export interface ISocket {
onData: OnSocketData | undefined;

onClose: OnSocketClose | undefined;

onError: OnSocketError | undefined;

onStateChanged: OnStateChanged | undefined;

open(host: string, port: number): Promise<void>;

write(data: SocketData): Promise<void>;

close(): Promise<void>;

get state(): State;
}

export class Socket implements ISocket {
private readonly logger = createLogger();

onStateChanged: OnStateChanged | undefined;

onData: OnSocketData | undefined;

onClose: OnSocketClose | undefined;
Expand Down Expand Up @@ -117,14 +123,15 @@ export class Socket implements ISocket {
//endregion

//region State & Helpers
private get state() {
get state() {
return this._state;
}

private set state(newState: State) {
const oldState = this._state;
this.logger.info(`Set state: "${newState}"`, {oldState});
this._state = newState;
this.onStateChanged?.(newState);
}

private getLink() {
Expand Down Expand Up @@ -171,7 +178,7 @@ export class Socket implements ISocket {
private onDataInternal(bytes: ByteArray) {
if (!this.checkState('opened')) return;
const data = new Uint8Array(bytes);
if (this.onData) this.onData(data);
this.onData?.(data);
}

private onErrorInternal(error: unknown) {
Expand All @@ -183,7 +190,7 @@ export class Socket implements ISocket {
}
this.logger.error(error, undefined, {level: ErrorLevel.Medium});
this.state = 'error';
if (this.onError) this.onError(error);
this.onError?.(error);
}

private onClosingInternal() {
Expand All @@ -194,7 +201,7 @@ export class Socket implements ISocket {
if (this.checkState('closed')) return;
if (this.checkState('error')) return;
this.state = 'closed';
if (this.onClose) this.onClose();
this.onClose?.();
}

private async closeInternal() {
Expand Down
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
export interface SocketOptions {}

export type SocketState =
| 'initial'
| 'opening'
| 'opened'
| 'closing'
| 'closed'
| 'error';

export type SocketData = Uint8Array;

export type OnStateChanged = (state: SocketState) => void;

export type OnSocketData = (data: SocketData) => void;
export type OnSocketClose = () => void;
export type OnSocketError = (error: unknown) => void;
Loading

0 comments on commit c23ea0d

Please sign in to comment.