Skip to content

Commit

Permalink
Use an object as the empty state to allow === checks
Browse files Browse the repository at this point in the history
  • Loading branch information
dionjwa committed Nov 7, 2024
1 parent 620f1ff commit 9e28ae3
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/libs/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@metapages/metapage",
"public": true,
"version": "1.0.14",
"version": "1.0.18",
"description": "Connect web pages together",
"repository": "https://github.com/metapages/metapage",
"homepage": "https://metapage.io/",
Expand Down
32 changes: 16 additions & 16 deletions app/libs/src/metapage/Metapage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@ export interface MetapageState {
metaframes: MetapageStatePartial;
}

const emptyState = (): MetapageState => {
return create<MetapageState>({
metaframes: {
inputs: {},
outputs: {},
},
}, draft => draft);
};
const emptyState :MetapageState= create<MetapageState>({
metaframes: {
inputs: {},
outputs: {},
},
}, draft => draft);

export const getLibraryVersionMatching = (
version: string
Expand Down Expand Up @@ -141,7 +139,7 @@ export class Metapage extends MetapageShared {
}

_id: MetapageId;
_state: MetapageState = emptyState();
_state: MetapageState = emptyState;
_metaframes: MetaframeClients = create({}, draft => draft); //<MetaframeId, MetapageIFrameRpcClient>

debug: boolean = isDebugFromUrlsParams();
Expand Down Expand Up @@ -272,7 +270,7 @@ export class Metapage extends MetapageShared {
);
});

if (this.listenerCount(MetapageEvents.State) > 0) {
if (this.listenerCount(MetapageEvents.State) > 0 && emptyState !== this._state) {
this.emit(MetapageEvents.State, this._state);
}
}
Expand Down Expand Up @@ -357,7 +355,7 @@ export class Metapage extends MetapageShared {
window.setTimeout(() => {
if (!this.isDisposed() && newDefinition === this._definition) {
this._emitDefinitionEvent();
if (state && this.listenerCount(MetapageEvents.State) > 0) {
if (state && this.listenerCount(MetapageEvents.State) > 0 && emptyState !== this._state) {
this.emit(MetapageEvents.State, this._state);
}
}
Expand Down Expand Up @@ -432,7 +430,7 @@ export class Metapage extends MetapageShared {
this._metaframes[id].dispose()
);
this._metaframes = create({}, draft => draft);
this._state = emptyState();
this._state = emptyState;
this._inputMap = create({}, draft => draft);
this._cachedInputLookupMap = create({}, draft => draft);
}
Expand Down Expand Up @@ -664,8 +662,10 @@ export class Metapage extends MetapageShared {
this.listenerCount(MetapageEvents.State) > 0 ||
this.listenerCount(MetapageEvents.Inputs) > 0
) {
this.emit(MetapageEvents.State, this._state);
this.emit(MetapageEvents.Inputs, this._state);
if (emptyState !== this._state) {
this.emit(MetapageEvents.State, this._state);
this.emit(MetapageEvents.Inputs, this._state?.metaframes?.inputs);
}
}
}

Expand Down Expand Up @@ -969,7 +969,7 @@ export class Metapage extends MetapageShared {
});
}
// only send a state event if downstream inputs were modified
if (this.listenerCount(MetapageEvents.State) > 0) {
if (this.listenerCount(MetapageEvents.State) > 0 && emptyState !== this._state) {
this.emit(MetapageEvents.State, this._state);
}
if (this.debug) {
Expand All @@ -995,7 +995,7 @@ export class Metapage extends MetapageShared {
// Currently on for setting metaframe inputs that haven't loaded yet
this.setInputStateOnlyMetaframeInputMap(metaframeId, inputs);
this._metaframes[metaframeId].setInputs(inputs);
if (this.listenerCount(MetapageEvents.State) > 0) {
if (this.listenerCount(MetapageEvents.State) > 0 && emptyState !== this._state) {
this.emit(MetapageEvents.State, this._state);
}

Expand Down
6 changes: 6 additions & 0 deletions app/libs/src/metapage/MetapageIFrameRpcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,13 @@ export class MetapageIFrameRpcClient extends EventEmitter<
}

async sendInputs(inputs: MetaframeInputMap) {
if (!inputs) {
return;
}
inputs = await serializeInputs(inputs);
if (this.isDisposed()) {
return;
}
this.sendRpc(JsonRpcMethodsFromParent.InputsUpdate, {
inputs: inputs,
parentId: this._parentId,
Expand Down
67 changes: 67 additions & 0 deletions app/libs/src/metapage/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,70 @@ export const possiblyDeserializeDatarefToValue = (value: any): any => {
} catch (e) {}
return value;
};

export const possiblyDeserializeDatarefToFile = (value: any): File | undefined => {
if (
!(
value &&
typeof value === "object" &&
(value as DataRefSerialized)._s === true
)
) {
return undefined;
}
const serializedRef = value as DataRefSerialized;
const _c: string = serializedRef._c;
if (_c === Blob.name) {
const serializedRefBlob = value as DataRefSerializedBlob;
const blob = new Blob(
[decode(serializedRef.value)],
{
type: serializedRefBlob.fileType,
}
);
return new File([blob], 'file', {
type: blob.type,
});
} else if (_c === File.name) {
const serializedRefFile = value as DataRefSerializedFile;
const file = new File(
[decode(serializedRef.value)],
serializedRefFile.name,
{
type: serializedRefFile.fileType,
lastModified: serializedRefFile.lastModified,
}
);
return file;
} else if (_c === ArrayBuffer.name) {
const arrayBuffer: ArrayBuffer = decode(serializedRef.value);
return new File(
[arrayBuffer],
"file",
{
type: "application/octet-stream",
}
);
}
// Assume typed array
const serializedRefTypedArray = value as DataRefSerializedTypedArray;
const arrayBuffer: ArrayBuffer = decode(
serializedRefTypedArray.value
);
const constructorName: string = serializedRefTypedArray._c;

try {
// @ts-ignore
const typedArray: ArrayBufferView = new globalThis[constructorName](
arrayBuffer,
);
return new File(
[typedArray],
"file",
{
type: "application/octet-stream",
}
);
} catch (e) {}
return undefined;
};

0 comments on commit 9e28ae3

Please sign in to comment.