Skip to content

Commit

Permalink
new approach
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia committed Sep 2, 2023
1 parent b92058d commit 7f8977a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 34 deletions.
13 changes: 5 additions & 8 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,14 @@ export default [
plugins: [
json(),
typescript(),
resolve({
preferBuiltins: false
}),
commonjs(),
del({
targets: [
'build/cjs/*',
'build/esm/*',
'!build/cjs/protobuf.js',
'!build/cjs/protobuf.js.map',
'!build/cjs/protobuf.d.ts',
'!build/esm/protobuf.js',
'!build/esm/protobuf.js.map',
'!build/esm/protobuf.d.ts',
'build/cjs/index.d.ts',
'build/esm/index.d.ts',
],
hook: 'writeBundle',
runOnce: true,
Expand Down
8 changes: 2 additions & 6 deletions src/browser.protobuf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ import { Centrifuge } from './centrifuge';
import { ProtobufCodec } from './protobuf';

export default class CentrifugeProtobuf extends Centrifuge {
protected _formatOverride(format: 'json' | 'protobuf') {
if (format === 'protobuf') {
this._codec = new ProtobufCodec();
return true;
}
return false;
protected _formatOverride() {
this._codec = new ProtobufCodec();
}
}

Expand Down
16 changes: 4 additions & 12 deletions src/centrifuge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
import EventEmitter from 'events';

const defaults: Options = {
codec: new JsonCodec(),
token: '',
getToken: null,
data: null,
Expand Down Expand Up @@ -417,15 +416,8 @@ export class Centrifuge extends (EventEmitter as new () => TypedEventEmitter<Cli
}

/** @internal */
private _setFormat(format: 'json' | 'protobuf') {
if (this._formatOverride(format)) {
return;
}
}

/** @internal */
protected _formatOverride(_format: 'json' | 'protobuf') {
return false;
protected _formatOverride() {
return;
}

private _configure() {
Expand All @@ -445,8 +437,8 @@ export class Centrifuge extends (EventEmitter as new () => TypedEventEmitter<Cli
this._data = this._config.data;
}

this._codec = this._config.codec;
this._setFormat(this._codec.name());
this._codec = new JsonCodec();
this._formatOverride();

if (this._config.debug === true ||
(typeof localStorage !== 'undefined' && localStorage.getItem('centrifuge.debug'))) {
Expand Down
24 changes: 19 additions & 5 deletions src/protobuf.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// import * as protobuf from 'protobufjs/light'
// import * as protoJSON from './client.proto.json';
// const proto = protobuf.Root.fromJSON(protoJSON);
const protobuf = require('protobufjs/light');
const proto = protobuf.Root.fromJSON(require('./client.proto.json'));
import { Centrifuge, UnauthorizedError } from './centrifuge';
import { Subscription } from './subscription';
export * from "./types";

import * as protobuf from 'protobufjs/light'
import * as protoJSON from './client.proto.json';
const proto = protobuf.Root.fromJSON(protoJSON);

const Command = proto.lookupType('protocol.Command');
const Reply = proto.lookupType('protocol.Reply');
Expand Down Expand Up @@ -54,3 +56,15 @@ export class ProtobufCodec {
};
}
}

class CentrifugeProtobuf extends Centrifuge {
protected _formatOverride() {
this._codec = new ProtobufCodec();
}
}

export {
CentrifugeProtobuf as Centrifuge,
UnauthorizedError,
Subscription
}
3 changes: 0 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ export interface TransportEndpoint {

/** Options for Centrifuge client. */
export interface Options {
/** select codec to use. Default is JSON. To use Protobuf protocol you need to use
* ProtobufCodec. */
codec: any;
/** allows enabling debug mode */
debug: boolean;
/** allows setting initial connection token (JWT) */
Expand Down

0 comments on commit 7f8977a

Please sign in to comment.