Skip to content

Commit

Permalink
fix(connection): fix connection issue (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
unadlib authored Sep 23, 2023
1 parent 53c7a46 commit 7552cdb
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 10 deletions.
2 changes: 2 additions & 0 deletions example/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ import { OriginStorageClient } from 'origin-storage';
(window as any).originStorageClient = new OriginStorageClient({
uri: 'http://localhost:9000/storage.html',
});
(window as any).originStorageClient.getItem('test').then(console.log);

2 changes: 1 addition & 1 deletion example/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { OriginStorage } from 'origin-storage';

(window as any).originStorage = new OriginStorage({
broadcastChanges: true,
targetOrigin: 'http://localhost:9000',
targetOrigin: 'http://127.0.0.1:8080/',
});
2 changes: 2 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default isProduction
commonjs(),
replace({
__DEV__: 'false',
preventAssignment: true,
}),
terser(),
],
Expand All @@ -56,6 +57,7 @@ export default isProduction
commonjs(),
replace({
__DEV__: 'true',
preventAssignment: true,
}),
],
external: ['data-transport', 'broadcast-channel'],
Expand Down
3 changes: 2 additions & 1 deletion src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ export interface IChangeData {
}

export type StorageToClient = {
connect(): Promise<LocalForageOptions>;
connect(): Promise<void>;
getConfig(): Promise<LocalForageOptions>;
change(options: { key: string | null }): Promise<void>;
};

Expand Down
3 changes: 2 additions & 1 deletion src/originStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ export class OriginStorage
if (!this._read && !this._write) {
throw new Error(NoAccessError);
}
const config = await this.emit('connect');
const config = await this.emit('getConfig');
this._localforage = localforage.createInstance(config);
await this.emit('connect');
}

@listen
Expand Down
22 changes: 18 additions & 4 deletions src/originStorageClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class OriginStorageClient
extends IFrameTransport.Main<{ emit: ClientToStorage }>
implements StorageToClient, IOriginStorageClient
{
protected _connect?: () => void;
protected _onConnectCallbacks: Set<() => void> = new Set();
protected _isConnect: boolean;
protected _storageOptions?: LocalForageOptions;
protected _change?: (data: IChangeData) => void;
Expand All @@ -39,8 +39,18 @@ export class OriginStorageClient

private _connectResolve?: () => void;

private _connect() {
this._onConnectCallbacks.forEach((callback) => callback());
}

onConnect(callback: () => void) {
this._connect = callback;
this._onConnectCallbacks.add(callback);
if (this._isConnect) {
callback();
}
return () => {
this._onConnectCallbacks.delete(callback);
};
}

async onChange(callback: (data: IChangeData) => void) {
Expand All @@ -66,17 +76,21 @@ export class OriginStorageClient
});
}

@listen
async getConfig() {
return this._storageOptions!;
}

@listen
async connect() {
if (typeof this._connect !== 'function') {
if (__DEV__) {
throw new Error(`'onConnect' has not been called.`);
}
}
this._connect?.();
this._isConnect = true;
this._connectResolve?.();
return this._storageOptions!;
this._connect?.();
}

async getItem(key: string) {
Expand Down
12 changes: 9 additions & 3 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ const getInstances = async ({
});

const fn = jest.fn();
const fn1 = jest.fn();

originStorageClient.onConnect(fn);
const unsubscribe = originStorageClient.onConnect(fn1);

unsubscribe();

const originStorage = new OriginStorage({
read,
Expand All @@ -68,9 +72,9 @@ const getInstances = async ({
mockInternalSend(JSON.parse(JSON.stringify(message)));
},
});

expect(fn.mock.calls.length).toBe(1);
await new Promise((r) => setTimeout(r));
expect(fn.mock.calls.length).toBe(1);
expect(fn1.mock.calls.length).toBe(0);
// mock
(originStorage as any)._localforage = new MemoryStorage();
return {
Expand Down Expand Up @@ -177,7 +181,9 @@ test('watch data change', async () => {
});
},
};
(instances1.originStorage as any)._localforage = (instances0.originStorage as any)._localforage;
(instances1.originStorage as any)._localforage = (
instances0.originStorage as any
)._localforage;

const value = { a: 1 };
let watch = new Promise((r) => {
Expand Down

0 comments on commit 7552cdb

Please sign in to comment.