Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race condition in Svelte template #425

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import type {
HolochainError,
} from '@holochain/client';
import { SignalType } from '@holochain/client'
import { clientContext } from '../../contexts';
import { type ClientContext, clientContext } from '../../contexts';
import {{pascal_case referenceable.name}}Detail from './{{pascal_case referenceable.name}}Detail.svelte';
import type { {{pascal_case coordinator_zome_manifest.name}}Signal } from './types';

const client: AppClient = (getContext(clientContext) as any).getClient();
let client: AppClient;
const appClientContext = getContext<ClientContext>(clientContext);

let hashes: Array<{{referenceable.hash_type}}> = [];
let loading = false;
Expand All @@ -32,6 +33,7 @@ onMount(async () => {
throw new Error(`The author input is required for the {{pascal_case collection_name}} element`);
}
{{/if}}
client = await appClientContext.getClient();
await fetch{{pascal_case (plural referenceable.name)}}();
client.on('signal', signal => {
if (!(SignalType.App in signal)) return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import { createEventDispatcher, getContext, onMount } from 'svelte';
import type { AppClient, Record, EntryHash, AgentPubKey, ActionHash, DnaHash, HolochainError } from '@holochain/client';
import { clientContext } from '../../contexts';
import { type ClientContext, clientContext } from '../../contexts';
import type { {{pascal_case entry_type.name}}{{#each entry_type.fields}}{{#if (eq field_type.type "Enum")}}, {{field_type.label}}{{/if}}{{/each}} } from './types';
{{#uniq_lines}}
{{#each entry_type.fields}}
Expand All @@ -13,7 +13,8 @@ import type { {{pascal_case entry_type.name}}{{#each entry_type.fields}}{{#if (e
{{/uniq_lines}}

const dispatch = createEventDispatcher();
const client: AppClient = (getContext(clientContext) as any).getClient();
let client: AppClient;
const appClientContext = getContext<ClientContext>(clientContext);

{{#each entry_type.fields}}
{{#if widget }}
Expand All @@ -38,7 +39,7 @@ export let {{camel_case field_name}}{{#if (eq cardinality "single")}}!{{/if}}: {
$: {{#each entry_type.fields}}{{camel_case field_name}}{{#unless @last}}, {{/unless}}{{/each}};
$: is{{pascal_case entry_type.name}}Valid = true{{#each entry_type.fields}}{{#if widget}}{{#if (eq cardinality "single")}} && {{> (concat field_type.type "/" widget "/is-valid") variable_to_validate=(camel_case field_name) }}{{/if}}{{#if (eq cardinality "vector")}} && {{camel_case field_name}}.every(e => {{> (concat field_type.type "/" widget "/is-valid") variable_to_validate="e" }}){{/if}}{{/if}}{{/each}};

onMount(() => {
onMount(async () => {
{{#each entry_type.fields}}
{{#if (not widget) }}
{{#if (ne cardinality "option")}}
Expand All @@ -48,6 +49,7 @@ onMount(() => {
{{/if}}
{{/if}}
{{/each}}
client = await appClientContext.getClient();
});

async function create{{pascal_case entry_type.name}}() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import type {
SignalType,
} from '@holochain/client';
import { SignalType } from '@holochain/client';
import { clientContext } from '../../contexts';
import { type ClientContext, clientContext } from '../../contexts';
import type { {{pascal_case ../entry_type.name}}, {{pascal_case ../coordinator_zome_manifest.name}}Signal } from './types';
import {{pascal_case ../entry_type.name}}Detail from './{{pascal_case ../entry_type.name}}Detail.svelte';

const client: AppClient = (getContext(clientContext) as any).getClient();
let client: AppClient;
const appClientContext = getContext<ClientContext>(clientContext);

let hashes: Array<ActionHash> | undefined = [];
let loading: boolean;
Expand All @@ -30,7 +31,7 @@ onMount(async () => {
if ({{camel_case linked_from.singular_arg}} === undefined) {
throw new Error(`The {{camel_case linked_from.singular_arg}} input is required for the {{pascal_case (plural ../entry_type.name)}}For{{pascal_case linked_from.name}} element`);
}

client = await appClientContext.getClient();
await fetch{{pascal_case (plural ../entry_type.name)}}();

client.on('signal', async signal => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { createEventDispatcher, getContext, onMount } from 'svelte';
import type { AppClient, Record, EntryHash, AgentPubKey, DnaHash, ActionHash, HolochainError } from '@holochain/client';
import { decode } from '@msgpack/msgpack';
import { clientContext } from '../../contexts';
import { type ClientContext, clientContext } from '../../contexts';
import type { {{pascal_case entry_type.name}}{{#each entry_type.fields}}{{#if (eq field_type.type "Enum")}}, {{field_type.label}}{{/if}}{{/each}} } from './types';
{{#uniq_lines}}
{{#each entry_type.fields}}
Expand All @@ -13,7 +13,8 @@ import type { {{pascal_case entry_type.name}}{{#each entry_type.fields}}{{#if (e
{{/each}}
{{/uniq_lines}}

const client: AppClient = (getContext(clientContext) as any).getClient();
let client: AppClient;
const appClientContext = getContext<ClientContext>(clientContext);
const dispatch = createEventDispatcher();

export let currentRecord!: Record;
Expand All @@ -35,7 +36,7 @@ let {{camel_case field_name}}: Array<{{> (concat field_type.type "/type")}} | un
$: {{#each (filter entry_type.fields "widget")}}{{camel_case field_name}}{{#unless @last}}, {{/unless}}{{/each}};
$: is{{pascal_case entry_type.name}}Valid = true{{#each entry_type.fields}}{{#if widget}}{{#if (eq cardinality "single")}} && {{> (concat field_type.type "/" widget "/is-valid") variable_to_validate=(camel_case field_name) }}{{/if}}{{#if (eq cardinality "vector")}} && {{camel_case field_name}}.every(e => {{> (concat field_type.type "/" widget "/is-valid") variable_to_validate="e" }}){{/if}}{{/if}}{{/each}};

onMount(() => {
onMount(async () => {
if (!currentRecord) {
throw new Error(`The currentRecord input is required for the Edit{{pascal_case entry_type.name}} element`);
}
Expand All @@ -44,6 +45,7 @@ onMount(() => {
throw new Error(`The original{{pascal_case entry_type.name}}Hash input is required for the Edit{{pascal_case entry_type.name}} element`);
}
{{/if}}
client = await appClientContext.getClient();
});

async function update{{pascal_case entry_type.name}}() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { createEventDispatcher, onMount, getContext } from 'svelte';
import { decode } from '@msgpack/msgpack';
import type { Record, ActionHash, AppClient, EntryHash, AgentPubKey, DnaHash, HolochainError } from '@holochain/client';
import { clientContext } from '../../contexts';
import { type ClientContext, clientContext } from '../../contexts';
import type { {{pascal_case entry_type.name}}{{#each entry_type.fields}}{{#if (eq field_type.type "Enum")}}, {{field_type.label}}{{/if}}{{/each}} } from './types';
{{#if crud.update}}
import Edit{{pascal_case entry_type.name}} from './Edit{{pascal_case entry_type.name}}.svelte';
Expand All @@ -16,7 +16,8 @@ import Edit{{pascal_case entry_type.name}} from './Edit{{pascal_case entry_type.
{{/each}}
{{/uniq_lines}}

const client: AppClient = (getContext(clientContext) as any).getClient();
let client: AppClient;
const appClientContext = getContext<ClientContext>(clientContext);
const dispatch = createEventDispatcher();

let loading: boolean = false;
Expand All @@ -35,6 +36,7 @@ onMount(async () => {
if ({{camel_case entry_type.name}}Hash === undefined) {
throw new Error(`The {{camel_case entry_type.name}}Hash input is required for the {{pascal_case entry_type.name}}Detail element`);
}
client = await appClientContext.getClient();
await fetch{{pascal_case entry_type.name}}();
});

Expand Down
17 changes: 11 additions & 6 deletions templates/ui-frameworks/svelte/example/ui/src/App.svelte.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,32 @@
import AllPosts from './forum/posts/AllPosts.svelte';
import CreatePost from './forum/posts/CreatePost.svelte';

import { clientContext } from './contexts';
import { type ClientContext, clientContext } from './contexts';

let client: AppClient | undefined;
let loading = false;

$: client, loading;
const appClientContext = {
getClient: async () => {
if (!client) {
client = await AppWebsocket.connect();
}
return client;
},
};

onMount(async () => {
loading = true;
try {
client = await AppWebsocket.connect();
client = await appClientContext.getClient();
} catch(e) {
console.error(e)
} finally {
loading = false;
}
});

setContext(clientContext, {
getClient: () => client,
});
setContext<ClientContext>(clientContext, appClientContext);
</script>

<main>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import type {
HolochainError,
} from '@holochain/client';
import { SignalType } from '@holochain/client';
import { clientContext } from '../../contexts';
import { type ClientContext, clientContext } from '../../contexts';
import {{pascal_case from_referenceable.name}}Detail from './{{pascal_case from_referenceable.name}}Detail.svelte';
import type { {{pascal_case coordinator_zome_manifest.name}}Signal } from './types';

const client: AppClient = (getContext(clientContext) as any).getClient();
let client: AppClient;
const appClientContext = getContext<ClientContext>(clientContext);

let hashes: Array<{{from_referenceable.hash_type}}> | undefined;
let loading = false;
Expand All @@ -32,6 +33,7 @@ onMount(async () => {

try {
loading = true;
client = await appClientContext.getClient();
const links: Array<Link> = await client.callZome({
role_name: '{{dna_role_name}}',
zome_name: '{{snake_case coordinator_zome_manifest.name}}',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import type {
HolochainError,
} from '@holochain/client';
import { SignalType } from '@holochain/client';
import { clientContext } from '../../contexts';
import { type ClientContext, clientContext } from '../../contexts';
import {{pascal_case to_referenceable.name}}Detail from './{{pascal_case to_referenceable.name}}Detail.svelte';
import type { {{pascal_case coordinator_zome_manifest.name}}Signal } from './types';

const client: AppClient = (getContext(clientContext) as any).getClient();
let client: AppClient;
const appClientContext = getContext<ClientContext>(clientContext);

let hashes: Array<{{to_referenceable.hash_type}}> | undefined;
let loading = false;
Expand All @@ -29,7 +30,7 @@ onMount(async () => {
if (!{{camel_case from_referenceable.singular_arg}}) {
throw new Error(`The {{camel_case from_referenceable.singular_arg}} input is required for the {{pascal_case (plural to_referenceable.name)}}For{{pascal_case from_referenceable.name}} element`);
}

client = await appClientContext.getClient();
try {
loading = true;
const links: Array<Link> = await client.callZome({
Expand Down
2 changes: 1 addition & 1 deletion templates/ui-frameworks/svelte/web-app/ui/package.json.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@msgpack/msgpack": "^2.8.0"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^2.5.3",
"@sveltejs/vite-plugin-svelte": "^3.1.2",
"@tsconfig/svelte": "^3.0.0",
"bestzip": "^2.2.1",
"rimraf": "^5.0.10",
Expand Down
19 changes: 13 additions & 6 deletions templates/ui-frameworks/svelte/web-app/ui/src/App.svelte.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{{/if}}

import logo from './assets/holochainLogo.svg';
import { clientContext } from './contexts';
import { type ClientContext, clientContext } from './contexts';

{{#if holo_enabled}}
const IS_HOLO = ['true', '1', 't'].includes(import.meta.env.VITE_APP_IS_HOLO?.toLowerCase())
Expand All @@ -21,6 +21,15 @@
let error: HolochainError | undefined;
let loading = false;

const appClientContext = {
getClient: async () => {
if (!client) {
client = await AppWebsocket.connect();
}
return client;
},
};

onMount(async () => {
{{#if holo_enabled}}
try {
Expand All @@ -37,7 +46,7 @@
});
(client as WebSdk).signUp({ cancellable: false });
} else {
client = await AppWebsocket.connect();
client = await appClientContext.getClient();
loading = false;
}
} catch (e) {
Expand All @@ -48,7 +57,7 @@
{{else}}
try {
loading = true;
client = await AppWebsocket.connect();
client = await appClientContext.getClient();
} catch (e) {
error = e as HolochainError;
} finally {
Expand All @@ -57,9 +66,7 @@
{{/if}}
});

setContext(clientContext, {
getClient: () => client,
});
setContext<ClientContext>(clientContext, appClientContext);
</script>

<main>
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
export const clientContext = 'AppClient';
import { type AppClient } from "@holochain/client";

export const clientContext = "AppClient";

export type ClientContext = {
getClient: () => Promise<AppClient>;
};
Loading