Skip to content

Commit

Permalink
Merge pull request #426 from holochain/backport-0.3/svelte-race-condi…
Browse files Browse the repository at this point in the history
…tion

chore: backport svelte race condition and release 0.3000.11
  • Loading branch information
pdaoust authored Dec 5, 2024
2 parents b787660 + 11c3aaa commit 4d97985
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
edition = "2021"
name = "holochain_scaffolding_cli"
version = "0.3000.10"
version = "0.3000.11"
description = "CLI to easily generate and modify holochain apps"
license = "CAL-1.0"
homepage = "https://developer.holochain.org"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import type {
NewEntryAction,
HolochainError,
} 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 @@ -31,6 +32,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 (signal.zome_name !== '{{coordinator_zome_manifest.name}}') 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 @@ -10,11 +10,12 @@ import type {
NewEntryAction,
HolochainError,
} 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 @@ -28,7 +29,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
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import type {
NewEntryAction,
HolochainError,
} 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 @@ -31,6 +32,7 @@ onMount(async () => {
try {
loading = true;
client = await appClientContext.getClient();
const links: Array<Link> = await client.callZome({
cap_secret: null,
role_name: '{{dna_role_name}}',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import type {
NewEntryAction,
HolochainError,
} 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 @@ -31,6 +32,7 @@ onMount(async () => {
try {
loading = true;
client = await appClientContext.getClient();
const links: Array<Link> = await client.callZome({
cap_secret: null,
role_name: '{{dna_role_name}}',
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>;
};

0 comments on commit 4d97985

Please sign in to comment.